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 */
49
50 #include <sys/types.h>
51 #include <sys/wait.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include "test.h"
57 #include "mem.h"
58
59 char *TCID = "swapping01";
60 int TST_TOTAL = 1;
61
62 /* allow swapping 1 * phy_mem in maximum */
63 #define COE_DELTA 1
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
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 int lc;
80
81 tst_parse_opts(argc, argv, NULL, NULL);
82
83 #if __WORDSIZE == 32
84 tst_brkm(TCONF, NULL, "test is not designed for 32-bit system.");
85 #endif
86
87 setup();
88
89 for (lc = 0; TEST_LOOPING(lc); lc++) {
90 tst_count = 0;
91
92 init_meminfo();
93
94 switch (pid = fork()) {
95 case -1:
96 tst_brkm(TBROK | TERRNO, cleanup, "fork");
97 case 0:
98 do_alloc();
99 exit(0);
100 default:
101 check_swapping();
102 }
103 }
104 cleanup();
105 tst_exit();
106 }
107
init_meminfo(void)108 static void init_meminfo(void)
109 {
110 swap_free_init = read_meminfo("SwapFree:");
111 mem_free_init = read_meminfo("MemFree:");
112 mem_over = mem_free_init * COE_SLIGHT_OVER;
113 mem_over_max = mem_free_init * COE_DELTA;
114
115 /* at least 10MB free physical memory needed */
116 if (mem_free_init < 10240) {
117 sleep(5);
118 if (mem_free_init < 10240)
119 tst_brkm(TCONF, cleanup,
120 "Not enough free memory to test.");
121 }
122 if (swap_free_init < mem_over)
123 tst_brkm(TCONF, cleanup, "Not enough swap space to test.");
124 }
125
do_alloc(void)126 static void do_alloc(void)
127 {
128 long mem_count;
129 void *s;
130
131 tst_resm(TINFO, "free physical memory: %ld MB", mem_free_init / 1024);
132 mem_count = mem_free_init + mem_over;
133 tst_resm(TINFO, "try to allocate: %ld MB", mem_count / 1024);
134 s = malloc(mem_count * 1024);
135 if (s == NULL)
136 tst_brkm(TBROK | TERRNO, cleanup, "malloc");
137 memset(s, 1, mem_count * 1024);
138 tst_resm(TINFO, "memory allocated: %ld MB", mem_count / 1024);
139 if (raise(SIGSTOP) == -1)
140 tst_brkm(TBROK | TERRNO, tst_exit, "kill");
141 free(s);
142 }
143
check_swapping(void)144 static void check_swapping(void)
145 {
146 int status, i;
147 long swapped;
148
149 /* wait child stop */
150 if (waitpid(pid, &status, WUNTRACED) == -1)
151 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
152 if (!WIFSTOPPED(status))
153 tst_brkm(TBROK, cleanup, "child was not stopped.");
154
155 /* Still occupying memory, loop for a while */
156 for (i = 0; i < 10; i++) {
157 swapped = swap_free_init - read_meminfo("SwapFree:");
158 if (swapped > mem_over_max) {
159 kill(pid, SIGCONT);
160 tst_brkm(TFAIL, cleanup, "heavy swapping detected: "
161 "%ld MB swapped.", swapped / 1024);
162 }
163 sleep(1);
164 }
165 tst_resm(TPASS, "no heavy swapping detected, %ld MB swapped.",
166 swapped / 1024);
167 kill(pid, SIGCONT);
168 /* wait child exit */
169 if (waitpid(pid, &status, 0) == -1)
170 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
171 }
172
setup(void)173 void setup(void)
174 {
175 tst_sig(FORK, DEF_HANDLER, cleanup);
176
177 TEST_PAUSE;
178 }
179
cleanup(void)180 void cleanup(void)
181 {
182 }
183