1818
1919#include < folly/Format.h>
2020#include < folly/Random.h>
21+ #include < numa.h>
22+ #include < numaif.h>
2123
2224#include < unordered_map>
2325
@@ -35,6 +37,57 @@ namespace facebook {
3537namespace cachelib {
3638namespace util {
3739
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+
3891// A wrapper class for functions to collect counters.
3992// It can be initialized by either
4093// 1. folly::StringPiece, double -> void, or
@@ -288,6 +341,25 @@ void* mmapAlignedZeroedMemory(size_t alignment,
288341 size_t numBytes,
289342 bool noAccess = false );
290343
344+ // destroy the mapping created by mmapAlignedZeroedMemory
345+ //
346+ // @param addr the pointer to the memory to unmap
347+ // @param size size of the memory region
348+ void munmapMemory (void * addr, size_t size);
349+
350+ // binds memory to the NUMA nodes specified by nmask.
351+ //
352+ // @param addr the pointer to the memory to bind.
353+ // @param len length of the memory.
354+ // @param mode mode supported by mmap call
355+ // @param mask mask specifies node ids
356+ // @param flags flags supported by mmap call
357+ void mbindMemory (void * addr,
358+ unsigned long len,
359+ int mode,
360+ const NumaBitMask& mask,
361+ unsigned int flags);
362+
291363// get the number of pages in the range which are resident in the process.
292364//
293365// @param mem memory start which is page aligned
0 commit comments