blob: c729fd12671b0ccaeff1a8fa0cac121345b8cf4a [file] [log] [blame]
Caspar Zhang817c7822011-06-30 01:50:33 +08001/*
2 * getrusage03_child.c - a child program executed by getrusage03
3 *
4 * Copyright (C) 2011 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * Further, this software is distributed without any warranty that it
14 * is free of the rightful claim of any third person regarding
15 * infringement or the like. Any license provided herein, whether
16 * implied or otherwise, applies only to this software file. Patent
17 * licenses, if any, provided herein do not apply to combinations of
18 * this program with other software, or any other product whatsoever.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * 02110-1301, USA.
24 */
25#include <sys/types.h>
26#include <sys/resource.h>
27#include <sys/time.h>
28#include <sys/wait.h>
29#include <errno.h>
30#include <unistd.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "test.h"
Caspar Zhang817c7822011-06-30 01:50:33 +080036#include "safe_macros.h"
37
38char *TCID = "getrusage03_child";
39int TST_TOTAL = 1;
40
41#define DELTA_MAX 10240
42
43static int opt_consume, opt_grand, opt_show, opt_self, opt_child;
44static char *consume_str, *grand_consume_str, *self_str, *child_str;
45
46option_t child_options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080047 {"n:", &opt_consume, &consume_str},
48 {"g:", &opt_grand, &grand_consume_str},
49 {"v", &opt_show, NULL},
50 {"s:", &opt_self, &self_str},
51 {"l:", &opt_child, &child_str},
52 {NULL, NULL, NULL}
Caspar Zhang817c7822011-06-30 01:50:33 +080053};
54
55static void usage(void);
56static void consume(int mega);
Caspar Zhang817c7822011-06-30 01:50:33 +080057static void setup(void);
58static void cleanup(void);
59
60int main(int argc, char *argv[])
61{
62 int lc;
63 pid_t pid;
64 long maxrss_self, maxrss_children, delta;
65 long consume_nr, grand_consume_nr, self_nr, child_nr;
66 struct rusage ru;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020067 const char *msg;
Caspar Zhang817c7822011-06-30 01:50:33 +080068
69 msg = parse_opts(argc, argv, child_options, usage);
70 if (msg != NULL)
71 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
72
73 setup();
74
75 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080076 tst_count = 0;
Caspar Zhang817c7822011-06-30 01:50:33 +080077
78 if (opt_consume) {
Caspar Zhang123f8002012-02-10 10:42:32 +080079 consume_nr = SAFE_STRTOL(cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +080080 consume_str, 0, LONG_MAX);
Caspar Zhang817c7822011-06-30 01:50:33 +080081 tst_resm(TINFO, "child allocate %ldMB", consume_nr);
82 consume(consume_nr);
83 }
84
85 if (opt_grand) {
Caspar Zhang123f8002012-02-10 10:42:32 +080086 grand_consume_nr = SAFE_STRTOL(cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 grand_consume_str, 0,
88 LONG_MAX);
Caspar Zhang817c7822011-06-30 01:50:33 +080089 tst_resm(TINFO, "grandchild allocate %ldMB",
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 grand_consume_nr);
Caspar Zhang817c7822011-06-30 01:50:33 +080091 switch (pid = fork()) {
92 case -1:
93 tst_brkm(TBROK, cleanup, "fork");
94 case 0:
95 consume(grand_consume_nr);
96 exit(0);
97 default:
98 break;
99 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 while (waitpid(-1, &pid, WUNTRACED | WCONTINUED) > 0)
Caspar Zhang817c7822011-06-30 01:50:33 +0800101 if (WEXITSTATUS(pid) != 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 tst_brkm(TBROK | TERRNO, cleanup,
103 "child exit status is not 0");
Caspar Zhang817c7822011-06-30 01:50:33 +0800104 }
105
106 if (opt_show) {
107 SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
108 maxrss_self = ru.ru_maxrss;
109 SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
110 maxrss_children = ru.ru_maxrss;
111 tst_resm(TINFO, "exec.self = %ld, exec.children = %ld",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 maxrss_self, maxrss_children);
Caspar Zhang817c7822011-06-30 01:50:33 +0800113 if (opt_self) {
Caspar Zhang123f8002012-02-10 10:42:32 +0800114 self_nr = SAFE_STRTOL(cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115 self_str, 0, LONG_MAX);
Caspar Zhang817c7822011-06-30 01:50:33 +0800116 delta = maxrss_self - self_nr;
117 if (delta >= -DELTA_MAX && delta <= DELTA_MAX)
118 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119 "initial.self ~= exec.self");
Caspar Zhang817c7822011-06-30 01:50:33 +0800120 else
121 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 "initial.self !~= exec.self");
Caspar Zhang817c7822011-06-30 01:50:33 +0800123 }
124 if (opt_child) {
Caspar Zhang123f8002012-02-10 10:42:32 +0800125 child_nr = SAFE_STRTOL(cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 child_str, 0, LONG_MAX);
Caspar Zhang817c7822011-06-30 01:50:33 +0800127 delta = maxrss_children - child_nr;
128 if (delta >= -DELTA_MAX && delta <= DELTA_MAX)
129 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 "initial.children ~= exec.children");
Caspar Zhang817c7822011-06-30 01:50:33 +0800131 else
132 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 "initial.children !~= exec.children");
Caspar Zhang817c7822011-06-30 01:50:33 +0800134 }
135 }
136 }
137
138 cleanup();
139 tst_exit();
140}
141
142static void usage(void)
143{
144 printf(" -n NUM consume NUM MB size\n");
145 printf(" -g NUM grandchild consume NUM MB size\n");
146 printf(" -v verbose mode, show rusage info\n");
147 printf(" -s NUM compare rusage_self.maxrss with given NUM\n");
148 printf(" -l NUM compare rusage_children.maxrss with given NUM\n");
149}
150
151static void consume(int mega)
152{
153 size_t sz;
154 void *ptr;
155
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 sz = mega * 1024 * 1024;
Caspar Zhang817c7822011-06-30 01:50:33 +0800157 ptr = SAFE_MALLOC(cleanup, sz);
158 memset(ptr, 0, sz);
159}
160
Caspar Zhang817c7822011-06-30 01:50:33 +0800161static void setup(void)
162{
163 tst_sig(FORK, DEF_HANDLER, cleanup);
164
165 TEST_PAUSE;
166}
167
168static void cleanup(void)
169{
Caspar Zhang817c7822011-06-30 01:50:33 +0800170}