1 /*
2 * Copyright (c) 2020-2021 The strace developers.
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "tests.h"
9 #include "scno.h"
10 #include "pidns.h"
11
12 #if defined __NR_getpid && (!defined __NR_getxpid || __NR_getxpid != __NR_getpid)
13
14 # include <stdio.h>
15 # include <unistd.h>
16 # include <sys/time.h>
17
18 # define SYSCALL_COUNT 10000
19
20 /**
21 * Max ratio of the execution time with and without pidns translation.
22 */
23 # define MAX_TIME_RATIO 20
24
25 static long
26 execute_syscalls(void)
27 {
28 /* Load our PID in the cache */
29 syscall(__NR_getpid);
30
31 struct timeval stop, start;
32 gettimeofday(&start, NULL);
33
34 for (int i = 0; i < SYSCALL_COUNT; i++)
35 syscall(__NR_getpid);
36
37 gettimeofday(&stop, NULL);
38
39 return (stop.tv_usec - start.tv_usec) +
40 (stop.tv_sec - start.tv_sec) * 1000000;
41 }
42
43 int
44 main(void)
45 {
46 long orig_us = execute_syscalls();
47 long max_us = orig_us * MAX_TIME_RATIO;
48
49 pidns_test_init();
50
51 long us = execute_syscalls();
52
53 fprintf(stderr, "Before PID NS test init: %ld\n"
54 "After PID NS test init: %ld (%.2f times slower)\n",
55 orig_us, us, (float) us / orig_us);
56
57 if (us > max_us)
58 error_msg_and_fail("pidns translation took too long: %ld us "
59 "(max: %ld us)", us, max_us);
60
61 return 0;
62 }
63
64 #else
65
66 SKIP_MAIN_UNDEFINED("__NR_getpid")
67
68 #endif