[bcf24e]: testcases / kernel / syscalls / getrusage / getrusage03.c  Maximize  Restore  History

Download this file

365 lines (314 with data), 10.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* getrusage03 - test ru_maxrss behaviors in struct rusage
*
* This test program is backported from upstream commit:
* 1f10206cf8e945220f7220a809d8bfc15c21f9a5, which fills ru_maxrss
* value in struct rusage according to rss hiwater mark. To make sure
* this feature works correctly, a series of tests are executed in
* this program.
*
* Copyright (C) 2011 Red Hat, Inc.
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Further, this software is distributed without any warranty that it
* is free of the rightful claim of any third person regarding
* infringement or the like. Any license provided herein, whether
* implied or otherwise, applies only to this software file. Patent
* licenses, if any, provided herein do not apply to combinations of
* this program with other software, or any other product whatsoever.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"
#include "usctest.h"
#include "safe_macros.h"
char *TCID = "getrusage03";
int TST_TOTAL = 1;
#define DELTA_MAX 10240
static struct rusage ru;
static long maxrss_init;
static int retval, status;
static pid_t pid;
static void inherit_fork(void);
static void inherit_fork2(void);
static void fork_malloc(void);
static void grandchild_maxrss(void);
static void zombie(void);
static void sig_ign(void);
static void exec_without_fork(void);
static void check_return(int status, char *pass_msg, char *fail_msg);
static int is_in_delta(long value);
static void consume(int mega);
static void setup(void);
static void cleanup(void);
int main(int argc, char *argv[])
{
int lc;
char *msg;
msg = parse_opts(argc, argv, NULL, NULL);
if (msg != NULL)
tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
setup();
for (lc = 0; TEST_LOOPING(lc); lc++) {
tst_count = 0;
tst_resm(TINFO, "allocate 100MB");
consume(100);
inherit_fork();
inherit_fork2();
fork_malloc();
grandchild_maxrss();
zombie();
sig_ign();
exec_without_fork();
}
cleanup();
tst_exit();
}
/* Testcase #01: fork inherit
* expect: initial.self ~= child.self */
static void inherit_fork(void)
{
tst_resm(TINFO, "Testcase #01: fork inherit");
SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
tst_resm(TINFO, "initial.self = %ld", ru.ru_maxrss);
switch (pid = fork()) {
case -1:
tst_brkm(TBROK | TERRNO, cleanup, "fork #1");
case 0:
maxrss_init = ru.ru_maxrss;
SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
tst_resm(TINFO, "child.self = %ld", ru.ru_maxrss);
exit(is_in_delta(maxrss_init - ru.ru_maxrss));
default:
break;
}
if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
check_return(WEXITSTATUS(status), "initial.self ~= child.self",
"initial.self !~= child.self");
}
/* Testcase #02: fork inherit (cont.)
* expect: initial.children ~= 100MB, child.children = 0 */
static void inherit_fork2(void)
{
tst_resm(TINFO, "Testcase #02: fork inherit(cont.)");
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
if (is_in_delta(ru.ru_maxrss - 102400))
tst_resm(TPASS, "initial.children ~= 100MB");
else
tst_resm(TFAIL, "initial.children !~= 100MB");
switch (pid = fork()) {
case -1:
tst_brkm(TBROK | TERRNO, cleanup, "fork #2");
case 0:
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "child.children = %ld", ru.ru_maxrss);
exit(ru.ru_maxrss == 0);
default:
break;
}
if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
check_return(WEXITSTATUS(status), "child.children == 0",
"child.children != 0");
}
/* Testcase #03: fork + malloc
* expect: initial.self + 50MB ~= child.self */
static void fork_malloc(void)
{
tst_resm(TINFO, "Testcase #03: fork + malloc");
SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
tst_resm(TINFO, "initial.self = %ld", ru.ru_maxrss);
switch (pid = fork()) {
case -1:
tst_brkm(TBROK | TERRNO, cleanup, "fork #3");
case 0:
maxrss_init = ru.ru_maxrss;
tst_resm(TINFO, "child allocate +50MB");
consume(50);
SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
tst_resm(TINFO, "child.self = %ld", ru.ru_maxrss);
exit(is_in_delta(maxrss_init + 51200 - ru.ru_maxrss));
default:
break;
}
if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
check_return(WEXITSTATUS(status), "initial.self + 50MB ~= child.self",
"initial.self + 50MB !~= child.self");
}
/* Testcase #04: grandchild maxrss
* expect: post_wait.children ~= 300MB */
static void grandchild_maxrss(void)
{
tst_resm(TINFO, "Testcase #04: grandchild maxrss");
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
switch (pid = fork()) {
case -1:
tst_brkm(TBROK | TERRNO, cleanup, "fork #4");
case 0:
retval = system("getrusage03_child -g 300");
if ((WIFEXITED(retval) && WEXITSTATUS(retval) != 0))
tst_brkm(TBROK | TERRNO, cleanup, "system");
exit(0);
default:
break;
}
if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
if (WEXITSTATUS(status) != 0)
tst_brkm(TBROK | TERRNO, cleanup, "child exit status is not 0");
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "post_wait.children = %ld", ru.ru_maxrss);
if (is_in_delta(ru.ru_maxrss - 307200))
tst_resm(TPASS, "child.children ~= 300MB");
else
tst_resm(TFAIL, "child.children !~= 300MB");
}
/* Testcase #05: zombie
* expect: initial ~= pre_wait, post_wait ~= 400MB */
static void zombie(void)
{
tst_resm(TINFO, "Testcase #05: zombie");
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
maxrss_init = ru.ru_maxrss;
switch (pid = fork()) {
case -1:
tst_brkm(TBROK, cleanup, "fork #5");
case 0:
retval = system("getrusage03_child -n 400");
if ((WIFEXITED(retval) && WEXITSTATUS(retval) != 0))
tst_brkm(TBROK | TERRNO, cleanup, "system");
exit(0);
default:
break;
}
sleep(1); /* children become zombie */
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "pre_wait.children = %ld", ru.ru_maxrss);
if (is_in_delta(ru.ru_maxrss - maxrss_init))
tst_resm(TPASS, "initial.children ~= pre_wait.children");
else
tst_resm(TFAIL, "initial.children !~= pre_wait.children");
if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
if (WEXITSTATUS(status) != 0)
tst_brkm(TBROK | TERRNO, cleanup, "child exit status is not 0");
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "post_wait.children = %ld", ru.ru_maxrss);
if (is_in_delta(ru.ru_maxrss - 409600))
tst_resm(TPASS, "post_wait.children ~= 400MB");
else
tst_resm(TFAIL, "post_wait.children !~= 400MB");
}
/* Testcase #06: SIG_IGN
* expect: initial ~= after_zombie */
static void sig_ign(void)
{
tst_resm(TINFO, "Testcase #06: SIG_IGN");
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
signal(SIGCHLD, SIG_IGN);
maxrss_init = ru.ru_maxrss;
switch (pid = fork()) {
case -1:
tst_brkm(TBROK, cleanup, "fork #6");
case 0:
retval = system("getrusage03_child -n 500");
if ((WIFEXITED(retval) && WEXITSTATUS(retval) != 0))
tst_brkm(TBROK | TERRNO, cleanup, "system");
exit(0);
default:
break;
}
sleep(1); /* children become zombie */
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
tst_resm(TINFO, "after_zombie.children = %ld", ru.ru_maxrss);
if (is_in_delta(ru.ru_maxrss - maxrss_init))
tst_resm(TPASS, "initial.children ~= after_zombie.children");
else
tst_resm(TFAIL, "initial.children !~= after_zombie.children");
signal(SIGCHLD, SIG_DFL);
}
/* Testcase #07: exec without fork
* expect: initial ~= fork */
static void exec_without_fork(void)
{
char str_maxrss_self[BUFSIZ], str_maxrss_child[BUFSIZ];
long maxrss_self, maxrss_child;
tst_resm(TINFO, "Testcase #07: exec without fork");
SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
maxrss_self = ru.ru_maxrss;
SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
maxrss_child = ru.ru_maxrss;
tst_resm(TINFO, "initial.self = %ld, initial.children = %ld",
maxrss_self, maxrss_child);
sprintf(str_maxrss_self, "%ld", maxrss_self);
sprintf(str_maxrss_child, "%ld", maxrss_child);
if (execlp("getrusage03_child", "getrusage03_child", "-v",
"-s", str_maxrss_self, "-l", str_maxrss_child, NULL) == -1)
tst_brkm(TBROK | TERRNO, cleanup, "execlp");
}
static int is_in_delta(long value)
{
return (value >= -DELTA_MAX && value <= DELTA_MAX);
}
static void check_return(int status, char *pass_msg, char *fail_msg)
{
switch (status) {
case 1:
tst_resm(TPASS, "%s", pass_msg);
break;
case 0:
tst_resm(TFAIL, "%s", fail_msg);
break;
default:
tst_resm(TFAIL, "child exit status is %d", status);
break;
}
}
static void consume(int mega)
{
size_t sz;
void *ptr;
sz = mega * 1024 * 1024;
ptr = SAFE_MALLOC(cleanup, sz);
memset(ptr, 0, sz);
}
static void setup(void)
{
/* Disable test if the version of the kernel is less than 2.6.32 */
if ((tst_kvercmp(2, 6, 32)) < 0) {
tst_resm(TCONF, "This ru_maxrss field is not supported");
tst_resm(TCONF, "before kernel 2.6.32");
tst_exit();
}
tst_sig(FORK, DEF_HANDLER, cleanup);
TEST_PAUSE;
}
static void cleanup(void)
{
TEST_CLEANUP;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks