1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Out Of Memory (OOM) for CPUSET 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 tst_res(TINFO, "OOM on CPUSET..."); 45f08c3bdfSopenharmony_ci testoom(0, 0, ENOMEM, 1); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci if (is_numa(NULL, NH_MEMS, 2) && 48f08c3bdfSopenharmony_ci SAFE_CG_HAS(tst_cg, "cpuset.memory_migrate")) { 49f08c3bdfSopenharmony_ci /* 50f08c3bdfSopenharmony_ci * Under NUMA system, the migration of cpuset's memory 51f08c3bdfSopenharmony_ci * is in charge of cpuset.memory_migrate, we can write 52f08c3bdfSopenharmony_ci * 1 to cpuset.memory_migrate to enable the migration. 53f08c3bdfSopenharmony_ci */ 54f08c3bdfSopenharmony_ci SAFE_CG_PRINT(tst_cg, "cpuset.memory_migrate", "1"); 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci tst_res(TINFO, "OOM on CPUSET with mem migrate:"); 57f08c3bdfSopenharmony_ci testoom(0, 0, ENOMEM, 1); 58f08c3bdfSopenharmony_ci } 59f08c3bdfSopenharmony_ci} 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_cistatic void setup(void) 62f08c3bdfSopenharmony_ci{ 63f08c3bdfSopenharmony_ci int memnode, ret; 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci if (!is_numa(NULL, NH_MEMS, 1)) 66f08c3bdfSopenharmony_ci tst_brk(TCONF, "requires NUMA with at least 1 node"); 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci /* 69f08c3bdfSopenharmony_ci * Some nodes do not contain memory, so use 70f08c3bdfSopenharmony_ci * get_allowed_nodes(NH_MEMS) to get a memory 71f08c3bdfSopenharmony_ci * node. This operation also applies to Non-NUMA 72f08c3bdfSopenharmony_ci * systems. 73f08c3bdfSopenharmony_ci */ 74f08c3bdfSopenharmony_ci ret = get_allowed_nodes(NH_MEMS, 1, &memnode); 75f08c3bdfSopenharmony_ci if (ret < 0) 76f08c3bdfSopenharmony_ci tst_brk(TBROK, "Failed to get a memory node " 77f08c3bdfSopenharmony_ci "using get_allowed_nodes()"); 78f08c3bdfSopenharmony_ci write_cpusets(tst_cg, memnode); 79f08c3bdfSopenharmony_ci SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid()); 80f08c3bdfSopenharmony_ci} 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_cistatic struct tst_test test = { 83f08c3bdfSopenharmony_ci .needs_root = 1, 84f08c3bdfSopenharmony_ci .forks_child = 1, 85f08c3bdfSopenharmony_ci .max_runtime = TST_UNLIMITED_RUNTIME, 86f08c3bdfSopenharmony_ci .setup = setup, 87f08c3bdfSopenharmony_ci .test_all = verify_oom, 88f08c3bdfSopenharmony_ci .needs_cgroup_ctrls = (const char *const []){ "cpuset", NULL }, 89f08c3bdfSopenharmony_ci .save_restore = (const struct tst_path_val[]) { 90f08c3bdfSopenharmony_ci {"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK}, 91f08c3bdfSopenharmony_ci {} 92f08c3bdfSopenharmony_ci }, 93f08c3bdfSopenharmony_ci}; 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci#else 96f08c3bdfSopenharmony_ci TST_TEST_TCONF(NUMA_ERROR_MSG); 97f08c3bdfSopenharmony_ci#endif 98