1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Out Of Memory (OOM)
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 <sys/types.h>
24f08c3bdfSopenharmony_ci#include <sys/stat.h>
25f08c3bdfSopenharmony_ci#include <errno.h>
26f08c3bdfSopenharmony_ci#include <fcntl.h>
27f08c3bdfSopenharmony_ci#include <stdio.h>
28f08c3bdfSopenharmony_ci#include <stdlib.h>
29f08c3bdfSopenharmony_ci#include <unistd.h>
30f08c3bdfSopenharmony_ci#include "lapi/abisize.h"
31f08c3bdfSopenharmony_ci#include "mem.h"
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void verify_oom(void)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci#ifdef TST_ABI32
36f08c3bdfSopenharmony_ci	tst_brk(TCONF, "test is not designed for 32-bit system.");
37f08c3bdfSopenharmony_ci#endif
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	/* we expect mmap to fail before OOM is hit */
40f08c3bdfSopenharmony_ci	set_sys_tune("overcommit_memory", 2, 1);
41f08c3bdfSopenharmony_ci	oom(NORMAL, 0, ENOMEM, 0);
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	/* with overcommit_memory set to 0 or 1 there's no
44f08c3bdfSopenharmony_ci	 * guarantee that mmap fails before OOM */
45f08c3bdfSopenharmony_ci	set_sys_tune("overcommit_memory", 0, 1);
46f08c3bdfSopenharmony_ci	oom(NORMAL, 0, ENOMEM, 1);
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	set_sys_tune("overcommit_memory", 1, 1);
49f08c3bdfSopenharmony_ci	testoom(0, 0, ENOMEM, 1);
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic struct tst_test test = {
53f08c3bdfSopenharmony_ci	.needs_root = 1,
54f08c3bdfSopenharmony_ci	.forks_child = 1,
55f08c3bdfSopenharmony_ci	.max_runtime = TST_UNLIMITED_RUNTIME,
56f08c3bdfSopenharmony_ci	.test_all = verify_oom,
57f08c3bdfSopenharmony_ci	.save_restore = (const struct tst_path_val[]) {
58f08c3bdfSopenharmony_ci		{"/proc/sys/vm/overcommit_memory", NULL, TST_SR_TBROK},
59f08c3bdfSopenharmony_ci		{}
60f08c3bdfSopenharmony_ci	},
61f08c3bdfSopenharmony_ci};
62