Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 1 | #include "config.h" |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 2 | #include <sys/types.h> |
| 3 | #include <sys/mman.h> |
| 4 | #include <sys/mount.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/wait.h> |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #if HAVE_NUMA_H |
Zhouping Liu | 9b70f09 | 2012-02-15 23:18:48 +0800 | [diff] [blame] | 10 | #include <numa.h> |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 11 | #endif |
| 12 | #if HAVE_NUMAIF_H |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 13 | #include <numaif.h> |
| 14 | #endif |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include "test.h" |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 21 | #include "safe_macros.h" |
Zhouping Liu | 448c1ee | 2013-03-19 11:40:49 +0800 | [diff] [blame] | 22 | #include "safe_file_ops.h" |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 23 | #include "mem.h" |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 24 | #include "numa_helper.h" |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 25 | |
Caspar Zhang | a7f0eed | 2012-02-28 15:44:04 +0800 | [diff] [blame] | 26 | /* OOM */ |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 27 | |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 28 | static int alloc_mem(long int length, int testcase) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 29 | { |
Jan Stancek | 7adb6ce | 2014-07-29 14:04:04 +0200 | [diff] [blame] | 30 | char *s; |
| 31 | long i, pagesz = getpagesize(); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 32 | |
| 33 | tst_resm(TINFO, "allocating %ld bytes.", length); |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 34 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 35 | s = mmap(NULL, length, PROT_READ | PROT_WRITE, |
| 36 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 37 | if (s == MAP_FAILED) |
| 38 | return errno; |
| 39 | |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 40 | if (testcase == MLOCK && mlock(s, length) == -1) |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 41 | return errno; |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 42 | #ifdef HAVE_MADV_MERGEABLE |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 43 | if (testcase == KSM && madvise(s, length, MADV_MERGEABLE) == -1) |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 44 | return errno; |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 45 | #endif |
Jan Stancek | 7adb6ce | 2014-07-29 14:04:04 +0200 | [diff] [blame] | 46 | for (i = 0; i < length; i += pagesz) |
| 47 | s[i] = '\a'; |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 52 | static void test_alloc(int testcase, int lite) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 53 | { |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 54 | int ret; |
| 55 | |
| 56 | if (lite) { |
| 57 | ret = alloc_mem(TESTMEM + MB, testcase); |
| 58 | } else { |
| 59 | ret = 0; |
| 60 | while (!ret) |
| 61 | ret = alloc_mem(LENGTH, testcase); |
| 62 | } |
| 63 | exit(ret); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 64 | } |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 65 | |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 66 | /* |
| 67 | * oom - allocates memory according to specified testcase and checks |
| 68 | * desired outcome (e.g. child killed, operation failed with ENOMEM) |
| 69 | * @testcase: selects how child allocates memory |
| 70 | * valid choices are: OVERCOMMIT, NORMAL, MLOCK and KSM |
| 71 | * @lite: if non-zero, child makes only single TESTMEM+MB allocation |
| 72 | * if zero, child keeps allocating memory until it gets killed |
| 73 | * or some operation fails |
| 74 | * @retcode: expected return code of child process |
| 75 | * if matches child ret code, this function reports PASS, |
| 76 | * otherwise it reports FAIL |
| 77 | * @allow_sigkill: if zero and child is killed, this function reports FAIL |
| 78 | * if non-zero, then if child is killed by SIGKILL |
| 79 | * it is considered as PASS |
| 80 | */ |
| 81 | void oom(int testcase, int lite, int retcode, int allow_sigkill) |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 82 | { |
| 83 | pid_t pid; |
| 84 | int status; |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 85 | |
Caspar Zhang | 05697c4 | 2012-02-20 19:20:57 +0800 | [diff] [blame] | 86 | switch (pid = fork()) { |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 87 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 89 | case 0: |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 90 | test_alloc(testcase, lite); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 91 | default: |
| 92 | break; |
| 93 | } |
Zhouping Liu | 62d50b7 | 2013-03-19 11:40:50 +0800 | [diff] [blame] | 94 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 95 | tst_resm(TINFO, "expected victim is %d.", pid); |
| 96 | if (waitpid(-1, &status, 0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 98 | |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 99 | if (WIFSIGNALED(status)) { |
| 100 | if (allow_sigkill && WTERMSIG(status) == SIGKILL) { |
| 101 | tst_resm(TPASS, "victim signalled: (%d) %s", |
| 102 | SIGKILL, |
| 103 | tst_strsig(SIGKILL)); |
| 104 | } else { |
| 105 | tst_resm(TFAIL, "victim signalled: (%d) %s", |
| 106 | WTERMSIG(status), |
| 107 | tst_strsig(WTERMSIG(status))); |
| 108 | } |
| 109 | } else if (WIFEXITED(status) && WEXITSTATUS(status) == retcode) { |
| 110 | tst_resm(TPASS, "victim retcode: (%d) %s", |
| 111 | retcode, strerror(retcode)); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 112 | } else { |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 113 | tst_resm(TFAIL, "victim unexpectedly ended with retcode: %d, " |
| 114 | "expected: %d", WEXITSTATUS(status), retcode); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Zhouping Liu | d6b67f8 | 2013-04-16 22:30:08 +0800 | [diff] [blame] | 118 | static void set_global_mempolicy(int mempolicy) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 119 | { |
Zhouping Liu | 62d50b7 | 2013-03-19 11:40:50 +0800 | [diff] [blame] | 120 | #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \ |
| 121 | && HAVE_MPOL_CONSTANTS |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 122 | unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 }; |
Zhouping Liu | 65ca1bd | 2013-05-13 16:43:28 +0800 | [diff] [blame] | 123 | int num_nodes, *nodes; |
Zhouping Liu | 62d50b7 | 2013-03-19 11:40:50 +0800 | [diff] [blame] | 124 | int ret; |
| 125 | |
| 126 | if (mempolicy) { |
| 127 | ret = get_allowed_nodes_arr(NH_MEMS|NH_CPUS, &num_nodes, &nodes); |
| 128 | if (ret != 0) |
| 129 | tst_brkm(TBROK|TERRNO, cleanup, |
| 130 | "get_allowed_nodes_arr"); |
| 131 | if (num_nodes < 2) { |
| 132 | tst_resm(TINFO, "mempolicy need NUMA system support"); |
| 133 | free(nodes); |
| 134 | return; |
| 135 | } |
| 136 | switch(mempolicy) { |
| 137 | case MPOL_BIND: |
| 138 | /* bind the second node */ |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 139 | set_node(nmask, nodes[1]); |
Zhouping Liu | 62d50b7 | 2013-03-19 11:40:50 +0800 | [diff] [blame] | 140 | break; |
| 141 | case MPOL_INTERLEAVE: |
| 142 | case MPOL_PREFERRED: |
| 143 | if (num_nodes == 2) { |
| 144 | tst_resm(TINFO, "The mempolicy need " |
| 145 | "more than 2 numa nodes"); |
| 146 | free(nodes); |
| 147 | return; |
| 148 | } else { |
| 149 | /* Using the 2nd,3rd node */ |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 150 | set_node(nmask, nodes[1]); |
| 151 | set_node(nmask, nodes[2]); |
Zhouping Liu | 62d50b7 | 2013-03-19 11:40:50 +0800 | [diff] [blame] | 152 | } |
| 153 | break; |
| 154 | default: |
| 155 | tst_brkm(TBROK|TERRNO, cleanup, "Bad mempolicy mode"); |
| 156 | } |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 157 | if (set_mempolicy(mempolicy, nmask, MAXNODES) == -1) |
Zhouping Liu | 62d50b7 | 2013-03-19 11:40:50 +0800 | [diff] [blame] | 158 | tst_brkm(TBROK|TERRNO, cleanup, "set_mempolicy"); |
| 159 | } |
| 160 | #endif |
Zhouping Liu | d6b67f8 | 2013-04-16 22:30:08 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 163 | void testoom(int mempolicy, int lite, int retcode, int allow_sigkill) |
Zhouping Liu | d6b67f8 | 2013-04-16 22:30:08 +0800 | [diff] [blame] | 164 | { |
| 165 | set_global_mempolicy(mempolicy); |
Caspar Zhang | 54616fd | 2012-02-20 20:10:02 +0800 | [diff] [blame] | 166 | |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 167 | tst_resm(TINFO, "start normal OOM testing."); |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 168 | oom(NORMAL, lite, retcode, allow_sigkill); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 169 | |
| 170 | tst_resm(TINFO, "start OOM testing for mlocked pages."); |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 171 | oom(MLOCK, lite, retcode, allow_sigkill); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 172 | |
Xiaoguang Wang | fc0e139 | 2014-04-28 20:26:09 +0800 | [diff] [blame] | 173 | if (access(PATH_KSM, F_OK) == -1) { |
| 174 | tst_resm(TINFO, "KSM configuration is not enabled, " |
| 175 | "skip OOM test for KSM pags"); |
| 176 | } else { |
| 177 | tst_resm(TINFO, "start OOM testing for KSM pages."); |
Jan Stancek | 30ac84c | 2014-07-25 09:58:47 +0200 | [diff] [blame] | 178 | oom(KSM, lite, retcode, allow_sigkill); |
Xiaoguang Wang | fc0e139 | 2014-04-28 20:26:09 +0800 | [diff] [blame] | 179 | } |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Caspar Zhang | a7f0eed | 2012-02-28 15:44:04 +0800 | [diff] [blame] | 182 | /* KSM */ |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 183 | |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 184 | static void check(char *path, long int value) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 185 | { |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 186 | char fullpath[BUFSIZ]; |
Caspar Zhang | f838d3e | 2012-07-09 17:51:52 +0800 | [diff] [blame] | 187 | long actual_val; |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 188 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 189 | snprintf(fullpath, BUFSIZ, PATH_KSM "%s", path); |
| 190 | SAFE_FILE_SCANF(cleanup, fullpath, "%ld", &actual_val); |
Zhouping Liu | f9ae09a | 2012-06-14 12:01:49 +0800 | [diff] [blame] | 191 | |
Caspar Zhang | f838d3e | 2012-07-09 17:51:52 +0800 | [diff] [blame] | 192 | tst_resm(TINFO, "%s is %ld.", path, actual_val); |
| 193 | if (actual_val != value) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 194 | tst_resm(TFAIL, "%s is not %ld.", path, value); |
| 195 | } |
| 196 | |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 197 | static void wait_ksmd_done(void) |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 198 | { |
Caspar Zhang | f838d3e | 2012-07-09 17:51:52 +0800 | [diff] [blame] | 199 | long pages_shared, pages_sharing, pages_volatile, pages_unshared; |
| 200 | long old_pages_shared = 0, old_pages_sharing = 0; |
| 201 | long old_pages_volatile = 0, old_pages_unshared = 0; |
| 202 | int changing = 1, count = 0; |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 203 | |
Caspar Zhang | f838d3e | 2012-07-09 17:51:52 +0800 | [diff] [blame] | 204 | while (changing) { |
Caspar Zhang | 5ad42f6 | 2012-07-09 17:51:53 +0800 | [diff] [blame] | 205 | sleep(10); |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 206 | count++; |
| 207 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 208 | SAFE_FILE_SCANF(cleanup, PATH_KSM "pages_shared", |
| 209 | "%ld", &pages_shared); |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 210 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 211 | SAFE_FILE_SCANF(cleanup, PATH_KSM "pages_sharing", |
| 212 | "%ld", &pages_sharing); |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 213 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 214 | SAFE_FILE_SCANF(cleanup, PATH_KSM "pages_volatile", |
| 215 | "%ld", &pages_volatile); |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 216 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 217 | SAFE_FILE_SCANF(cleanup, PATH_KSM "pages_unshared", |
| 218 | "%ld", &pages_unshared); |
Caspar Zhang | f838d3e | 2012-07-09 17:51:52 +0800 | [diff] [blame] | 219 | |
| 220 | if (pages_shared != old_pages_shared || |
| 221 | pages_sharing != old_pages_sharing || |
| 222 | pages_volatile != old_pages_volatile || |
| 223 | pages_unshared != old_pages_unshared) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 224 | old_pages_shared = pages_shared; |
| 225 | old_pages_sharing = pages_sharing; |
Caspar Zhang | f838d3e | 2012-07-09 17:51:52 +0800 | [diff] [blame] | 226 | old_pages_volatile = pages_volatile; |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 227 | old_pages_unshared = pages_unshared; |
Caspar Zhang | f838d3e | 2012-07-09 17:51:52 +0800 | [diff] [blame] | 228 | } else { |
| 229 | changing = 0; |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Caspar Zhang | f838d3e | 2012-07-09 17:51:52 +0800 | [diff] [blame] | 233 | tst_resm(TINFO, "ksm daemon takes %ds to scan all mergeable pages", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 234 | count * 10); |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 235 | } |
| 236 | |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 237 | static void group_check(int run, int pages_shared, int pages_sharing, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 238 | int pages_volatile, int pages_unshared, |
| 239 | int sleep_millisecs, int pages_to_scan) |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 240 | { |
Zhouping Liu | 60c41a5 | 2012-06-14 12:01:29 +0800 | [diff] [blame] | 241 | /* wait for ksm daemon to scan all mergeable pages. */ |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 242 | wait_ksmd_done(); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 243 | |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 244 | tst_resm(TINFO, "check!"); |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 245 | check("run", run); |
| 246 | check("pages_shared", pages_shared); |
| 247 | check("pages_sharing", pages_sharing); |
| 248 | check("pages_volatile", pages_volatile); |
| 249 | check("pages_unshared", pages_unshared); |
| 250 | check("sleep_millisecs", sleep_millisecs); |
| 251 | check("pages_to_scan", pages_to_scan); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 254 | static void verify(char **memory, char value, int proc, |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 255 | int start, int end, int start2, int end2) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 256 | { |
| 257 | int i, j; |
| 258 | void *s = NULL; |
| 259 | |
| 260 | s = malloc((end - start) * (end2 - start2)); |
| 261 | if (s == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 262 | tst_brkm(TBROK | TERRNO, tst_exit, "malloc"); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 263 | |
| 264 | tst_resm(TINFO, "child %d verifies memory content.", proc); |
| 265 | memset(s, value, (end - start) * (end2 - start2)); |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 266 | if (memcmp(memory[start], s, (end - start) * (end2 - start2)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 267 | != 0) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 268 | for (j = start; j < end; j++) |
| 269 | for (i = start2; i < end2; i++) |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 270 | if (memory[j][i] != value) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 271 | tst_resm(TFAIL, "child %d has %c at " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 272 | "%d,%d,%d.", |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 273 | proc, memory[j][i], proc, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 274 | j, i); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 275 | free(s); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 278 | void write_memcg(void) |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 279 | { |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 280 | SAFE_FILE_PRINTF(NULL, MEMCG_LIMIT, "%ld", TESTMEM); |
Garrett Cooper | 366a928 | 2011-02-23 21:29:07 -0800 | [diff] [blame] | 281 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 282 | SAFE_FILE_PRINTF(NULL, MEMCG_PATH_NEW "/tasks", "%d", getpid()); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 285 | struct ksm_merge_data { |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 286 | char data; |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 287 | unsigned int mergeable_size; |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | static void ksm_child_memset(int child_num, int size, int total_unit, |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 291 | struct ksm_merge_data ksm_merge_data, char **memory) |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 292 | { |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 293 | int i = 0, j; |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 294 | int unit = size / total_unit; |
| 295 | |
| 296 | tst_resm(TINFO, "child %d continues...", child_num); |
| 297 | |
| 298 | if (ksm_merge_data.mergeable_size == size * MB) { |
| 299 | tst_resm(TINFO, "child %d allocates %d MB filled with '%c'", |
| 300 | child_num, size, ksm_merge_data.data); |
| 301 | |
| 302 | } else { |
| 303 | tst_resm(TINFO, "child %d allocates %d MB filled with '%c'" |
| 304 | " except one page with 'e'", |
| 305 | child_num, size, ksm_merge_data.data); |
| 306 | } |
| 307 | |
| 308 | for (j = 0; j < total_unit; j++) { |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 309 | for (i = 0; (unsigned int)i < unit * MB; i++) |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 310 | memory[j][i] = ksm_merge_data.data; |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* if it contains unshared page, then set 'e' char |
| 314 | * at the end of the last page |
| 315 | */ |
| 316 | if (ksm_merge_data.mergeable_size < size * MB) |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 317 | memory[j-1][i-1] = 'e'; |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static void create_ksm_child(int child_num, int size, int unit, |
| 321 | struct ksm_merge_data *ksm_merge_data) |
| 322 | { |
| 323 | int j, total_unit; |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 324 | char **memory; |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 325 | |
| 326 | /* The total units in all */ |
| 327 | total_unit = size / unit; |
| 328 | |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 329 | /* Apply for the space for memory */ |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 330 | memory = malloc(total_unit * sizeof(char *)); |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 331 | for (j = 0; j < total_unit; j++) { |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 332 | memory[j] = mmap(NULL, unit * MB, PROT_READ|PROT_WRITE, |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 333 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 334 | if (memory[j] == MAP_FAILED) |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 335 | tst_brkm(TBROK|TERRNO, tst_exit, "mmap"); |
| 336 | #ifdef HAVE_MADV_MERGEABLE |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 337 | if (madvise(memory[j], unit * MB, MADV_MERGEABLE) == -1) |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 338 | tst_brkm(TBROK|TERRNO, tst_exit, "madvise"); |
| 339 | #endif |
| 340 | } |
| 341 | |
| 342 | tst_resm(TINFO, "child %d stops.", child_num); |
| 343 | if (raise(SIGSTOP) == -1) |
| 344 | tst_brkm(TBROK|TERRNO, tst_exit, "kill"); |
| 345 | fflush(stdout); |
| 346 | |
| 347 | for (j = 0; j < 4; j++) { |
| 348 | |
| 349 | ksm_child_memset(child_num, size, total_unit, |
Zhouping Liu | f23ce91 | 2013-01-04 14:12:10 +0800 | [diff] [blame] | 350 | ksm_merge_data[j], memory); |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 351 | |
| 352 | fflush(stdout); |
| 353 | |
| 354 | tst_resm(TINFO, "child %d stops.", child_num); |
| 355 | if (raise(SIGSTOP) == -1) |
| 356 | tst_brkm(TBROK|TERRNO, tst_exit, "kill"); |
| 357 | |
| 358 | if (ksm_merge_data[j].mergeable_size < size * MB) { |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 359 | verify(memory, 'e', child_num, total_unit - 1, |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 360 | total_unit, unit * MB - 1, unit * MB); |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 361 | verify(memory, ksm_merge_data[j].data, child_num, |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 362 | 0, total_unit, 0, unit * MB - 1); |
| 363 | } else { |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 364 | verify(memory, ksm_merge_data[j].data, child_num, |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 365 | 0, total_unit, 0, unit * MB); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | tst_resm(TINFO, "child %d finished.", child_num); |
| 370 | } |
| 371 | |
| 372 | static void stop_ksm_children(int *child, int num) |
| 373 | { |
| 374 | int k, status; |
| 375 | |
| 376 | tst_resm(TINFO, "wait for all children to stop."); |
| 377 | for (k = 0; k < num; k++) { |
| 378 | if (waitpid(child[k], &status, WUNTRACED) == -1) |
| 379 | tst_brkm(TBROK|TERRNO, cleanup, "waitpid"); |
| 380 | if (!WIFSTOPPED(status)) |
| 381 | tst_brkm(TBROK, cleanup, "child %d was not stopped", k); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static void resume_ksm_children(int *child, int num) |
| 386 | { |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 387 | int k; |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 388 | |
| 389 | tst_resm(TINFO, "resume all children."); |
| 390 | for (k = 0; k < num; k++) { |
| 391 | if (kill(child[k], SIGCONT) == -1) |
| 392 | tst_brkm(TBROK|TERRNO, cleanup, "kill child[%d]", k); |
| 393 | } |
| 394 | fflush(stdout); |
| 395 | } |
| 396 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 397 | void create_same_memory(int size, int num, int unit) |
| 398 | { |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 399 | int i, j, status, *child; |
| 400 | unsigned long ps, pages; |
| 401 | struct ksm_merge_data **ksm_data; |
| 402 | |
| 403 | struct ksm_merge_data ksm_data0[] = { |
| 404 | {'c', size*MB}, {'c', size*MB}, {'d', size*MB}, {'d', size*MB}, |
| 405 | }; |
| 406 | struct ksm_merge_data ksm_data1[] = { |
| 407 | {'a', size*MB}, {'b', size*MB}, {'d', size*MB}, {'d', size*MB-1}, |
| 408 | }; |
| 409 | struct ksm_merge_data ksm_data2[] = { |
| 410 | {'a', size*MB}, {'a', size*MB}, {'d', size*MB}, {'d', size*MB}, |
| 411 | }; |
Caspar Zhang | 346aab2 | 2011-05-12 00:52:37 +0800 | [diff] [blame] | 412 | |
| 413 | ps = sysconf(_SC_PAGE_SIZE); |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 414 | pages = MB / ps; |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 415 | |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 416 | ksm_data = malloc((num - 3) * sizeof(struct ksm_merge_data *)); |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 417 | /* Since from third child, the data is same with the first child's */ |
| 418 | for (i = 0; i < num - 3; i++) { |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 419 | ksm_data[i] = malloc(4 * sizeof(struct ksm_merge_data)); |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 420 | for (j = 0; j < 4; j++) { |
| 421 | ksm_data[i][j].data = ksm_data0[j].data; |
| 422 | ksm_data[i][j].mergeable_size = |
| 423 | ksm_data0[j].mergeable_size; |
| 424 | } |
| 425 | } |
| 426 | |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 427 | child = malloc(num * sizeof(int)); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 428 | if (child == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 429 | tst_brkm(TBROK | TERRNO, cleanup, "malloc"); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 430 | |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 431 | for (i = 0; i < num; i++) { |
| 432 | fflush(stdout); |
| 433 | switch (child[i] = fork()) { |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 434 | case -1: |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 435 | tst_brkm(TBROK|TERRNO, cleanup, "fork"); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 436 | case 0: |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 437 | if (i == 0) { |
| 438 | create_ksm_child(i, size, unit, ksm_data0); |
| 439 | exit(0); |
| 440 | } else if (i == 1) { |
| 441 | create_ksm_child(i, size, unit, ksm_data1); |
| 442 | exit(0); |
| 443 | } else if (i == 2) { |
| 444 | create_ksm_child(i, size, unit, ksm_data2); |
| 445 | exit(0); |
| 446 | } else { |
| 447 | create_ksm_child(i, size, unit, ksm_data[i-3]); |
| 448 | exit(0); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 449 | } |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 452 | |
| 453 | stop_ksm_children(child, num); |
| 454 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 455 | tst_resm(TINFO, "KSM merging..."); |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 456 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "1"); |
| 457 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "pages_to_scan", "%ld", |
| 458 | size * pages *num); |
| 459 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "sleep_millisecs", "0"); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 460 | |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 461 | resume_ksm_children(child, num); |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 462 | group_check(1, 2, size * num * pages - 2, 0, 0, 0, size * pages * num); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 463 | |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 464 | stop_ksm_children(child, num); |
| 465 | resume_ksm_children(child, num); |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 466 | group_check(1, 3, size * num * pages - 3, 0, 0, 0, size * pages * num); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 467 | |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 468 | stop_ksm_children(child, num); |
| 469 | resume_ksm_children(child, num); |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 470 | group_check(1, 1, size * num * pages - 1, 0, 0, 0, size * pages * num); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 471 | |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 472 | stop_ksm_children(child, num); |
| 473 | resume_ksm_children(child, num); |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 474 | group_check(1, 1, size * num * pages - 2, 0, 1, 0, size * pages * num); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 475 | |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 476 | stop_ksm_children(child, num); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 477 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 478 | tst_resm(TINFO, "KSM unmerging..."); |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 479 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "2"); |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 480 | |
| 481 | resume_ksm_children(child, num); |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 482 | group_check(2, 0, 0, 0, 0, 0, size * pages * num); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 483 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 484 | tst_resm(TINFO, "stop KSM."); |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 485 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "0"); |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 486 | group_check(0, 0, 0, 0, 0, 0, size * pages * num); |
Zhouping Liu | f2dc277 | 2013-01-04 14:12:09 +0800 | [diff] [blame] | 487 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 488 | while (waitpid(-1, &status, WUNTRACED | WCONTINUED) > 0) |
| 489 | if (WEXITSTATUS(status) != 0) |
| 490 | tst_resm(TFAIL, "child exit status is %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 491 | WEXITSTATUS(status)); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Zhouping Liu | 65ca1bd | 2013-05-13 16:43:28 +0800 | [diff] [blame] | 494 | void test_ksm_merge_across_nodes(unsigned long nr_pages) |
| 495 | { |
| 496 | char **memory; |
| 497 | int i, ret; |
| 498 | int num_nodes, *nodes; |
| 499 | unsigned long length; |
| 500 | unsigned long pagesize; |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 501 | |
| 502 | #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \ |
| 503 | && HAVE_MPOL_CONSTANTS |
Zhouping Liu | 65ca1bd | 2013-05-13 16:43:28 +0800 | [diff] [blame] | 504 | unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 }; |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 505 | #endif |
Zhouping Liu | 65ca1bd | 2013-05-13 16:43:28 +0800 | [diff] [blame] | 506 | |
| 507 | ret = get_allowed_nodes_arr(NH_MEMS|NH_CPUS, &num_nodes, &nodes); |
| 508 | if (ret != 0) |
| 509 | tst_brkm(TBROK|TERRNO, cleanup, "get_allowed_nodes_arr"); |
| 510 | if (num_nodes < 2) { |
| 511 | tst_resm(TINFO, "need NUMA system support"); |
| 512 | free(nodes); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | pagesize = sysconf(_SC_PAGE_SIZE); |
| 517 | length = nr_pages * pagesize; |
| 518 | |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 519 | memory = malloc(num_nodes * sizeof(char *)); |
Zhouping Liu | 65ca1bd | 2013-05-13 16:43:28 +0800 | [diff] [blame] | 520 | for (i = 0; i < num_nodes; i++) { |
| 521 | memory[i] = mmap(NULL, length, PROT_READ|PROT_WRITE, |
| 522 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
| 523 | if (memory[i] == MAP_FAILED) |
| 524 | tst_brkm(TBROK|TERRNO, tst_exit, "mmap"); |
DAN LI | 8cda96c | 2013-05-17 10:32:16 +0800 | [diff] [blame] | 525 | #ifdef HAVE_MADV_MERGEABLE |
Zhouping Liu | 65ca1bd | 2013-05-13 16:43:28 +0800 | [diff] [blame] | 526 | if (madvise(memory[i], length, MADV_MERGEABLE) == -1) |
| 527 | tst_brkm(TBROK|TERRNO, tst_exit, "madvise"); |
DAN LI | 8cda96c | 2013-05-17 10:32:16 +0800 | [diff] [blame] | 528 | #endif |
Zhouping Liu | 65ca1bd | 2013-05-13 16:43:28 +0800 | [diff] [blame] | 529 | |
| 530 | #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \ |
| 531 | && HAVE_MPOL_CONSTANTS |
| 532 | clean_node(nmask); |
| 533 | set_node(nmask, nodes[i]); |
| 534 | /* |
| 535 | * Use mbind() to make sure each node contains |
| 536 | * length size memory. |
| 537 | */ |
| 538 | ret = mbind(memory[i], length, MPOL_BIND, nmask, MAXNODES, 0); |
| 539 | if (ret == -1) |
| 540 | tst_brkm(TBROK|TERRNO, tst_exit, "mbind"); |
| 541 | #endif |
| 542 | |
| 543 | memset(memory[i], 10, length); |
| 544 | } |
| 545 | |
| 546 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "sleep_millisecs", "0"); |
| 547 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "pages_to_scan", "%ld", |
| 548 | nr_pages * num_nodes); |
| 549 | /* |
| 550 | * merge_across_nodes setting can be changed only when there |
| 551 | * are no ksm shared pages in system, so set run 2 to unmerge |
| 552 | * pages first, then to 1 after changing merge_across_nodes, |
| 553 | * to remerge according to the new setting. |
| 554 | */ |
| 555 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "2"); |
| 556 | wait_ksmd_done(); |
| 557 | tst_resm(TINFO, "Start to test KSM with merge_across_nodes=1"); |
| 558 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "merge_across_nodes", "1"); |
| 559 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "1"); |
| 560 | group_check(1, 1, nr_pages * num_nodes - 1, 0, 0, 0, |
| 561 | nr_pages * num_nodes); |
| 562 | |
| 563 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "2"); |
| 564 | wait_ksmd_done(); |
| 565 | tst_resm(TINFO, "Start to test KSM with merge_across_nodes=0"); |
| 566 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "merge_across_nodes", "0"); |
| 567 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "1"); |
| 568 | group_check(1, num_nodes, nr_pages * num_nodes - num_nodes, |
| 569 | 0, 0, 0, nr_pages * num_nodes); |
| 570 | |
| 571 | SAFE_FILE_PRINTF(cleanup, PATH_KSM "run", "2"); |
| 572 | wait_ksmd_done(); |
| 573 | } |
| 574 | |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 575 | void check_ksm_options(int *size, int *num, int *unit) |
| 576 | { |
| 577 | if (opt_size) { |
| 578 | *size = atoi(opt_sizestr); |
| 579 | if (*size < 1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 580 | tst_brkm(TBROK, cleanup, "size cannot be less than 1."); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 581 | } |
| 582 | if (opt_unit) { |
| 583 | *unit = atoi(opt_unitstr); |
| 584 | if (*unit > *size) |
| 585 | tst_brkm(TBROK, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 586 | "unit cannot be greater than size."); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 587 | if (*size % *unit != 0) |
| 588 | tst_brkm(TBROK, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 589 | "the remainder of division of size by unit is " |
| 590 | "not zero."); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 591 | } |
| 592 | if (opt_num) { |
| 593 | *num = atoi(opt_numstr); |
| 594 | if (*num < 3) |
| 595 | tst_brkm(TBROK, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 596 | "process number cannot be less 3."); |
Garrett Cooper | ecd667e | 2011-01-19 01:06:18 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
Caspar Zhang | 233e122 | 2011-12-23 13:02:56 +0800 | [diff] [blame] | 599 | |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 600 | void ksm_usage(void) |
| 601 | { |
| 602 | printf(" -n Number of processes\n"); |
| 603 | printf(" -s Memory allocation size in MB\n"); |
| 604 | printf(" -u Memory allocation unit in MB\n"); |
| 605 | } |
| 606 | |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 607 | /* THP */ |
| 608 | |
| 609 | static int alloc_transparent_hugepages(int nr_thps, int hg_aligned) |
| 610 | { |
| 611 | unsigned long hugepagesize, size; |
| 612 | void *addr; |
| 613 | int ret; |
| 614 | |
| 615 | hugepagesize = read_meminfo("Hugepagesize:") * KB; |
| 616 | size = nr_thps * hugepagesize; |
| 617 | |
| 618 | if (hg_aligned) { |
| 619 | ret = posix_memalign(&addr, hugepagesize, size); |
| 620 | if (ret != 0) { |
| 621 | printf("posix_memalign failed\n"); |
| 622 | return -1; |
| 623 | } |
| 624 | } else { |
| 625 | addr = mmap(NULL, size, PROT_READ|PROT_WRITE, |
| 626 | MAP_PRIVATE|MAP_ANON, -1, 0); |
| 627 | if (addr == MAP_FAILED) { |
| 628 | perror("mmap"); |
| 629 | return -1; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | memset(addr, 10, size); |
| 634 | |
| 635 | tst_resm(TINFO, "child[%d] stop here", getpid()); |
| 636 | /* |
| 637 | * stop here, until the father finish to calculate |
| 638 | * all the transparent hugepages. |
| 639 | */ |
| 640 | if (raise(SIGSTOP) == -1) { |
| 641 | perror("kill"); |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static void khugepaged_scan_done(void) |
| 649 | { |
| 650 | int changing = 1, count = 0, interval; |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 651 | long old_pages_collapsed = 0, old_max_ptes_none = 0, |
| 652 | old_pages_to_scan = 0; |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 653 | long pages_collapsed = 0, max_ptes_none = 0, pages_to_scan = 0; |
| 654 | |
| 655 | /* |
| 656 | * as 'khugepaged' run 100% during testing, so 5s is an |
| 657 | * enough interval for us to recognize if 'khugepaged' |
| 658 | * finish scanning proceses' anonymous hugepages or not. |
| 659 | */ |
| 660 | interval = 5; |
| 661 | |
| 662 | while (changing) { |
| 663 | sleep(interval); |
| 664 | count++; |
| 665 | |
| 666 | SAFE_FILE_SCANF(cleanup, PATH_KHPD "pages_collapsed", |
| 667 | "%ld", &pages_collapsed); |
| 668 | SAFE_FILE_SCANF(cleanup, PATH_KHPD "max_ptes_none", |
| 669 | "%ld", &max_ptes_none); |
| 670 | SAFE_FILE_SCANF(cleanup, PATH_KHPD "pages_to_scan", |
| 671 | "%ld", &pages_to_scan); |
| 672 | |
| 673 | if (pages_collapsed != old_pages_collapsed || |
| 674 | max_ptes_none != old_max_ptes_none || |
| 675 | pages_to_scan != old_pages_to_scan) { |
| 676 | old_pages_collapsed = pages_collapsed; |
| 677 | old_max_ptes_none = max_ptes_none; |
| 678 | old_pages_to_scan = pages_to_scan; |
| 679 | } else { |
| 680 | changing = 0; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | tst_resm(TINFO, "khugepaged daemon takes %ds to scan all thp pages", |
| 685 | count * interval); |
| 686 | } |
| 687 | |
| 688 | static void verify_thp_size(int *children, int nr_children, int nr_thps) |
| 689 | { |
| 690 | FILE *fp; |
| 691 | char path[BUFSIZ], buf[BUFSIZ], line[BUFSIZ]; |
| 692 | int i, ret; |
| 693 | long expect_thps; /* the amount of per child's transparent hugepages */ |
| 694 | long val, actual_thps; |
| 695 | long hugepagesize; |
| 696 | |
| 697 | hugepagesize = read_meminfo("Hugepagesize:"); |
| 698 | expect_thps = nr_thps * hugepagesize; |
| 699 | |
| 700 | for (i = 0; i < nr_children; i++) { |
| 701 | actual_thps = 0; |
| 702 | |
| 703 | snprintf(path, BUFSIZ, "/proc/%d/smaps", children[i]); |
| 704 | fp = fopen(path, "r"); |
| 705 | while (fgets(line, BUFSIZ, fp) != NULL) { |
| 706 | ret = sscanf(line, "%64s %ld", buf, &val); |
| 707 | if (ret == 2 && val != 0) { |
| 708 | if (strcmp(buf, "AnonHugePages:") == 0) |
| 709 | actual_thps += val; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (actual_thps != expect_thps) |
| 714 | tst_resm(TFAIL, "child[%d] got %ldKB thps - expect %ld" |
| 715 | "KB thps", getpid(), actual_thps, expect_thps); |
| 716 | fclose(fp); |
| 717 | } |
| 718 | } |
| 719 | |
Zhouping Liu | d6b67f8 | 2013-04-16 22:30:08 +0800 | [diff] [blame] | 720 | void test_transparent_hugepage(int nr_children, int nr_thps, |
| 721 | int hg_aligned, int mempolicy) |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 722 | { |
| 723 | unsigned long hugepagesize, memfree; |
| 724 | int i, *pids, ret, status; |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 725 | |
Zhouping Liu | d6b67f8 | 2013-04-16 22:30:08 +0800 | [diff] [blame] | 726 | if (mempolicy) |
| 727 | set_global_mempolicy(mempolicy); |
| 728 | |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 729 | memfree = read_meminfo("MemFree:"); |
| 730 | tst_resm(TINFO, "The current MemFree is %luMB", memfree / KB); |
| 731 | if (memfree < MB) |
| 732 | tst_resm(TCONF, "Not enough memory for testing"); |
| 733 | |
| 734 | hugepagesize = read_meminfo("Hugepagesize:"); |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 735 | tst_resm(TINFO, "The current Hugepagesize is %luMB", hugepagesize / KB); |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 736 | |
| 737 | pids = malloc(nr_children * sizeof(int)); |
| 738 | if (pids == NULL) |
| 739 | tst_brkm(TBROK | TERRNO, cleanup, "malloc"); |
| 740 | |
| 741 | for (i = 0; i < nr_children; i++) { |
| 742 | switch (pids[i] = fork()) { |
| 743 | case -1: |
| 744 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
| 745 | |
| 746 | case 0: |
| 747 | ret = alloc_transparent_hugepages(nr_thps, hg_aligned); |
| 748 | exit(ret); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | tst_resm(TINFO, "Stop all children..."); |
| 753 | for (i = 0; i < nr_children; i++) { |
| 754 | if (waitpid(pids[i], &status, WUNTRACED) == -1) |
| 755 | tst_brkm(TBROK|TERRNO, cleanup, "waitpid"); |
| 756 | if (!WIFSTOPPED(status)) |
| 757 | tst_brkm(TBROK, cleanup, |
| 758 | "child[%d] was not stoppted", pids[i]); |
| 759 | } |
| 760 | |
| 761 | tst_resm(TINFO, "Start to scan all transparent hugepages..."); |
| 762 | khugepaged_scan_done(); |
| 763 | |
| 764 | tst_resm(TINFO, "Start to verify transparent hugepage size..."); |
| 765 | verify_thp_size(pids, nr_children, nr_thps); |
| 766 | |
| 767 | tst_resm(TINFO, "Wake up all children..."); |
| 768 | for (i = 0; i < nr_children; i++) { |
| 769 | if (kill(pids[i], SIGCONT) == -1) |
| 770 | tst_brkm(TBROK | TERRNO, cleanup, |
| 771 | "signal continue child[%d]", pids[i]); |
| 772 | } |
| 773 | |
| 774 | /* wait all children finish their task */ |
| 775 | for (i = 0; i < nr_children; i++) { |
| 776 | if (waitpid(pids[i], &status, 0) == -1) |
| 777 | tst_brkm(TBROK|TERRNO, cleanup, "waitpid %d", pids[i]); |
| 778 | |
| 779 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 780 | tst_resm(TFAIL, "the child[%d] unexpectedly failed:" |
| 781 | " %d", pids[i], status); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void check_thp_options(int *nr_children, int *nr_thps) |
| 786 | { |
| 787 | if (opt_nr_children) |
| 788 | *nr_children = SAFE_STRTOL(NULL, opt_nr_children_str, |
| 789 | 0, LONG_MAX); |
| 790 | if (opt_nr_thps) |
| 791 | *nr_thps = SAFE_STRTOL(NULL, opt_nr_thps_str, 0, LONG_MAX); |
| 792 | } |
| 793 | |
| 794 | void thp_usage(void) |
| 795 | { |
| 796 | printf(" -n Number of processes\n"); |
| 797 | printf(" -N Number of transparent hugepages\n"); |
| 798 | } |
| 799 | |
Caspar Zhang | a7f0eed | 2012-02-28 15:44:04 +0800 | [diff] [blame] | 800 | /* cpuset/memcg */ |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 801 | |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 802 | static void gather_node_cpus(char *cpus, long nd) |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 803 | { |
| 804 | int ncpus = 0; |
| 805 | int i; |
Zhouping Liu | 448c1ee | 2013-03-19 11:40:49 +0800 | [diff] [blame] | 806 | long online; |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 807 | char buf[BUFSIZ]; |
Zhouping Liu | 448c1ee | 2013-03-19 11:40:49 +0800 | [diff] [blame] | 808 | char path[BUFSIZ], path1[BUFSIZ]; |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 809 | |
| 810 | while (path_exist(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus)) |
| 811 | ncpus++; |
| 812 | |
Zhouping Liu | 448c1ee | 2013-03-19 11:40:49 +0800 | [diff] [blame] | 813 | for (i = 0; i < ncpus; i++) { |
| 814 | snprintf(path, BUFSIZ, |
| 815 | PATH_SYS_SYSTEM "/node/node%ld/cpu%d", nd, i); |
Jan Stancek | df7d484 | 2013-08-27 14:16:19 +0200 | [diff] [blame] | 816 | if (path_exist(path)) { |
Zhouping Liu | 448c1ee | 2013-03-19 11:40:49 +0800 | [diff] [blame] | 817 | snprintf(path1, BUFSIZ, "%s/online", path); |
| 818 | /* |
Dave Kleikamp | 371375c | 2013-08-20 15:34:39 -0500 | [diff] [blame] | 819 | * if there is no online knob, then the cpu cannot |
| 820 | * be taken offline |
Zhouping Liu | 448c1ee | 2013-03-19 11:40:49 +0800 | [diff] [blame] | 821 | */ |
Dave Kleikamp | 371375c | 2013-08-20 15:34:39 -0500 | [diff] [blame] | 822 | if (path_exist(path1)) { |
| 823 | SAFE_FILE_SCANF(cleanup, path1, "%ld", &online); |
| 824 | if (online == 0) |
| 825 | continue; |
| 826 | } |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 827 | sprintf(buf, "%d,", i); |
| 828 | strcat(cpus, buf); |
| 829 | } |
Zhouping Liu | 448c1ee | 2013-03-19 11:40:49 +0800 | [diff] [blame] | 830 | } |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 831 | /* Remove the trailing comma. */ |
| 832 | cpus[strlen(cpus) - 1] = '\0'; |
| 833 | } |
| 834 | |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 835 | void read_cpuset_files(char *prefix, char *filename, char *retbuf) |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 836 | { |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 837 | int fd; |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 838 | char path[BUFSIZ]; |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 839 | |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 840 | /* |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 841 | * try either '/dev/cpuset/XXXX' or '/dev/cpuset/cpuset.XXXX' |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 842 | * please see Documentation/cgroups/cpusets.txt from kernel src |
| 843 | * for details |
| 844 | */ |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 845 | snprintf(path, BUFSIZ, "%s/%s", prefix, filename); |
| 846 | fd = open(path, O_RDONLY); |
| 847 | if (fd == -1) { |
| 848 | if (errno == ENOENT) { |
| 849 | snprintf(path, BUFSIZ, "%s/cpuset.%s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 850 | prefix, filename); |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 851 | fd = open(path, O_RDONLY); |
| 852 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 853 | tst_brkm(TBROK | TERRNO, cleanup, |
| 854 | "open %s", path); |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 855 | } else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 856 | tst_brkm(TBROK | TERRNO, cleanup, "open %s", path); |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 857 | } |
| 858 | if (read(fd, retbuf, BUFSIZ) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 859 | tst_brkm(TBROK | TERRNO, cleanup, "read %s", path); |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 860 | close(fd); |
| 861 | } |
| 862 | |
| 863 | void write_cpuset_files(char *prefix, char *filename, char *buf) |
| 864 | { |
| 865 | int fd; |
| 866 | char path[BUFSIZ]; |
| 867 | |
| 868 | /* |
| 869 | * try either '/dev/cpuset/XXXX' or '/dev/cpuset/cpuset.XXXX' |
| 870 | * please see Documentation/cgroups/cpusets.txt from kernel src |
| 871 | * for details |
| 872 | */ |
| 873 | snprintf(path, BUFSIZ, "%s/%s", prefix, filename); |
Caspar Zhang | 633c755 | 2012-02-21 11:01:03 +0800 | [diff] [blame] | 874 | fd = open(path, O_WRONLY); |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 875 | if (fd == -1) { |
| 876 | if (errno == ENOENT) { |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 877 | snprintf(path, BUFSIZ, "%s/cpuset.%s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 878 | prefix, filename); |
Caspar Zhang | 633c755 | 2012-02-21 11:01:03 +0800 | [diff] [blame] | 879 | fd = open(path, O_WRONLY); |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 880 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 881 | tst_brkm(TBROK | TERRNO, cleanup, |
| 882 | "open %s", path); |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 883 | } else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 884 | tst_brkm(TBROK | TERRNO, cleanup, "open %s", path); |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 885 | } |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 886 | if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 887 | tst_brkm(TBROK | TERRNO, cleanup, "write %s", path); |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 888 | close(fd); |
Caspar Zhang | 6d4044f | 2012-02-28 16:12:15 +0800 | [diff] [blame] | 889 | } |
| 890 | |
Caspar Zhang | 6d4044f | 2012-02-28 16:12:15 +0800 | [diff] [blame] | 891 | void write_cpusets(long nd) |
| 892 | { |
| 893 | char buf[BUFSIZ]; |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 894 | char cpus[BUFSIZ] = ""; |
Caspar Zhang | 6d4044f | 2012-02-28 16:12:15 +0800 | [diff] [blame] | 895 | |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 896 | snprintf(buf, BUFSIZ, "%ld", nd); |
| 897 | write_cpuset_files(CPATH_NEW, "mems", buf); |
| 898 | |
Zhouping Liu | 4ac1b8e | 2013-03-19 11:40:55 +0800 | [diff] [blame] | 899 | gather_node_cpus(cpus, nd); |
Zhouping Liu | 630f90d | 2013-04-29 14:07:17 +0800 | [diff] [blame] | 900 | /* |
| 901 | * If the 'nd' node doesn't contain any CPUs, |
| 902 | * the first ID of CPU '0' will be used as |
| 903 | * the value of cpuset.cpus. |
| 904 | */ |
| 905 | if (strlen(cpus) != 0) { |
| 906 | write_cpuset_files(CPATH_NEW, "cpus", cpus); |
| 907 | } else { |
| 908 | tst_resm(TINFO, "No CPUs in the node%ld; " |
| 909 | "using only CPU0", nd); |
| 910 | write_cpuset_files(CPATH_NEW, "cpus", "0"); |
| 911 | } |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 912 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 913 | SAFE_FILE_PRINTF(NULL, CPATH_NEW "/tasks", "%d", getpid()); |
Caspar Zhang | 7042271 | 2012-02-20 19:54:22 +0800 | [diff] [blame] | 914 | } |
| 915 | |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 916 | void umount_mem(char *path, char *path_new) |
| 917 | { |
| 918 | FILE *fp; |
| 919 | int fd; |
| 920 | char s_new[BUFSIZ], s[BUFSIZ], value[BUFSIZ]; |
| 921 | |
| 922 | /* Move all processes in task to its parent node. */ |
| 923 | sprintf(s, "%s/tasks", path); |
| 924 | fd = open(s, O_WRONLY); |
| 925 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 926 | tst_resm(TWARN | TERRNO, "open %s", s); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 927 | |
| 928 | snprintf(s_new, BUFSIZ, "%s/tasks", path_new); |
| 929 | fp = fopen(s_new, "r"); |
| 930 | if (fp == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 931 | tst_resm(TWARN | TERRNO, "fopen %s", s_new); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 932 | if ((fd != -1) && (fp != NULL)) { |
| 933 | while (fgets(value, BUFSIZ, fp) != NULL) |
| 934 | if (write(fd, value, strlen(value) - 1) |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 935 | != (ssize_t)strlen(value) - 1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 936 | tst_resm(TWARN | TERRNO, "write %s", s); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 937 | } |
| 938 | if (fd != -1) |
| 939 | close(fd); |
| 940 | if (fp != NULL) |
| 941 | fclose(fp); |
| 942 | if (rmdir(path_new) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 943 | tst_resm(TWARN | TERRNO, "rmdir %s", path_new); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 944 | if (umount(path) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 945 | tst_resm(TWARN | TERRNO, "umount %s", path); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 946 | if (rmdir(path) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 947 | tst_resm(TWARN | TERRNO, "rmdir %s", path); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | void mount_mem(char *name, char *fs, char *options, char *path, char *path_new) |
| 951 | { |
| 952 | if (mkdir(path, 0777) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 953 | tst_brkm(TBROK | TERRNO, cleanup, "mkdir %s", path); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 954 | if (mount(name, path, fs, 0, options) == -1) { |
| 955 | if (errno == ENODEV) { |
| 956 | if (rmdir(path) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 957 | tst_resm(TWARN | TERRNO, "rmdir %s failed", |
| 958 | path); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 959 | tst_brkm(TCONF, NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 960 | "file system %s is not configured in kernel", |
| 961 | fs); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 962 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 963 | tst_brkm(TBROK | TERRNO, cleanup, "mount %s", path); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 964 | } |
| 965 | if (mkdir(path_new, 0777) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 966 | tst_brkm(TBROK | TERRNO, cleanup, "mkdir %s", path_new); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 967 | } |
| 968 | |
Caspar Zhang | a7f0eed | 2012-02-28 15:44:04 +0800 | [diff] [blame] | 969 | /* shared */ |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 970 | |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 971 | /* Warning: *DO NOT* use this function in child */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 972 | unsigned int get_a_numa_node(void (*cleanup_fn) (void)) |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 973 | { |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 974 | unsigned int nd1, nd2; |
| 975 | int ret; |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 976 | |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 977 | ret = get_allowed_nodes(0, 2, &nd1, &nd2); |
| 978 | switch (ret) { |
| 979 | case 0: |
| 980 | break; |
| 981 | case -3: |
| 982 | tst_brkm(TCONF, cleanup_fn, "requires a NUMA system."); |
| 983 | default: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 984 | tst_brkm(TBROK | TERRNO, cleanup_fn, "1st get_allowed_nodes"); |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 985 | } |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 986 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 987 | ret = get_allowed_nodes(NH_MEMS | NH_CPUS, 1, &nd1); |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 988 | switch (ret) { |
| 989 | case 0: |
Xiaoguang Wang | f200be5 | 2014-04-28 16:47:13 +0800 | [diff] [blame] | 990 | tst_resm(TINFO, "get node%u.", nd1); |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 991 | return nd1; |
| 992 | case -3: |
| 993 | tst_brkm(TCONF, cleanup_fn, "requires a NUMA system that has " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 994 | "at least one node with both memory and CPU " |
| 995 | "available."); |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 996 | default: |
| 997 | break; |
| 998 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 999 | tst_brkm(TBROK | TERRNO, cleanup_fn, "2nd get_allowed_nodes"); |
Caspar Zhang | 6009edf | 2012-02-20 18:42:36 +0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | int path_exist(const char *path, ...) |
| 1003 | { |
| 1004 | va_list ap; |
| 1005 | char pathbuf[PATH_MAX]; |
| 1006 | |
| 1007 | va_start(ap, path); |
| 1008 | vsnprintf(pathbuf, sizeof(pathbuf), path, ap); |
| 1009 | va_end(ap); |
| 1010 | |
| 1011 | return access(pathbuf, F_OK) == 0; |
| 1012 | } |
| 1013 | |
Caspar Zhang | 233e122 | 2011-12-23 13:02:56 +0800 | [diff] [blame] | 1014 | long read_meminfo(char *item) |
| 1015 | { |
| 1016 | FILE *fp; |
| 1017 | char line[BUFSIZ], buf[BUFSIZ]; |
| 1018 | long val; |
| 1019 | |
| 1020 | fp = fopen(PATH_MEMINFO, "r"); |
| 1021 | if (fp == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1022 | tst_brkm(TBROK | TERRNO, cleanup, "fopen %s", PATH_MEMINFO); |
Caspar Zhang | 233e122 | 2011-12-23 13:02:56 +0800 | [diff] [blame] | 1023 | |
| 1024 | while (fgets(line, BUFSIZ, fp) != NULL) { |
| 1025 | if (sscanf(line, "%64s %ld", buf, &val) == 2) |
| 1026 | if (strcmp(buf, item) == 0) { |
| 1027 | fclose(fp); |
| 1028 | return val; |
| 1029 | } |
| 1030 | continue; |
| 1031 | } |
| 1032 | fclose(fp); |
| 1033 | |
| 1034 | tst_brkm(TBROK, cleanup, "cannot find \"%s\" in %s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1035 | item, PATH_MEMINFO); |
Caspar Zhang | 233e122 | 2011-12-23 13:02:56 +0800 | [diff] [blame] | 1036 | } |
Caspar Zhang | 3fa874a | 2011-12-28 14:16:20 +0800 | [diff] [blame] | 1037 | |
| 1038 | void set_sys_tune(char *sys_file, long tune, int check) |
| 1039 | { |
Caspar Zhang | 3fa874a | 2011-12-28 14:16:20 +0800 | [diff] [blame] | 1040 | long val; |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 1041 | char path[BUFSIZ]; |
Caspar Zhang | 3fa874a | 2011-12-28 14:16:20 +0800 | [diff] [blame] | 1042 | |
| 1043 | tst_resm(TINFO, "set %s to %ld", sys_file, tune); |
| 1044 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 1045 | snprintf(path, BUFSIZ, PATH_SYSVM "%s", sys_file); |
| 1046 | SAFE_FILE_PRINTF(NULL, path, "%ld", tune); |
Caspar Zhang | 3fa874a | 2011-12-28 14:16:20 +0800 | [diff] [blame] | 1047 | |
| 1048 | if (check) { |
| 1049 | val = get_sys_tune(sys_file); |
| 1050 | if (val != tune) |
| 1051 | tst_brkm(TBROK, cleanup, "%s = %ld, but expect %ld", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1052 | sys_file, val, tune); |
Caspar Zhang | 3fa874a | 2011-12-28 14:16:20 +0800 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | long get_sys_tune(char *sys_file) |
| 1057 | { |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 1058 | char path[BUFSIZ]; |
| 1059 | long tune; |
Caspar Zhang | 3fa874a | 2011-12-28 14:16:20 +0800 | [diff] [blame] | 1060 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 1061 | snprintf(path, BUFSIZ, PATH_SYSVM "%s", sys_file); |
| 1062 | SAFE_FILE_SCANF(NULL, path, "%ld", &tune); |
Caspar Zhang | 3459f50 | 2012-02-27 17:11:33 +0800 | [diff] [blame] | 1063 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 1064 | return tune; |
Caspar Zhang | 3459f50 | 2012-02-27 17:11:33 +0800 | [diff] [blame] | 1065 | } |
Wanlong Gao | 892153a | 2012-04-02 16:21:59 +0800 | [diff] [blame] | 1066 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1067 | void update_shm_size(size_t * shm_size) |
Wanlong Gao | 892153a | 2012-04-02 16:21:59 +0800 | [diff] [blame] | 1068 | { |
Wanlong Gao | 892153a | 2012-04-02 16:21:59 +0800 | [diff] [blame] | 1069 | size_t shmmax; |
| 1070 | |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 1071 | SAFE_FILE_SCANF(cleanup, PATH_SHMMAX, "%ld", &shmmax); |
Wanlong Gao | 892153a | 2012-04-02 16:21:59 +0800 | [diff] [blame] | 1072 | if (*shm_size > shmmax) { |
| 1073 | tst_resm(TINFO, "Set shm_size to shmmax: %ld", shmmax); |
| 1074 | *shm_size = shmmax; |
| 1075 | } |
| 1076 | } |