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"
49 #if HAVE_MACH_ABSOLUTE_TIME
50 #include <mach/mach_time.h>
51 #elif HAVE_CLOCK_GETTIME
59 # include "aarch64/timer.h"
61 # include "arm/timer.h"
63 # include "ppc/timer.h"
65 # include "x86/timer.h"
67 # include "loongarch/timer.h"
70 #if !defined(AV_READ_TIME)
72 # define AV_READ_TIME gethrtime
73 # elif HAVE_MACH_ABSOLUTE_TIME
74 # define AV_READ_TIME mach_absolute_time
75 # elif HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
76 static inline int64_t ff_read_time(void)
79 clock_gettime(CLOCK_MONOTONIC
, &ts
);
80 return ts
.tv_sec
* INT64_C(1000000000) + ts
.tv_nsec
;
82 # define AV_READ_TIME ff_read_time
83 # define FF_TIMER_UNITS "ns"
87 #ifndef FF_TIMER_UNITS
88 # define FF_TIMER_UNITS "UNITS"
91 #define TIMER_REPORT(id, tdiff) \
93 static uint64_t tsum = 0; \
94 static int tcount = 0; \
95 static int tskip_count = 0; \
96 static int thistogram[32] = {0}; \
97 thistogram[av_log2(tdiff)]++; \
99 (tdiff) < 8 * tsum / tcount || \
105 if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \
107 av_log(NULL, AV_LOG_ERROR, \
108 "%7" PRIu64 " " FF_TIMER_UNITS " in %s,%8d runs,%7d skips",\
109 tsum * 10 / tcount, id, tcount, tskip_count); \
110 for (i = 0; i < 32; i++) \
111 av_log(NULL, AV_LOG_VERBOSE, " %2d", av_log2(2*thistogram[i]));\
112 av_log(NULL, AV_LOG_ERROR, "\n"); \
116 #if CONFIG_LINUX_PERF
118 #define START_TIMER \
119 static int linux_perf_fd = -1; \
121 if (linux_perf_fd == -1) { \
122 struct perf_event_attr attr = { \
123 .type = PERF_TYPE_HARDWARE, \
124 .size = sizeof(struct perf_event_attr), \
125 .config = PERF_COUNT_HW_CPU_CYCLES, \
127 .exclude_kernel = 1, \
130 linux_perf_fd = syscall(__NR_perf_event_open, &attr, \
133 if (linux_perf_fd == -1) { \
134 av_log(NULL, AV_LOG_ERROR, "perf_event_open failed: %s\n", \
135 av_err2str(AVERROR(errno))); \
137 ioctl(linux_perf_fd, PERF_EVENT_IOC_RESET, 0); \
138 ioctl(linux_perf_fd, PERF_EVENT_IOC_ENABLE, 0); \
141 #define STOP_TIMER(id) \
142 ioctl(linux_perf_fd, PERF_EVENT_IOC_DISABLE, 0); \
143 read(linux_perf_fd, &tperf, sizeof(tperf)); \
144 TIMER_REPORT(id, tperf)
146 #elif CONFIG_MACOS_KPERF
148 #define START_TIMER \
151 tperf = ff_kperf_cycles();
153 #define STOP_TIMER(id) \
154 TIMER_REPORT(id, ff_kperf_cycles() - tperf);
156 #elif defined(AV_READ_TIME)
157 #define START_TIMER \
159 uint64_t tstart = AV_READ_TIME(); \
161 #define STOP_TIMER(id) \
162 tend = AV_READ_TIME(); \
163 TIMER_REPORT(id, tend - tstart)
166 #define STOP_TIMER(id) { }
169 #endif /* AVUTIL_TIMER_H */