xref: /aosp_15_r20/system/extras/simpleperf/include/simpleperf.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #ifndef _SIMPLEPERF_H
18*288bf522SAndroid Build Coastguard Worker #define _SIMPLEPERF_H
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <sys/types.h>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker #include <string>
23*288bf522SAndroid Build Coastguard Worker #include <vector>
24*288bf522SAndroid Build Coastguard Worker 
25*288bf522SAndroid Build Coastguard Worker #ifndef SIMPLEPERF_EXPORT
26*288bf522SAndroid Build Coastguard Worker #define SIMPLEPERF_EXPORT
27*288bf522SAndroid Build Coastguard Worker #endif
28*288bf522SAndroid Build Coastguard Worker 
29*288bf522SAndroid Build Coastguard Worker namespace simpleperf {
30*288bf522SAndroid Build Coastguard Worker 
31*288bf522SAndroid Build Coastguard Worker std::vector<std::string> GetAllEvents() SIMPLEPERF_EXPORT;
32*288bf522SAndroid Build Coastguard Worker bool IsEventSupported(const std::string& name) SIMPLEPERF_EXPORT;
33*288bf522SAndroid Build Coastguard Worker 
34*288bf522SAndroid Build Coastguard Worker struct Counter {
35*288bf522SAndroid Build Coastguard Worker   std::string event;
36*288bf522SAndroid Build Coastguard Worker   uint64_t value;
37*288bf522SAndroid Build Coastguard Worker   // If there is not enough hardware counters, kernel will share counters between events.
38*288bf522SAndroid Build Coastguard Worker   // time_enabled_in_ns is the period when counting is enabled, and time_running_in_ns is
39*288bf522SAndroid Build Coastguard Worker   // the period when counting really happens in hardware.
40*288bf522SAndroid Build Coastguard Worker   uint64_t time_enabled_in_ns;
41*288bf522SAndroid Build Coastguard Worker   uint64_t time_running_in_ns;
42*288bf522SAndroid Build Coastguard Worker };
43*288bf522SAndroid Build Coastguard Worker 
44*288bf522SAndroid Build Coastguard Worker // PerfEventSet can be used to count perf events or record perf events in perf.data.
45*288bf522SAndroid Build Coastguard Worker // To count perf events, you can do as follows:
46*288bf522SAndroid Build Coastguard Worker //  1. Create PerfEventSet instance.
47*288bf522SAndroid Build Coastguard Worker //  2. Select perf events to count. You can add more than one events.
48*288bf522SAndroid Build Coastguard Worker //  3. Set monitored targets.
49*288bf522SAndroid Build Coastguard Worker //  4. Start/stop/read counters when needed.
50*288bf522SAndroid Build Coastguard Worker // An example is as below:
51*288bf522SAndroid Build Coastguard Worker //    PerfEventSet* perf = PerfEventSet::CreateInstance(PerfEventSetType::kPerfForCounting);
52*288bf522SAndroid Build Coastguard Worker //    perf->AddEvent("cpu-cycles");
53*288bf522SAndroid Build Coastguard Worker //    perf->AddEvent("instructions");
54*288bf522SAndroid Build Coastguard Worker //    perf->MonitorCurrentProcess();
55*288bf522SAndroid Build Coastguard Worker //    perf->StartCounters();
56*288bf522SAndroid Build Coastguard Worker //    perf->StopCounters();
57*288bf522SAndroid Build Coastguard Worker //    perf->ReadCounters(&counters);
58*288bf522SAndroid Build Coastguard Worker //
59*288bf522SAndroid Build Coastguard Worker // PerfEventSet is not thread-safe. To access it from different threads, please protect
60*288bf522SAndroid Build Coastguard Worker // it under locks.
61*288bf522SAndroid Build Coastguard Worker class SIMPLEPERF_EXPORT PerfEventSet {
62*288bf522SAndroid Build Coastguard Worker  public:
63*288bf522SAndroid Build Coastguard Worker   enum Type {
64*288bf522SAndroid Build Coastguard Worker     kPerfForCounting,
65*288bf522SAndroid Build Coastguard Worker     kPerfForRecording,
66*288bf522SAndroid Build Coastguard Worker   };
67*288bf522SAndroid Build Coastguard Worker 
68*288bf522SAndroid Build Coastguard Worker   static PerfEventSet* CreateInstance(Type type);
~PerfEventSet()69*288bf522SAndroid Build Coastguard Worker   virtual ~PerfEventSet() {}
70*288bf522SAndroid Build Coastguard Worker 
71*288bf522SAndroid Build Coastguard Worker   // Add event in the set. All valid events are returned by GetAllEvents().
72*288bf522SAndroid Build Coastguard Worker   // To only monitor events happen in user space, add :u suffix, like cpu-cycles:u.
73*288bf522SAndroid Build Coastguard Worker   virtual bool AddEvent(const std::string& name);
74*288bf522SAndroid Build Coastguard Worker 
75*288bf522SAndroid Build Coastguard Worker   // Set monitored target. You can only monitor threads in current process.
76*288bf522SAndroid Build Coastguard Worker   virtual bool MonitorCurrentProcess();
77*288bf522SAndroid Build Coastguard Worker   virtual bool MonitorCurrentThread();
78*288bf522SAndroid Build Coastguard Worker   virtual bool MonitorThreadsInCurrentProcess(const std::vector<pid_t>& threads);
79*288bf522SAndroid Build Coastguard Worker 
80*288bf522SAndroid Build Coastguard Worker   // Counting interface:
81*288bf522SAndroid Build Coastguard Worker   // Start counters. When the PerfEventSet instance is created, the counters are stopped.
82*288bf522SAndroid Build Coastguard Worker   virtual bool StartCounters();
83*288bf522SAndroid Build Coastguard Worker   // Stop counters. The values of the counters will not change until the next StartCounters().
84*288bf522SAndroid Build Coastguard Worker   virtual bool StopCounters();
85*288bf522SAndroid Build Coastguard Worker   // Read counter values. There is a value for each event. You don't need to stop counters before
86*288bf522SAndroid Build Coastguard Worker   // reading them. The counter values are the accumulated value from the first StartCounters().
87*288bf522SAndroid Build Coastguard Worker   virtual bool ReadCounters(std::vector<Counter>* counters);
88*288bf522SAndroid Build Coastguard Worker 
89*288bf522SAndroid Build Coastguard Worker  protected:
PerfEventSet()90*288bf522SAndroid Build Coastguard Worker   PerfEventSet() {}
91*288bf522SAndroid Build Coastguard Worker };
92*288bf522SAndroid Build Coastguard Worker 
93*288bf522SAndroid Build Coastguard Worker }  // namespace simpleperf
94*288bf522SAndroid Build Coastguard Worker 
95*288bf522SAndroid Build Coastguard Worker #undef SIMPLEPERF_EXPORT
96*288bf522SAndroid Build Coastguard Worker 
97*288bf522SAndroid Build Coastguard Worker #endif  // _SIMPLEPERF_H
98