1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
5 */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <ctype.h>
10 #include "config.h"
11 #ifdef HAVE_NUMA_V2
12 # include <numa.h>
13 # include <numaif.h>
14 #endif
15
16 #define TST_NO_DEFAULT_MAIN
17 #include "tst_test.h"
18 #include "tst_numa.h"
19
tst_nodemap_print_counters(struct tst_nodemap * nodes)20 void tst_nodemap_print_counters(struct tst_nodemap *nodes)
21 {
22 unsigned int i;
23
24 for (i = 0; i < nodes->cnt; i++) {
25 tst_res(TINFO, "Node %i allocated %u pages",
26 nodes->map[i], nodes->counters[i]);
27 }
28 }
29
tst_nodemap_reset_counters(struct tst_nodemap * nodes)30 void tst_nodemap_reset_counters(struct tst_nodemap *nodes)
31 {
32 size_t arr_size = sizeof(unsigned int) * nodes->cnt;
33
34 if (!nodes->counters)
35 nodes->counters = SAFE_MALLOC(arr_size);
36
37 memset(nodes->counters, 0, arr_size);
38 }
39
tst_nodemap_free(struct tst_nodemap * nodes)40 void tst_nodemap_free(struct tst_nodemap *nodes)
41 {
42 free(nodes->counters);
43 free(nodes);
44 }
45
46 #ifdef HAVE_NUMA_V2
47
tst_numa_mode_name(int mode)48 const char *tst_numa_mode_name(int mode)
49 {
50 switch (mode) {
51 case MPOL_DEFAULT:
52 return "MPOL_DEFAULT";
53 case MPOL_BIND:
54 return "MPOL_BIND";
55 case MPOL_PREFERRED:
56 return "MPOL_PREFERRED";
57 case MPOL_INTERLEAVE:
58 return "MPOL_INTERLEAVE";
59 default:
60 return "???";
61 }
62 }
63
64
inc_counter(unsigned int node,struct tst_nodemap * nodes)65 static void inc_counter(unsigned int node, struct tst_nodemap *nodes)
66 {
67 unsigned int i;
68
69 for (i = 0; i < nodes->cnt; i++) {
70 if (nodes->map[i] == node) {
71 nodes->counters[i]++;
72 break;
73 }
74 }
75 }
76
tst_nodemap_count_pages(struct tst_nodemap * nodes,void * ptr,size_t size)77 void tst_nodemap_count_pages(struct tst_nodemap *nodes,
78 void *ptr, size_t size)
79 {
80 size_t page_size = getpagesize();
81 unsigned int i;
82 int node;
83 long ret;
84 unsigned int pages = (size + page_size - 1)/page_size;
85
86 for (i = 0; i < pages; i++) {
87 ret = get_mempolicy(&node, NULL, 0, ptr + i * page_size, MPOL_F_NODE | MPOL_F_ADDR);
88 if (ret < 0)
89 tst_brk(TBROK | TERRNO, "get_mempolicy() failed");
90
91 if (node < 0) {
92 tst_res(TWARN,
93 "get_mempolicy(...) returned invalid node %i\n", node);
94 continue;
95 }
96
97 inc_counter(node, nodes);
98 }
99 }
100
tst_numa_map(const char * path,size_t size)101 void *tst_numa_map(const char *path, size_t size)
102 {
103 char *ptr;
104 int fd = -1;
105 int flags = MAP_PRIVATE|MAP_ANONYMOUS;
106
107 if (path) {
108 fd = SAFE_OPEN(path, O_CREAT | O_EXCL | O_RDWR, 0666);
109 SAFE_FTRUNCATE(fd, size);
110 flags = MAP_SHARED;
111 }
112
113 ptr = SAFE_MMAP(NULL, size,
114 PROT_READ|PROT_WRITE, flags, fd, 0);
115
116 if (path) {
117 SAFE_CLOSE(fd);
118 SAFE_UNLINK(path);
119 }
120
121 return ptr;
122 }
123
node_has_enough_memory(int node,size_t min_kb)124 static int node_has_enough_memory(int node, size_t min_kb)
125 {
126 char path[1024];
127 char buf[1024];
128 long mem_total = 0;
129 long mem_used = 0;
130
131 /* Make sure there is some space for kernel upkeeping as well */
132 min_kb += 4096;
133
134 snprintf(path, sizeof(path), "/sys/devices/system/node/node%i/meminfo", node);
135
136 if (access(path, F_OK)) {
137 tst_res(TINFO, "File '%s' does not exist! NUMA not enabled?", path);
138 return 0;
139 }
140
141 FILE *fp = fopen(path, "r");
142 if (!fp)
143 tst_brk(TBROK | TERRNO, "Failed to open '%s'", path);
144
145 while (fgets(buf, sizeof(buf), fp)) {
146 long val;
147
148 if (sscanf(buf, "%*s %*i MemTotal: %li", &val) == 1)
149 mem_total = val;
150
151 if (sscanf(buf, "%*s %*i MemUsed: %li", &val) == 1)
152 mem_used = val;
153 }
154
155 fclose(fp);
156
157 if (!mem_total || !mem_used) {
158 tst_res(TWARN, "Failed to parse '%s'", path);
159 return 0;
160 }
161
162 if (mem_total - mem_used < (long)min_kb) {
163 tst_res(TINFO,
164 "Not enough free RAM on node %i, have %likB needs %zukB",
165 node, mem_total - mem_used, min_kb);
166 return 0;
167 }
168
169 return 1;
170 }
171
tst_get_nodemap(int type,size_t min_mem_kb)172 struct tst_nodemap *tst_get_nodemap(int type, size_t min_mem_kb)
173 {
174 struct bitmask *membind;
175 struct tst_nodemap *nodes;
176 unsigned int i, cnt;
177
178 if (type & ~(TST_NUMA_MEM))
179 tst_brk(TBROK, "Invalid type %i\n", type);
180
181 membind = numa_get_membind();
182
183 cnt = 0;
184 for (i = 0; i < membind->size; i++) {
185 if (type & TST_NUMA_MEM && !numa_bitmask_isbitset(membind, i))
186 continue;
187
188 cnt++;
189 }
190
191 tst_res(TINFO, "Found %u NUMA memory nodes", cnt);
192
193 nodes = SAFE_MALLOC(sizeof(struct tst_nodemap)
194 + sizeof(unsigned int) * cnt);
195 nodes->cnt = cnt;
196 nodes->counters = NULL;
197
198 cnt = 0;
199 for (i = 0; i < membind->size; i++) {
200 if (type & TST_NUMA_MEM &&
201 (!numa_bitmask_isbitset(membind, i) ||
202 !node_has_enough_memory(i, min_mem_kb)))
203 continue;
204
205 nodes->map[cnt++] = i;
206 }
207
208 nodes->cnt = cnt;
209
210 numa_bitmask_free(membind);
211
212 return nodes;
213 }
214
215 #endif
216