xref: /third_party/ltp/include/tst_numa.h (revision f08c3bdf)
1/* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
3 */
4
5#ifndef TST_NUMA_H__
6#define TST_NUMA_H__
7
8#include <string.h>
9
10/**
11 * Numa nodemap.
12 */
13struct tst_nodemap {
14        /** Number of nodes in map */
15	unsigned int cnt;
16	/** Page allocation counters */
17	unsigned int *counters;
18	/** Array of numa ids */
19	unsigned int map[];
20};
21
22/**
23 * Clears numa counters. The counters are lazy-allocated on first call of this function.
24 *
25 * @nodes Numa nodemap.
26 */
27void tst_nodemap_reset_counters(struct tst_nodemap *nodes);
28
29/**
30 * Prints pages allocated per each node.
31 *
32 * @nodes Numa nodemap.
33 */
34void tst_nodemap_print_counters(struct tst_nodemap *nodes);
35
36/**
37 * Returns a name for a mempolicy/mbind mode.
38 *
39 * @mode Numa mempolicy mode.
40 */
41const char *tst_mempolicy_mode_name(int mode);
42
43/**
44 * Maps pages into memory, if path is NULL the mapping is anonymous otherwise is backed by the file.
45 *
46 * @path Path to a file, if not NULL mapping is file based.
47 * @size Mapping size.
48 */
49void *tst_numa_map(const char *path, size_t size);
50
51/*
52 * Writes to memory in order to get the pages faulted.
53 *
54 * @ptr Start of the mapping.
55 * @size Size of the mapping.
56 */
57static inline void tst_numa_fault(void *ptr, size_t size)
58{
59	memset(ptr, 'a', size);
60}
61
62/*
63 * Frees the memory.
64 *
65 * @ptr Start of the mapping.
66 * @size Size of the mapping.
67 */
68static inline void tst_numa_unmap(void *ptr, size_t size)
69{
70	SAFE_MUNMAP(ptr, size);
71}
72
73/**
74 * Check on which numa node resides each page of the mapping starting at ptr
75 * and continuing pages long and increases nodemap counters accordingly.
76 *
77 * @nodes Nodemap with initialized counters.
78 * @ptr   Pointer to start of a mapping.
79 * @size  Size of the mapping.
80 */
81void tst_nodemap_count_pages(struct tst_nodemap *nodes, void *ptr, size_t size);
82
83/**
84 * Frees nodemap.
85 *
86 * @nodes Numa nodemap to be freed.
87 */
88void tst_nodemap_free(struct tst_nodemap *nodes);
89
90/**
91 * Bitflags for tst_get_nodemap() function.
92 */
93enum tst_numa_types {
94	TST_NUMA_ANY = 0x00,
95	TST_NUMA_MEM = 0x01,
96};
97
98/**
99 * Allocates and returns numa node map, which is an array of numa nodes which
100 * contain desired resources e.g. memory.
101 *
102 * @type       Bitflags of enum tst_numa_types specifying desired resources.
103 * @min_mem_kb Minimal free RAM on memory nodes, if given node has less than
104 *             requested amount of free+buffers memory it's not included in
105 *             the resulting list of nodes.
106 *
107 * @return On success returns allocated and initialized struct tst_nodemap which contains
108 *         array of numa node ids that contains desired resources.
109 */
110struct tst_nodemap *tst_get_nodemap(int type, size_t min_mem_kb);
111
112#endif /* TST_NUMA_H__ */
113