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 | |
| 24 | #include <base/logging.h> |
| 25 | |
| 26 | #include "event_type.h" |
| 27 | #include "utils.h" |
| 28 | |
Yabin Cui | ed91cd9 | 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 | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 48 | static std::vector<std::pair<int, std::string>> sample_type_names = { |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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 | }; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 61 | return BitsToString("sample_type", sample_type, sample_type_names); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 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 | |
| 74 | perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) { |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 82 | attr.disabled = 1; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 83 | // Changing read_format affects the layout of the data read from perf_event_file, namely |
| 84 | // PerfCounter in event_fd.h. |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 88 | return attr; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 91 | void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent) { |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 92 | std::string event_name = "unknown"; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 93 | const EventType* event_type = EventTypeFactory::FindEventTypeByConfig(attr.type, attr.config); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 94 | if (event_type != nullptr) { |
| 95 | event_name = event_type->name; |
| 96 | } |
| 97 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 98 | PrintIndented(indent, "event_attr: for event %s\n", event_name.c_str()); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 99 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 100 | 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] | 101 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 102 | if (attr.freq != 0) { |
| 103 | PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 104 | } else { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 105 | PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 108 | PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type, |
| 109 | SampleTypeToString(attr.sample_type).c_str()); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 110 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 111 | PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format, |
| 112 | ReadFormatToString(attr.read_format).c_str()); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 113 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 114 | PrintIndented(indent + 1, "disabled %llu, inherit %llu, pinned %llu, exclusive %llu\n", |
| 115 | attr.disabled, attr.inherit, attr.pinned, attr.exclusive); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 116 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 117 | PrintIndented(indent + 1, "exclude_user %llu, exclude_kernel %llu, exclude_hv %llu\n", |
| 118 | attr.exclude_user, attr.exclude_kernel, attr.exclude_hv); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 119 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 120 | PrintIndented(indent + 1, "exclude_idle %llu, mmap %llu, comm %llu, freq %llu\n", |
| 121 | attr.exclude_idle, attr.mmap, attr.comm, attr.freq); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 122 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 123 | PrintIndented(indent + 1, "inherit_stat %llu, enable_on_exec %llu, task %llu\n", |
| 124 | attr.inherit_stat, attr.enable_on_exec, attr.task); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 125 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 126 | PrintIndented(indent + 1, "watermark %llu, precise_ip %llu, mmap_data %llu\n", attr.watermark, |
| 127 | attr.precise_ip, attr.mmap_data); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 128 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 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); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 131 | } |