OSDN Git Service

Implement simpleperf list subcommand.
[android-x86/system-extras.git] / simpleperf / event_attr.cpp
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
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"},
42   };
43
44   std::string result;
45   for (auto p : map) {
46     if (sample_type & p.first) {
47       sample_type &= ~p.first;
48       if (!result.empty()) {
49         result += ", ";
50       }
51       result += p.second;
52     }
53   }
54   if (sample_type != 0) {
55     LOG(DEBUG) << "unknown sample_type bits: " << std::hex << sample_type;
56   }
57
58   return result;
59 }
60
61 EventAttr EventAttr::CreateDefaultAttrToMonitorEvent(const EventType& event_type) {
62   perf_event_attr attr;
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;
67   attr.mmap = 1;
68   attr.comm = 1;
69   attr.read_format =
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);
73 }
74
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;
80   }
81
82   PrintIndented(indent, "event_attr_: for event %s\n", event_name.c_str());
83
84   PrintIndented(indent + 2, "type %u, size %u, config %llu\n", attr_.type, attr_.size, attr_.config);
85
86   if (attr_.freq != 0) {
87     PrintIndented(indent + 2, "sample_freq %llu\n", attr_.sample_freq);
88   } else {
89     PrintIndented(indent + 2, "sample_period %llu\n", attr_.sample_period);
90   }
91
92   PrintIndented(indent + 2, "sample_type (0x%llx) %s\n", attr_.sample_type,
93                 SampleTypeToString(attr_.sample_type).c_str());
94
95   PrintIndented(indent + 2, "read_format (0x%llx)\n", attr_.read_format);
96
97   PrintIndented(indent + 2, "disabled %llu, inherit %llu, pinned %llu, exclusive %llu\n",
98                 attr_.disabled, attr_.inherit, attr_.pinned, attr_.exclusive);
99
100   PrintIndented(indent + 2, "exclude_user %llu, exclude_kernel %llu, exclude_hv %llu\n",
101                 attr_.exclude_user, attr_.exclude_kernel, attr_.exclude_hv);
102
103   PrintIndented(indent + 2, "exclude_idle %llu, mmap %llu, comm %llu, freq %llu\n",
104                 attr_.exclude_idle, attr_.mmap, attr_.comm, attr_.freq);
105
106   PrintIndented(indent + 2, "inherit_stat %llu, enable_on_exec %llu, task %llu\n",
107                 attr_.inherit_stat, attr_.enable_on_exec, attr_.task);
108
109   PrintIndented(indent + 2, "watermark %llu, precise_ip %llu, mmap_data %llu\n", attr_.watermark,
110                 attr_.precise_ip, attr_.mmap_data);
111
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);
114 }