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>
68 #if !defined(AV_READ_TIME)
70 # define AV_READ_TIME gethrtime
71 # elif HAVE_MACH_ABSOLUTE_TIME
72 # define AV_READ_TIME mach_absolute_time
76 #ifndef FF_TIMER_UNITS
77 # define FF_TIMER_UNITS "UNITS"
80 #define TIMER_REPORT(id, tdiff) \
82 static uint64_t tsum = 0; \
83 static int tcount = 0; \
84 static int tskip_count = 0; \
85 static int thistogram[32] = {0}; \
86 thistogram[av_log2(tdiff)]++; \
88 (tdiff) < 8 * tsum / tcount || \
94 if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \
96 av_log(NULL, AV_LOG_ERROR, \
97 "%7" PRIu64 " " FF_TIMER_UNITS " in %s,%8d runs,%7d skips",\
98 tsum * 10 / tcount, id, tcount, tskip_count); \
99 for (i = 0; i < 32; i++) \
100 av_log(NULL, AV_LOG_VERBOSE, " %2d", av_log2(2*thistogram[i]));\
101 av_log(NULL, AV_LOG_ERROR, "\n"); \
105 #if CONFIG_LINUX_PERF
107 #define START_TIMER \
108 static int linux_perf_fd = -1; \
110 if (linux_perf_fd == -1) { \
111 struct perf_event_attr attr = { \
112 .type = PERF_TYPE_HARDWARE, \
113 .size = sizeof(struct perf_event_attr), \
114 .config = PERF_COUNT_HW_CPU_CYCLES, \
116 .exclude_kernel = 1, \
119 linux_perf_fd = syscall(__NR_perf_event_open, &attr, \
122 if (linux_perf_fd == -1) { \
123 av_log(NULL, AV_LOG_ERROR, "perf_event_open failed: %s\n", \
124 av_err2str(AVERROR(errno))); \
126 ioctl(linux_perf_fd, PERF_EVENT_IOC_RESET, 0); \
127 ioctl(linux_perf_fd, PERF_EVENT_IOC_ENABLE, 0); \
130 #define STOP_TIMER(id) \
131 ioctl(linux_perf_fd, PERF_EVENT_IOC_DISABLE, 0); \
132 read(linux_perf_fd, &tperf, sizeof(tperf)); \
133 TIMER_REPORT(id, tperf)
135 #elif CONFIG_MACOS_KPERF
137 #define START_TIMER \
140 tperf = ff_kperf_cycles();
142 #define STOP_TIMER(id) \
143 TIMER_REPORT(id, ff_kperf_cycles() - tperf);
145 #elif defined(AV_READ_TIME)
146 #define START_TIMER \
148 uint64_t tstart = AV_READ_TIME(); \
150 #define STOP_TIMER(id) \
151 tend = AV_READ_TIME(); \
152 TIMER_REPORT(id, tend - tstart)
155 #define STOP_TIMER(id) { }