xref: /third_party/ltp/testcases/kernel/mem/oom/oom03.c (revision f08c3bdf)
1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Out Of Memory (OOM) for Memory Resource Controller
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * The program is designed to cope with unpredictable like amount and
5f08c3bdfSopenharmony_ci * system physical memory, swap size and other VMM technology like KSM,
6f08c3bdfSopenharmony_ci * memcg, memory hotplug and so on which may affect the OOM
7f08c3bdfSopenharmony_ci * behaviours. It simply increase the memory consumption 3G each time
8f08c3bdfSopenharmony_ci * until all the available memory is consumed and OOM is triggered.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Copyright (C) 2010-2017  Red Hat, Inc.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * This program is free software;  you can redistribute it and/or modify
13f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by
14f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
15f08c3bdfSopenharmony_ci * (at your option) any later version.
16f08c3bdfSopenharmony_ci *
17f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
18f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY;  without even the implied warranty of
19f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
20f08c3bdfSopenharmony_ci * the GNU General Public License for more details.
21f08c3bdfSopenharmony_ci */
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include "config.h"
24f08c3bdfSopenharmony_ci#include <sys/types.h>
25f08c3bdfSopenharmony_ci#include <sys/stat.h>
26f08c3bdfSopenharmony_ci#include <errno.h>
27f08c3bdfSopenharmony_ci#include <fcntl.h>
28f08c3bdfSopenharmony_ci#include <stdio.h>
29f08c3bdfSopenharmony_ci#if HAVE_NUMA_H
30f08c3bdfSopenharmony_ci#include <numa.h>
31f08c3bdfSopenharmony_ci#endif
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci#include "lapi/abisize.h"
34f08c3bdfSopenharmony_ci#include "numa_helper.h"
35f08c3bdfSopenharmony_ci#include "mem.h"
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci#ifdef HAVE_NUMA_V2
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void verify_oom(void)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci#ifdef TST_ABI32
42f08c3bdfSopenharmony_ci	tst_brk(TCONF, "test is not designed for 32-bit system.");
43f08c3bdfSopenharmony_ci#endif
44f08c3bdfSopenharmony_ci	testoom(0, 0, ENOMEM, 1);
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
47f08c3bdfSopenharmony_ci		tst_res(TINFO, "OOM on MEMCG with special memswap limitation:");
48f08c3bdfSopenharmony_ci		/*
49f08c3bdfSopenharmony_ci		 * Cgroup v2 tracks memory and swap in separate, which splits
50f08c3bdfSopenharmony_ci		 * memory and swap counter. So with swappiness enable (default
51f08c3bdfSopenharmony_ci		 * value is 60 on RHEL), it likely has part of memory swapping
52f08c3bdfSopenharmony_ci		 * out during the allocating.
53f08c3bdfSopenharmony_ci		 *
54f08c3bdfSopenharmony_ci		 * To get more opportunities to reach the swap limitation,
55f08c3bdfSopenharmony_ci		 * let's scale down the value of 'memory.swap.max' to only
56f08c3bdfSopenharmony_ci		 * 1MB for CGroup v2.
57f08c3bdfSopenharmony_ci		 */
58f08c3bdfSopenharmony_ci		if (!TST_CG_VER_IS_V1(tst_cg, "memory"))
59f08c3bdfSopenharmony_ci			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", MB);
60f08c3bdfSopenharmony_ci		else
61f08c3bdfSopenharmony_ci			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", TESTMEM + MB);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci		testoom(0, 1, ENOMEM, 1);
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci		if (TST_CG_VER_IS_V1(tst_cg, "memory"))
66f08c3bdfSopenharmony_ci			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
67f08c3bdfSopenharmony_ci		else
68f08c3bdfSopenharmony_ci			SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
69f08c3bdfSopenharmony_ci	}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	/* OOM for MEMCG with mempolicy */
72f08c3bdfSopenharmony_ci	if (is_numa(NULL, NH_MEMS, 2)) {
73f08c3bdfSopenharmony_ci		tst_res(TINFO, "OOM on MEMCG & mempolicy...");
74f08c3bdfSopenharmony_ci		testoom(MPOL_BIND, 0, ENOMEM, 1);
75f08c3bdfSopenharmony_ci		testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
76f08c3bdfSopenharmony_ci		testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
77f08c3bdfSopenharmony_ci	}
78f08c3bdfSopenharmony_ci}
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_cistatic void setup(void)
81f08c3bdfSopenharmony_ci{
82f08c3bdfSopenharmony_ci	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
83f08c3bdfSopenharmony_ci	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cistatic struct tst_test test = {
87f08c3bdfSopenharmony_ci	.needs_root = 1,
88f08c3bdfSopenharmony_ci	.forks_child = 1,
89f08c3bdfSopenharmony_ci	.max_runtime = TST_UNLIMITED_RUNTIME,
90f08c3bdfSopenharmony_ci	.setup = setup,
91f08c3bdfSopenharmony_ci	.test_all = verify_oom,
92f08c3bdfSopenharmony_ci	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
93f08c3bdfSopenharmony_ci	.save_restore = (const struct tst_path_val[]) {
94f08c3bdfSopenharmony_ci		{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
95f08c3bdfSopenharmony_ci		{}
96f08c3bdfSopenharmony_ci	},
97f08c3bdfSopenharmony_ci};
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci#else
100f08c3bdfSopenharmony_ci	TST_TEST_TCONF(NUMA_ERROR_MSG);
101f08c3bdfSopenharmony_ci#endif
102