1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "event_attr.h"
18
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <string>
22 #include <unordered_map>
23
24 #include <base/logging.h>
25
26 #include "event_type.h"
27 #include "utils.h"
28
BitsToString(const std::string & name,uint64_t bits,const std::vector<std::pair<int,std::string>> & bit_names)29 static std::string BitsToString(const std::string& name, uint64_t bits,
30 const std::vector<std::pair<int, std::string>>& bit_names) {
31 std::string result;
32 for (auto& p : bit_names) {
33 if (bits & p.first) {
34 bits &= ~p.first;
35 if (!result.empty()) {
36 result += ", ";
37 }
38 result += p.second;
39 }
40 }
41 if (bits != 0) {
42 LOG(DEBUG) << "unknown " << name << " bits: " << std::hex << bits;
43 }
44 return result;
45 }
46
SampleTypeToString(uint64_t sample_type)47 static std::string SampleTypeToString(uint64_t sample_type) {
48 static std::vector<std::pair<int, std::string>> sample_type_names = {
49 {PERF_SAMPLE_IP, "ip"},
50 {PERF_SAMPLE_TID, "tid"},
51 {PERF_SAMPLE_TIME, "time"},
52 {PERF_SAMPLE_ADDR, "addr"},
53 {PERF_SAMPLE_READ, "read"},
54 {PERF_SAMPLE_CALLCHAIN, "callchain"},
55 {PERF_SAMPLE_ID, "id"},
56 {PERF_SAMPLE_CPU, "cpu"},
57 {PERF_SAMPLE_PERIOD, "period"},
58 {PERF_SAMPLE_STREAM_ID, "stream_id"},
59 {PERF_SAMPLE_RAW, "raw"},
60 };
61 return BitsToString("sample_type", sample_type, sample_type_names);
62 }
63
ReadFormatToString(uint64_t read_format)64 static std::string ReadFormatToString(uint64_t read_format) {
65 static std::vector<std::pair<int, std::string>> read_format_names = {
66 {PERF_FORMAT_TOTAL_TIME_ENABLED, "total_time_enabled"},
67 {PERF_FORMAT_TOTAL_TIME_RUNNING, "total_time_running"},
68 {PERF_FORMAT_ID, "id"},
69 {PERF_FORMAT_GROUP, "group"},
70 };
71 return BitsToString("read_format", read_format, read_format_names);
72 }
73
CreateDefaultPerfEventAttr(const EventType & event_type)74 perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) {
75 perf_event_attr attr;
76 memset(&attr, 0, sizeof(attr));
77 attr.size = sizeof(perf_event_attr);
78 attr.type = event_type.type;
79 attr.config = event_type.config;
80 attr.mmap = 1;
81 attr.comm = 1;
82 attr.disabled = 1;
83 // Changing read_format affects the layout of the data read from perf_event_file, namely
84 // PerfCounter in event_fd.h.
85 attr.read_format =
86 PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
87 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD;
88 return attr;
89 }
90
DumpPerfEventAttr(const perf_event_attr & attr,size_t indent)91 void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent) {
92 std::string event_name = "unknown";
93 const EventType* event_type = EventTypeFactory::FindEventTypeByConfig(attr.type, attr.config);
94 if (event_type != nullptr) {
95 event_name = event_type->name;
96 }
97
98 PrintIndented(indent, "event_attr: for event %s\n", event_name.c_str());
99
100 PrintIndented(indent + 1, "type %u, size %u, config %llu\n", attr.type, attr.size, attr.config);
101
102 if (attr.freq != 0) {
103 PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq);
104 } else {
105 PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period);
106 }
107
108 PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type,
109 SampleTypeToString(attr.sample_type).c_str());
110
111 PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format,
112 ReadFormatToString(attr.read_format).c_str());
113
114 PrintIndented(indent + 1, "disabled %llu, inherit %llu, pinned %llu, exclusive %llu\n",
115 attr.disabled, attr.inherit, attr.pinned, attr.exclusive);
116
117 PrintIndented(indent + 1, "exclude_user %llu, exclude_kernel %llu, exclude_hv %llu\n",
118 attr.exclude_user, attr.exclude_kernel, attr.exclude_hv);
119
120 PrintIndented(indent + 1, "exclude_idle %llu, mmap %llu, comm %llu, freq %llu\n",
121 attr.exclude_idle, attr.mmap, attr.comm, attr.freq);
122
123 PrintIndented(indent + 1, "inherit_stat %llu, enable_on_exec %llu, task %llu\n",
124 attr.inherit_stat, attr.enable_on_exec, attr.task);
125
126 PrintIndented(indent + 1, "watermark %llu, precise_ip %llu, mmap_data %llu\n", attr.watermark,
127 attr.precise_ip, attr.mmap_data);
128
129 PrintIndented(indent + 1, "sample_id_all %llu, exclude_host %llu, exclude_guest %llu\n",
130 attr.sample_id_all, attr.exclude_host, attr.exclude_guest);
131 }
132