1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2011  Red Hat, Inc.
4f08c3bdfSopenharmony_ci * Copyright (C) 2021 Xie Ziyao <xieziyao@huawei.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Child program executed by getrusage03.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#define TST_NO_DEFAULT_MAIN
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <stdlib.h>
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci#include "getrusage03.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ciint main(int argc, char *argv[])
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	if (argc < 3)
23f08c3bdfSopenharmony_ci		tst_brk(TFAIL, "argc is %d, expected more than two", argc);
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	pid_t pid;
26f08c3bdfSopenharmony_ci	struct rusage ru;
27f08c3bdfSopenharmony_ci	long maxrss_self, maxrss_children;
28f08c3bdfSopenharmony_ci	long consume_nr, grand_consume_nr, self_nr, child_nr;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	tst_reinit();
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	if (!strcmp(argv[1], "consume")) {
33f08c3bdfSopenharmony_ci		consume_nr = SAFE_STRTOL(argv[2], 0, LONG_MAX);
34f08c3bdfSopenharmony_ci		consume_mb(consume_nr);
35f08c3bdfSopenharmony_ci	} else if (!strcmp(argv[1], "grand_consume")) {
36f08c3bdfSopenharmony_ci		grand_consume_nr = SAFE_STRTOL(argv[2], 0, LONG_MAX);
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci		pid = fork();
39f08c3bdfSopenharmony_ci		if (pid == -1)
40f08c3bdfSopenharmony_ci			tst_brk(TBROK, "fork failed");
41f08c3bdfSopenharmony_ci		else if (pid == 0) {
42f08c3bdfSopenharmony_ci			consume_mb(grand_consume_nr);
43f08c3bdfSopenharmony_ci			exit(0);
44f08c3bdfSopenharmony_ci		}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci		tst_reap_children();
47f08c3bdfSopenharmony_ci	} else if (!strcmp(argv[1], "compare")) {
48f08c3bdfSopenharmony_ci		self_nr = SAFE_STRTOL(argv[2], 0, LONG_MAX);
49f08c3bdfSopenharmony_ci		child_nr = SAFE_STRTOL(argv[3], 0, LONG_MAX);
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci		SAFE_GETRUSAGE(RUSAGE_SELF, &ru);
52f08c3bdfSopenharmony_ci		maxrss_self = ru.ru_maxrss;
53f08c3bdfSopenharmony_ci		SAFE_GETRUSAGE(RUSAGE_CHILDREN, &ru);
54f08c3bdfSopenharmony_ci		maxrss_children = ru.ru_maxrss;
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci		if (is_in_delta(maxrss_self - self_nr))
57f08c3bdfSopenharmony_ci			tst_res(TPASS, "initial.self ~= exec.self");
58f08c3bdfSopenharmony_ci		else
59f08c3bdfSopenharmony_ci			tst_res(TFAIL, "initial.self !~= exec.self");
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci		if (is_in_delta(maxrss_children - child_nr))
62f08c3bdfSopenharmony_ci			tst_res(TPASS, "initial.children ~= exec.children");
63f08c3bdfSopenharmony_ci		else
64f08c3bdfSopenharmony_ci			tst_res(TFAIL, "initial.children !~= exec.children");
65f08c3bdfSopenharmony_ci	}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	return 0;
68f08c3bdfSopenharmony_ci}
69