Go to the documentation of this file.
26 #ifndef AVUTIL_TIMER_H
27 #define AVUTIL_TIMER_H
36 # include <sys/ioctl.h>
37 # include <asm/unistd.h>
38 # include <linux/perf_event.h>
45 #if CONFIG_MACOS_KPERF
47 #elif HAVE_MACH_ABSOLUTE_TIME
48 #include <mach/mach_time.h>
66 #if !defined(AV_READ_TIME)
68 # define AV_READ_TIME gethrtime
69 # elif HAVE_MACH_ABSOLUTE_TIME
70 # define AV_READ_TIME mach_absolute_time
74 #ifndef FF_TIMER_UNITS
75 # define FF_TIMER_UNITS "UNITS"
78 #define TIMER_REPORT(id, tdiff) \
80 static uint64_t tsum = 0; \
81 static int tcount = 0; \
82 static int tskip_count = 0; \
83 static int thistogram[32] = {0}; \
84 thistogram[av_log2(tdiff)]++; \
86 (tdiff) < 8 * tsum / tcount || \
92 if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \
94 av_log(NULL, AV_LOG_ERROR, \
95 "%7" PRIu64 " " FF_TIMER_UNITS " in %s,%8d runs,%7d skips",\
96 tsum * 10 / tcount, id, tcount, tskip_count); \
97 for (i = 0; i < 32; i++) \
98 av_log(NULL, AV_LOG_VERBOSE, " %2d", av_log2(2*thistogram[i]));\
99 av_log(NULL, AV_LOG_ERROR, "\n"); \
103 #if CONFIG_LINUX_PERF
105 #define START_TIMER \
106 static int linux_perf_fd; \
108 if (!linux_perf_fd) { \
109 struct perf_event_attr attr = { \
110 .type = PERF_TYPE_HARDWARE, \
111 .size = sizeof(struct perf_event_attr), \
112 .config = PERF_COUNT_HW_CPU_CYCLES, \
114 .exclude_kernel = 1, \
117 linux_perf_fd = syscall(__NR_perf_event_open, &attr, \
120 if (linux_perf_fd == -1) { \
121 av_log(NULL, AV_LOG_ERROR, "perf_event_open failed: %s\n", \
122 av_err2str(AVERROR(errno))); \
124 ioctl(linux_perf_fd, PERF_EVENT_IOC_RESET, 0); \
125 ioctl(linux_perf_fd, PERF_EVENT_IOC_ENABLE, 0); \
128 #define STOP_TIMER(id) \
129 ioctl(linux_perf_fd, PERF_EVENT_IOC_DISABLE, 0); \
130 read(linux_perf_fd, &tperf, sizeof(tperf)); \
131 TIMER_REPORT(id, tperf)
133 #elif CONFIG_MACOS_KPERF
135 #define START_TIMER \
138 tperf = ff_kperf_cycles();
140 #define STOP_TIMER(id) \
141 TIMER_REPORT(id, ff_kperf_cycles() - tperf);
143 #elif defined(AV_READ_TIME)
144 #define START_TIMER \
146 uint64_t tstart = AV_READ_TIME(); \
148 #define STOP_TIMER(id) \
149 tend = AV_READ_TIME(); \
150 TIMER_REPORT(id, tend - tstart)
153 #define STOP_TIMER(id) { }