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