xref: /linux/tools/testing/selftests/damon/access_memory.c (revision 55d0969c451159cff86949b38c39171cab962069)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Artificial memory access program for testing DAMON.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <time.h>
10 
11 int main(int argc, char *argv[])
12 {
13 	char **regions;
14 	clock_t start_clock;
15 	int nr_regions;
16 	int sz_region;
17 	int access_time_ms;
18 	int i;
19 
20 	if (argc != 4) {
21 		printf("Usage: %s <number> <size (bytes)> <time (ms)>\n",
22 				argv[0]);
23 		return -1;
24 	}
25 
26 	nr_regions = atoi(argv[1]);
27 	sz_region = atoi(argv[2]);
28 	access_time_ms = atoi(argv[3]);
29 
30 	regions = malloc(sizeof(*regions) * nr_regions);
31 	for (i = 0; i < nr_regions; i++)
32 		regions[i] = malloc(sz_region);
33 
34 	for (i = 0; i < nr_regions; i++) {
35 		start_clock = clock();
36 		while ((clock() - start_clock) * 1000 / CLOCKS_PER_SEC <
37 				access_time_ms)
38 			memset(regions[i], i, sz_region);
39 	}
40 	return 0;
41 }
42