aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorCyril Hrubis <chrubis@suse.cz>2019-03-06 16:57:57 +0100
committerCyril Hrubis <chrubis@suse.cz>2019-03-07 13:41:57 +0100
commitb70e19de2815985581c915ba01d995d0206d9dd1 (patch)
treecff18c2971215a7267ab44f963aa44246ac49d1f /include
parenteea3ba496b7323608b457024d5d05e2641911871 (diff)
lib/tst_numa: Add library for NUMA related syscalls
The library implements a nodemap, which is a map of nodes (array) containing node ids for a subset of system nodes. Currently nodes can be filtered based on memory requirements, which is needed for testing memory related NUMA syscalls such as set_mempolicy() and membind() where we usually need at least two memory nodes with free memory. It also implements counters, so that we can precisely count, for a given memory range, how much memory was allocated on each node in the map and a few simple functions for mapping, faulting and unmapping memory. Signed-off-by: Cyril Hrubis <chrubis@suse.cz> CC: Vlastimil Babka <vbabka@suse.cz> CC: Michal Hocko <mhocko@kernel.org> CC: Jan Stancek <jstancek@redhat.com> Acked-by: Jan Stancek <jstancek@redhat.com>
Diffstat (limited to 'include')
-rw-r--r--include/tst_numa.h112
1 files changed, 112 insertions, 0 deletions
diff --git a/include/tst_numa.h b/include/tst_numa.h
new file mode 100644
index 000000000..a4cd1be37
--- /dev/null
+++ b/include/tst_numa.h
@@ -0,0 +1,112 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+#ifndef TST_NUMA_H__
+#define TST_NUMA_H__
+
+/**
+ * Numa nodemap.
+ */
+struct tst_nodemap {
+ /** Number of nodes in map */
+ unsigned int cnt;
+ /** Page allocation counters */
+ unsigned int *counters;
+ /** Array of numa ids */
+ unsigned int map[];
+};
+
+/**
+ * Clears numa counters. The counters are lazy-allocated on first call of this function.
+ *
+ * @nodes Numa nodemap.
+ */
+void tst_nodemap_reset_counters(struct tst_nodemap *nodes);
+
+/**
+ * Prints pages allocated per each node.
+ *
+ * @nodes Numa nodemap.
+ */
+void tst_nodemap_print_counters(struct tst_nodemap *nodes);
+
+/**
+ * Returns a name for a mempolicy/mbind mode.
+ *
+ * @mode Numa mempolicy mode.
+ */
+const char *tst_numa_mode_name(int mode);
+
+/**
+ * Maps pages into memory, if path is NULL the mapping is anonymous otherwise is backed by the file.
+ *
+ * @path Path to a file, if not NULL mapping is file based.
+ * @size Mapping size.
+ */
+void *tst_numa_map(const char *path, size_t size);
+
+/*
+ * Writes to memory in order to get the pages faulted.
+ *
+ * @ptr Start of the mapping.
+ * @size Size of the mapping.
+ */
+static inline void tst_numa_fault(void *ptr, size_t size)
+{
+ memset(ptr, 'a', size);
+}
+
+/*
+ * Frees the memory.
+ *
+ * @ptr Start of the mapping.
+ * @size Size of the mapping.
+ */
+static inline void tst_numa_unmap(void *ptr, size_t size)
+{
+ SAFE_MUNMAP(ptr, size);
+}
+
+/**
+ * Check on which numa node resides each page of the mapping starting at ptr
+ * and continuing pages long and increases nodemap counters accordingly.
+ *
+ * @nodes Nodemap with initialized counters.
+ * @ptr Pointer to start of a mapping.
+ * @size Size of the mapping.
+ */
+void tst_nodemap_count_pages(struct tst_nodemap *nodes, void *ptr, size_t size);
+
+/**
+ * Frees nodemap.
+ *
+ * @nodes Numa nodemap to be freed.
+ */
+void tst_nodemap_free(struct tst_nodemap *nodes);
+
+/**
+ * Bitflags for tst_get_nodemap() function.
+ */
+enum tst_numa_types {
+ TST_NUMA_ANY = 0x00,
+ TST_NUMA_MEM = 0x01,
+};
+
+/**
+ * Allocates and returns numa node map, which is an array of numa nodes which
+ * contain desired resources e.g. memory.
+ *
+ * @type Bitflags of enum tst_numa_types specifying desired resources.
+ * @min_mem_kb Minimal free RAM on memory nodes, if given node has less than
+ * requested amount of free+buffers memory it's not included in
+ * the resulting list of nodes.
+ *
+ * @return On success returns allocated and initialized struct tst_nodemap which contains
+ * array of numa node ids that contains desired resources.
+ */
+struct tst_nodemap *tst_get_nodemap(int type, size_t min_mem_kb);
+
+#endif /* TST_NUMA_H__ */