FFmpeg
linux.c
Go to the documentation of this file.
1 /*
2  * Copyright © 2025, Niklas Haas
3  * Copyright © 2018, VideoLAN and dav1d authors
4  * Copyright © 2018, Two Orioles, LLC
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "checkasm_config.h"
30 
31 #if HAVE_LINUX_PERF
32 
33  #ifndef _GNU_SOURCE
34  #define _GNU_SOURCE
35  #endif
36 
37  #include <linux/perf_event.h>
38  #include <sys/ioctl.h>
39  #include <sys/syscall.h>
40  #include <unistd.h>
41 
42  #include "internal.h"
43 
44 static int perf_sysfd = -1;
45 
46 static uint64_t perf_start(void)
47 {
48  ioctl(perf_sysfd, PERF_EVENT_IOC_RESET, 0);
49  ioctl(perf_sysfd, PERF_EVENT_IOC_ENABLE, 0);
50  return 0;
51 }
52 
53 static uint64_t perf_stop(uint64_t t)
54 {
55  ioctl(perf_sysfd, PERF_EVENT_IOC_DISABLE, 0);
56  long ret = read(perf_sysfd, &t, sizeof(t));
57  (void) ret;
58  return t;
59 }
60 
62 {
63  struct perf_event_attr attr = {
64  .type = PERF_TYPE_HARDWARE,
65  .size = sizeof(struct perf_event_attr),
66  .config = PERF_COUNT_HW_CPU_CYCLES,
67  .disabled = 1, // start counting only on demand
68  .exclude_kernel = 1,
69  .exclude_hv = 1,
70 #if !ARCH_X86
71  .exclude_guest = 1,
72 #endif
73  };
74 
75  if (perf_sysfd == -1) {
76  perf_sysfd = (int) syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
77  if (perf_sysfd == -1) {
78  perror("perf_event_open");
79  return 1;
80  }
81  }
82 
83  perf->start = perf_start;
84  perf->stop = perf_stop;
85  perf->name = "linux (perf)";
86  perf->unit = "tick";
88 }
89 
90 #endif /* HAVE_LINUX_PERF */
COLD
#define COLD
Definition: internal.h:45
checkasm_config.h
CheckasmPerf::start
uint64_t(* start)(void)
Start timing measurement.
Definition: test.h:533
tf_sess_config.config
config
Definition: tf_sess_config.py:33
CheckasmPerf
Definition: test.h:527
CheckasmPerf::stop
uint64_t(* stop)(uint64_t start_time)
Stop timing measurement.
Definition: test.h:540
CheckasmPerf::name
const char * name
Name of the timing mechanism (e.g., "clock_gettime")
Definition: test.h:543
checkasm_perf_init_linux
int checkasm_perf_init_linux(CheckasmPerf *perf)
checkasm_perf_validate_start_stop
int checkasm_perf_validate_start_stop(const CheckasmPerf *perf)
Definition: perf.c:150
CheckasmPerf::unit
const char * unit
Unit of measurement (e.g., "ns", "cycles")
Definition: test.h:546
ret
ret
Definition: filter_design.txt:187
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:239