| 1 |
/* |
| 2 |
* Copyright (C) 2017 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 _SIMPLEPERF_H |
| 18 |
#define _SIMPLEPERF_H |
| 19 |
|
| 20 |
#include <sys/types.h> |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#ifndef SIMPLEPERF_EXPORT |
| 26 |
#define SIMPLEPERF_EXPORT |
| 27 |
#endif |
| 28 |
|
| 29 |
namespace simpleperf { |
| 30 |
|
| 31 |
std::vector<std::string> GetAllEvents() SIMPLEPERF_EXPORT; |
| 32 |
bool IsEventSupported(const std::string& name) SIMPLEPERF_EXPORT; |
| 33 |
|
| 34 |
struct Counter { |
| 35 |
std::string event; |
| 36 |
uint64_t value; |
| 37 |
// If there is not enough hardware counters, kernel will share counters between events. |
| 38 |
// time_enabled_in_ns is the period when counting is enabled, and time_running_in_ns is |
| 39 |
// the period when counting really happens in hardware. |
| 40 |
uint64_t time_enabled_in_ns; |
| 41 |
uint64_t time_running_in_ns; |
| 42 |
}; |
| 43 |
|
| 44 |
// PerfEventSet can be used to count perf events or record perf events in perf.data. |
| 45 |
// To count perf events, you can do as follows: |
| 46 |
// 1. Create PerfEventSet instance. |
| 47 |
// 2. Select perf events to count. You can add more than one events. |
| 48 |
// 3. Set monitored targets. |
| 49 |
// 4. Start/stop/read counters when needed. |
| 50 |
// An example is as below: |
| 51 |
// PerfEventSet* perf = PerfEventSet::CreateInstance(PerfEventSetType::kPerfForCounting); |
| 52 |
// perf->AddEvent("cpu-cycles"); |
| 53 |
// perf->AddEvent("instructions"); |
| 54 |
// perf->MonitorCurrentProcess(); |
| 55 |
// perf->StartCounters(); |
| 56 |
// perf->StopCounters(); |
| 57 |
// perf->ReadCounters(&counters); |
| 58 |
// |
| 59 |
// PerfEventSet is not thread-safe. To access it from different threads, please protect |
| 60 |
// it under locks. |
| 61 |
class SIMPLEPERF_EXPORT PerfEventSet { |
| 62 |
public: |
| 63 |
enum Type { |
| 64 |
kPerfForCounting, |
| 65 |
kPerfForRecording, |
| 66 |
}; |
| 67 |
|
| 68 |
static PerfEventSet* CreateInstance(Type type); |
| 69 |
virtual ~PerfEventSet() {} |
| 70 |
|
| 71 |
// Add event in the set. All valid events are returned by GetAllEvents(). |
| 72 |
// To only monitor events happen in user space, add :u suffix, like cpu-cycles:u. |
| 73 |
virtual bool AddEvent(const std::string& name); |
| 74 |
|
| 75 |
// Set monitored target. You can only monitor threads in current process. |
| 76 |
virtual bool MonitorCurrentProcess(); |
| 77 |
virtual bool MonitorCurrentThread(); |
| 78 |
virtual bool MonitorThreadsInCurrentProcess(const std::vector<pid_t>& threads); |
| 79 |
|
| 80 |
// Counting interface: |
| 81 |
// Start counters. When the PerfEventSet instance is created, the counters are stopped. |
| 82 |
virtual bool StartCounters(); |
| 83 |
// Stop counters. The values of the counters will not change until the next StartCounters(). |
| 84 |
virtual bool StopCounters(); |
| 85 |
// Read counter values. There is a value for each event. You don't need to stop counters before |
| 86 |
// reading them. The counter values are the accumulated value from the first StartCounters(). |
| 87 |
virtual bool ReadCounters(std::vector<Counter>* counters); |
| 88 |
|
| 89 |
protected: |
| 90 |
PerfEventSet() {} |
| 91 |
}; |
| 92 |
|
| 93 |
} // namespace simpleperf |
| 94 |
|
| 95 |
#undef SIMPLEPERF_EXPORT |
| 96 |
|
| 97 |
#endif // _SIMPLEPERF_H |