Coverage Report

Created: 2026-10-28 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavutil/timer.h
Line
Count
Source
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_PPC
61
#   include "ppc/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
#   elif HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
74
        static inline int64_t ff_read_time(void)
75
0
        {
76
0
            struct timespec ts;
77
0
            clock_gettime(CLOCK_MONOTONIC, &ts);
78
0
            return ts.tv_sec * INT64_C(1000000000) + ts.tv_nsec;
79
0
        }
80
0
#       define AV_READ_TIME ff_read_time
81
#       define FF_TIMER_UNITS "ns"
82
#   endif
83
#endif
84
85
#ifndef FF_TIMER_UNITS
86
#   define FF_TIMER_UNITS "UNITS"
87
#endif
88
89
#define TIMER_REPORT(id, tdiff)                                           \
90
    {                                                                     \
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)]++;                                     \
96
        if (tcount < 2                ||                                  \
97
            (tdiff) < 8 * tsum / tcount ||                                \
98
            (tdiff) < 2000) {                                             \
99
            tsum += (tdiff);                                              \
100
            tcount++;                                                     \
101
        } else                                                            \
102
            tskip_count++;                                                \
103
        if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \
104
            int i;                                                        \
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");                             \
111
        }                                                                 \
112
    }
113
114
#if CONFIG_LINUX_PERF
115
116
#define START_TIMER                                                         \
117
    static int linux_perf_fd = -1;                                          \
118
    uint64_t tperf;                                                         \
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,                     \
124
            .disabled       = 1,                                            \
125
            .exclude_kernel = 1,                                            \
126
            .exclude_hv     = 1,                                            \
127
        };                                                                  \
128
        linux_perf_fd = syscall(__NR_perf_event_open, &attr,                \
129
                                0, -1, -1, 0);                              \
130
    }                                                                       \
131
    if (linux_perf_fd == -1) {                                              \
132
        av_log(NULL, AV_LOG_ERROR, "perf_event_open failed: %s\n",          \
133
               av_err2str(AVERROR(errno)));                                 \
134
    } else {                                                                \
135
        ioctl(linux_perf_fd, PERF_EVENT_IOC_RESET, 0);                      \
136
        ioctl(linux_perf_fd, PERF_EVENT_IOC_ENABLE, 0);                     \
137
    }
138
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))        \
142
        tperf = 0;                                                          \
143
    TIMER_REPORT(id, tperf)
144
145
#elif CONFIG_MACOS_KPERF
146
147
#define START_TIMER                                                         \
148
    uint64_t tperf;                                                         \
149
    ff_kperf_init();                                                        \
150
    tperf = ff_kperf_cycles();
151
152
#define STOP_TIMER(id)                                                      \
153
    TIMER_REPORT(id, ff_kperf_cycles() - tperf);
154
155
#elif defined(AV_READ_TIME)
156
#define START_TIMER                             \
157
    uint64_t tend;                              \
158
    uint64_t tstart = AV_READ_TIME();           \
159
160
#define STOP_TIMER(id)                                                    \
161
    tend = AV_READ_TIME();                                                \
162
    TIMER_REPORT(id, tend - tstart)
163
#else
164
#define START_TIMER
165
#define STOP_TIMER(id) { }
166
#endif
167
168
#endif /* AVUTIL_TIMER_H */