drm/bridge: adv7511: Switch to atomic operations
[drm/drm-misc.git] / tools / testing / selftests / damon / debugfs_target_ids_pid_leak.c
blob0cc2eef7d1425c2f145e760b14832e16501df440
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Author: SeongJae Park <sj@kernel.org>
4 */
6 #define _GNU_SOURCE
8 #include <fcntl.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/time.h>
16 #include <unistd.h>
18 #define DBGFS_TARGET_IDS "/sys/kernel/debug/damon/target_ids"
20 static void write_targetid_exit(void)
22 int target_ids_fd = open(DBGFS_TARGET_IDS, O_RDWR);
23 char pid_str[128];
25 snprintf(pid_str, sizeof(pid_str), "%d", getpid());
26 write(target_ids_fd, pid_str, sizeof(pid_str));
27 close(target_ids_fd);
28 exit(0);
31 unsigned long msec_timestamp(void)
33 struct timeval tv;
35 gettimeofday(&tv, NULL);
36 return tv.tv_sec * 1000UL + tv.tv_usec / 1000;
39 int main(int argc, char *argv[])
41 unsigned long start_ms;
42 int time_to_run, nr_forks = 0;
44 if (argc != 2) {
45 fprintf(stderr, "Usage: %s <msecs to run>\n", argv[0]);
46 exit(1);
48 time_to_run = atoi(argv[1]);
50 start_ms = msec_timestamp();
51 while (true) {
52 int pid = fork();
54 if (pid < 0) {
55 fprintf(stderr, "fork() failed\n");
56 exit(1);
58 if (pid == 0)
59 write_targetid_exit();
60 wait(NULL);
61 nr_forks++;
63 if (msec_timestamp() - start_ms > time_to_run)
64 break;
66 printf("%d\n", nr_forks);
67 return 0;