Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Red Hat, Inc. |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it would be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | * |
| 11 | * Further, this software is distributed without any warranty that it |
| 12 | * is free of the rightful claim of any third person regarding |
| 13 | * infringement or the like. Any license provided herein, whether |
| 14 | * implied or otherwise, applies only to this software file. Patent |
| 15 | * licenses, if any, provided herein do not apply to combinations of |
| 16 | * this program with other software, or any other product whatsoever. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 21 | * 02110-1301, USA. |
| 22 | */ |
| 23 | /* |
| 24 | * swapping01 - first time swap use results in heavy swapping |
| 25 | * |
| 26 | * This case is used for testing upstream commit: 50a1598 |
| 27 | * |
| 28 | * The upstream commit fixed a issue on s390/x platform that heavy |
| 29 | * swapping might occur in some condition, however since the patch |
| 30 | * was quite general, this testcase will be run on all supported |
| 31 | * platforms to ensure no regression been introduced. |
| 32 | * |
| 33 | * Details of the upstream fix: |
| 34 | * On x86 a page without a mapper is by definition not referenced / old. |
| 35 | * The s390 architecture keeps the reference bit in the storage key and |
| 36 | * the current code will check the storage key for page without a mapper. |
| 37 | * This leads to an interesting effect: the first time an s390 system |
| 38 | * needs to write pages to swap it only finds referenced pages. This |
| 39 | * causes a lot of pages to get added and written to the swap device. |
| 40 | * To avoid this behaviour change page_referenced to query the storage |
| 41 | * key only if there is a mapper of the page. |
| 42 | * |
| 43 | * Test Strategy: |
| 44 | * Try to allocate memory which size is slightly larger than current |
| 45 | * available memory. After allocation done, continue loop for a while |
| 46 | * and calculate the used swap size. The used swap size should be small |
| 47 | * enough, else it indicates that heavy swapping is occured unexpectedly. |
| 48 | */ |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 49 | |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 50 | #include <sys/types.h> |
| 51 | #include <sys/wait.h> |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <string.h> |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 55 | #include <unistd.h> |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 56 | #include "test.h" |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 57 | #include "mem.h" |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 58 | |
| 59 | char *TCID = "swapping01"; |
| 60 | int TST_TOTAL = 1; |
| 61 | |
Caspar Zhang | 1f43a50 | 2012-03-20 13:17:20 +0800 | [diff] [blame] | 62 | /* allow swapping 1 * phy_mem in maximum */ |
| 63 | #define COE_DELTA 1 |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 64 | /* will try to alloc 1.3 * phy_mem */ |
| 65 | #define COE_SLIGHT_OVER 0.3 |
| 66 | |
| 67 | static void init_meminfo(void); |
| 68 | static void do_alloc(void); |
| 69 | static void check_swapping(void); |
| 70 | |
| 71 | static long mem_free_init; |
| 72 | static long swap_free_init; |
| 73 | static long mem_over; |
| 74 | static long mem_over_max; |
| 75 | static pid_t pid; |
| 76 | |
| 77 | int main(int argc, char *argv[]) |
| 78 | { |
| 79 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 80 | const char *msg; |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 81 | |
| 82 | msg = parse_opts(argc, argv, NULL, NULL); |
| 83 | if (msg != NULL) |
| 84 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 85 | |
| 86 | #if __WORDSIZE == 32 |
| 87 | tst_brkm(TCONF, NULL, "test is not designed for 32-bit system."); |
| 88 | #endif |
| 89 | |
| 90 | setup(); |
| 91 | |
| 92 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 93 | tst_count = 0; |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 94 | |
| 95 | init_meminfo(); |
| 96 | |
| 97 | switch (pid = fork()) { |
| 98 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 100 | case 0: |
| 101 | do_alloc(); |
| 102 | exit(0); |
| 103 | default: |
| 104 | check_swapping(); |
| 105 | } |
| 106 | } |
| 107 | cleanup(); |
| 108 | tst_exit(); |
| 109 | } |
| 110 | |
| 111 | static void init_meminfo(void) |
| 112 | { |
| 113 | swap_free_init = read_meminfo("SwapFree:"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | mem_free_init = read_meminfo("MemFree:"); |
| 115 | mem_over = mem_free_init * COE_SLIGHT_OVER; |
| 116 | mem_over_max = mem_free_init * COE_DELTA; |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 117 | |
| 118 | /* at least 10MB free physical memory needed */ |
| 119 | if (mem_free_init < 10240) { |
| 120 | sleep(5); |
| 121 | if (mem_free_init < 10240) |
| 122 | tst_brkm(TCONF, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | "Not enough free memory to test."); |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 124 | } |
| 125 | if (swap_free_init < mem_over) |
| 126 | tst_brkm(TCONF, cleanup, "Not enough swap space to test."); |
| 127 | } |
| 128 | |
| 129 | static void do_alloc(void) |
| 130 | { |
| 131 | long mem_count; |
| 132 | void *s; |
| 133 | |
| 134 | tst_resm(TINFO, "free physical memory: %ld MB", mem_free_init / 1024); |
| 135 | mem_count = mem_free_init + mem_over; |
| 136 | tst_resm(TINFO, "try to allocate: %ld MB", mem_count / 1024); |
| 137 | s = malloc(mem_count * 1024); |
| 138 | if (s == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | tst_brkm(TBROK | TERRNO, cleanup, "malloc"); |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 140 | memset(s, 1, mem_count * 1024); |
| 141 | tst_resm(TINFO, "memory allocated: %ld MB", mem_count / 1024); |
| 142 | if (raise(SIGSTOP) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 143 | tst_brkm(TBROK | TERRNO, tst_exit, "kill"); |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 144 | free(s); |
| 145 | } |
| 146 | |
| 147 | static void check_swapping(void) |
| 148 | { |
| 149 | int status, i; |
| 150 | long swapped; |
| 151 | |
| 152 | /* wait child stop */ |
| 153 | if (waitpid(pid, &status, WUNTRACED) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 154 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 155 | if (!WIFSTOPPED(status)) |
| 156 | tst_brkm(TBROK, cleanup, "child was not stopped."); |
| 157 | |
| 158 | /* Still occupying memory, loop for a while */ |
| 159 | for (i = 0; i < 10; i++) { |
| 160 | swapped = swap_free_init - read_meminfo("SwapFree:"); |
| 161 | if (swapped > mem_over_max) { |
| 162 | kill(pid, SIGCONT); |
| 163 | tst_brkm(TFAIL, cleanup, "heavy swapping detected: " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | "%ld MB swapped.", swapped / 1024); |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 165 | } |
| 166 | sleep(1); |
| 167 | } |
| 168 | tst_resm(TPASS, "no heavy swapping detected, %ld MB swapped.", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | swapped / 1024); |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 170 | kill(pid, SIGCONT); |
| 171 | /* wait child exit */ |
| 172 | if (waitpid(pid, &status, 0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 173 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void setup(void) |
| 177 | { |
| 178 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 179 | |
| 180 | TEST_PAUSE; |
| 181 | } |
| 182 | |
| 183 | void cleanup(void) |
| 184 | { |
Caspar Zhang | 7668f7e | 2011-10-18 19:02:19 +0800 | [diff] [blame] | 185 | } |