Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 1 | /* |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 2 | * Kernel Samepage Merging (KSM) |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 3 | * |
| 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 Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 57 | |
| 58 | #include <sys/types.h> |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 59 | #include <sys/mman.h> |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 60 | #include <sys/stat.h> |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 61 | #include <sys/wait.h> |
| 62 | #include <errno.h> |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 63 | #include <fcntl.h> |
| 64 | #include <signal.h> |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 65 | #include <stdio.h> |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 66 | #include <stdlib.h> |
| 67 | #include <string.h> |
| 68 | #include <unistd.h> |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 69 | #include "test.h" |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 70 | #include "mem.h" |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 71 | |
| 72 | char *TCID = "ksm01"; |
| 73 | int TST_TOTAL = 1; |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 74 | |
Zhouping Liu | fbcafd3 | 2013-05-13 16:43:26 +0800 | [diff] [blame] | 75 | static int merge_across_nodes; |
| 76 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 77 | option_t ksm_options[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | {"n:", &opt_num, &opt_numstr}, |
| 79 | {"s:", &opt_size, &opt_sizestr}, |
| 80 | {"u:", &opt_unit, &opt_unitstr}, |
| 81 | {NULL, NULL, NULL} |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 82 | }; |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 83 | |
| 84 | int main(int argc, char *argv[]) |
| 85 | { |
| 86 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 87 | const char *msg; |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 88 | int size = 128, num = 3, unit = 1; |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 89 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 90 | 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 Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 93 | setup(); |
| 94 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 95 | tst_count = 0; |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 96 | check_ksm_options(&size, &num, &unit); |
| 97 | create_same_memory(size, num, unit); |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 98 | } |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 99 | cleanup(); |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 100 | tst_exit(); |
| 101 | } |
| 102 | |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 103 | void setup(void) |
| 104 | { |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 105 | tst_require_root(NULL); |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 106 | |
Peng Haitao | 800540b | 2011-05-23 17:16:22 +0800 | [diff] [blame] | 107 | if (tst_kvercmp(2, 6, 32) < 0) |
| 108 | tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required"); |
Nageswara R Sastry | c0299bb | 2011-09-08 15:20:32 +0530 | [diff] [blame] | 109 | if (access(PATH_KSM, F_OK) == -1) |
| 110 | tst_brkm(TCONF, NULL, "KSM configuration is not enabled"); |
Peng Haitao | 800540b | 2011-05-23 17:16:22 +0800 | [diff] [blame] | 111 | |
Zhouping Liu | fbcafd3 | 2013-05-13 16:43:26 +0800 | [diff] [blame] | 112 | /* |
| 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 Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 125 | tst_sig(FORK, DEF_HANDLER, NULL); |
| 126 | TEST_PAUSE; |
| 127 | } |
| 128 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 129 | void cleanup(void) |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 130 | { |
Zhouping Liu | fbcafd3 | 2013-05-13 16:43:26 +0800 | [diff] [blame] | 131 | if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) |
Li Wang | 8bfd7c1 | 2014-12-03 08:11:36 -0500 | [diff] [blame] | 132 | FILE_PRINTF(PATH_KSM "merge_across_nodes", |
Zhouping Liu | fbcafd3 | 2013-05-13 16:43:26 +0800 | [diff] [blame] | 133 | "%d", merge_across_nodes); |
Garrett Cooper | 0542c81 | 2010-12-08 00:48:03 -0800 | [diff] [blame] | 134 | } |