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 <android-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_ADDR, "addr"},
50 {PERF_SAMPLE_BRANCH_STACK, "branch_stack"},
51 {PERF_SAMPLE_CALLCHAIN, "callchain"},
52 {PERF_SAMPLE_CPU, "cpu"},
53 {PERF_SAMPLE_ID, "id"},
54 {PERF_SAMPLE_IP, "ip"},
55 {PERF_SAMPLE_PERIOD, "period"},
56 {PERF_SAMPLE_RAW, "raw"},
57 {PERF_SAMPLE_READ, "read"},
58 {PERF_SAMPLE_REGS_USER, "regs_user"},
59 {PERF_SAMPLE_STACK_USER, "stack_user"},
60 {PERF_SAMPLE_STREAM_ID, "stream_id"},
61 {PERF_SAMPLE_TID, "tid"},
62 {PERF_SAMPLE_TIME, "time"},
63 };
64 return BitsToString("sample_type", sample_type, sample_type_names);
65 }
66
ReadFormatToString(uint64_t read_format)67 static std::string ReadFormatToString(uint64_t read_format) {
68 static std::vector<std::pair<int, std::string>> read_format_names = {
69 {PERF_FORMAT_TOTAL_TIME_ENABLED, "total_time_enabled"},
70 {PERF_FORMAT_TOTAL_TIME_RUNNING, "total_time_running"},
71 {PERF_FORMAT_ID, "id"},
72 {PERF_FORMAT_GROUP, "group"},
73 };
74 return BitsToString("read_format", read_format, read_format_names);
75 }
76
CreateDefaultPerfEventAttr(const EventType & event_type)77 perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) {
78 perf_event_attr attr;
79 memset(&attr, 0, sizeof(attr));
80 attr.size = sizeof(perf_event_attr);
81 attr.type = event_type.type;
82 attr.config = event_type.config;
83 attr.mmap = 1;
84 attr.comm = 1;
85 attr.disabled = 0;
86 // Changing read_format affects the layout of the data read from perf_event_file, namely
87 // PerfCounter in event_fd.h.
88 attr.read_format =
89 PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
90 attr.sample_type |=
91 PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD | PERF_SAMPLE_CPU;
92
93 if (attr.type == PERF_TYPE_TRACEPOINT) {
94 attr.sample_freq = 0;
95 attr.sample_period = 1;
96 // Tracepoint information are stored in raw data in sample records.
97 attr.sample_type |= PERF_SAMPLE_RAW;
98 }
99 return attr;
100 }
101
DumpPerfEventAttr(const perf_event_attr & attr,size_t indent)102 void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent) {
103 std::string event_name = "unknown";
104 const EventType* event_type = FindEventTypeByConfig(attr.type, attr.config);
105 if (event_type != nullptr) {
106 event_name = event_type->name;
107 }
108
109 PrintIndented(indent, "event_attr: for event type %s\n", event_name.c_str());
110
111 PrintIndented(indent + 1, "type %u, size %u, config %llu\n", attr.type, attr.size, attr.config);
112
113 if (attr.freq != 0) {
114 PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq);
115 } else {
116 PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period);
117 }
118
119 PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type,
120 SampleTypeToString(attr.sample_type).c_str());
121
122 PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format,
123 ReadFormatToString(attr.read_format).c_str());
124
125 PrintIndented(indent + 1, "disabled %u, inherit %u, pinned %u, exclusive %u\n", attr.disabled,
126 attr.inherit, attr.pinned, attr.exclusive);
127
128 PrintIndented(indent + 1, "exclude_user %u, exclude_kernel %u, exclude_hv %u\n",
129 attr.exclude_user, attr.exclude_kernel, attr.exclude_hv);
130
131 PrintIndented(indent + 1, "exclude_idle %u, mmap %u, comm %u, freq %u\n", attr.exclude_idle,
132 attr.mmap, attr.comm, attr.freq);
133
134 PrintIndented(indent + 1, "inherit_stat %u, enable_on_exec %u, task %u\n", attr.inherit_stat,
135 attr.enable_on_exec, attr.task);
136
137 PrintIndented(indent + 1, "watermark %u, precise_ip %u, mmap_data %u\n", attr.watermark,
138 attr.precise_ip, attr.mmap_data);
139
140 PrintIndented(indent + 1, "sample_id_all %u, exclude_host %u, exclude_guest %u\n",
141 attr.sample_id_all, attr.exclude_host, attr.exclude_guest);
142 PrintIndented(indent + 1, "branch_sample_type 0x%" PRIx64 "\n", attr.branch_sample_type);
143 PrintIndented(indent + 1, "exclude_callchain_kernel %u, exclude_callchain_user %u\n",
144 attr.exclude_callchain_kernel, attr.exclude_callchain_user);
145 PrintIndented(indent + 1, "sample_regs_user 0x%" PRIx64 "\n", attr.sample_regs_user);
146 PrintIndented(indent + 1, "sample_stack_user 0x%" PRIx64 "\n", attr.sample_stack_user);
147 }
148