1#pragma once
2
3#if defined(__ANDROID__) || defined(__linux__)
4
5#include <unistd.h>
6
7#include <sys/ioctl.h>
8#include <sys/syscall.h>
9
10#include <linux/perf_event.h>
11
12#endif /* __ANDROID__ || __linux__ */
13
14#include <torch/csrc/profiler/perf.h>
15
16namespace torch {
17namespace profiler {
18namespace impl {
19namespace linux_perf {
20
21/*
22 * PerfEvent
23 * ---------
24 */
25
26inline void PerfEvent::Disable() const {
27#if defined(__ANDROID__) || defined(__linux__)
28 ioctl(fd_, PERF_EVENT_IOC_DISABLE, 0);
29#endif /* __ANDROID__ || __linux__ */
30}
31
32inline void PerfEvent::Enable() const {
33#if defined(__ANDROID__) || defined(__linux__)
34 ioctl(fd_, PERF_EVENT_IOC_ENABLE, 0);
35#endif /* __ANDROID__ || __linux__ */
36}
37
38inline void PerfEvent::Reset() const {
39#if defined(__ANDROID__) || defined(__linux__)
40 ioctl(fd_, PERF_EVENT_IOC_RESET, 0);
41#endif /* __ANDROID__ || __linux__ */
42}
43
44/*
45 * PerfProfiler
46 * ------------
47 */
48
49inline uint64_t PerfProfiler::CalcDelta(uint64_t start, uint64_t end) const {
50 if (end < start) { // overflow
51 return end + (std::numeric_limits<uint64_t>::max() - start);
52 }
53 // not possible to wrap around start for a 64b cycle counter
54 return end - start;
55}
56
57inline void PerfProfiler::StartCounting() const {
58 for (auto& e : events_) {
59 e.Enable();
60 }
61}
62
63inline void PerfProfiler::StopCounting() const {
64 for (auto& e : events_) {
65 e.Disable();
66 }
67}
68
69} // namespace linux_perf
70} // namespace impl
71} // namespace profiler
72} // namespace torch
73