xref: /kvm-unit-tests/common/memory-verify.c (revision fe151b46d2870e67be32b1a388afa84d7424c1a4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Simple memory verification test, used to exercise dirty memory migration.
4  */
5 #include <libcflat.h>
6 #include <migrate.h>
7 #include <alloc.h>
8 #include <asm/page.h>
9 #include <asm/time.h>
10 
11 #define NR_PAGES 32
12 #define SIZE (NR_PAGES * PAGE_SIZE)
13 
14 static unsigned time_sec = 5;
15 
do_getopts(int argc,char ** argv)16 static void do_getopts(int argc, char **argv)
17 {
18 	int i;
19 
20 	for (i = 0; i < argc; ++i) {
21 		if (strcmp(argv[i], "-t") == 0) {
22 			i++;
23 			if (i == argc)
24 				break;
25 			time_sec = atol(argv[i]);
26 		}
27 	}
28 
29 	printf("running for %d secs\n", time_sec);
30 }
31 
main(int argc,char ** argv)32 int main(int argc, char **argv)
33 {
34 	void *mem = memalign(PAGE_SIZE, SIZE);
35 	bool success = true;
36 	uint64_t ms;
37 	long i;
38 
39 	do_getopts(argc, argv);
40 
41 	report_prefix_push("memory");
42 
43 	memset(mem, 0, SIZE);
44 
45 	migrate_begin_continuous();
46 	ms = get_clock_ms();
47 	i = 0;
48 	do {
49 		int j;
50 
51 		for (j = 0; j < SIZE; j += PAGE_SIZE) {
52 			if (*(volatile long *)(mem + j) != i) {
53 				success = false;
54 				goto out;
55 			}
56 			*(volatile long *)(mem + j) = i + 1;
57 		}
58 		i++;
59 	} while (get_clock_ms() - ms < time_sec * 1000);
60 out:
61 	migrate_end_continuous();
62 
63 	report(success, "memory verification stress test");
64 
65 	report_prefix_pop();
66 
67 	return report_summary();
68 }
69