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