blob: 974fccfe3c1722645acc95ce5c3b603a40bd4995 [file] [log] [blame]
Garrett Cooper0542c812010-12-08 00:48:03 -08001/*
Garrett Cooperecd667e2011-01-19 01:06:18 -08002 * Kernel Samepage Merging (KSM)
Garrett Cooper0542c812010-12-08 00:48:03 -08003 *
4 * Basic tests were to start several programs with same and different
5 * memory contents and ensure only to merge the ones with the same
6 * contents. When changed the content of one of merged pages in a
7 * process and to the mode "unmerging", it should discard all merged
8 * pages there. Also tested it is possible to disable KSM. There are
9 * also command-line options to specify the memory allocation size, and
10 * number of processes have same memory contents so it is possible to
11 * test more advanced things like KSM + OOM etc.
12 *
13 * Prerequisites:
14 *
15 * 1) ksm and ksmtuned daemons need to be disabled. Otherwise, it could
16 * distrub the testing as they also change some ksm tunables depends
17 * on current workloads.
18 *
19 * The test steps are:
20 * - Check ksm feature and backup current run setting.
21 * - Change run setting to 1 - merging.
22 * - 3 memory allocation programs have the memory contents that 2 of
23 * them are all 'a' and one is all 'b'.
24 * - Check ksm statistics and verify the content.
25 * - 1 program changes the memory content from all 'a' to all 'b'.
26 * - Check ksm statistics and verify the content.
27 * - All programs change the memory content to all 'd'.
28 * - Check ksm statistics and verify the content.
29 * - Change one page of a process.
30 * - Check ksm statistics and verify the content.
31 * - Change run setting to 2 - unmerging.
32 * - Check ksm statistics and verify the content.
33 * - Change run setting to 0 - stop.
34 *
35 * Copyright (C) 2010 Red Hat, Inc.
36 *
37 * This program is free software; you can redistribute it and/or
38 * modify it under the terms of version 2 of the GNU General Public
39 * License as published by the Free Software Foundation.
40 *
41 * This program is distributed in the hope that it would be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
44 *
45 * Further, this software is distributed without any warranty that it
46 * is free of the rightful claim of any third person regarding
47 * infringement or the like. Any license provided herein, whether
48 * implied or otherwise, applies only to this software file. Patent
49 * licenses, if any, provided herein do not apply to combinations of
50 * this program with other software, or any other product whatsoever.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, write the Free Software
54 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
55 * 02110-1301, USA.
56 */
Caspar Zhang79667fa2012-03-12 14:41:46 +080057
58#include <sys/types.h>
Garrett Cooper0542c812010-12-08 00:48:03 -080059#include <sys/mman.h>
Garrett Cooper0542c812010-12-08 00:48:03 -080060#include <sys/stat.h>
Caspar Zhang79667fa2012-03-12 14:41:46 +080061#include <sys/wait.h>
62#include <errno.h>
Garrett Cooper0542c812010-12-08 00:48:03 -080063#include <fcntl.h>
64#include <signal.h>
Caspar Zhang79667fa2012-03-12 14:41:46 +080065#include <stdio.h>
Garrett Cooper0542c812010-12-08 00:48:03 -080066#include <stdlib.h>
67#include <string.h>
68#include <unistd.h>
Garrett Cooper0542c812010-12-08 00:48:03 -080069#include "test.h"
Caspar Zhang79667fa2012-03-12 14:41:46 +080070#include "mem.h"
Garrett Cooper0542c812010-12-08 00:48:03 -080071
72char *TCID = "ksm01";
73int TST_TOTAL = 1;
Garrett Cooperecd667e2011-01-19 01:06:18 -080074
Zhouping Liufbcafd32013-05-13 16:43:26 +080075static int merge_across_nodes;
76
Garrett Cooperecd667e2011-01-19 01:06:18 -080077option_t ksm_options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 {"n:", &opt_num, &opt_numstr},
79 {"s:", &opt_size, &opt_sizestr},
80 {"u:", &opt_unit, &opt_unitstr},
81 {NULL, NULL, NULL}
Garrett Cooper0542c812010-12-08 00:48:03 -080082};
Garrett Cooper0542c812010-12-08 00:48:03 -080083
84int main(int argc, char *argv[])
85{
86 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020087 const char *msg;
Garrett Cooperecd667e2011-01-19 01:06:18 -080088 int size = 128, num = 3, unit = 1;
Garrett Cooper0542c812010-12-08 00:48:03 -080089
Garrett Cooperecd667e2011-01-19 01:06:18 -080090 msg = parse_opts(argc, argv, ksm_options, ksm_usage);
91 if (msg != NULL)
92 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper0542c812010-12-08 00:48:03 -080093 setup();
94 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080095 tst_count = 0;
Garrett Cooperecd667e2011-01-19 01:06:18 -080096 check_ksm_options(&size, &num, &unit);
97 create_same_memory(size, num, unit);
Garrett Cooper0542c812010-12-08 00:48:03 -080098 }
Garrett Cooperecd667e2011-01-19 01:06:18 -080099 cleanup();
Garrett Cooper0542c812010-12-08 00:48:03 -0800100 tst_exit();
101}
102
Garrett Cooper0542c812010-12-08 00:48:03 -0800103void setup(void)
104{
Garrett Cooperecd667e2011-01-19 01:06:18 -0800105 tst_require_root(NULL);
Garrett Cooper0542c812010-12-08 00:48:03 -0800106
Peng Haitao800540b2011-05-23 17:16:22 +0800107 if (tst_kvercmp(2, 6, 32) < 0)
108 tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required");
Nageswara R Sastryc0299bb2011-09-08 15:20:32 +0530109 if (access(PATH_KSM, F_OK) == -1)
110 tst_brkm(TCONF, NULL, "KSM configuration is not enabled");
Peng Haitao800540b2011-05-23 17:16:22 +0800111
Zhouping Liufbcafd32013-05-13 16:43:26 +0800112 /*
113 * kernel commit 90bd6fd introduced a new KSM sysfs knob
114 * /sys/kernel/mm/ksm/merge_across_nodes, setting it to '0'
115 * will prevent KSM pages being merged across numa nodes,
116 * which will cause the case fail, so we need to make sure
117 * it is enabled before testing.
118 */
119 if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) {
120 SAFE_FILE_SCANF(NULL, PATH_KSM "merge_across_nodes",
121 "%d", &merge_across_nodes);
122 SAFE_FILE_PRINTF(NULL, PATH_KSM "merge_across_nodes", "1");
123 }
124
Garrett Cooper0542c812010-12-08 00:48:03 -0800125 tst_sig(FORK, DEF_HANDLER, NULL);
126 TEST_PAUSE;
127}
128
Garrett Cooperecd667e2011-01-19 01:06:18 -0800129void cleanup(void)
Garrett Cooper0542c812010-12-08 00:48:03 -0800130{
Zhouping Liufbcafd32013-05-13 16:43:26 +0800131 if (access(PATH_KSM "merge_across_nodes", F_OK) == 0)
Li Wang8bfd7c12014-12-03 08:11:36 -0500132 FILE_PRINTF(PATH_KSM "merge_across_nodes",
Zhouping Liufbcafd32013-05-13 16:43:26 +0800133 "%d", merge_across_nodes);
Garrett Cooper0542c812010-12-08 00:48:03 -0800134}