1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) Linux Test Project., 2019
5 *
6 * DESCRIPTION:
7 *
8 * mtest01 mallocs memory <chunksize> at a time until malloc fails.
9 *
10 * Parent process starts several child processes (each child process is
11 * tasked with allocating some amount of memory), it waits until all child
12 * processes send SIGRTMIN signal and resumes all children by sending the
13 * SIGCONT signal.
14 *
15 * Child process allocates certain amount of memory and fills it with some
16 * data (the '-w' option) so the pages are actually allocated when the desired
17 * amount of memory is allocated then it sends SIGRTMIN signal to the parent
18 * process, it pauses itself by raise SIGSTOP until get parent SIGCONT signal
19 * to continue and exit.
20 */
21
22 #include <sys/types.h>
23 #include <sys/sysinfo.h>
24 #include <sys/wait.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30
31 #include "lapi/abisize.h"
32 #include "tst_test.h"
33
34 #define FIVE_HUNDRED_MB (500ULL*1024*1024)
35
36 #if defined(__s390__) || defined(__s390x__)
37 #define ALLOC_THRESHOLD FIVE_HUNDRED_MB
38 #elif defined(TST_ABI32)
39 #define ALLOC_THRESHOLD (2*FIVE_HUNDRED_MB)
40 #elif defined(TST_ABI64)
41 #define ALLOC_THRESHOLD (6*FIVE_HUNDRED_MB)
42 #endif
43
44 #define STOP_THRESHOLD 15 /* seconds remaining before reaching timeout */
45
46 static pid_t *pid_list;
47 static sig_atomic_t children_done;
48 static int max_pids;
49 static unsigned long long alloc_maxbytes;
50
51 static int chunksize = 1024*1024;
52 static int maxpercent = 20;
53 static long maxbytes = 0;
54 static char *dowrite;
55 static char *verbose;
56
57 static char *opt_chunksize, *opt_maxbytes, *opt_maxpercent;
58
parse_mtest_options(char * str_chunksize,int * chunksize,char * str_maxbytes,long * maxbytes,char * str_maxpercent,int * maxpercent)59 static void parse_mtest_options(char *str_chunksize, int *chunksize,
60 char *str_maxbytes, long *maxbytes,
61 char *str_maxpercent, int *maxpercent)
62 {
63 if (str_chunksize)
64 if (tst_parse_int(str_chunksize, chunksize, 1, INT_MAX))
65 tst_brk(TBROK, "Invalid chunksize '%s'", str_chunksize);
66
67 if (str_maxbytes) {
68 if (tst_parse_long(str_maxbytes, maxbytes, 1, LONG_MAX)) {
69 tst_brk(TBROK, "Invalid maxbytes '%s'", str_maxbytes);
70 } else if (str_maxpercent) {
71 tst_brk(TBROK, "ERROR: -b option cannot be used with -p "
72 "option at the same time");
73 }
74 alloc_maxbytes = (unsigned long long)maxbytes;
75 }
76
77 if (str_maxpercent) {
78 if (tst_parse_int(str_maxpercent, maxpercent, 1, 99)) {
79 tst_brk(TBROK, "Invalid maxpercent '%s'", str_maxpercent);
80 } else if (str_maxbytes) {
81 tst_brk(TBROK, "ERROR: -p option cannot be used with -b "
82 "option at the same time");
83 }
84 }
85 }
86
handler(int sig LTP_ATTRIBUTE_UNUSED)87 static void handler(int sig LTP_ATTRIBUTE_UNUSED)
88 {
89 children_done++;
90 }
91
do_write_mem(char * mem,int chunksize)92 static void do_write_mem(char *mem, int chunksize)
93 {
94 int i, pagesz = getpagesize();
95
96 for (i = 0; i < chunksize; i += pagesz)
97 *(mem + i) = 'a';
98 }
99
setup(void)100 static void setup(void)
101 {
102 struct sysinfo sstats;
103 unsigned long long total_free;
104
105 struct sigaction act;
106 act.sa_handler = handler;
107 act.sa_flags = 0;
108 sigemptyset(&act.sa_mask);
109 sigaction(SIGRTMIN, &act, 0);
110
111 parse_mtest_options(opt_chunksize, &chunksize,
112 opt_maxbytes, &maxbytes,
113 opt_maxpercent, &maxpercent);
114 sysinfo(&sstats);
115 total_free = sstats.freeram;
116
117 max_pids = total_free * sstats.mem_unit
118 / (unsigned long)ALLOC_THRESHOLD + 10;
119 pid_list = SAFE_MALLOC(max_pids * sizeof(pid_t));
120
121 if (!alloc_maxbytes) {
122 /* set alloc_maxbytes to the extra amount we want to allocate */
123 alloc_maxbytes = ((float)maxpercent / 100.00)
124 * (sstats.mem_unit * total_free);
125 tst_res(TINFO, "Filling up %d%% of free ram which is %llu kbytes",
126 maxpercent, alloc_maxbytes / 1024);
127 }
128 }
129
cleanup(void)130 static void cleanup(void)
131 {
132 if(pid_list)
133 free(pid_list);
134 }
135
child_loop_alloc(unsigned long long alloc_bytes)136 static void child_loop_alloc(unsigned long long alloc_bytes)
137 {
138 unsigned long bytecount = 0;
139 char *mem;
140
141 tst_res(TINFO, "... child %d starting", getpid());
142
143 while (1) {
144 mem = SAFE_MALLOC(chunksize);
145 if (dowrite)
146 do_write_mem(mem, chunksize);
147
148 if (verbose)
149 tst_res(TINFO,
150 "child %d allocated %lu bytes chunksize is %d",
151 getpid(), bytecount, chunksize);
152 bytecount += chunksize;
153 if (bytecount >= alloc_bytes)
154 break;
155 }
156 if (dowrite)
157 tst_res(TINFO, "... [t=%d] %lu bytes allocated and used in child %d",
158 tst_timeout_remaining(), bytecount, getpid());
159 else
160 tst_res(TINFO, "... [t=%d] %lu bytes allocated only in child %d",
161 tst_timeout_remaining(), bytecount, getpid());
162
163 kill(getppid(), SIGRTMIN);
164 raise(SIGSTOP);
165 exit(0);
166 }
167
mem_test(void)168 static void mem_test(void)
169 {
170 pid_t pid;
171 int i = 0, pid_cntr = 0;
172 unsigned long long alloc_bytes = alloc_maxbytes;
173 const char *write_msg = "";
174
175 if (dowrite)
176 write_msg = "(and written to) ";
177
178 /* to make mtest01 support -i N */
179 children_done = 0;
180
181 do {
182 pid = SAFE_FORK();
183 if (pid == 0) {
184 alloc_bytes = MIN(ALLOC_THRESHOLD, alloc_bytes);
185 child_loop_alloc(alloc_bytes);
186 }
187
188 pid_list[pid_cntr++] = pid;
189
190 if (alloc_bytes <= ALLOC_THRESHOLD)
191 break;
192
193 alloc_bytes -= ALLOC_THRESHOLD;
194 } while (pid_cntr < max_pids);
195
196 /* wait in the loop for all children finish allocating */
197 while (children_done < pid_cntr) {
198 if (tst_timeout_remaining() < STOP_THRESHOLD) {
199 tst_res(TWARN,
200 "the remaininig time is not enough for testing");
201
202 break;
203 }
204
205 usleep(100000);
206 }
207
208 if (children_done < pid_cntr) {
209 tst_res(TFAIL, "kbytes allocated %sless than expected %llu",
210 write_msg, alloc_maxbytes / 1024);
211
212 for (i = 0; i < pid_cntr; i++)
213 kill(pid_list[i], SIGKILL);
214
215 return;
216 }
217
218 tst_res(TPASS, "%llu kbytes allocated %s",
219 alloc_maxbytes / 1024, write_msg);
220
221 for (i = 0; i < pid_cntr; i++) {
222 TST_PROCESS_STATE_WAIT(pid_list[i], 'T', 0);
223 kill(pid_list[i], SIGCONT);
224 }
225 }
226
227 static struct tst_test test = {
228 .forks_child = 1,
229 .options = (struct tst_option[]) {
230 {"c:", &opt_chunksize, "Size of chunk in bytes to malloc on each pass"},
231 {"b:", &opt_maxbytes, "Maximum number of bytes to allocate before stopping"},
232 {"p:", &opt_maxpercent, "Percent of total memory used at which the program stops"},
233 {"w", &dowrite, "Write to the memory after allocating"},
234 {"v", &verbose, "Verbose"},
235 {}
236 },
237 .setup = setup,
238 .cleanup = cleanup,
239 .test_all = mem_test,
240 };
241