1 /*
2 * Copyright (C) 2012-2017 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 */
14 /*
15 * swapping01 - first time swap use results in heavy swapping
16 *
17 * This case is used for testing upstream commit: 50a1598
18 *
19 * The upstream commit fixed a issue on s390/x platform that heavy
20 * swapping might occur in some condition, however since the patch
21 * was quite general, this testcase will be run on all supported
22 * platforms to ensure no regression been introduced.
23 *
24 * Details of the upstream fix:
25 * On x86 a page without a mapper is by definition not referenced / old.
26 * The s390 architecture keeps the reference bit in the storage key and
27 * the current code will check the storage key for page without a mapper.
28 * This leads to an interesting effect: the first time an s390 system
29 * needs to write pages to swap it only finds referenced pages. This
30 * causes a lot of pages to get added and written to the swap device.
31 * To avoid this behaviour change page_referenced to query the storage
32 * key only if there is a mapper of the page.
33 *
34 * Test Strategy:
35 * Try to allocate memory which size is slightly larger than current
36 * available memory. After allocation done, continue loop for a while
37 * and calculate the used swap size. The used swap size should be small
38 * enough, else it indicates that heavy swapping is occured unexpectedly.
39 */
40
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include "lapi/abisize.h"
48 #include "mem.h"
49
50 /* allow swapping 1 * phy_mem in maximum */
51 #define COE_DELTA 1
52 /* will try to alloc 1.3 * phy_mem */
53 #define COE_SLIGHT_OVER 0.3
54
55 static void init_meminfo(void);
56 static void do_alloc(int allow_raise);
57 static void check_swapping(void);
58
59 static long mem_available_init;
60 static long swap_free_init;
61 static long mem_over;
62 static long mem_over_max;
63 static pid_t pid;
64
test_swapping(void)65 static void test_swapping(void)
66 {
67 #ifdef TST_ABI32
68 tst_brk(TCONF, "test is not designed for 32-bit system.");
69 #endif
70
71 init_meminfo();
72
73 switch (pid = SAFE_FORK()) {
74 case 0:
75 do_alloc(0);
76 do_alloc(1);
77 exit(0);
78 default:
79 check_swapping();
80 }
81 }
82
init_meminfo(void)83 static void init_meminfo(void)
84 {
85 swap_free_init = SAFE_READ_MEMINFO("SwapFree:");
86 if (FILE_LINES_SCANF("/proc/meminfo", "MemAvailable: %ld",
87 &mem_available_init)) {
88 mem_available_init = SAFE_READ_MEMINFO("MemFree:")
89 + SAFE_READ_MEMINFO("Cached:");
90 }
91 mem_over = mem_available_init * COE_SLIGHT_OVER;
92 mem_over_max = mem_available_init * COE_DELTA;
93
94 /* at least 10MB available physical memory needed */
95 if (mem_available_init < 10240)
96 tst_brk(TCONF, "Not enough available mem to test.");
97
98 if (swap_free_init < mem_over_max)
99 tst_brk(TCONF, "Not enough swap space to test: swap_free_init(%ldkB) < mem_over_max(%ldkB)",
100 swap_free_init, mem_over_max);
101 }
102
do_alloc(int allow_raise)103 static void do_alloc(int allow_raise)
104 {
105 long mem_count;
106 void *s;
107
108 if (allow_raise == 1)
109 tst_res(TINFO, "available physical memory: %ld MB",
110 mem_available_init / 1024);
111 mem_count = mem_available_init + mem_over;
112 if (allow_raise == 1)
113 tst_res(TINFO, "try to allocate: %ld MB", mem_count / 1024);
114 s = SAFE_MALLOC(mem_count * 1024);
115 memset(s, 1, mem_count * 1024);
116 if ((allow_raise == 1) && (raise(SIGSTOP) == -1)) {
117 tst_res(TINFO, "memory allocated: %ld MB", mem_count / 1024);
118 tst_brk(TBROK | TERRNO, "kill");
119 }
120 free(s);
121 }
122
check_swapping(void)123 static void check_swapping(void)
124 {
125 int status, i;
126 long swap_free_now, swapped;
127
128 /* wait child stop */
129 SAFE_WAITPID(pid, &status, WUNTRACED);
130 if (!WIFSTOPPED(status))
131 tst_brk(TBROK, "child was not stopped.");
132
133 /* Still occupying memory, loop for a while */
134 i = 0;
135 while (i < 30) {
136 swap_free_now = SAFE_READ_MEMINFO("SwapFree:");
137 sleep(1);
138 if (labs(swap_free_now - SAFE_READ_MEMINFO("SwapFree:")) < 10)
139 break;
140
141 i++;
142 }
143
144 swapped = SAFE_READ_PROC_STATUS(pid, "VmSwap:");
145 if (swapped > mem_over_max) {
146 kill(pid, SIGCONT);
147 tst_brk(TFAIL, "heavy swapping detected: "
148 "%ld MB swapped.", swapped / 1024);
149 }
150
151 tst_res(TPASS, "no heavy swapping detected, %ld MB swapped.",
152 swapped / 1024);
153 kill(pid, SIGCONT);
154 /* wait child exit */
155 SAFE_WAITPID(pid, &status, 0);
156 }
157
158 static struct tst_test test = {
159 .needs_root = 1,
160 .forks_child = 1,
161 .test_all = test_swapping,
162 };
163