69 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * Author: SeongJae Park <sj@kernel.org>4 */5 6#define _GNU_SOURCE7 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>17 18#define DBGFS_TARGET_IDS "/sys/kernel/debug/damon/target_ids"19 20static void write_targetid_exit(void)21{22 int target_ids_fd = open(DBGFS_TARGET_IDS, O_RDWR);23 char pid_str[128];24 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);29}30 31unsigned long msec_timestamp(void)32{33 struct timeval tv;34 35 gettimeofday(&tv, NULL);36 return tv.tv_sec * 1000UL + tv.tv_usec / 1000;37}38 39int main(int argc, char *argv[])40{41 unsigned long start_ms;42 int time_to_run, nr_forks = 0;43 44 if (argc != 2) {45 fprintf(stderr, "Usage: %s <msecs to run>\n", argv[0]);46 exit(1);47 }48 time_to_run = atoi(argv[1]);49 50 start_ms = msec_timestamp();51 while (true) {52 int pid = fork();53 54 if (pid < 0) {55 fprintf(stderr, "fork() failed\n");56 exit(1);57 }58 if (pid == 0)59 write_targetid_exit();60 wait(NULL);61 nr_forks++;62 63 if (msec_timestamp() - start_ms > time_to_run)64 break;65 }66 printf("%d\n", nr_forks);67 return 0;68}69