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 "config.h"
49*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
54*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
55*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
56*49cdfc7eSAndroid Build Coastguard Worker #include "mem.h"
57*49cdfc7eSAndroid Build Coastguard Worker #include "ksm_common.h"
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_NUMA_V2
60*49cdfc7eSAndroid Build Coastguard Worker #include <numaif.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 unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
65*49cdfc7eSAndroid Build Coastguard Worker unsigned int node;
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker node = get_a_numa_node();
68*49cdfc7eSAndroid Build Coastguard Worker set_node(nmask, node);
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
71*49cdfc7eSAndroid Build Coastguard Worker if (errno != ENOSYS)
72*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "set_mempolicy");
73*49cdfc7eSAndroid Build Coastguard Worker else
74*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "set_mempolicy syscall is not "
75*49cdfc7eSAndroid Build Coastguard Worker "implemented on your system.");
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker create_same_memory(size, num, unit);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker write_cpusets(tst_cg, node);
80*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
81*49cdfc7eSAndroid Build Coastguard Worker create_same_memory(size, num, unit);
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid());
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
setup(void)85*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker if (opt_sizestr && size > DEFAULT_MEMSIZE)
90*49cdfc7eSAndroid Build Coastguard Worker tst_set_max_runtime(32 * (size / DEFAULT_MEMSIZE));
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
94*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
95*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
96*49cdfc7eSAndroid Build Coastguard Worker .options = (struct tst_option[]) {
97*49cdfc7eSAndroid Build Coastguard Worker {"n:", &opt_numstr, "Number of processes"},
98*49cdfc7eSAndroid Build Coastguard Worker {"s:", &opt_sizestr, "Memory allocation size in MB"},
99*49cdfc7eSAndroid Build Coastguard Worker {"u:", &opt_unitstr, "Memory allocation unit in MB"},
100*49cdfc7eSAndroid Build Coastguard Worker {}
101*49cdfc7eSAndroid Build Coastguard Worker },
102*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
103*49cdfc7eSAndroid Build Coastguard Worker .save_restore = (const struct tst_path_val[]) {
104*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/run", NULL, TST_SR_TBROK},
105*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TBROK},
106*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/max_page_sharing", NULL,
107*49cdfc7eSAndroid Build Coastguard Worker TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
108*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/merge_across_nodes", "1",
109*49cdfc7eSAndroid Build Coastguard Worker TST_SR_SKIP_MISSING | TST_SR_TCONF_RO},
110*49cdfc7eSAndroid Build Coastguard Worker {"/sys/kernel/mm/ksm/smart_scan", "0",
111*49cdfc7eSAndroid Build Coastguard Worker TST_SR_SKIP_MISSING | TST_SR_TBROK_RO},
112*49cdfc7eSAndroid Build Coastguard Worker {}
113*49cdfc7eSAndroid Build Coastguard Worker },
114*49cdfc7eSAndroid Build Coastguard Worker .needs_kconfigs = (const char *const[]){
115*49cdfc7eSAndroid Build Coastguard Worker "CONFIG_KSM=y",
116*49cdfc7eSAndroid Build Coastguard Worker NULL
117*49cdfc7eSAndroid Build Coastguard Worker },
118*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_ksm,
119*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 32,
120*49cdfc7eSAndroid Build Coastguard Worker .needs_cgroup_ctrls = (const char *const []){ "cpuset", NULL },
121*49cdfc7eSAndroid Build Coastguard Worker };
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker #else
124*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF(NUMA_ERROR_MSG);
125*49cdfc7eSAndroid Build Coastguard Worker #endif
126