36# include <sys/ioctl.h>
37# include <asm/unistd.h>
38# include <linux/perf_event.h>
49#if HAVE_MACH_ABSOLUTE_TIME
50#include <mach/mach_time.h>
51#elif HAVE_CLOCK_GETTIME
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
73# elif HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
74 static inline int64_t ff_read_time(
void)
77 clock_gettime(CLOCK_MONOTONIC, &ts);
78 return ts.tv_sec * INT64_C(1000000000) + ts.tv_nsec;
80# define AV_READ_TIME ff_read_time
81# define FF_TIMER_UNITS "ns"
86# define FF_TIMER_UNITS "UNITS"
89#define TIMER_REPORT(id, tdiff) \
91 static uint64_t tsum = 0; \
92 static int tcount = 0; \
93 static int tskip_count = 0; \
94 static int thistogram[32] = {0}; \
95 thistogram[av_log2(tdiff)]++; \
97 (tdiff) < 8 * tsum / tcount || \
103 if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \
105 av_log(NULL, AV_LOG_ERROR, \
106 "%7" PRIu64 " " FF_TIMER_UNITS " in %s,%8d runs,%7d skips",\
107 tsum * 10 / tcount, id, tcount, tskip_count); \
108 for (i = 0; i < 32; i++) \
109 av_log(NULL, AV_LOG_VERBOSE, " %2d", av_log2(2*thistogram[i]));\
110 av_log(NULL, AV_LOG_ERROR, "\n"); \
117 static int linux_perf_fd = -1; \
119 if (linux_perf_fd == -1) { \
120 struct perf_event_attr attr = { \
121 .type = PERF_TYPE_HARDWARE, \
122 .size = sizeof(struct perf_event_attr), \
123 .config = PERF_COUNT_HW_CPU_CYCLES, \
125 .exclude_kernel = 1, \
128 linux_perf_fd = syscall(__NR_perf_event_open, &attr, \
131 if (linux_perf_fd == -1) { \
132 av_log(NULL, AV_LOG_ERROR, "perf_event_open failed: %s\n", \
133 av_err2str(AVERROR(errno))); \
135 ioctl(linux_perf_fd, PERF_EVENT_IOC_RESET, 0); \
136 ioctl(linux_perf_fd, PERF_EVENT_IOC_ENABLE, 0); \
139#define STOP_TIMER(id) \
140 ioctl(linux_perf_fd, PERF_EVENT_IOC_DISABLE, 0); \
141 if (read(linux_perf_fd, &tperf, sizeof(tperf)) != sizeof(tperf)) \
143 TIMER_REPORT(id, tperf)
145#elif CONFIG_MACOS_KPERF
150 tperf = ff_kperf_cycles();
152#define STOP_TIMER(id) \
153 TIMER_REPORT(id, ff_kperf_cycles() - tperf);
155#elif defined(AV_READ_TIME)
158 uint64_t tstart = AV_READ_TIME(); \
160#define STOP_TIMER(id) \
161 tend = AV_READ_TIME(); \
162 TIMER_REPORT(id, tend - tstart)
165#define STOP_TIMER(id) { }
common internal and external API header