xref: /aosp_15_r20/external/pytorch/torch/csrc/profiler/perf-inl.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
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 
16 namespace torch::profiler::impl::linux_perf {
17 
18 /*
19  * PerfEvent
20  * ---------
21  */
22 
Disable()23 inline void PerfEvent::Disable() const {
24 #if defined(__ANDROID__) || defined(__linux__)
25   ioctl(fd_, PERF_EVENT_IOC_DISABLE, 0);
26 #endif /* __ANDROID__ || __linux__ */
27 }
28 
Enable()29 inline void PerfEvent::Enable() const {
30 #if defined(__ANDROID__) || defined(__linux__)
31   ioctl(fd_, PERF_EVENT_IOC_ENABLE, 0);
32 #endif /* __ANDROID__ || __linux__ */
33 }
34 
Reset()35 inline void PerfEvent::Reset() const {
36 #if defined(__ANDROID__) || defined(__linux__)
37   ioctl(fd_, PERF_EVENT_IOC_RESET, 0);
38 #endif /* __ANDROID__ || __linux__ */
39 }
40 
41 /*
42  * PerfProfiler
43  * ------------
44  */
45 
CalcDelta(uint64_t start,uint64_t end)46 inline uint64_t PerfProfiler::CalcDelta(uint64_t start, uint64_t end) const {
47   if (end < start) { // overflow
48     return end + (std::numeric_limits<uint64_t>::max() - start);
49   }
50   // not possible to wrap around start for a 64b cycle counter
51   return end - start;
52 }
53 
StartCounting()54 inline void PerfProfiler::StartCounting() const {
55   for (auto& e : events_) {
56     e.Enable();
57   }
58 }
59 
StopCounting()60 inline void PerfProfiler::StopCounting() const {
61   for (auto& e : events_) {
62     e.Disable();
63   }
64 }
65 
66 } // namespace torch::profiler::impl::linux_perf
67