18
18
19
19
#include < folly/Format.h>
20
20
#include < folly/Random.h>
21
+ #include < numa.h>
22
+ #include < numaif.h>
21
23
22
24
#include < unordered_map>
23
25
@@ -35,6 +37,57 @@ namespace facebook {
35
37
namespace cachelib {
36
38
namespace util {
37
39
40
+ class NumaBitMask {
41
+ public:
42
+ using native_bitmask_type = struct bitmask *;
43
+
44
+ NumaBitMask () { nodesMask = numa_allocate_nodemask (); }
45
+
46
+ NumaBitMask (const NumaBitMask& other) {
47
+ nodesMask = numa_allocate_nodemask ();
48
+ copy_bitmask_to_bitmask (other.nodesMask , nodesMask);
49
+ }
50
+
51
+ NumaBitMask (NumaBitMask&& other) {
52
+ nodesMask = other.nodesMask ;
53
+ other.nodesMask = nullptr ;
54
+ }
55
+
56
+ NumaBitMask (const std::string& str) {
57
+ nodesMask = numa_parse_nodestring_all (str.c_str ());
58
+ }
59
+
60
+ ~NumaBitMask () {
61
+ if (nodesMask) {
62
+ numa_bitmask_free (nodesMask);
63
+ }
64
+ }
65
+
66
+ constexpr NumaBitMask& operator =(const NumaBitMask& other) {
67
+ if (this != &other) {
68
+ if (!nodesMask) {
69
+ nodesMask = numa_allocate_nodemask ();
70
+ }
71
+ copy_bitmask_to_bitmask (other.nodesMask , nodesMask);
72
+ }
73
+ return *this ;
74
+ }
75
+
76
+ native_bitmask_type getNativeBitmask () const noexcept { return nodesMask; }
77
+
78
+ NumaBitMask& setBit (unsigned int n) {
79
+ numa_bitmask_setbit (nodesMask, n);
80
+ return *this ;
81
+ }
82
+
83
+ bool empty () const noexcept {
84
+ return numa_bitmask_equal (numa_no_nodes_ptr, nodesMask) == 1 ;
85
+ }
86
+
87
+ protected:
88
+ native_bitmask_type nodesMask = nullptr ;
89
+ };
90
+
38
91
// A wrapper class for functions to collect counters.
39
92
// It can be initialized by either
40
93
// 1. folly::StringPiece, double -> void, or
@@ -295,6 +348,25 @@ void* mmapAlignedZeroedMemory(size_t alignment,
295
348
size_t numBytes,
296
349
bool noAccess = false );
297
350
351
+ // destroy the mapping created by mmapAlignedZeroedMemory
352
+ //
353
+ // @param addr the pointer to the memory to unmap
354
+ // @param size size of the memory region
355
+ void munmapMemory (void * addr, size_t size);
356
+
357
+ // binds memory to the NUMA nodes specified by nmask.
358
+ //
359
+ // @param addr the pointer to the memory to bind.
360
+ // @param len length of the memory.
361
+ // @param mode mode supported by mmap call
362
+ // @param mask mask specifies node ids
363
+ // @param flags flags supported by mmap call
364
+ void mbindMemory (void * addr,
365
+ unsigned long len,
366
+ int mode,
367
+ const NumaBitMask& mask,
368
+ unsigned int flags);
369
+
298
370
// get the number of pages in the range which are resident in the process.
299
371
//
300
372
// @param mem memory start which is page aligned
0 commit comments