1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/* Copyright (C) 2023 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Lukas Sismis <lsismis@oisf.net>
*/
#include "suricata.h"
#include "util-debug.h"
#include "util-hugepages.h"
static uint16_t SystemHugepageSizesCntPerNodeGet(uint16_t node_index);
static uint16_t SystemNodeCountGet(void);
static void SystemHugepagePerNodeGetHugepageSizes(
uint16_t node_index, uint16_t hp_sizes_cnt, uint32_t *hp_sizes);
static HugepageInfo *SystemHugepageHugepageInfoCreate(uint16_t hp_size_cnt);
static int16_t SystemHugepagePerNodeGetHugepageInfo(uint16_t node_index, NodeInfo *node);
static void SystemHugepageHugepageInfoDestroy(HugepageInfo *h);
static void SystemHugepageNodeInfoDestroy(NodeInfo *n);
static void SystemHugepageNodeInfoDump(NodeInfo *n);
static void SystemHugepageSnapshotDump(SystemHugepageSnapshot *s);
static bool SystemHugepageSupported(void)
{
#if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun
return true;
#else
return false;
#endif /* !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun */
}
// block of all hugepage-specific internal functions
#if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun
/**
* \brief Linux-specific function to detect number of NUMA nodes on the system
* \returns number of NUMA nodes, 0 on error
*/
static uint16_t SystemNodeCountGetLinux(void)
{
char dir_path[] = "/sys/devices/system/node/";
DIR *dir = opendir(dir_path);
if (dir == NULL) {
SCLogError("unable to open %s", dir_path);
return 0;
}
uint16_t count = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
char d_name[] = "node";
if (entry->d_type == DT_DIR && strncmp(entry->d_name, d_name, strlen(d_name)) == 0)
count++;
}
closedir(dir);
return count;
}
/**
* \brief Linux-specific function to detect number of unique hugepage sizes
* \param[in] node_index index of the NUMA node
* \returns number of hugepage sizes, 0 on error
*/
static uint16_t SystemHugepageSizesCntPerNodeGetLinux(uint16_t node_index)
{
char dir_path[256];
snprintf(dir_path, sizeof(dir_path), "/sys/devices/system/node/node%d/hugepages/", node_index);
DIR *dir = opendir(dir_path);
if (dir == NULL) {
SCLogError("unable to open %s", dir_path);
return 0;
}
uint16_t count = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
char d_name[] = "hugepages-";
if (entry->d_type == DT_DIR && strncmp(entry->d_name, d_name, strlen(d_name)) == 0)
count++;
}
closedir(dir);
return count;
}
/**
* \brief Linux-specific function to detect unique hugepage sizes
* \note Arrays `hugepages` and `hp_sizes` are expected to have the same size
* \param[in] node_index index of the NUMA node
* \param[in] hp_sizes_cnt number of the unique hugepage sizes
* \param[out] hp_sizes a pointer to the array of hugepage sizes
*/
static void SystemHugepagePerNodeGetHugepageSizesLinux(
uint16_t node_index, uint16_t hp_sizes_cnt, uint32_t *hp_sizes)
{
char dir_path[256];
snprintf(dir_path, sizeof(dir_path), "/sys/devices/system/node/node%d/hugepages/", node_index);
DIR *dir = opendir(dir_path);
if (dir == NULL) {
SCLogError("unable to open %s", dir_path);
return;
}
uint16_t index = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR && strncmp(entry->d_name, "hugepages-", 10) == 0) {
sscanf(entry->d_name, "hugepages-%ukB", &(hp_sizes[index]));
index++;
}
}
closedir(dir);
}
/**
* \brief Linux-specific function to detect number of unique hugepage sizes
* \note Arrays `hugepages` and `hp_sizes` are expected to have the same size
* \param[out] hugepages a pointer to the array of hugepage info structures
* \param[in] hp_sizes a pointer to the array of hugepage sizes
* \param[in] hp_sizes_cnt number of hugepage sizes
* \param[in] node_index index of the NUMA node
* \returns 0 on success, negative number on error
*/
static int16_t SystemHugepagePerNodeGetHugepageInfoLinux(
HugepageInfo *hugepages, uint32_t *hp_sizes, uint16_t hp_sizes_cnt, uint16_t node_index)
{
for (int16_t i = 0; i < hp_sizes_cnt; i++) {
hugepages[i].size_kb = hp_sizes[i];
char path[256];
snprintf(path, sizeof(path),
"/sys/devices/system/node/node%hu/hugepages/hugepages-%ukB/nr_hugepages",
node_index, hp_sizes[i]);
FILE *f = fopen(path, "r");
if (!f) {
SCLogError("unable to open %s", path);
return -SC_EEXIST;
}
if (fscanf(f, "%hu", &hugepages[i].allocated) != 1) {
SCLogError("failed to read the total number of allocated hugepages (%ukB) on node %hu",
hp_sizes[i], node_index);
fclose(f);
return -SC_EINVAL;
}
fclose(f);
snprintf(path, sizeof(path),
"/sys/devices/system/node/node%hu/hugepages/hugepages-%ukB/free_hugepages",
node_index, hp_sizes[i]);
f = fopen(path, "r");
if (!f) {
SCLogError("unable to open %s", path);
return -SC_EEXIST;
}
if (fscanf(f, "%hu", &hugepages[i].free) != 1) {
SCLogError("failed to read the total number of free hugepages (%ukB) on node %hu",
hp_sizes[i], node_index);
fclose(f);
return -SC_EINVAL;
}
fclose(f);
}
return 0;
}
#endif /* !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun */
/**
* \brief The function gathers information about hugepages on a given node
* \param[in] node_index index of the NUMA node
* \param[out] node a pointer to the structure to hold hugepage info
* \returns 0 on success, negative number on error
*/
static int16_t SystemHugepagePerNodeGetHugepageInfo(uint16_t node_index, NodeInfo *node)
{
uint16_t hp_sizes_cnt = SystemHugepageSizesCntPerNodeGet(node_index);
if (hp_sizes_cnt == 0) {
SCLogError("hugepages not found for node %d", node_index);
return -SC_EEXIST;
}
uint32_t *hp_sizes = SCCalloc(hp_sizes_cnt, sizeof(*hp_sizes));
if (hp_sizes == NULL) {
FatalError("failed to allocate memory for hugepage info");
}
SystemHugepagePerNodeGetHugepageSizes(node_index, hp_sizes_cnt, hp_sizes);
node->hugepages = SystemHugepageHugepageInfoCreate(hp_sizes_cnt);
node->num_hugepage_sizes = hp_sizes_cnt;
int16_t ret = 0;
#if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun
ret = SystemHugepagePerNodeGetHugepageInfoLinux(
node->hugepages, hp_sizes, node->num_hugepage_sizes, node_index);
#endif /* !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun */
SCFree(hp_sizes);
return ret;
}
/**
* \brief The function detects number of NUMA nodes on the system
* \returns 0 if detection is unsuccessful, otherwise number of detected nodes
*/
static uint16_t SystemNodeCountGet(void)
{
#if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun
return SystemNodeCountGetLinux();
#endif /* !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun */
return 0;
}
/**
* \brief The function detects the number of unique hugepage sizes
* \returns 0 if detection is unsuccessful, otherwise number of hugepage sizes
*/
static uint16_t SystemHugepageSizesCntPerNodeGet(uint16_t node_index)
{
#if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun
return SystemHugepageSizesCntPerNodeGetLinux(node_index);
#endif /* !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun */
return 0;
}
/**
* \brief The function fills an array with unique hugepage sizes
* \note Arrays `hugepages` and `hp_sizes` are expected to have the same size
* \param[in] node_index index of the NUMA node
* \param[in] hp_sizes_cnt number of hugepage sizes
* \param[out] hp_sizes a pointer to the array of hugepage sizes
*/
static void SystemHugepagePerNodeGetHugepageSizes(
uint16_t node_index, uint16_t hp_sizes_cnt, uint32_t *hp_sizes)
{
#if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun
return SystemHugepagePerNodeGetHugepageSizesLinux(node_index, hp_sizes_cnt, hp_sizes);
#endif /* !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun */
}
static HugepageInfo *SystemHugepageHugepageInfoCreate(uint16_t hp_size_cnt)
{
HugepageInfo *h = SCCalloc(hp_size_cnt, sizeof(*h));
if (h == NULL) {
FatalError("failed to allocate hugepage info array");
}
return h;
}
static void SystemHugepageHugepageInfoDestroy(HugepageInfo *h)
{
if (h != NULL)
SCFree(h);
}
static void SystemHugepageNodeInfoDestroy(NodeInfo *n)
{
if (n == NULL)
return;
SystemHugepageHugepageInfoDestroy(n->hugepages);
}
static void SystemHugepageNodeInfoDump(NodeInfo *n)
{
if (n == NULL)
return;
for (uint16_t i = 0; i < n->num_hugepage_sizes; i++) {
SCLogDebug("Hugepage size - %dkB - allocated: %d free: %d", n->hugepages[i].size_kb,
n->hugepages[i].allocated, n->hugepages[i].free);
}
}
/**
* \brief The function prints out the hugepage snapshot
* \param[in] s a pointer to the snapshot
*/
static void SystemHugepageSnapshotDump(SystemHugepageSnapshot *s)
{
if (s == NULL)
return;
for (uint16_t i = 0; i < s->num_nodes; i++) {
SCLogDebug("NUMA Node %d", i);
SystemHugepageNodeInfoDump(&(s->nodes[i]));
}
}
void SystemHugepageSnapshotDestroy(SystemHugepageSnapshot *s)
{
if (s == NULL)
return;
for (uint16_t i = 0; i < s->num_nodes; i++) {
SystemHugepageNodeInfoDestroy(&(s->nodes[i]));
}
SCFree(s->nodes);
SCFree(s);
}
/**
* \brief The function creates a snapshot of the system's hugepage usage
* per NUMA node and per hugepage size.
* The snapshot is used to evaluate the system's hugepage usage after
* initialization of Suricata.
* \returns a pointer to the snapshot, NULL on error
*/
SystemHugepageSnapshot *SystemHugepageSnapshotCreate(void)
{
if (!SystemHugepageSupported())
return NULL;
uint16_t node_cnt = SystemNodeCountGet();
if (node_cnt == 0) {
SCLogError("failed to obtain number of NUMA nodes in the system");
return NULL;
}
NodeInfo *nodes = SCCalloc(node_cnt, sizeof(*nodes));
if (nodes == NULL) {
FatalError("failed to allocate memory for NUMA node info");
}
SystemHugepageSnapshot *s = SCCalloc(1, sizeof(*s));
if (s == NULL) {
SCFree(nodes);
FatalError("failed to allocate memory for NUMA node snapshot");
}
s->num_nodes = node_cnt;
s->nodes = nodes;
for (uint16_t i = 0; i < s->num_nodes; i++) {
int16_t ret = SystemHugepagePerNodeGetHugepageInfo(i, &s->nodes[i]);
if (ret != 0) {
SystemHugepageSnapshotDestroy(s);
return NULL;
}
}
return s;
}
/**
* \brief The function compares two hugepage snapshots and prints out
* recommendations for hugepage configuration
* \param[in] pre_s a pointer to the snapshot taken before Suricata initialization
* \param[in] post_s a pointer to the snapshot taken after Suricata initialization
*/
void SystemHugepageEvaluateHugepages(SystemHugepageSnapshot *pre_s, SystemHugepageSnapshot *post_s)
{
if (!SystemHugepageSupported() || pre_s == NULL || post_s == NULL)
return;
SCLogDebug("Hugepages before initialization");
SystemHugepageSnapshotDump(pre_s);
SCLogDebug("Hugepages after initialization");
SystemHugepageSnapshotDump(post_s);
if (pre_s->num_nodes != post_s->num_nodes)
FatalError("Number of NUMA nodes changed during hugepage evaluation");
for (int32_t i = 0; i < post_s->num_nodes; i++) {
if (pre_s->nodes[i].num_hugepage_sizes != post_s->nodes[i].num_hugepage_sizes)
FatalError("Number of NUMA node hugepage sizes changed during hugepage evaluation");
for (int32_t j = 0; j < post_s->nodes->num_hugepage_sizes; j++) {
HugepageInfo *prerun_hp = &pre_s->nodes[i].hugepages[j];
HugepageInfo *postrun_hp = &post_s->nodes[i].hugepages[j];
if (prerun_hp->free == 0) {
continue; // this HP size on this node has no HPs allocated
} else if (prerun_hp->free < postrun_hp->free) {
SCLogWarning(
"Hugepage usage decreased while it should only increase/stay the same");
} else if (prerun_hp->free > 0 && prerun_hp->free == postrun_hp->free) {
SCLogPerf("Hugepages on NUMA node %u are unused and can be deallocated", i);
} else { // assumes this is an active NUMA node because at least some hugepages were
// used
// speculative hint only for 2048kB pages as e.g. 1 GB pages can leave a lot of room
// for additional allocations
if (postrun_hp->size_kb == 2048 && postrun_hp->free == 0) {
SCLogPerf("all %ukB hugepages used on NUMA node %d - consider increasing to "
"prevent memory allocation from other NUMA nodes",
postrun_hp->size_kb, i);
}
float free_hugepages_ratio = (float)postrun_hp->free / (float)prerun_hp->free;
if (free_hugepages_ratio > 0.5) {
int32_t used_hps = prerun_hp->free - postrun_hp->free;
SCLogPerf("Hugepages on NUMA node %u can be set to %.0lf (only using %u/%u "
"%ukB hugepages)",
i, ceil((prerun_hp->free - postrun_hp->free) * 1.15), used_hps,
prerun_hp->free, postrun_hp->size_kb);
}
}
}
}
}
|