• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "environment.h"
27 #include "event_type.h"
28 #include "utils.h"
29 
30 namespace simpleperf {
31 
BitsToString(const std::string & name,uint64_t bits,const std::vector<std::pair<int,std::string>> & bit_names)32 static std::string BitsToString(const std::string& name, uint64_t bits,
33                                 const std::vector<std::pair<int, std::string>>& bit_names) {
34   std::string result;
35   for (auto& p : bit_names) {
36     if (bits & p.first) {
37       bits &= ~p.first;
38       if (!result.empty()) {
39         result += ", ";
40       }
41       result += p.second;
42     }
43   }
44   if (bits != 0) {
45     LOG(DEBUG) << "unknown " << name << " bits: " << std::hex << bits;
46   }
47   return result;
48 }
49 
SampleTypeToString(uint64_t sample_type)50 static std::string SampleTypeToString(uint64_t sample_type) {
51   static std::vector<std::pair<int, std::string>> sample_type_names = {
52       {PERF_SAMPLE_ADDR, "addr"},
53       {PERF_SAMPLE_BRANCH_STACK, "branch_stack"},
54       {PERF_SAMPLE_CALLCHAIN, "callchain"},
55       {PERF_SAMPLE_CPU, "cpu"},
56       {PERF_SAMPLE_ID, "id"},
57       {PERF_SAMPLE_IP, "ip"},
58       {PERF_SAMPLE_PERIOD, "period"},
59       {PERF_SAMPLE_RAW, "raw"},
60       {PERF_SAMPLE_READ, "read"},
61       {PERF_SAMPLE_REGS_USER, "regs_user"},
62       {PERF_SAMPLE_STACK_USER, "stack_user"},
63       {PERF_SAMPLE_STREAM_ID, "stream_id"},
64       {PERF_SAMPLE_TID, "tid"},
65       {PERF_SAMPLE_TIME, "time"},
66   };
67   return BitsToString("sample_type", sample_type, sample_type_names);
68 }
69 
ReadFormatToString(uint64_t read_format)70 static std::string ReadFormatToString(uint64_t read_format) {
71   static std::vector<std::pair<int, std::string>> read_format_names = {
72       {PERF_FORMAT_TOTAL_TIME_ENABLED, "total_time_enabled"},
73       {PERF_FORMAT_TOTAL_TIME_RUNNING, "total_time_running"},
74       {PERF_FORMAT_ID, "id"},
75       {PERF_FORMAT_GROUP, "group"},
76   };
77   return BitsToString("read_format", read_format, read_format_names);
78 }
79 
CreateDefaultPerfEventAttr(const EventType & event_type)80 perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) {
81   perf_event_attr attr;
82   memset(&attr, 0, sizeof(attr));
83   attr.size = sizeof(perf_event_attr);
84   attr.type = event_type.type;
85   attr.config = event_type.config;
86   attr.disabled = 0;
87   // Changing read_format affects the layout of the data read from perf_event_file, namely
88   // PerfCounter in event_fd.h.
89   attr.read_format =
90       PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
91   attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD |
92                       PERF_SAMPLE_CPU | PERF_SAMPLE_ID;
93 
94   if (attr.type == PERF_TYPE_TRACEPOINT) {
95     // Tracepoint information are stored in raw data in sample records.
96     if (CanRecordRawData()) {
97       attr.sample_type |= PERF_SAMPLE_RAW;
98     }
99   }
100   return attr;
101 }
102 
DumpPerfEventAttr(const perf_event_attr & attr,size_t indent)103 void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent) {
104   std::string event_name = GetEventNameByAttr(attr);
105   PrintIndented(indent, "event_attr: for event type %s\n", event_name.c_str());
106 
107   PrintIndented(indent + 1, "type %u, size %u, config %llu\n", attr.type, attr.size, attr.config);
108 
109   if (attr.freq != 0) {
110     PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq);
111   } else {
112     PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period);
113   }
114 
115   PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type,
116                 SampleTypeToString(attr.sample_type).c_str());
117 
118   PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format,
119                 ReadFormatToString(attr.read_format).c_str());
120 
121   PrintIndented(indent + 1, "disabled %u, inherit %u, pinned %u, exclusive %u\n", attr.disabled,
122                 attr.inherit, attr.pinned, attr.exclusive);
123 
124   PrintIndented(indent + 1, "exclude_user %u, exclude_kernel %u, exclude_hv %u\n",
125                 attr.exclude_user, attr.exclude_kernel, attr.exclude_hv);
126 
127   PrintIndented(indent + 1, "exclude_idle %u, mmap %u, mmap2 %u, comm %u, freq %u\n",
128                 attr.exclude_idle, attr.mmap, attr.mmap2, attr.comm, attr.freq);
129 
130   PrintIndented(indent + 1, "inherit_stat %u, enable_on_exec %u, task %u\n", attr.inherit_stat,
131                 attr.enable_on_exec, attr.task);
132 
133   PrintIndented(indent + 1, "watermark %u, precise_ip %u, mmap_data %u\n", attr.watermark,
134                 attr.precise_ip, attr.mmap_data);
135 
136   PrintIndented(indent + 1, "sample_id_all %u, exclude_host %u, exclude_guest %u\n",
137                 attr.sample_id_all, attr.exclude_host, attr.exclude_guest);
138   PrintIndented(indent + 1, "config2 0x%llx\n", attr.config2);
139   PrintIndented(indent + 1, "branch_sample_type 0x%" PRIx64 "\n", attr.branch_sample_type);
140   PrintIndented(indent + 1, "exclude_callchain_kernel %u, exclude_callchain_user %u\n",
141                 attr.exclude_callchain_kernel, attr.exclude_callchain_user);
142   PrintIndented(indent + 1, "sample_regs_user 0x%" PRIx64 "\n", attr.sample_regs_user);
143   PrintIndented(indent + 1, "sample_stack_user 0x%" PRIx64 "\n", attr.sample_stack_user);
144 }
145 
GetCommonEventIdPositionsForAttrs(std::vector<perf_event_attr> & attrs,size_t * event_id_pos_in_sample_records,size_t * event_id_reverse_pos_in_non_sample_records)146 bool GetCommonEventIdPositionsForAttrs(std::vector<perf_event_attr>& attrs,
147                                        size_t* event_id_pos_in_sample_records,
148                                        size_t* event_id_reverse_pos_in_non_sample_records) {
149   // When there are more than one perf_event_attrs, we need to read event id
150   // in each record to decide current record should use which attr. So
151   // we need to determine the event id position in a record here.
152   std::vector<uint64_t> sample_types;
153   for (const auto& attr : attrs) {
154     sample_types.push_back(attr.sample_type);
155   }
156   // First determine event_id_pos_in_sample_records.
157   // If PERF_SAMPLE_IDENTIFIER is enabled, it is just after perf_event_header.
158   // If PERF_SAMPLE_ID is enabled, then PERF_SAMPLE_IDENTIFIER | IP | TID | TIME | ADDR
159   // should also be the same.
160   bool identifier_enabled = true;
161   bool id_enabled = true;
162   uint64_t flags_before_id_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | PERF_SAMPLE_TID |
163                                   PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR;
164   uint64_t flags_before_id = sample_types[0] & flags_before_id_mask;
165   bool flags_before_id_are_the_same = true;
166   for (auto type : sample_types) {
167     identifier_enabled &= (type & PERF_SAMPLE_IDENTIFIER) != 0;
168     id_enabled &= (type & PERF_SAMPLE_ID) != 0;
169     flags_before_id_are_the_same &= (type & flags_before_id_mask) == flags_before_id;
170   }
171   if (identifier_enabled) {
172     *event_id_pos_in_sample_records = sizeof(perf_event_header);
173   } else if (id_enabled && flags_before_id_are_the_same) {
174     uint64_t pos = sizeof(perf_event_header);
175     while (flags_before_id != 0) {
176       // Each flags takes 8 bytes in sample records.
177       flags_before_id &= flags_before_id - 1;
178       pos += 8;
179     }
180     *event_id_pos_in_sample_records = pos;
181   } else {
182     LOG(ERROR) << "perf_event_attrs don't have a common event id position in sample records";
183     return false;
184   }
185 
186   // Secondly determine event_id_reverse_pos_in_non_sample_record.
187   // If sample_id_all is not enabled, there is no event id in non sample records.
188   // If PERF_SAMPLE_IDENTIFIER is enabled, it is at the last 8 bytes of the record.
189   // If PERF_SAMPLE_ID is enabled, then PERF_SAMPLE_IDENTIFIER | CPU | STREAM_ID should
190   // also be the same.
191   bool sample_id_all_enabled = true;
192   for (const auto& attr : attrs) {
193     if (attr.sample_id_all == 0) {
194       sample_id_all_enabled = false;
195     }
196   }
197   if (!sample_id_all_enabled) {
198     LOG(ERROR) << "there are perf_event_attrs not enabling sample_id_all, so can't determine "
199                << "perf_event_attr for non sample records";
200     return false;
201   }
202   uint64_t flags_after_id_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID;
203   uint64_t flags_after_id = sample_types[0] & flags_after_id_mask;
204   bool flags_after_id_are_the_same = true;
205   for (auto type : sample_types) {
206     flags_after_id_are_the_same &= (type & flags_after_id_mask) == flags_after_id;
207   }
208   if (identifier_enabled) {
209     *event_id_reverse_pos_in_non_sample_records = 8;
210   } else if (id_enabled && flags_after_id_are_the_same) {
211     uint64_t pos = 8;
212     while (flags_after_id != 0) {
213       // Each flag takes 8 bytes in sample_id of non sample records.
214       flags_after_id &= flags_after_id - 1;
215       pos += 8;
216     }
217     *event_id_reverse_pos_in_non_sample_records = pos;
218   } else {
219     LOG(ERROR)
220         << "perf_event_attrs don't have a common event id reverse position in non sample records";
221     return false;
222   }
223   return true;
224 }
225 
IsTimestampSupported(const perf_event_attr & attr)226 bool IsTimestampSupported(const perf_event_attr& attr) {
227   return attr.sample_id_all && (attr.sample_type & PERF_SAMPLE_TIME);
228 }
229 
IsCpuSupported(const perf_event_attr & attr)230 bool IsCpuSupported(const perf_event_attr& attr) {
231   return attr.sample_id_all && (attr.sample_type & PERF_SAMPLE_CPU);
232 }
233 
GetEventNameByAttr(const perf_event_attr & attr)234 std::string GetEventNameByAttr(const perf_event_attr& attr) {
235   std::string name = "unknown";
236   auto callback = [&](const EventType& event_type) {
237     // An event type uses both type and config value to define itself. But etm event type
238     // only uses type value (whose config value is used to set etm options).
239     if (event_type.type == attr.type &&
240         (event_type.config == attr.config || event_type.IsEtmEvent())) {
241       name = event_type.name;
242       if (attr.exclude_user && !attr.exclude_kernel) {
243         name += ":k";
244       } else if (attr.exclude_kernel && !attr.exclude_user) {
245         name += ":u";
246       }
247       return false;
248     }
249     return true;
250   };
251   EventTypeManager::Instance().ForEachType(callback);
252   return name;
253 }
254 
255 }  // namespace simpleperf
256