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 <base/logging.h>
26 #include "event_type.h"
29 static std::string SampleTypeToString(uint64_t sample_type) {
30 std::unordered_map<int, std::string> map = {
31 {PERF_SAMPLE_IP, "ip"},
32 {PERF_SAMPLE_TID, "tid"},
33 {PERF_SAMPLE_TIME, "time"},
34 {PERF_SAMPLE_ADDR, "addr"},
35 {PERF_SAMPLE_READ, "read"},
36 {PERF_SAMPLE_CALLCHAIN, "callchain"},
37 {PERF_SAMPLE_ID, "id"},
38 {PERF_SAMPLE_CPU, "cpu"},
39 {PERF_SAMPLE_PERIOD, "period"},
40 {PERF_SAMPLE_STREAM_ID, "stream_id"},
41 {PERF_SAMPLE_RAW, "raw"},
46 if (sample_type & p.first) {
47 sample_type &= ~p.first;
48 if (!result.empty()) {
54 if (sample_type != 0) {
55 LOG(DEBUG) << "unknown sample_type bits: " << std::hex << sample_type;
61 EventAttr EventAttr::CreateDefaultAttrToMonitorEvent(const EventType& event_type) {
63 memset(&attr, 0, sizeof(attr));
64 attr.size = sizeof(perf_event_attr);
65 attr.type = event_type.type;
66 attr.config = event_type.config;
70 PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
71 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD;
72 return EventAttr(attr);
75 void EventAttr::Dump(size_t indent) const {
76 std::string event_name = "unknown";
77 const EventType* event_type = EventTypeFactory::FindEventTypeByConfig(attr_.type, attr_.config);
78 if (event_type != nullptr) {
79 event_name = event_type->name;
82 PrintIndented(indent, "event_attr_: for event %s\n", event_name.c_str());
84 PrintIndented(indent + 2, "type %u, size %u, config %llu\n", attr_.type, attr_.size, attr_.config);
86 if (attr_.freq != 0) {
87 PrintIndented(indent + 2, "sample_freq %llu\n", attr_.sample_freq);
89 PrintIndented(indent + 2, "sample_period %llu\n", attr_.sample_period);
92 PrintIndented(indent + 2, "sample_type (0x%llx) %s\n", attr_.sample_type,
93 SampleTypeToString(attr_.sample_type).c_str());
95 PrintIndented(indent + 2, "read_format (0x%llx)\n", attr_.read_format);
97 PrintIndented(indent + 2, "disabled %llu, inherit %llu, pinned %llu, exclusive %llu\n",
98 attr_.disabled, attr_.inherit, attr_.pinned, attr_.exclusive);
100 PrintIndented(indent + 2, "exclude_user %llu, exclude_kernel %llu, exclude_hv %llu\n",
101 attr_.exclude_user, attr_.exclude_kernel, attr_.exclude_hv);
103 PrintIndented(indent + 2, "exclude_idle %llu, mmap %llu, comm %llu, freq %llu\n",
104 attr_.exclude_idle, attr_.mmap, attr_.comm, attr_.freq);
106 PrintIndented(indent + 2, "inherit_stat %llu, enable_on_exec %llu, task %llu\n",
107 attr_.inherit_stat, attr_.enable_on_exec, attr_.task);
109 PrintIndented(indent + 2, "watermark %llu, precise_ip %llu, mmap_data %llu\n", attr_.watermark,
110 attr_.precise_ip, attr_.mmap_data);
112 PrintIndented(indent + 2, "sample_id_all %llu, exclude_host %llu, exclude_guest %llu\n",
113 attr_.sample_id_all, attr_.exclude_host, attr_.exclude_guest);