xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/ksm/ksm02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (C) 2010-2017  Red Hat, Inc.
3  *
4  * This program is free software;  you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  * the GNU General Public License for more details.
13  *
14  * Kernel Samepage Merging (KSM)
15  *
16  * Basic tests were to start several programs with same and different
17  * memory contents and ensure only to merge the ones with the same
18  * contents. When changed the content of one of merged pages in a
19  * process and to the mode "unmerging", it should discard all merged
20  * pages there. Also tested it is possible to disable KSM. There are
21  * also command-line options to specify the memory allocation size, and
22  * number of processes have same memory contents so it is possible to
23  * test more advanced things like KSM + OOM etc.
24  *
25  * Prerequisites:
26  *
27  * 1) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
28  *    distrub the testing as they also change some ksm tunables depends
29  *    on current workloads.
30  *
31  * The test steps are:
32  * - Check ksm feature and backup current run setting.
33  * - Change run setting to 1 - merging.
34  * - 3 memory allocation programs have the memory contents that 2 of
35  *   them are all 'a' and one is all 'b'.
36  * - Check ksm statistics and verify the content.
37  * - 1 program changes the memory content from all 'a' to all 'b'.
38  * - Check ksm statistics and verify the content.
39  * - All programs change the memory content to all 'd'.
40  * - Check ksm statistics and verify the content.
41  * - Change one page of a process.
42  * - Check ksm statistics and verify the content.
43  * - Change run setting to 2 - unmerging.
44  * - Check ksm statistics and verify the content.
45  * - Change run setting to 0 - stop.
46  */
47 
48 #include "config.h"
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <unistd.h>
56 #include "mem.h"
57 #include "ksm_common.h"
58 
59 #ifdef HAVE_NUMA_V2
60 #include <numaif.h>
61 
verify_ksm(void)62 static void verify_ksm(void)
63 {
64 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
65 	unsigned int node;
66 
67 	node = get_a_numa_node();
68 	set_node(nmask, node);
69 
70 	if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
71 		if (errno != ENOSYS)
72 			tst_brk(TBROK | TERRNO, "set_mempolicy");
73 		else
74 			tst_brk(TCONF, "set_mempolicy syscall is not "
75 				 "implemented on your system.");
76 	}
77 	create_same_memory(size, num, unit);
78 
79 	write_cpusets(tst_cg, node);
80 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
81 	create_same_memory(size, num, unit);
82 	SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid());
83 }
84 
setup(void)85 static void setup(void)
86 {
87 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
88 
89 	if (opt_sizestr && size > DEFAULT_MEMSIZE)
90 		tst_set_max_runtime(32 * (size / DEFAULT_MEMSIZE));
91 }
92 
93 static struct tst_test test = {
94 	.needs_root = 1,
95 	.forks_child = 1,
96 	.options = (struct tst_option[]) {
97 		{"n:", &opt_numstr,  "Number of processes"},
98 		{"s:", &opt_sizestr, "Memory allocation size in MB"},
99 		{"u:", &opt_unitstr, "Memory allocation unit in MB"},
100 		{}
101 	},
102 	.setup = setup,
103 	.save_restore = (const struct tst_path_val[]) {
104 		{"/sys/kernel/mm/ksm/run", NULL, TST_SR_TBROK},
105 		{"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TBROK},
106 		{"/sys/kernel/mm/ksm/max_page_sharing", NULL,
107 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
108 		{"/sys/kernel/mm/ksm/merge_across_nodes", "1",
109 			TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
110 		{"/sys/kernel/mm/ksm/smart_scan", "0",
111 			TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
112 		{}
113 	},
114 	.needs_kconfigs = (const char *const[]){
115 		"CONFIG_KSM=y",
116 		NULL
117 	},
118 	.test_all = verify_ksm,
119 	.max_runtime = 32,
120 	.needs_cgroup_ctrls = (const char *const []){ "cpuset", NULL },
121 };
122 
123 #else
124 	TST_TEST_TCONF(NUMA_ERROR_MSG);
125 #endif
126