/home/runner/work/LabNeura/LabNeura/src/labneura/runtime.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/runtime.h" |
2 | | |
3 | | #include "runtime_internal.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <atomic> |
7 | | #include <charconv> |
8 | | #include <cstdint> |
9 | | #include <fstream> |
10 | | #include <stdexcept> |
11 | | #include <string> |
12 | | #include <thread> |
13 | | |
14 | | #if defined(__APPLE__) |
15 | | #include <sys/sysctl.h> |
16 | | #elif defined(__linux__) |
17 | | #include <sched.h> |
18 | | #include <unistd.h> |
19 | | #endif |
20 | | |
21 | | namespace labneura { |
22 | | namespace { |
23 | | |
24 | | std::atomic<std::size_t> configured_thread_count{1}; |
25 | | |
26 | | #if defined(__APPLE__) |
27 | | std::size_t sysctl_cpu_count(const char* name) noexcept { |
28 | | int value = 0; |
29 | | std::size_t length = sizeof(value); |
30 | | if (sysctlbyname(name, &value, &length, nullptr, 0) != 0 || |
31 | | length != sizeof(value) || value <= 0) { |
32 | | return 0; |
33 | | } |
34 | | return static_cast<std::size_t>(value); |
35 | | } |
36 | | #elif defined(__linux__) |
37 | | bool parse_positive_integer(const std::string& text, |
38 | 0 | std::uint64_t& value) noexcept { |
39 | 0 | if (text.empty()) return false; |
40 | 0 | const auto result = |
41 | 0 | std::from_chars(text.data(), text.data() + text.size(), value); |
42 | 0 | return result.ec == std::errc{} && result.ptr == text.data() + text.size() && |
43 | 0 | value > 0; |
44 | 0 | } |
45 | | |
46 | 1 | std::size_t cgroup_v2_cpu_limit() { |
47 | 1 | std::ifstream input("/sys/fs/cgroup/cpu.max"); |
48 | 1 | std::string quota_text; |
49 | 1 | std::string period_text; |
50 | 1 | if (!(input >> quota_text >> period_text) || quota_text == "max") return 0; |
51 | | |
52 | 0 | std::uint64_t quota = 0; |
53 | 0 | std::uint64_t period = 0; |
54 | 0 | if (!parse_positive_integer(quota_text, quota) || |
55 | 0 | !parse_positive_integer(period_text, period)) { |
56 | 0 | return 0; |
57 | 0 | } |
58 | 0 | return static_cast<std::size_t>(std::max<std::uint64_t>(1, quota / period)); |
59 | 0 | } |
60 | | |
61 | 1 | std::size_t cgroup_v1_cpu_limit() { |
62 | 1 | std::ifstream quota_file("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"); |
63 | 1 | std::ifstream period_file("/sys/fs/cgroup/cpu/cpu.cfs_period_us"); |
64 | 1 | std::int64_t quota = 0; |
65 | 1 | std::uint64_t period = 0; |
66 | 1 | if (!(quota_file >> quota) || !(period_file >> period) || quota <= 0 || |
67 | 1 | period == 0) { |
68 | 1 | return 0; |
69 | 1 | } |
70 | 0 | return static_cast<std::size_t>( |
71 | 0 | std::max<std::uint64_t>(1, static_cast<std::uint64_t>(quota) / period)); |
72 | 1 | } |
73 | | |
74 | 1 | std::size_t linux_cpu_count() { |
75 | 1 | std::size_t count = 0; |
76 | | |
77 | 1 | cpu_set_t affinity; |
78 | 1 | CPU_ZERO(&affinity); |
79 | 1 | if (sched_getaffinity(0, sizeof(affinity), &affinity) == 0) { |
80 | 1 | const int affinity_count = CPU_COUNT(&affinity); |
81 | 1 | if (affinity_count > 0) { |
82 | 1 | count = static_cast<std::size_t>(affinity_count); |
83 | 1 | } |
84 | 1 | } |
85 | | |
86 | 1 | if (count == 0) { |
87 | 0 | const long online = sysconf(_SC_NPROCESSORS_ONLN); |
88 | 0 | if (online > 0) count = static_cast<std::size_t>(online); |
89 | 0 | } |
90 | | |
91 | 1 | const std::size_t quota = cgroup_v2_cpu_limit(); |
92 | 1 | const std::size_t legacy_quota = |
93 | 1 | quota == 0 ? cgroup_v1_cpu_limit() : quota; |
94 | 1 | if (legacy_quota > 0) count = count == 0 ? legacy_quota |
95 | 0 | : std::min(count, legacy_quota); |
96 | 1 | return count; |
97 | 1 | } |
98 | | #endif |
99 | | |
100 | 1 | std::size_t detect_available_cpu_count() { |
101 | 1 | std::size_t detected = 0; |
102 | | #if defined(__APPLE__) |
103 | | detected = sysctl_cpu_count("hw.activecpu"); |
104 | | if (detected == 0) detected = sysctl_cpu_count("hw.logicalcpu"); |
105 | | if (detected == 0) detected = sysctl_cpu_count("hw.ncpu"); |
106 | | #elif defined(__linux__) |
107 | | detected = linux_cpu_count(); |
108 | 1 | #endif |
109 | 1 | return detail::resolve_cpu_count(detected, |
110 | 1 | static_cast<std::size_t>( |
111 | 1 | std::thread::hardware_concurrency())); |
112 | 1 | } |
113 | | |
114 | | } // namespace |
115 | | |
116 | | namespace detail { |
117 | | |
118 | | std::size_t resolve_cpu_count(std::size_t detected, |
119 | 1 | std::size_t fallback) noexcept { |
120 | 1 | if (detected > 0) return detected; |
121 | 0 | return fallback > 0 ? fallback : 1; |
122 | 1 | } |
123 | | |
124 | | } // namespace detail |
125 | | |
126 | 5 | std::size_t available_cpu_count() { |
127 | 5 | static const std::size_t count = detect_available_cpu_count(); |
128 | 5 | return count; |
129 | 5 | } |
130 | | |
131 | 4 | std::size_t execution_thread_count() noexcept { |
132 | 4 | return configured_thread_count.load(std::memory_order_relaxed); |
133 | 4 | } |
134 | | |
135 | 4 | void set_execution_thread_count(std::size_t count) { |
136 | 4 | if (count == 0 || count > available_cpu_count()) { |
137 | 2 | throw std::invalid_argument( |
138 | 2 | "Execution thread count must be between 1 and available_cpu_count()"); |
139 | 2 | } |
140 | 2 | configured_thread_count.store(count, std::memory_order_relaxed); |
141 | 2 | } |
142 | | |
143 | 1 | void set_execution_thread_count_auto() { |
144 | 1 | configured_thread_count.store(available_cpu_count(), |
145 | 1 | std::memory_order_relaxed); |
146 | 1 | } |
147 | | |
148 | | } // namespace labneura |