FFmpeg
timer.h
Go to the documentation of this file.
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * high precision timer, useful to profile code
24  */
25 
26 #ifndef AVUTIL_TIMER_H
27 #define AVUTIL_TIMER_H
28 
29 #include "config.h"
30 
31 #if CONFIG_LINUX_PERF
32 # ifndef _GNU_SOURCE
33 # define _GNU_SOURCE
34 # endif
35 # include <unistd.h> // read(3)
36 # include <sys/ioctl.h>
37 # include <asm/unistd.h>
38 # include <linux/perf_event.h>
39 #endif
40 
41 #include <stdlib.h>
42 #include <stdint.h>
43 #include <inttypes.h>
44 
45 #if CONFIG_MACOS_KPERF
46 #include "macos_kperf.h"
47 #elif HAVE_MACH_ABSOLUTE_TIME
48 #include <mach/mach_time.h>
49 #endif
50 
51 #include "common.h"
52 #include "log.h"
53 
54 #if ARCH_AARCH64
55 # include "aarch64/timer.h"
56 #elif ARCH_ARM
57 # include "arm/timer.h"
58 #elif ARCH_PPC
59 # include "ppc/timer.h"
60 #elif ARCH_RISCV
61 # include "riscv/timer.h"
62 #elif ARCH_X86
63 # include "x86/timer.h"
64 #elif ARCH_LOONGARCH
65 # include "loongarch/timer.h"
66 #endif
67 
68 #if !defined(AV_READ_TIME)
69 # if HAVE_GETHRTIME
70 # define AV_READ_TIME gethrtime
71 # elif HAVE_MACH_ABSOLUTE_TIME
72 # define AV_READ_TIME mach_absolute_time
73 # endif
74 #endif
75 
76 #ifndef FF_TIMER_UNITS
77 # define FF_TIMER_UNITS "UNITS"
78 #endif
79 
80 #define TIMER_REPORT(id, tdiff) \
81  { \
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)]++; \
87  if (tcount < 2 || \
88  (tdiff) < 8 * tsum / tcount || \
89  (tdiff) < 2000) { \
90  tsum += (tdiff); \
91  tcount++; \
92  } else \
93  tskip_count++; \
94  if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \
95  int i; \
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"); \
102  } \
103  }
104 
105 #if CONFIG_LINUX_PERF
106 
107 #define START_TIMER \
108  static int linux_perf_fd; \
109  uint64_t tperf; \
110  if (!linux_perf_fd) { \
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, \
115  .disabled = 1, \
116  .exclude_kernel = 1, \
117  .exclude_hv = 1, \
118  }; \
119  linux_perf_fd = syscall(__NR_perf_event_open, &attr, \
120  0, -1, -1, 0); \
121  } \
122  if (linux_perf_fd == -1) { \
123  av_log(NULL, AV_LOG_ERROR, "perf_event_open failed: %s\n", \
124  av_err2str(AVERROR(errno))); \
125  } else { \
126  ioctl(linux_perf_fd, PERF_EVENT_IOC_RESET, 0); \
127  ioctl(linux_perf_fd, PERF_EVENT_IOC_ENABLE, 0); \
128  }
129 
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)
134 
135 #elif CONFIG_MACOS_KPERF
136 
137 #define START_TIMER \
138  uint64_t tperf; \
139  ff_kperf_init(); \
140  tperf = ff_kperf_cycles();
141 
142 #define STOP_TIMER(id) \
143  TIMER_REPORT(id, ff_kperf_cycles() - tperf);
144 
145 #elif defined(AV_READ_TIME)
146 #define START_TIMER \
147  uint64_t tend; \
148  uint64_t tstart = AV_READ_TIME(); \
149 
150 #define STOP_TIMER(id) \
151  tend = AV_READ_TIME(); \
152  TIMER_REPORT(id, tend - tstart)
153 #else
154 #define START_TIMER
155 #define STOP_TIMER(id) { }
156 #endif
157 
158 #endif /* AVUTIL_TIMER_H */
timer.h
timer.h
macos_kperf.h
timer.h
log.h
common.h
timer.h
timer.h
timer.h