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