1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci/* 7f08c3bdfSopenharmony_ci * We are testing set_mempolicy() with MPOL_INTERLEAVE on mmaped buffers backed 8f08c3bdfSopenharmony_ci * by files. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * Apparently it takes a larger sample for the allocations to be correctly 11f08c3bdfSopenharmony_ci * interleaved. The reason for this is that buffers for file metadata are 12f08c3bdfSopenharmony_ci * allocated in batches in order not to loose performance. Also the pages 13f08c3bdfSopenharmony_ci * cannot be interleaved completely evenly unless the number of pages is 14f08c3bdfSopenharmony_ci * divideable by the number of nodes, which will not happen even if we tried 15f08c3bdfSopenharmony_ci * hard since we do not have controll over metadata blocks for instance. Hence 16f08c3bdfSopenharmony_ci * we cannot really expect to allocate a single file and have the memory 17f08c3bdfSopenharmony_ci * interleaved precisely but it works well if we allocate statistic for a more 18f08c3bdfSopenharmony_ci * than a few files. 19f08c3bdfSopenharmony_ci */ 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci#include <stdio.h> 22f08c3bdfSopenharmony_ci#include <errno.h> 23f08c3bdfSopenharmony_ci#include "config.h" 24f08c3bdfSopenharmony_ci#ifdef HAVE_NUMA_V2 25f08c3bdfSopenharmony_ci# include <numa.h> 26f08c3bdfSopenharmony_ci# include <numaif.h> 27f08c3bdfSopenharmony_ci#endif 28f08c3bdfSopenharmony_ci#include "tst_test.h" 29f08c3bdfSopenharmony_ci#include "tst_numa.h" 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ci#define MNTPOINT "mntpoint" 32f08c3bdfSopenharmony_ci#define FILES 10 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_ci#ifdef HAVE_NUMA_V2 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci#include "set_mempolicy.h" 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cistatic size_t page_size; 39f08c3bdfSopenharmony_cistatic struct tst_nodemap *nodes; 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_cistatic void setup(void) 42f08c3bdfSopenharmony_ci{ 43f08c3bdfSopenharmony_ci int node_min_pages = FILES * (FILES + 1) / 2 * 10 + FILES * 10; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci page_size = getpagesize(); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci nodes = tst_get_nodemap(TST_NUMA_MEM, node_min_pages * page_size / 1024); 48f08c3bdfSopenharmony_ci if (nodes->cnt <= 1) 49f08c3bdfSopenharmony_ci tst_brk(TCONF, "Test requires at least two NUMA memory nodes"); 50f08c3bdfSopenharmony_ci} 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_cistatic void cleanup(void) 53f08c3bdfSopenharmony_ci{ 54f08c3bdfSopenharmony_ci tst_nodemap_free(nodes); 55f08c3bdfSopenharmony_ci} 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_cistatic void alloc_and_check(void) 58f08c3bdfSopenharmony_ci{ 59f08c3bdfSopenharmony_ci unsigned int i, j; 60f08c3bdfSopenharmony_ci char path[1024]; 61f08c3bdfSopenharmony_ci unsigned int total_pages = 0; 62f08c3bdfSopenharmony_ci unsigned int sum_pages = 0; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci tst_nodemap_reset_counters(nodes); 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci /* 67f08c3bdfSopenharmony_ci * The inner loop loops node->cnt times to ensure the sum could 68f08c3bdfSopenharmony_ci * be evenly distributed among the nodes. 69f08c3bdfSopenharmony_ci */ 70f08c3bdfSopenharmony_ci for (i = 1; i <= FILES; i++) { 71f08c3bdfSopenharmony_ci for (j = 1; j <= nodes->cnt; j++) { 72f08c3bdfSopenharmony_ci size_t size = 10 * i + j % 10; 73f08c3bdfSopenharmony_ci snprintf(path, sizeof(path), MNTPOINT "/numa-test-file-%i-%i", i, j); 74f08c3bdfSopenharmony_ci alloc_fault_count(nodes, path, size * page_size); 75f08c3bdfSopenharmony_ci total_pages += size; 76f08c3bdfSopenharmony_ci } 77f08c3bdfSopenharmony_ci } 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci for (i = 0; i < nodes->cnt; i++) { 80f08c3bdfSopenharmony_ci float treshold = 1.00 * total_pages / 60; /* five percents */ 81f08c3bdfSopenharmony_ci float min_pages = 1.00 * total_pages / nodes->cnt - treshold; 82f08c3bdfSopenharmony_ci float max_pages = 1.00 * total_pages / nodes->cnt + treshold; 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_ci if (nodes->counters[i] > min_pages && nodes->counters[i] < max_pages) { 85f08c3bdfSopenharmony_ci tst_res(TPASS, "Node %u allocated %u <%.2f,%.2f>", 86f08c3bdfSopenharmony_ci nodes->map[i], nodes->counters[i], min_pages, max_pages); 87f08c3bdfSopenharmony_ci } else { 88f08c3bdfSopenharmony_ci tst_res(TFAIL, "Node %u allocated %u, expected <%.2f,%.2f>", 89f08c3bdfSopenharmony_ci nodes->map[i], nodes->counters[i], min_pages, max_pages); 90f08c3bdfSopenharmony_ci } 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci sum_pages += nodes->counters[i]; 93f08c3bdfSopenharmony_ci } 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci if (sum_pages != total_pages) { 96f08c3bdfSopenharmony_ci tst_res(TFAIL, "Sum of nodes %u != allocated pages %u", 97f08c3bdfSopenharmony_ci sum_pages, total_pages); 98f08c3bdfSopenharmony_ci return; 99f08c3bdfSopenharmony_ci } 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci tst_res(TPASS, "Sum of nodes equals to allocated pages (%u)", total_pages); 102f08c3bdfSopenharmony_ci} 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_cistatic void verify_set_mempolicy(void) 105f08c3bdfSopenharmony_ci{ 106f08c3bdfSopenharmony_ci struct bitmask *bm = numa_allocate_nodemask(); 107f08c3bdfSopenharmony_ci unsigned int alloc_on_nodes = nodes->cnt; 108f08c3bdfSopenharmony_ci unsigned int i; 109f08c3bdfSopenharmony_ci 110f08c3bdfSopenharmony_ci for (i = 0; i < alloc_on_nodes; i++) 111f08c3bdfSopenharmony_ci numa_bitmask_setbit(bm, nodes->map[i]); 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci TEST(set_mempolicy(MPOL_INTERLEAVE, bm->maskp, bm->size+1)); 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci if (TST_RET) { 116f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 117f08c3bdfSopenharmony_ci "set_mempolicy(MPOL_INTERLEAVE)"); 118f08c3bdfSopenharmony_ci return; 119f08c3bdfSopenharmony_ci } 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci tst_res(TPASS, "set_mempolicy(MPOL_INTERLEAVE)"); 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_ci alloc_and_check(); 124f08c3bdfSopenharmony_ci 125f08c3bdfSopenharmony_ci numa_free_nodemask(bm); 126f08c3bdfSopenharmony_ci} 127f08c3bdfSopenharmony_ci 128f08c3bdfSopenharmony_cistatic struct tst_test test = { 129f08c3bdfSopenharmony_ci .setup = setup, 130f08c3bdfSopenharmony_ci .cleanup = cleanup, 131f08c3bdfSopenharmony_ci .test_all = verify_set_mempolicy, 132f08c3bdfSopenharmony_ci .forks_child = 1, 133f08c3bdfSopenharmony_ci .needs_root = 1, 134f08c3bdfSopenharmony_ci .all_filesystems = 1, 135f08c3bdfSopenharmony_ci .mntpoint = MNTPOINT, 136f08c3bdfSopenharmony_ci .needs_checkpoints = 1, 137f08c3bdfSopenharmony_ci}; 138f08c3bdfSopenharmony_ci 139f08c3bdfSopenharmony_ci#else 140f08c3bdfSopenharmony_ci 141f08c3bdfSopenharmony_ciTST_TEST_TCONF(NUMA_ERROR_MSG); 142f08c3bdfSopenharmony_ci 143f08c3bdfSopenharmony_ci#endif /* HAVE_NUMA_V2 */ 144