2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * high precision timer, useful to profile code
26 #ifndef AVUTIL_TIMER_H
27 #define AVUTIL_TIMER_H
35 # include <unistd.h> // read(3)
36 # include <sys/ioctl.h>
37 # include <asm/unistd.h>
38 # include <linux/perf_event.h>
45 #if CONFIG_MACOS_KPERF
46 #include "macos_kperf.h"
47 #elif HAVE_MACH_ABSOLUTE_TIME
48 #include <mach/mach_time.h>
55 # include "aarch64/timer.h"
57 # include "arm/timer.h"
59 # include "ppc/timer.h"
61 # include "riscv/timer.h"
63 # include "x86/timer.h"
65 # include "loongarch/timer.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) { }
158 #endif /* AVUTIL_TIMER_H */