NUMA++ 0.11.0
Loading...
Searching...
No Matches
cpumask.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 CpumaskTrait
7 */
8#ifndef NUMAPP_CPUMASK_HPP_
9#define NUMAPP_CPUMASK_HPP_
10#include <numa.h>
11#include <system_error>
12
13#include <system_error>
14
15#include "bitmask.hpp"
16
17namespace numapp {
18
19/**
20 * Type-safe CPU mask
21 *
22 * @sa ForEach
23 * @ingroup numapp_numa
24 */
25class Cpumask : public Bitmask<Cpumask> {
26public:
27 /**
28 * Inherit constructors from Bitmask.
29 */
30 using Bitmask<Cpumask>::Bitmask;
31
32 /**
33 * Construct Cpumask from cpustring that does not consider current cpuset.
34 *
35 * @note Uses @c numa_parse_cpustring_all to parse.
36 *
37 * @manpages
38 * @manpage{numa,3}
39 *
40 * @param cpustring Mask expressed as cpustring in the form of comma separated list of cpu
41 * numbers or ranges, e.g. `1,3-8`. See man page for additional details.
42 * @throws std::system_error containing error code if parsing fails.
43 */
44 [[nodiscard]] static Cpumask MakeFromCpuStringAll(char const* cpustring) {
45 // note: numa_parse_cpustring_all is used rather than numa_parse_cpustring
46 // to *not* consider current cpuset, which would e.g. exclude CPUs isolated with
47 // isolcpus.
48 return MakeFromString(cpustring, numa_parse_cpustring_all);
49 }
50
51 /**
52 * Construct Cpumask from cpustring that considers current cpuset.
53 *
54 * @note Uses @c numa_parse_cpustring to parse.
55 *
56 * @manpages
57 * @manpage{numa,3}
58 *
59 * @param cpustring Mask expressed as cpustring in the form of comma separated list of cpu
60 * numbers or ranges, e.g. `1,3-8`. See man page for additional details.
61 * @throws std::system_error containing error code if parsing fails.
62 */
63 [[nodiscard]] static Cpumask MakeFromCpuString(char const* cpustring) {
64 return MakeFromString(cpustring, numa_parse_cpustring);
65 }
66
67protected:
68 using ParseFunc = struct bitmask* (*)(const char*);
69 [[nodiscard]] static Cpumask MakeFromString(char const* cpustring, ParseFunc func) {
70 // note: numa_parse functions do not set errc.
71 auto* mask_ptr = func(cpustring);
72 if (mask_ptr == nullptr) {
73 throw std::system_error(std::make_error_code(std::errc::invalid_argument),
74 "Could not parse cpustring");
75 } else {
76 return Cpumask(NumaBitmaskPtr(mask_ptr, &numa_free_cpumask));
77 }
78 }
79 friend class Bitmask<Cpumask>;
80 [[nodiscard]] static NumaBitmaskPtr Alloc() {
81 return NumaBitmaskPtr(numa_allocate_cpumask(), &numa_free_cpumask);
82 }
83};
84
85/**
86 * Formats @a mask and inserts it to @a os.
87 *
88 * Mask up to number of configured CPUs are formatted if no higher bits are set.
89 *
90 * @param os output stream to insert into.
91 * @param mask Cpumask to format.
92 * @returns @a os
93 *
94 * @ingroup numapp_numa
95 */
96inline std::ostream& operator<<(std::ostream& os, Cpumask const& mask) {
97 return FormatBitmask(os, mask, numa_num_configured_cpus());
98}
99
100} // namespace numapp
101
102#endif // #ifndef NUMAPP_CPUMASK_HPP_
Contains declarations for numapp::Bitmask.
Type-safe CPU mask.
Definition cpumask.hpp:25
static Cpumask MakeFromCpuString(char const *cpustring)
Construct Cpumask from cpustring that considers current cpuset.
Definition cpumask.hpp:63
static Cpumask MakeFromCpuStringAll(char const *cpustring)
Construct Cpumask from cpustring that does not consider current cpuset.
Definition cpumask.hpp:44
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