2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "event_attr.h"
22 #include <unordered_map>
24 #include <android-base/logging.h>
26 #include "event_type.h"
29 static std::string BitsToString(const std::string& name, uint64_t bits,
30 const std::vector<std::pair<int, std::string>>& bit_names) {
32 for (auto& p : bit_names) {
35 if (!result.empty()) {
42 LOG(DEBUG) << "unknown " << name << " bits: " << std::hex << bits;
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"},
64 return BitsToString("sample_type", sample_type, sample_type_names);
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"},
74 return BitsToString("read_format", read_format, read_format_names);
77 perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) {
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;
86 // Changing read_format affects the layout of the data read from perf_event_file, namely
87 // PerfCounter in event_fd.h.
89 PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
91 PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD | PERF_SAMPLE_CPU;
93 if (attr.type == PERF_TYPE_TRACEPOINT) {
95 attr.sample_period = 1;
96 // Tracepoint information are stored in raw data in sample records.
97 attr.sample_type |= PERF_SAMPLE_RAW;
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;
109 PrintIndented(indent, "event_attr: for event type %s\n", event_name.c_str());
111 PrintIndented(indent + 1, "type %u, size %u, config %llu\n", attr.type, attr.size, attr.config);
113 if (attr.freq != 0) {
114 PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq);
116 PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period);
119 PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type,
120 SampleTypeToString(attr.sample_type).c_str());
122 PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format,
123 ReadFormatToString(attr.read_format).c_str());
125 PrintIndented(indent + 1, "disabled %u, inherit %u, pinned %u, exclusive %u\n", attr.disabled,
126 attr.inherit, attr.pinned, attr.exclusive);
128 PrintIndented(indent + 1, "exclude_user %u, exclude_kernel %u, exclude_hv %u\n",
129 attr.exclude_user, attr.exclude_kernel, attr.exclude_hv);
131 PrintIndented(indent + 1, "exclude_idle %u, mmap %u, comm %u, freq %u\n", attr.exclude_idle,
132 attr.mmap, attr.comm, attr.freq);
134 PrintIndented(indent + 1, "inherit_stat %u, enable_on_exec %u, task %u\n", attr.inherit_stat,
135 attr.enable_on_exec, attr.task);
137 PrintIndented(indent + 1, "watermark %u, precise_ip %u, mmap_data %u\n", attr.watermark,
138 attr.precise_ip, attr.mmap_data);
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);