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 #ifndef SIMPLE_PERF_EVENT_ATTR_H_
18 #define SIMPLE_PERF_EVENT_ATTR_H_
19 
20 #include <stddef.h>
21 #include <string.h>
22 
23 #include <string>
24 #include <vector>
25 
26 #include "perf_event.h"
27 
28 namespace simpleperf {
29 
30 struct EventType;
31 
32 struct EventAttrWithId {
33   perf_event_attr attr;
34   std::vector<uint64_t> ids;
35 };
36 
37 using EventAttrIds = std::vector<EventAttrWithId>;
38 
39 inline constexpr uint64_t INFINITE_SAMPLE_PERIOD = 1ULL << 62;
40 
41 perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type);
42 void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent = 0);
43 bool GetCommonEventIdPositionsForAttrs(const EventAttrIds& attrs,
44                                        size_t* event_id_pos_in_sample_records,
45                                        size_t* event_id_reverse_pos_in_non_sample_records);
46 bool IsTimestampSupported(const perf_event_attr& attr);
47 bool IsCpuSupported(const perf_event_attr& attr);
48 // Return event name with modifier if the event is found, otherwise return "unknown".
49 // This function is slow for using linear search, so only used when reporting.
50 std::string GetEventNameByAttr(const perf_event_attr& attr);
51 void ReplaceRegAndStackWithCallChain(perf_event_attr& attr);
52 
53 inline bool operator==(const perf_event_attr& attr1, const perf_event_attr& attr2) {
54   return memcmp(&attr1, &attr2, sizeof(perf_event_attr)) == 0;
55 }
56 
57 inline bool operator!=(const perf_event_attr& attr1, const perf_event_attr& attr2) {
58   return !(attr1 == attr2);
59 }
60 
61 }  // namespace simpleperf
62 
63 #endif  // SIMPLE_PERF_EVENT_ATTR_H_
64