FFmpeg
libavutil
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
#endif
48
49
#if HAVE_MACH_ABSOLUTE_TIME
50
#include <mach/mach_time.h>
51
#elif HAVE_CLOCK_GETTIME
52
#include <
time.h
>
53
#endif
54
55
#include "
common.h
"
56
#include "
log.h
"
57
58
#if ARCH_AARCH64
59
# include "
aarch64/timer.h
"
60
#elif ARCH_ARM
61
# include "
arm/timer.h
"
62
#elif ARCH_PPC
63
# include "
ppc/timer.h
"
64
#elif ARCH_RISCV
65
# include "
riscv/timer.h
"
66
#elif ARCH_X86
67
# include "
x86/timer.h
"
68
#elif ARCH_LOONGARCH
69
# include "
loongarch/timer.h
"
70
#endif
71
72
#if !defined(AV_READ_TIME)
73
# if HAVE_GETHRTIME
74
# define AV_READ_TIME gethrtime
75
# elif HAVE_MACH_ABSOLUTE_TIME
76
# define AV_READ_TIME mach_absolute_time
77
# elif HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
78
static
inline
int64_t
ff_read_time(
void
)
79
{
80
struct
timespec ts;
81
clock_gettime(CLOCK_MONOTONIC, &ts);
82
return
ts.tv_sec * INT64_C(1000000000) + ts.tv_nsec;
83
}
84
# define AV_READ_TIME ff_read_time
85
# endif
86
#endif
87
88
#ifndef FF_TIMER_UNITS
89
# define FF_TIMER_UNITS "UNITS"
90
#endif
91
92
#define TIMER_REPORT(id, tdiff) \
93
{ \
94
static uint64_t tsum = 0; \
95
static int tcount = 0; \
96
static int tskip_count = 0; \
97
static int thistogram[32] = {0}; \
98
thistogram[av_log2(tdiff)]++; \
99
if (tcount < 2 || \
100
(tdiff) < 8 * tsum / tcount || \
101
(tdiff) < 2000) { \
102
tsum += (tdiff); \
103
tcount++; \
104
} else \
105
tskip_count++; \
106
if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \
107
int i; \
108
av_log(NULL, AV_LOG_ERROR, \
109
"%7" PRIu64 " " FF_TIMER_UNITS " in %s,%8d runs,%7d skips",\
110
tsum * 10 / tcount, id, tcount, tskip_count); \
111
for (i = 0; i < 32; i++) \
112
av_log(NULL, AV_LOG_VERBOSE, " %2d", av_log2(2*thistogram[i]));\
113
av_log(NULL, AV_LOG_ERROR, "\n"); \
114
} \
115
}
116
117
#if CONFIG_LINUX_PERF
118
119
#define START_TIMER \
120
static int linux_perf_fd = -1; \
121
uint64_t tperf; \
122
if (linux_perf_fd == -1) { \
123
struct perf_event_attr attr = { \
124
.type = PERF_TYPE_HARDWARE, \
125
.size = sizeof(struct perf_event_attr), \
126
.config = PERF_COUNT_HW_CPU_CYCLES, \
127
.disabled = 1, \
128
.exclude_kernel = 1, \
129
.exclude_hv = 1, \
130
}; \
131
linux_perf_fd = syscall(__NR_perf_event_open, &attr, \
132
0, -1, -1, 0); \
133
} \
134
if (linux_perf_fd == -1) { \
135
av_log(NULL, AV_LOG_ERROR, "perf_event_open failed: %s\n", \
136
av_err2str(AVERROR(errno))); \
137
} else { \
138
ioctl(linux_perf_fd, PERF_EVENT_IOC_RESET, 0); \
139
ioctl(linux_perf_fd, PERF_EVENT_IOC_ENABLE, 0); \
140
}
141
142
#define STOP_TIMER(id) \
143
ioctl(linux_perf_fd, PERF_EVENT_IOC_DISABLE, 0); \
144
read(linux_perf_fd, &tperf, sizeof(tperf)); \
145
TIMER_REPORT(id, tperf)
146
147
#elif CONFIG_MACOS_KPERF
148
149
#define START_TIMER \
150
uint64_t tperf; \
151
ff_kperf_init(); \
152
tperf = ff_kperf_cycles();
153
154
#define STOP_TIMER(id) \
155
TIMER_REPORT(id, ff_kperf_cycles() - tperf);
156
157
#elif defined(AV_READ_TIME)
158
#define START_TIMER \
159
uint64_t tend; \
160
uint64_t tstart = AV_READ_TIME(); \
161
162
#define STOP_TIMER(id) \
163
tend = AV_READ_TIME(); \
164
TIMER_REPORT(id, tend - tstart)
165
#else
166
#define START_TIMER
167
#define STOP_TIMER(id) { }
168
#endif
169
170
#endif
/* AVUTIL_TIMER_H */
int64_t
long long int64_t
Definition:
coverity.c:34
timer.h
timer.h
macos_kperf.h
time.h
timer.h
log.h
common.h
timer.h
timer.h
timer.h
Generated on Fri Jul 26 2024 19:22:47 for FFmpeg by
1.8.17