OSDN Git Service

am f667af58: am 224d3ba8: Merge "Simpleperf: correct the event type name printed...
[android-x86/system-extras.git] / simpleperf / event_selection_set.h
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_SELECTION_SET_H_
18 #define SIMPLE_PERF_EVENT_SELECTION_SET_H_
19
20 #include <poll.h>
21 #include <functional>
22 #include <map>
23 #include <vector>
24
25 #include <base/macros.h>
26
27 #include "event_fd.h"
28 #include "event_type.h"
29 #include "perf_event.h"
30
31 // EventSelectionSet helps to monitor events.
32 // Firstly, the user creates an EventSelectionSet, and adds the specific event types to monitor.
33 // Secondly, the user defines how to monitor the events (by setting enable_on_exec flag,
34 // sample frequency, etc).
35 // Then, the user can start monitoring by ordering the EventSelectionSet to open perf event files
36 // and enable events (if enable_on_exec flag isn't used).
37 // After that, the user can read counters or read mapped event records.
38 // At last, the EventSelectionSet will clean up resources at destruction automatically.
39
40 class EventSelectionSet {
41  public:
42   EventSelectionSet() {
43   }
44
45   bool Empty() const {
46     return selections_.empty();
47   }
48
49   bool AddEventType(const EventTypeAndModifier& event_type_modifier);
50
51   void SetEnableOnExec(bool enable);
52   bool GetEnableOnExec();
53   void SampleIdAll();
54   void SetSampleFreq(uint64_t sample_freq);
55   void SetSamplePeriod(uint64_t sample_period);
56   bool SetBranchSampling(uint64_t branch_sample_type);
57   void EnableFpCallChainSampling();
58   bool EnableDwarfCallChainSampling(uint32_t dump_stack_size);
59   void SetInherit(bool enable);
60
61   bool OpenEventFilesForAllCpus();
62   bool OpenEventFilesForThreads(const std::vector<pid_t>& threads);
63   bool OpenEventFilesForThreadsOnAllCpus(const std::vector<pid_t>& threads);
64   bool EnableEvents();
65   bool ReadCounters(std::map<const EventTypeAndModifier*, std::vector<PerfCounter>>* counters_map);
66   void PreparePollForEventFiles(std::vector<pollfd>* pollfds);
67   bool MmapEventFiles(size_t mmap_pages);
68   bool ReadMmapEventData(std::function<bool(const char*, size_t)> callback);
69
70   std::string FindEventFileNameById(uint64_t id);
71   const perf_event_attr& FindEventAttrByType(const EventTypeAndModifier& event_type_modifier);
72   const std::vector<std::unique_ptr<EventFd>>& FindEventFdsByType(
73       const EventTypeAndModifier& event_type_modifier);
74
75  private:
76   bool OpenEventFiles(const std::vector<pid_t>& threads, const std::vector<int>& cpus);
77
78   struct EventSelection {
79     EventTypeAndModifier event_type_modifier;
80     perf_event_attr event_attr;
81     std::vector<std::unique_ptr<EventFd>> event_fds;
82   };
83   EventSelection* FindSelectionByType(const EventTypeAndModifier& event_type_modifier);
84
85   std::vector<EventSelection> selections_;
86
87   DISALLOW_COPY_AND_ASSIGN(EventSelectionSet);
88 };
89
90 bool IsBranchSamplingSupported();
91 bool IsDwarfCallChainSamplingSupported();
92
93 #endif  // SIMPLE_PERF_EVENT_SELECTION_SET_H_