-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSysfs.cc
82 lines (68 loc) · 1.69 KB
/
Sysfs.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2018 Intel Corporation
*
* Authors: Fengguang Wu <fengguang.wu@intel.com>
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <numa.h>
#include "Sysfs.h"
#include <iostream>
#include <fstream>
bool Sysfs::file_exists(char path[])
{
struct stat buf;
return stat(path, &buf) == 0;
}
int Sysfs::read_int(std::string dir, std::string name)
{
std::ifstream infile(dir + '/' + name);
int val;
infile >> val;
return val;
}
void Sysfs::load_hugetlb()
{
int max_node = numa_max_node();
char path[100];
hugetlb_map.resize(max_node + 1);
for (int i = 0; i < max_node; ++i)
{
snprintf(path, sizeof(path),
"/sys/devices/system/node/node%d/hugepages/hugepages-2048kB", i);
if (!file_exists(path))
continue;
#define read_hugetlb(name) hugetlb_map[i][name] = read_int(path, name)
read_hugetlb("nr_hugepages");
read_hugetlb("free_hugepages");
read_hugetlb("surplus_hugepages");
#undef read_hugetlb
}
load_global_hugetlb();
}
void Sysfs::load_global_hugetlb()
{
const char path[] = "/sys/kernel/mm/hugepages/hugepages-2048kB";
#define read_hugetlb(name) global_hugetlb_map[name] = read_int(path, name)
read_hugetlb("nr_hugepages");
read_hugetlb("free_hugepages");
read_hugetlb("surplus_hugepages");
read_hugetlb("nr_overcommit_hugepages");
read_hugetlb("resv_hugepages");
#undef read_hugetlb
}
int Sysfs::hugetlb(int nid, std::string name)
{
if (hugetlb_map.empty())
load_hugetlb();
return hugetlb_map.at(nid).at(name);
}
int Sysfs::hugetlb(std::string name)
{
if (global_hugetlb_map.empty())
load_global_hugetlb();
return global_hugetlb_map.at(name);
}