VeloGraphX
High-performance dynamic graph analytics in C++20
Loading...
Searching...
No Matches
numa.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <filesystem>
5#include <fstream>
6#include <string>
7#include <thread>
8#include <vector>
9
10namespace velographx {
11
13
15 std::size_t id{0};
16 std::vector<std::size_t> cpus;
17};
18
19struct NumaInfo {
20 std::size_t hardware_threads{std::thread::hardware_concurrency()};
21 std::size_t nodes{1};
22 bool native_support{false};
23 std::vector<NumaNodeInfo> topology;
24};
25
26inline std::vector<std::size_t> parse_cpu_list(const std::string& text) {
27 std::vector<std::size_t> cpus;
28 std::size_t pos = 0;
29 while (pos < text.size()) {
30 const auto comma = text.find(',', pos);
31 const auto token = text.substr(pos, comma == std::string::npos ? std::string::npos : comma - pos);
32 const auto dash = token.find('-');
33 if (!token.empty()) {
34 if (dash == std::string::npos) {
35 try { cpus.push_back(static_cast<std::size_t>(std::stoull(token))); } catch (...) {}
36 } else {
37 try {
38 const auto first = static_cast<std::size_t>(std::stoull(token.substr(0, dash)));
39 const auto last = static_cast<std::size_t>(std::stoull(token.substr(dash + 1)));
40 if (last >= first) for (std::size_t cpu = first; cpu <= last; ++cpu) cpus.push_back(cpu);
41 } catch (...) {}
42 }
43 }
44 if (comma == std::string::npos) break;
45 pos = comma + 1;
46 }
47 return cpus;
48}
49
51 NumaInfo info;
52#if defined(__linux__)
53 namespace fs = std::filesystem;
54 const fs::path root{"/sys/devices/system/node"};
55 std::error_code ec;
56 if (fs::exists(root, ec) && !ec) {
57 for (const auto& entry : fs::directory_iterator(root, ec)) {
58 if (ec) break;
59 const auto name = entry.path().filename().string();
60 if (name.rfind("node", 0) != 0 || name.size() <= 4) continue;
61 try {
62 const auto id = static_cast<std::size_t>(std::stoull(name.substr(4)));
63 std::ifstream in(entry.path() / "cpulist");
64 if (!in) continue;
65 std::string cpulist;
66 std::getline(in, cpulist);
67 info.topology.push_back({id, parse_cpu_list(cpulist)});
68 } catch (...) {}
69 }
70 if (!info.topology.empty()) {
71 info.nodes = info.topology.size();
72 info.native_support = true;
73 }
74 }
75#endif
76 return info;
77}
78
79inline std::string numa_mode_name(NumaMode mode) {
80 return mode == NumaMode::off ? "off" : (mode == NumaMode::interleave ? "interleave" : "auto");
81}
82
83} // namespace velographx
std::vector< std::size_t > parse_cpu_list(const std::string &text)
Definition numa.hpp:26
NumaInfo detect_numa()
Definition numa.hpp:50
std::string numa_mode_name(NumaMode mode)
Definition numa.hpp:79
std::size_t nodes
Definition numa.hpp:21
std::size_t hardware_threads
Definition numa.hpp:20
std::vector< NumaNodeInfo > topology
Definition numa.hpp:23
std::vector< std::size_t > cpus
Definition numa.hpp:16