Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 25 | |
| 26 | #include "event_type.h" |
| 27 | #include "utils.h" |
| 28 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 47 | static std::string SampleTypeToString(uint64_t sample_type) { |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 48 | static std::vector<std::pair<int, std::string>> sample_type_names = { |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 49 | {PERF_SAMPLE_ADDR, "addr"}, |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 50 | {PERF_SAMPLE_BRANCH_STACK, "branch_stack"}, |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 51 | {PERF_SAMPLE_CALLCHAIN, "callchain"}, |
| 52 | {PERF_SAMPLE_CPU, "cpu"}, |
| 53 | {PERF_SAMPLE_ID, "id"}, |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 54 | {PERF_SAMPLE_IP, "ip"}, |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 55 | {PERF_SAMPLE_PERIOD, "period"}, |
| 56 | {PERF_SAMPLE_RAW, "raw"}, |
| 57 | {PERF_SAMPLE_READ, "read"}, |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 58 | {PERF_SAMPLE_REGS_USER, "regs_user"}, |
| 59 | {PERF_SAMPLE_STACK_USER, "stack_user"}, |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 60 | {PERF_SAMPLE_STREAM_ID, "stream_id"}, |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 61 | {PERF_SAMPLE_TID, "tid"}, |
| 62 | {PERF_SAMPLE_TIME, "time"}, |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 63 | }; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 64 | return BitsToString("sample_type", sample_type, sample_type_names); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 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 | |
| 77 | perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) { |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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; |
Yabin Cui | a46794f | 2015-12-03 10:31:37 -0800 | [diff] [blame] | 85 | attr.disabled = 0; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 86 | // Changing read_format affects the layout of the data read from perf_event_file, namely |
| 87 | // PerfCounter in event_fd.h. |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 88 | attr.read_format = |
| 89 | PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID; |
Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 90 | attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD | |
| 91 | PERF_SAMPLE_CPU | PERF_SAMPLE_ID; |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 92 | |
| 93 | if (attr.type == PERF_TYPE_TRACEPOINT) { |
Yabin Cui | bfc11b6 | 2015-08-19 10:12:51 -0700 | [diff] [blame] | 94 | // Tracepoint information are stored in raw data in sample records. |
| 95 | attr.sample_type |= PERF_SAMPLE_RAW; |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 96 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 97 | return attr; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 100 | void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent) { |
Yabin Cui | c10a9dc | 2016-06-15 12:10:33 -0700 | [diff] [blame] | 101 | std::string event_name = GetEventNameByAttr(attr); |
Yabin Cui | 2672dea | 2015-05-21 12:17:23 -0700 | [diff] [blame] | 102 | PrintIndented(indent, "event_attr: for event type %s\n", event_name.c_str()); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 103 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 104 | PrintIndented(indent + 1, "type %u, size %u, config %llu\n", attr.type, attr.size, attr.config); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 105 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 106 | if (attr.freq != 0) { |
| 107 | PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 108 | } else { |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 109 | PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 112 | PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type, |
| 113 | SampleTypeToString(attr.sample_type).c_str()); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 114 | |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 115 | PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format, |
| 116 | ReadFormatToString(attr.read_format).c_str()); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 117 | |
Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 118 | PrintIndented(indent + 1, "disabled %u, inherit %u, pinned %u, exclusive %u\n", attr.disabled, |
| 119 | attr.inherit, attr.pinned, attr.exclusive); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 120 | |
Yabin Cui | d115b2f | 2015-06-15 13:57:23 -0700 | [diff] [blame] | 121 | PrintIndented(indent + 1, "exclude_user %u, exclude_kernel %u, exclude_hv %u\n", |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 122 | attr.exclude_user, attr.exclude_kernel, attr.exclude_hv); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 123 | |
Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 124 | PrintIndented(indent + 1, "exclude_idle %u, mmap %u, comm %u, freq %u\n", attr.exclude_idle, |
| 125 | attr.mmap, attr.comm, attr.freq); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 126 | |
Yabin Cui | 41d4ba9 | 2015-06-22 12:27:58 -0700 | [diff] [blame] | 127 | PrintIndented(indent + 1, "inherit_stat %u, enable_on_exec %u, task %u\n", attr.inherit_stat, |
| 128 | attr.enable_on_exec, attr.task); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 129 | |
Yabin Cui | d115b2f | 2015-06-15 13:57:23 -0700 | [diff] [blame] | 130 | PrintIndented(indent + 1, "watermark %u, precise_ip %u, mmap_data %u\n", attr.watermark, |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 131 | attr.precise_ip, attr.mmap_data); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 132 | |
Yabin Cui | d115b2f | 2015-06-15 13:57:23 -0700 | [diff] [blame] | 133 | PrintIndented(indent + 1, "sample_id_all %u, exclude_host %u, exclude_guest %u\n", |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 134 | attr.sample_id_all, attr.exclude_host, attr.exclude_guest); |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 135 | PrintIndented(indent + 1, "branch_sample_type 0x%" PRIx64 "\n", attr.branch_sample_type); |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 136 | PrintIndented(indent + 1, "exclude_callchain_kernel %u, exclude_callchain_user %u\n", |
| 137 | attr.exclude_callchain_kernel, attr.exclude_callchain_user); |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 138 | PrintIndented(indent + 1, "sample_regs_user 0x%" PRIx64 "\n", attr.sample_regs_user); |
| 139 | PrintIndented(indent + 1, "sample_stack_user 0x%" PRIx64 "\n", attr.sample_stack_user); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 140 | } |
Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 141 | |
| 142 | bool GetCommonEventIdPositionsForAttrs(std::vector<perf_event_attr>& attrs, |
| 143 | size_t* event_id_pos_in_sample_records, |
| 144 | size_t* event_id_reverse_pos_in_non_sample_records) { |
| 145 | // When there are more than one perf_event_attrs, we need to read event id |
| 146 | // in each record to decide current record should use which attr. So |
| 147 | // we need to determine the event id position in a record here. |
| 148 | std::vector<uint64_t> sample_types; |
| 149 | for (const auto& attr : attrs) { |
| 150 | sample_types.push_back(attr.sample_type); |
| 151 | } |
| 152 | // First determine event_id_pos_in_sample_records. |
| 153 | // If PERF_SAMPLE_IDENTIFIER is enabled, it is just after perf_event_header. |
| 154 | // If PERF_SAMPLE_ID is enabled, then PERF_SAMPLE_IDENTIFIER | IP | TID | TIME | ADDR |
| 155 | // should also be the same. |
| 156 | bool identifier_enabled = true; |
| 157 | bool id_enabled = true; |
| 158 | uint64_t flags_before_id_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | PERF_SAMPLE_TID | |
| 159 | PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR; |
| 160 | uint64_t flags_before_id = sample_types[0] & flags_before_id_mask; |
| 161 | bool flags_before_id_are_the_same = true; |
| 162 | for (auto type : sample_types) { |
| 163 | identifier_enabled &= (type & PERF_SAMPLE_IDENTIFIER) != 0; |
| 164 | id_enabled &= (type & PERF_SAMPLE_ID) != 0; |
| 165 | flags_before_id_are_the_same &= (type & flags_before_id_mask) == flags_before_id; |
| 166 | } |
| 167 | if (identifier_enabled) { |
| 168 | *event_id_pos_in_sample_records = sizeof(perf_event_header); |
| 169 | } else if (id_enabled && flags_before_id_are_the_same) { |
| 170 | uint64_t pos = sizeof(perf_event_header); |
| 171 | while (flags_before_id != 0) { |
| 172 | // Each flags takes 8 bytes in sample records. |
| 173 | flags_before_id &= flags_before_id - 1; |
| 174 | pos += 8; |
| 175 | } |
| 176 | *event_id_pos_in_sample_records = pos; |
| 177 | } else { |
| 178 | LOG(ERROR) << "perf_event_attrs don't have a common event id position in sample records"; |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | // Secondly determine event_id_reverse_pos_in_non_sample_record. |
| 183 | // If sample_id_all is not enabled, there is no event id in non sample records. |
| 184 | // If PERF_SAMPLE_IDENTIFIER is enabled, it is at the last 8 bytes of the record. |
| 185 | // If PERF_SAMPLE_ID is enabled, then PERF_SAMPLE_IDENTIFIER | CPU | STREAM_ID should |
| 186 | // also be the same. |
| 187 | bool sample_id_all_enabled = true; |
| 188 | for (const auto& attr : attrs) { |
| 189 | if (attr.sample_id_all == 0) { |
| 190 | sample_id_all_enabled = false; |
| 191 | } |
| 192 | } |
| 193 | if (!sample_id_all_enabled) { |
| 194 | LOG(ERROR) << "there are perf_event_attrs not enabling sample_id_all, so can't determine " |
| 195 | << "perf_event_attr for non sample records"; |
| 196 | return false; |
| 197 | } |
| 198 | uint64_t flags_after_id_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID; |
| 199 | uint64_t flags_after_id = sample_types[0] & flags_after_id_mask; |
| 200 | bool flags_after_id_are_the_same = true; |
| 201 | for (auto type : sample_types) { |
| 202 | flags_after_id_are_the_same &= (type & flags_after_id_mask) == flags_after_id; |
| 203 | } |
| 204 | if (identifier_enabled) { |
| 205 | *event_id_reverse_pos_in_non_sample_records = 8; |
| 206 | } else if (id_enabled && flags_after_id_are_the_same) { |
| 207 | uint64_t pos = 8; |
| 208 | while (flags_after_id != 0) { |
| 209 | // Each flag takes 8 bytes in sample_id of non sample records. |
| 210 | flags_after_id &= flags_after_id - 1; |
| 211 | pos += 8; |
| 212 | } |
| 213 | *event_id_reverse_pos_in_non_sample_records = pos; |
| 214 | } else { |
| 215 | LOG(ERROR) << "perf_event_attrs don't have a common event id reverse position in non sample records"; |
| 216 | return false; |
| 217 | } |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | bool IsTimestampSupported(const perf_event_attr& attr) { |
| 222 | return attr.sample_id_all && (attr.sample_type & PERF_SAMPLE_TIME); |
| 223 | } |
Yabin Cui | c10a9dc | 2016-06-15 12:10:33 -0700 | [diff] [blame] | 224 | |
Yabin Cui | fc22b8f | 2016-08-04 14:47:50 -0700 | [diff] [blame] | 225 | bool IsCpuSupported(const perf_event_attr& attr) { |
| 226 | return attr.sample_id_all && (attr.sample_type & PERF_SAMPLE_CPU); |
| 227 | } |
| 228 | |
Yabin Cui | c10a9dc | 2016-06-15 12:10:33 -0700 | [diff] [blame] | 229 | std::string GetEventNameByAttr(const perf_event_attr& attr) { |
| 230 | for (const auto& event_type : GetAllEventTypes()) { |
| 231 | if (event_type.type == attr.type && event_type.config == attr.config) { |
| 232 | std::string name = event_type.name; |
| 233 | if (attr.exclude_user && !attr.exclude_kernel) { |
| 234 | name += ":k"; |
| 235 | } else if (attr.exclude_kernel && !attr.exclude_user) { |
| 236 | name += ":u"; |
| 237 | } |
| 238 | return name; |
| 239 | } |
| 240 | } |
| 241 | return "unknown"; |
| 242 | } |