-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProcVmstat.cc
180 lines (144 loc) · 3.75 KB
/
ProcVmstat.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2018 Intel Corporation
*
* Authors: Fengguang Wu <fengguang.wu@intel.com>
* Yao Yuan <yuan.yao@intel.com>
*/
#include <numa.h>
#include <stdio.h>
#include <sys/user.h>
#include <string>
#include <vector>
#include "lib/stats.h"
#include "ProcVmstat.h"
#include "Numa.h"
int ProcVmstat::load_vmstat()
{
proc_vmstat = __load_vmstat("/proc/vmstat");
return proc_vmstat.empty();
}
int ProcVmstat::load_numa_vmstat()
{
char path[50];
int rc = 0;
int max_node = numa_max_node();
numa_vmstat.clear();
numa_vmstat.resize(max_node + 1);
for (int i = 0; i <= max_node; ++i) {
snprintf(path, sizeof(path), "/sys/devices/system/node/node%d/vmstat", i);
numa_vmstat[i] = __load_vmstat(path);
if (numa_vmstat[i].empty())
++rc;
}
return rc;
}
vmstat_map ProcVmstat::__load_vmstat(const char *path)
{
vmstat_map map;
char line[80];
char key[50];
unsigned long val;
FILE *f = fopen(path, "r");
if (!f) {
perror(path);
goto out;
}
while (fgets(line, sizeof(line), f))
{
int ret = sscanf(line, "%49s %lu\n", key, &val);
if (ret < 2) {
if (line[0] != ' ') // ignore known kernel bug
fprintf(stderr, "parse failed: %d %s\n%s", ret, path, line);
continue;
}
map[key] = val;
}
fclose(f);
out:
return map;
}
unsigned long ProcVmstat::vmstat(std::string name)
{
if (proc_vmstat.empty())
load_vmstat();
return proc_vmstat.at(name);
}
unsigned long ProcVmstat::vmstat(int nid, std::string name)
{
if (proc_vmstat.empty())
load_numa_vmstat();
return numa_vmstat.at(nid).at(name);
}
unsigned long ProcVmstat::anon_capacity()
{
unsigned long sum = vmstat("nr_free_pages");
sum += vmstat("nr_inactive_anon");
sum += vmstat("nr_active_anon");
return sum;
}
unsigned long ProcVmstat::anon_capacity(int nid)
{
unsigned long sum = vmstat(nid, "nr_free_pages");
sum += vmstat(nid, "nr_inactive_anon");
sum += vmstat(nid, "nr_active_anon");
return sum;
}
unsigned long ProcVmstat::vmstat(std::vector<int>& nid)
{
unsigned long sum = 0;
for (auto& i : nid)
sum += anon_capacity(i);
return sum;
}
void ProcVmstat::show_numa_stats(NumaNodeCollection* numa_collection)
{
unsigned long pmem_anon_kb = 0;
unsigned long dram_anon_kb = 0;
NumaNode* numa_obj;
load_vmstat();
load_numa_vmstat();
const auto& numa_vmstat = get_numa_vmstat();
unsigned long total_anon_kb = vmstat("nr_inactive_anon") +
vmstat("nr_active_anon") +
vmstat("nr_isolated_anon");
total_anon_kb *= PAGE_SIZE >> 10;
printf("\nAnonymous page distribution across NUMA nodes:\n");
printf("%'15lu anon total\n", total_anon_kb);
int nid = 0;
for (auto& map: numa_vmstat) {
unsigned long anon_kb = map.at("nr_inactive_anon") +
map.at("nr_active_anon") +
map.at("nr_isolated_anon");
anon_kb *= PAGE_SIZE >> 10;
printf("%'15lu %2d%% anon node %d\n", anon_kb, percent(anon_kb, total_anon_kb), nid);
if (!numa_collection) {
++nid;
continue;
}
numa_obj = numa_collection->get_node(nid);
if (!numa_obj) {
++nid;
continue;
}
switch (numa_obj->type()) {
case NUMA_NODE_DRAM:
dram_anon_kb += anon_kb;
break;
case NUMA_NODE_PMEM:
pmem_anon_kb += anon_kb;
break;
default:
//for unknown type do nothing
break;
}
++nid;
}
if (numa_collection) {
printf("Anon DRAM nodes size: %'15lu %2d%%\n",
dram_anon_kb, percent(dram_anon_kb, total_anon_kb));
printf("Anon PMEM nodes size: %'15lu %2d%%\n",
pmem_anon_kb, percent(pmem_anon_kb, total_anon_kb));
}
}