blob: 2b05931285c1e334fb1e032e96c1e706b05afe28 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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 Cuied91cd92015-04-28 15:54:13 -070029static 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 Cui67d3abd2015-04-16 15:26:31 -070047static std::string SampleTypeToString(uint64_t sample_type) {
Yabin Cuied91cd92015-04-28 15:54:13 -070048 static std::vector<std::pair<int, std::string>> sample_type_names = {
Yabin Cui67d3abd2015-04-16 15:26:31 -070049 {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 Cuied91cd92015-04-28 15:54:13 -070061 return BitsToString("sample_type", sample_type, sample_type_names);
Yabin Cui67d3abd2015-04-16 15:26:31 -070062}
63
Yabin Cuied91cd92015-04-28 15:54:13 -070064static 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
74perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) {
Yabin Cui67d3abd2015-04-16 15:26:31 -070075 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 Cuied91cd92015-04-28 15:54:13 -070082 attr.disabled = 1;
Yabin Cui323e9452015-04-20 18:07:17 -070083 // Changing read_format affects the layout of the data read from perf_event_file, namely
84 // PerfCounter in event_fd.h.
Yabin Cui67d3abd2015-04-16 15:26:31 -070085 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 Cuied91cd92015-04-28 15:54:13 -070088 return attr;
Yabin Cui67d3abd2015-04-16 15:26:31 -070089}
90
Yabin Cuied91cd92015-04-28 15:54:13 -070091void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent) {
Yabin Cui67d3abd2015-04-16 15:26:31 -070092 std::string event_name = "unknown";
Yabin Cuied91cd92015-04-28 15:54:13 -070093 const EventType* event_type = EventTypeFactory::FindEventTypeByConfig(attr.type, attr.config);
Yabin Cui67d3abd2015-04-16 15:26:31 -070094 if (event_type != nullptr) {
95 event_name = event_type->name;
96 }
97
Yabin Cuied91cd92015-04-28 15:54:13 -070098 PrintIndented(indent, "event_attr: for event %s\n", event_name.c_str());
Yabin Cui67d3abd2015-04-16 15:26:31 -070099
Yabin Cuied91cd92015-04-28 15:54:13 -0700100 PrintIndented(indent + 1, "type %u, size %u, config %llu\n", attr.type, attr.size, attr.config);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700101
Yabin Cuied91cd92015-04-28 15:54:13 -0700102 if (attr.freq != 0) {
103 PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700104 } else {
Yabin Cuied91cd92015-04-28 15:54:13 -0700105 PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700106 }
107
Yabin Cuied91cd92015-04-28 15:54:13 -0700108 PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type,
109 SampleTypeToString(attr.sample_type).c_str());
Yabin Cui67d3abd2015-04-16 15:26:31 -0700110
Yabin Cuied91cd92015-04-28 15:54:13 -0700111 PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format,
112 ReadFormatToString(attr.read_format).c_str());
Yabin Cui67d3abd2015-04-16 15:26:31 -0700113
Yabin Cuied91cd92015-04-28 15:54:13 -0700114 PrintIndented(indent + 1, "disabled %llu, inherit %llu, pinned %llu, exclusive %llu\n",
115 attr.disabled, attr.inherit, attr.pinned, attr.exclusive);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700116
Yabin Cuied91cd92015-04-28 15:54:13 -0700117 PrintIndented(indent + 1, "exclude_user %llu, exclude_kernel %llu, exclude_hv %llu\n",
118 attr.exclude_user, attr.exclude_kernel, attr.exclude_hv);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700119
Yabin Cuied91cd92015-04-28 15:54:13 -0700120 PrintIndented(indent + 1, "exclude_idle %llu, mmap %llu, comm %llu, freq %llu\n",
121 attr.exclude_idle, attr.mmap, attr.comm, attr.freq);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700122
Yabin Cuied91cd92015-04-28 15:54:13 -0700123 PrintIndented(indent + 1, "inherit_stat %llu, enable_on_exec %llu, task %llu\n",
124 attr.inherit_stat, attr.enable_on_exec, attr.task);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700125
Yabin Cuied91cd92015-04-28 15:54:13 -0700126 PrintIndented(indent + 1, "watermark %llu, precise_ip %llu, mmap_data %llu\n", attr.watermark,
127 attr.precise_ip, attr.mmap_data);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700128
Yabin Cuied91cd92015-04-28 15:54:13 -0700129 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 Cui67d3abd2015-04-16 15:26:31 -0700131}