NUMA++ 0.11.0
Loading...
Searching...
No Matches
nodemask.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup numapp_numa
4 * @copyright ESO 2024 - European Southern Observatory
5 *
6 * @brief Contains declarations for Nodemask
7 */
8#ifndef NUMAPP_NODEMASK_HPP_
9#define NUMAPP_NODEMASK_HPP_
10#include <system_error>
11#include <numa.h>
12
13#include "bitmask.hpp"
14
15namespace numapp {
16
17/**
18 * Type-safe NUMA node mask
19 *
20 * @sa ForEach
21 * @ingroup numapp_numa
22 */
23struct Nodemask : public Bitmask<Nodemask> {
24public:
25 /**
26 * Inherit constructors from Bitmask.
27 */
28 using Bitmask<Nodemask>::Bitmask;
29
30 /**
31 * Construct Nodemask from nodestring that considers current cpuset.
32 *
33 * @note Uses @c numa_parse_nodestring to parse.
34 *
35 * @manpages
36 * @manpage{numa,3}
37 *
38 * @param nodestring Mask expressed as nodestring. See man page.
39 * @throws std::system_error containing error code if parsing fails.
40 */
41 [[nodiscard]] static Nodemask MakeFromNodestring(char const* nodestring) {
42 return MakeFromString(nodestring, numa_parse_nodestring);
43 }
44
45 /**
46 * Construct Nodemask from nodestring that considers does not consider current cpuset.
47 *
48 * @note Uses @c numa_parse_nodestring_all to parse.
49 *
50 * @manpages
51 * @manpage{numa,3}
52 *
53 * @param nodestring Mask expressed as nodestring. See man page.
54 * @throws std::system_error containing error code if parsing fails.
55 */
56 [[nodiscard]] static Nodemask MakeFromNodestringAll(char const* nodestring) {
57 return MakeFromString(nodestring, numa_parse_nodestring_all);
58 }
59protected:
60 using ParseFunc = struct bitmask *(*)(const char *);
61 [[nodiscard]] static Nodemask MakeFromString(char const* nodestring, ParseFunc func) {
62 // note: numa_parse functions do not set errno.
63 auto* mask_ptr = func(nodestring);
64 if (mask_ptr == nullptr) {
65 throw std::system_error(std::make_error_code(std::errc::invalid_argument),
66 "Could not parse nodestring");
67 } else if (mask_ptr == numa_no_nodes_ptr) {
68 // CpuAffinity takes ownership of the struct bitmask* so we create our own empty
69 // set instead of returning the globally shared numa_no_nodes_ptr.
70 return Alloc();
71 } else {
72 return NumaBitmaskPtr(mask_ptr, &numa_free_nodemask);
73 }
74 }
75
76 friend class Bitmask<Nodemask>;
77 [[nodiscard]] static NumaBitmaskPtr Alloc() {
78 return NumaBitmaskPtr(numa_allocate_nodemask(), &numa_free_nodemask);
79 }
80};
81
82
83/**
84 * Formats @a mask and inserts it to @a os.
85 *
86 * Mask up to number of configured NUMA nodes are formatted if no higher bits are set.
87 *
88 * @param os output stream to insert into.
89 * @param mask Nodemask to format.
90 * @returns @a os
91 *
92 * @ingroup numapp_numa
93 */
94inline std::ostream& operator<<(std::ostream& os, Nodemask const& mask) {
95 return FormatBitmask(os, mask, numa_num_configured_nodes());
96}
97
98} // namespace numapp
99
100#endif // #ifndef NUMAPP_NODEMASK_HPP_
Contains declarations for numapp::Bitmask.
std::unique_ptr< struct bitmask, void(*)(struct bitmask *)> NumaBitmaskPtr
Lowlevel bitmask type.
Definition bitmask.hpp:23
std::ostream & FormatBitmask(std::ostream &os, bitmask const *mask, int min_bits)
Formats mask and inserts it to os.
Definition bitmask.cpp:13
Type-safe NUMA node mask.
Definition nodemask.hpp:23
static Nodemask MakeFromNodestringAll(char const *nodestring)
Construct Nodemask from nodestring that considers does not consider current cpuset.
Definition nodemask.hpp:56
static Nodemask MakeFromNodestring(char const *nodestring)
Construct Nodemask from nodestring that considers current cpuset.
Definition nodemask.hpp:41