| 226 | } |
| 227 | |
| 228 | static void readers() { |
| 229 | const fs::path root = fs::temp_directory_path() / ("card_node_test_" + std::to_string((long long) std::time(nullptr))); |
| 230 | fs::remove_all(root); |
| 231 | const fs::path node = root / "devices/system/node"; |
| 232 | write(node / "node0/cpulist", "0-1,4-5\n"); |
| 233 | write(node / "node1/cpulist", "2-3,6-7\n"); |
| 234 | write(node / "node2/cpulist", "\n"); // a memory-only node (CXL): not counted |
| 235 | write(node / "online", "0-2\n"); |
| 236 | fs::create_directories(node / "nodeX"); |
| 237 | write(node / "node1/meminfo", "Node 1 MemTotal: 264000000 kB\nNode 1 MemFree: 112233 kB\nNode 1 MemUsed: 1 kB\n"); |
| 238 | write(node / "node0/meminfo", "Node 0 MemTotal: 5 kB\n"); // no MemFree line |
| 239 | for (int c = 0; c < 8; ++c) { |
| 240 | const fs::path t = root / ("devices/system/cpu/cpu" + std::to_string(c)) / "topology"; |
| 241 | write(t / "physical_package_id", std::to_string((c / 2) % 2) + "\n"); |
| 242 | write(t / "core_id", std::to_string(c % 2) + "\n"); |
| 243 | } |
| 244 | #if !defined(_WIN32) |
| 245 | // (a PCI address has colons, which no Windows file name may have: create_directories throws there. The engine reads sysfs on Linux only.) |
| 246 | write(root / "bus/pci/devices/0000:e1:00.0/numa_node", "1\n"); |
| 247 | write(root / "bus/pci/devices/0000:21:00.0/numa_node", "-1\n"); |
| 248 | write(root / "bus/pci/devices/0000:22:00.0/numa_node", "junk\n"); |
| 249 | #endif |
| 250 | |
| 251 | #if defined(__linux__) |
| 252 | const auto nodes = cpu::card_node_node_cpus(root.string()); |
| 253 | expect(nodes.size() == 2, "fake sysfs: 2 nodes with CPUs (memory-only node2 and nodeX left out)"); |
| 254 | expect(nodes.count(0) && nodes.at(0) == std::vector<int>({0, 1, 4, 5}), "fake sysfs: node 0's CPUs"); |
| 255 | expect(nodes.count(1) && nodes.at(1) == std::vector<int>({2, 3, 6, 7}), "fake sysfs: node 1's CPUs"); |
| 256 | const auto desc = cpu::card_node_describe(root.string(), {0, 3, 6, 9}, nodes); |
| 257 | expect(desc.size() == 4 && desc[0].node == 0 && desc[1].node == 1 && desc[2].node == 1 && desc[3].node == -1, "describe: nodes"); |
| 258 | expect(desc[1].pkg == 1 && desc[1].core == 1 && desc[3].pkg == -1 && desc[3].core == -1, "describe: package and core, missing CPU -1"); |
| 259 | expect(cpu::card_node_node_cpus((root / "nothing").string()).empty(), "no node folder: no nodes"); |
| 260 | #endif |
| 261 | #if !defined(_WIN32) |
| 262 | expect(cpu::card_node_pci_node(root.string(), "0000:e1:00.0") == 1, "pci numa_node 1"); |
| 263 | expect(cpu::card_node_pci_node(root.string(), "0000:21:00.0") == -1, "pci numa_node -1"); |
| 264 | expect(cpu::card_node_pci_node(root.string(), "0000:22:00.0") == -1, "pci numa_node junk: -1"); |
| 265 | expect(cpu::card_node_pci_node(root.string(), "0000:99:00.0") == -1, "pci device missing: -1"); |
| 266 | #endif |
| 267 | expect(cpu::card_node_free_bytes(root.string(), 1) == 112233ll * 1024, "node 1 MemFree"); |
| 268 | expect(cpu::card_node_free_bytes(root.string(), 0) == -1, "node 0 without MemFree: -1"); |
| 269 | expect(cpu::card_node_free_bytes(root.string(), 5) == -1, "node 5 missing: -1"); |
| 270 | expect(cpu::card_node_free_bytes(root.string(), -1) == -1, "node -1: -1"); |
| 271 | expect(cpu::card_node_bdf(0, 0xE1, 0) == "0000:e1:00.0", "bdf 0000:e1:00.0"); |
| 272 | expect(cpu::card_node_bdf(0x10, 0x3b, 0x1f, 1) == "0010:3b:1f.1", "bdf 0010:3b:1f.1"); |
| 273 | fs::remove_all(root); |
| 274 | |
| 275 | std::vector<int> v; |
| 276 | expect(cpu::card_node_parse_cpulist("0-31,64-95\n", v) && v.size() == 64 && v.front() == 0 && v.back() == 95, "cpulist 0-31,64-95"); |
| 277 | expect(cpu::card_node_parse_cpulist(" 3 ", v) && v == std::vector<int>({3}), "cpulist 3"); |
| 278 | expect(cpu::card_node_parse_cpulist("\n", v) && v.empty(), "cpulist empty"); |
| 279 | expect(!cpu::card_node_parse_cpulist("3-1", v) && v.empty(), "cpulist reversed: refused"); |
| 280 | expect(!cpu::card_node_parse_cpulist("1,,2", v), "cpulist empty item: refused"); |
| 281 | expect(!cpu::card_node_parse_cpulist("a", v), "cpulist letters: refused"); |
| 282 | std::vector<int> mems; |
| 283 | expect(cpu::card_node_mems_allowed("Name:\tstrata\nMems_allowed:\t00000003\nMems_allowed_list:\t0-1\nVoluntary: 1\n", mems) && |
| 284 | mems == std::vector<int>({0, 1}), "Mems_allowed_list 0-1"); |
| 285 | expect(cpu::card_node_mems_allowed("Mems_allowed_list:\t1\n", mems) && mems == std::vector<int>({1}), "Mems_allowed_list 1"); |
no test coverage detected