blob: df11af288c14821966f9727d21b89d19b9084506 [file] [log] [blame]
Caspar Zhang7668f7e2011-10-18 19:02:19 +08001/*
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 Zhang79667fa2012-03-12 14:41:46 +080049
Caspar Zhang7668f7e2011-10-18 19:02:19 +080050#include <sys/types.h>
51#include <sys/wait.h>
Caspar Zhang7668f7e2011-10-18 19:02:19 +080052#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
Caspar Zhang79667fa2012-03-12 14:41:46 +080055#include <unistd.h>
Caspar Zhang7668f7e2011-10-18 19:02:19 +080056#include "test.h"
Caspar Zhang79667fa2012-03-12 14:41:46 +080057#include "mem.h"
Caspar Zhang7668f7e2011-10-18 19:02:19 +080058
59char *TCID = "swapping01";
60int TST_TOTAL = 1;
61
Caspar Zhang1f43a502012-03-20 13:17:20 +080062/* allow swapping 1 * phy_mem in maximum */
63#define COE_DELTA 1
Caspar Zhang7668f7e2011-10-18 19:02:19 +080064/* will try to alloc 1.3 * phy_mem */
65#define COE_SLIGHT_OVER 0.3
66
67static void init_meminfo(void);
68static void do_alloc(void);
69static void check_swapping(void);
70
71static long mem_free_init;
72static long swap_free_init;
73static long mem_over;
74static long mem_over_max;
75static pid_t pid;
76
77int main(int argc, char *argv[])
78{
79 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020080 const char *msg;
Caspar Zhang7668f7e2011-10-18 19:02:19 +080081
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 Zhangd59a6592013-03-07 14:59:12 +080093 tst_count = 0;
Caspar Zhang7668f7e2011-10-18 19:02:19 +080094
95 init_meminfo();
96
97 switch (pid = fork()) {
98 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800100 case 0:
101 do_alloc();
102 exit(0);
103 default:
104 check_swapping();
105 }
106 }
107 cleanup();
108 tst_exit();
109}
110
111static void init_meminfo(void)
112{
113 swap_free_init = read_meminfo("SwapFree:");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 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 Zhang7668f7e2011-10-18 19:02:19 +0800117
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 Gao354ebb42012-12-07 10:10:04 +0800123 "Not enough free memory to test.");
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800124 }
125 if (swap_free_init < mem_over)
126 tst_brkm(TCONF, cleanup, "Not enough swap space to test.");
127}
128
129static 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 Gao354ebb42012-12-07 10:10:04 +0800139 tst_brkm(TBROK | TERRNO, cleanup, "malloc");
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800140 memset(s, 1, mem_count * 1024);
141 tst_resm(TINFO, "memory allocated: %ld MB", mem_count / 1024);
142 if (raise(SIGSTOP) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 tst_brkm(TBROK | TERRNO, tst_exit, "kill");
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800144 free(s);
145}
146
147static 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 Gao354ebb42012-12-07 10:10:04 +0800154 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800155 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 Gao354ebb42012-12-07 10:10:04 +0800164 "%ld MB swapped.", swapped / 1024);
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800165 }
166 sleep(1);
167 }
168 tst_resm(TPASS, "no heavy swapping detected, %ld MB swapped.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 swapped / 1024);
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800170 kill(pid, SIGCONT);
171 /* wait child exit */
172 if (waitpid(pid, &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800174}
175
176void setup(void)
177{
178 tst_sig(FORK, DEF_HANDLER, cleanup);
179
180 TEST_PAUSE;
181}
182
183void cleanup(void)
184{
Caspar Zhang7668f7e2011-10-18 19:02:19 +0800185}