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 { |
17 | namespace profiler { |
18 | namespace impl { |
19 | namespace linux_perf { |
20 | |
21 | /* |
22 | * PerfEvent |
23 | * --------- |
24 | */ |
25 | |
26 | inline void PerfEvent::Disable() const { |
27 | #if defined(__ANDROID__) || defined(__linux__) |
28 | ioctl(fd_, PERF_EVENT_IOC_DISABLE, 0); |
29 | #endif /* __ANDROID__ || __linux__ */ |
30 | } |
31 | |
32 | inline void PerfEvent::Enable() const { |
33 | #if defined(__ANDROID__) || defined(__linux__) |
34 | ioctl(fd_, PERF_EVENT_IOC_ENABLE, 0); |
35 | #endif /* __ANDROID__ || __linux__ */ |
36 | } |
37 | |
38 | inline 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 | |
49 | inline 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 | |
57 | inline void PerfProfiler::StartCounting() const { |
58 | for (auto& e : events_) { |
59 | e.Enable(); |
60 | } |
61 | } |
62 | |
63 | inline 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 |