Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel Samepage Merging (KSM) for NUMA |
| 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 "config.h" |
| 59 | #include <sys/types.h> |
| 60 | #include <sys/stat.h> |
| 61 | #include <errno.h> |
| 62 | #include <fcntl.h> |
| 63 | #if HAVE_NUMAIF_H |
| 64 | #include <numaif.h> |
| 65 | #endif |
| 66 | #include <signal.h> |
| 67 | #include <stdio.h> |
| 68 | #include <unistd.h> |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -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 | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 71 | |
| 72 | char *TCID = "ksm02"; |
| 73 | int TST_TOTAL = 1; |
| 74 | |
Zhouping Liu | fbcafd3 | 2013-05-13 16:43:26 +0800 | [diff] [blame] | 75 | static int merge_across_nodes; |
| 76 | |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 77 | #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \ |
| 78 | && HAVE_MPOL_CONSTANTS |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 79 | option_t ksm_options[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | {"n:", &opt_num, &opt_numstr}, |
| 81 | {"s:", &opt_size, &opt_sizestr}, |
| 82 | {"u:", &opt_unit, &opt_unitstr}, |
| 83 | {NULL, NULL, NULL} |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | int main(int argc, char *argv[]) |
| 87 | { |
| 88 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 89 | const char *msg; |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 90 | int size = 128, num = 3, unit = 1; |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 91 | unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 }; |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 92 | unsigned int node; |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 93 | |
| 94 | msg = parse_opts(argc, argv, ksm_options, ksm_usage); |
| 95 | if (msg != NULL) |
| 96 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 97 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | node = get_a_numa_node(tst_exit); |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 99 | set_node(nmask, node); |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 100 | |
| 101 | setup(); |
| 102 | |
| 103 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 104 | tst_count = 0; |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 105 | check_ksm_options(&size, &num, &unit); |
| 106 | |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 107 | if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) { |
Caspar Zhang | 9b15268 | 2011-04-12 16:13:40 +0800 | [diff] [blame] | 108 | if (errno != ENOSYS) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | tst_brkm(TBROK | TERRNO, cleanup, |
| 110 | "set_mempolicy"); |
Caspar Zhang | 9b15268 | 2011-04-12 16:13:40 +0800 | [diff] [blame] | 111 | else |
Caspar Zhang | e6bb543 | 2011-05-10 23:53:46 +0800 | [diff] [blame] | 112 | tst_brkm(TCONF, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | "set_mempolicy syscall is not " |
| 114 | "implemented on your system."); |
Caspar Zhang | 9b15268 | 2011-04-12 16:13:40 +0800 | [diff] [blame] | 115 | } |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 116 | create_same_memory(size, num, unit); |
| 117 | |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 118 | write_cpusets(node); |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 119 | create_same_memory(size, num, unit); |
| 120 | } |
| 121 | cleanup(); |
| 122 | tst_exit(); |
| 123 | } |
| 124 | |
| 125 | void cleanup(void) |
| 126 | { |
Zhouping Liu | fbcafd3 | 2013-05-13 16:43:26 +0800 | [diff] [blame] | 127 | if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) |
Li Wang | 8bfd7c1 | 2014-12-03 08:11:36 -0500 | [diff] [blame] | 128 | FILE_PRINTF(PATH_KSM "merge_across_nodes", |
Zhouping Liu | fbcafd3 | 2013-05-13 16:43:26 +0800 | [diff] [blame] | 129 | "%d", merge_across_nodes); |
| 130 | |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 131 | umount_mem(CPATH, CPATH_NEW); |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void setup(void) |
| 135 | { |
| 136 | tst_require_root(NULL); |
| 137 | |
Peng Haitao | 800540b | 2011-05-23 17:16:22 +0800 | [diff] [blame] | 138 | if (tst_kvercmp(2, 6, 32) < 0) |
| 139 | tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required"); |
Caspar Zhang | e9d874d | 2011-09-15 16:17:41 +0800 | [diff] [blame] | 140 | if (access(PATH_KSM, F_OK) == -1) |
| 141 | tst_brkm(TCONF, NULL, "KSM configuration is not enabled"); |
Peng Haitao | 800540b | 2011-05-23 17:16:22 +0800 | [diff] [blame] | 142 | |
Zhouping Liu | fbcafd3 | 2013-05-13 16:43:26 +0800 | [diff] [blame] | 143 | if (access(PATH_KSM "merge_across_nodes", F_OK) == 0) { |
| 144 | SAFE_FILE_SCANF(NULL, PATH_KSM "merge_across_nodes", |
| 145 | "%d", &merge_across_nodes); |
| 146 | SAFE_FILE_PRINTF(NULL, PATH_KSM "merge_across_nodes", "1"); |
| 147 | } |
| 148 | |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 149 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 150 | TEST_PAUSE; |
| 151 | mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW); |
| 152 | } |
| 153 | |
| 154 | #else /* no NUMA */ |
| 155 | int main(void) |
| 156 | { |
Caspar Zhang | 05697c4 | 2012-02-20 19:20:57 +0800 | [diff] [blame] | 157 | tst_brkm(TCONF, NULL, "no NUMA development packages installed."); |
Garrett Cooper | f65b86e | 2011-01-19 01:09:37 -0800 | [diff] [blame] | 158 | } |
| 159 | #endif |