This repository was archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathEPTScan.cc
More file actions
359 lines (291 loc) · 8.56 KB
/
EPTScan.cc
File metadata and controls
359 lines (291 loc) · 8.56 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2018 Intel Corporation
*
* Authors: Fengguang Wu <fengguang.wu@intel.com>
*/
#include <unistd.h>
#include "EPTScan.h"
#include "lib/debug.h"
#include "lib/stats.h"
extern Option option;
bool EPTScan::should_stop()
{
if (!option.dram_percent)
return false;
// page_refs.get_top_bytes() is 0 when nr_walks == 1
if (nr_walks <= 2)
return false;
unsigned long young_bytes = 0;
unsigned long pmem_young_bytes = 0;
unsigned long top_bytes = 0;
unsigned long all_bytes = 0;
gather_walk_stats(young_bytes, pmem_young_bytes,
top_bytes, all_bytes);
return 2 * 100 * top_bytes < option.dram_percent * all_bytes;
}
void EPTScan::gather_walk_stats(unsigned long& young_bytes,
unsigned long& pmem_young_bytes,
unsigned long& top_bytes,
unsigned long& all_bytes)
{
unsigned long y = 0;
unsigned long py = 0;
unsigned long t = 0;
unsigned long a = 0;
if (io_error) {
printdd("gather_walk_stats: skip pid %d\n", pid);
return;
}
for (auto& prc: pagetype_refs) {
y += prc.page_refs.get_young_bytes();
py += prc.page_refs.get_young_bytes(AddrSequence::LOC_PMEM);
t += prc.page_refs.get_top_bytes();
a += prc.page_refs.size() << prc.page_refs.get_pageshift();
}
printdd("pid=%d %lx-%lx top_bytes=%'lu young_bytes=%'lu all_bytes=%'lu\n",
pid, va_start, va_end, t, y, a);
young_bytes += y;
pmem_young_bytes += py;
top_bytes += t;
all_bytes += a;
}
int EPTScan::walk_multi(int nr, float interval)
{
int err;
const int max_walks = 30;
bool auto_stop = false;
auto maps = proc_maps.load(pid);
if (maps.empty()) {
io_error = -ENOENT;
return -ENOENT;
}
if (nr > 0xff) {
printf("limiting nr_walks to uint8_t size\n");
nr = 0xff;
} else if (nr == 0) {
auto_stop = true;
nr = max_walks;
}
prepare_walks(nr);
for (int i = 0; i < nr; ++i)
{
err = walk();
if (err < 0)
return err;
if (auto_stop && should_stop())
break;
usleep(interval * 1000000);
}
return 0;
}
void EPTScan::prepare_walks(int max_walks)
{
nr_walks = 0; // for use by count_refs()
for (int type = 0; type <= MAX_ACCESSED; ++type) {
auto& prc = pagetype_refs[type];
prc.page_refs.clear();
prc.page_refs.set_pageshift(pagetype_shift[type]);
for (auto& histogram: prc.histogram_2d)
histogram.clear();
}
}
histogram_2d_type EPTScan::sys_refs_count[MAX_ACCESSED + 1];
void EPTScan::reset_sys_refs_count(int nr_walks)
{
for (auto& src: sys_refs_count) {
reset_one_ref_count(src, nr_walks + 1);
}
}
void EPTScan::count_refs_one(ProcIdleRefs& prc)
{
int rc;
unsigned long addr;
uint8_t ref_count;
int8_t nid;
int loc_index;
histogram_2d_type& refs_count = prc.histogram_2d;
AddrSequence& page_refs = prc.page_refs;
reset_one_ref_count(refs_count, nr_walks + 1);
// save page NID + refs information into refs_count
rc = page_refs.get_first(addr, ref_count, nid);
while (!rc) {
// In the rare case of changed VMAs, their start/end boundary may not align
// with the underlying huge page size. If the same huge page is covered by
// 2 VMAs, there will be duplicate accounting for the same page. The easy
// workaround is to enforce min() check here.
if (nid >= 0)
refs_count[nid][std::min(ref_count, (uint8_t)nr_walks)] += 1;
else
refs_count[REF_LOC_UNKNOWN][std::min(ref_count, (uint8_t)nr_walks)] += 1;
rc = page_refs.get_next(addr, ref_count, nid);
}
// save DRAM/PMEM/ALL refs count from page NID + refs information
for (int i = 0; i <= MAX_NID; ++i) {
if (!numa_collection->is_valid_nid(i))
continue;
loc_index = (numa_collection->get_node(i)->is_pmem()) ?
REF_LOC_PMEM : REF_LOC_DRAM;
for (int j = 0; j <= nr_walks; ++j) {
refs_count[loc_index][j] += refs_count[i][j];
refs_count[REF_LOC_ALL][j] += refs_count[i][j];
}
}
// save UNKNOWN count into ALL
for (int k = 0; k <= nr_walks; ++k) {
refs_count[REF_LOC_ALL][k] += refs_count[REF_LOC_UNKNOWN][k];
}
return;
}
void EPTScan::reset_one_ref_count(histogram_2d_type& ref_count_obj, int node_size)
{
for (auto& node_ref: ref_count_obj) {
node_ref.clear();
node_ref.resize(node_size, 0);
}
}
void EPTScan::count_refs()
{
if (io_error) {
printd("count_refs: skip %d\n", pid);
return;
}
for (int type = 0; type <= MAX_ACCESSED; ++type) {
auto& src = sys_refs_count[type];
auto& prc = pagetype_refs[type];
count_refs_one(prc);
if ((unsigned long)nr_walks + 1 != prc.histogram_2d[REF_LOC_ALL].size())
fprintf(stderr, "ERROR: nr_walks mismatch: %d %lu\n",
nr_walks, prc.histogram_2d[REF_LOC_ALL].size());
for (int j = 0; j < REF_LOC_MAX; ++j) {
for (int i = 0; i <= nr_walks; ++i) {
src[j][i] += prc.histogram_2d[j][i];
}
}
}
}
int EPTScan::save_counts(std::string filename)
{
int err = 0;
FILE *file;
if (filename.empty())
file = stdout;
else
file = fopen(filename.c_str(), "w");
if (!file) {
std::cerr << "open file " << filename << "failed" << std::endl;
perror(filename.c_str());
return -1;
}
fprintf(file, "Scan result: memory (KB) group by reference count\n");
fprintf(file, "%4s %15s %15s %15s\n",
"refs",
"4k_page",
"2M_page",
"1G_page");
fprintf(file, "======================================================\n");
unsigned long sum_kb[IDLE_PAGE_TYPE_MAX] = {};
int nr = sys_refs_count[PTE_ACCESSED][REF_LOC_ALL].size();
for (int i = nr - 1; i >= 0; i--) {
fprintf(file, "%4d", i);
for (const int& type: {PTE_ACCESSED, PMD_ACCESSED, PUD_PRESENT}) {
unsigned long pages = sys_refs_count[type][REF_LOC_ALL][i];
unsigned long kb = pages * (pagetype_size[type] >> 10);
fprintf(file, " %'15lu", kb);
sum_kb[type] += kb;
}
fprintf(file, "\n");
}
fprintf(file, "SUM ");
unsigned long total_kb = 0;
for (const int& type: {PTE_ACCESSED, PMD_ACCESSED, PUD_PRESENT}) {
unsigned long kb = sum_kb[type];
fprintf(file, " %'15lu", kb);
total_kb += kb;
}
fprintf(file, "\nALL %'15lu\n", total_kb);
if (file != stdout)
fclose(file);
return err;
}
int EPTScan::get_memory_type_range(void** addrs, unsigned long count,
AddrSequence& addrobj)
{
std::vector<int> addr_locate;
MovePages locator;
int ret;
int nid;
int location;
if (0 == count)
return 0;
locator.set_pid(pid);
ret = locator.move_pages(addrs, addr_locate, count, true);
if (ret < 0) {
fprintf(stderr, "get_memory_type_range failed. return: %d\n", ret);
return ret;
}
for (unsigned long i = 0; i < count; ++i) {
nid = addr_locate[i];
if (nid < -128) {
fprintf(stderr,"limiting negative nid = %d from move_pages() to int8_t size\n", nid);
nid = -128;
}
if (nid > MAX_NID) {
fprintf(stderr,"nid = %d from move_pages() exceeded MAX_NID = %d\n", nid, MAX_NID);
exit(-1);
}
if (nid >= 0) {
NumaNode* node = numa_collection->get_node(nid);
if (node) {
location = node->is_pmem() ?
AddrSequence::LOC_PMEM : AddrSequence::LOC_DRAM;
goto update_nid;
}
}
location = AddrSequence::LOC_UNKNOW;
update_nid:
addrobj.update_nodeid((unsigned long)addrs[i], (int8_t)nid, (int8_t)location);
}
return ret;
}
int EPTScan::get_memory_type()
{
int rc;
unsigned long addr;
uint8_t unused_count;
int8_t nid;
std::vector<void*> addr_set;
for (auto& each : pagetype_refs) {
AddrSequence& page_refs = each.page_refs;
addr_set.clear();
page_refs.prepare_update();
rc = page_refs.get_first(addr, unused_count, nid);
while(!rc) {
addr_set.push_back((void*)addr);
if (addr_set.size() >= 1024) {
get_memory_type_range(&addr_set[0], addr_set.size(), page_refs);
addr_set.clear();
}
rc = page_refs.get_next(addr, unused_count, nid);
}
// handle the remain unaligned part
if (addr_set.size()) {
get_memory_type_range(&addr_set[0], addr_set.size(), page_refs);
}
}
return 0;
}
unsigned long EPTScan::get_total_memory_page_count(ProcIdlePageType type,
ref_location location)
{
unsigned long sum = 0;
if (location >= REF_LOC_MAX)
return 0;
histogram_type& page_stats
= get_sys_refs_count(type)[location];
for (auto& item : page_stats) {
sum += item;
}
return sum;
}