forked from intel/memory-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVMAInspect.cc
151 lines (123 loc) · 3.44 KB
/
VMAInspect.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
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2018 Intel Corporation
*
* Authors: Fengguang Wu <fengguang.wu@intel.com>
* Yao Yuan <yuan.yao@intel.com>
* Huang Ying <ying.huang@intel.com>
* Liu Jingqi <jingqi.liu@intel.com>
*/
#include <sys/user.h>
#include "lib/debug.h"
#include "lib/stats.h"
#include "ProcMaps.h"
#include "Formatter.h"
#include "VMAInspect.h"
#include "Numa.h"
void VMAInspect::fill_addrs(std::vector<void *>& addrs, unsigned long start)
{
void **p = &addrs[0];
void **pp = &addrs[addrs.size()-1];
for (; p <= pp; ++p) {
*p = (void *)start;
start += PAGE_SIZE;
}
}
void VMAInspect::dump_node_percent(Formatter* fmt, int slot)
{
auto status_count = locator.get_status_count();
size_t dram_nodes_size = 0;
if (!fmt)
return;
if (numa_collection) {
for (auto node: numa_collection->get_dram_nodes())
dram_nodes_size += (size_t)status_count[node->id()];
}
int pct = percent(dram_nodes_size, locator.get_status().size());
fmt->print("%2d %3d%% |", slot, pct);
for (int i = 0; i < pct; ++i)
fmt->print("#");
fmt->print("\n");
}
int VMAInspect::dump_vma_nodes(Formatter* fmt, int is_split_vma,
proc_maps_entry& vma, MovePagesStatusCount& status_sum)
{
unsigned long nr_pages;
int err = 0;
nr_pages = (vma.end - vma.start) >> PAGE_SHIFT;
if (!nr_pages)
return 0;
unsigned long total_mb = (vma.end - vma.start) >> 20;
int nr_slots = 1;
unsigned long slot_pages = nr_pages;
if (total_mb >= (1<<10) && is_split_vma) {
if (fmt) {
fmt->print("\nDRAM page distribution across 10 VMA slots: ");
fmt->print("(pid=%d vma_mb=%'lu)\n", pid, total_mb);
}
nr_slots = 10;
slot_pages = nr_pages / nr_slots;
}
locator.set_pid(pid);
std::vector<void *> addrs;
addrs.resize(slot_pages);
for (int i = 0; i < nr_slots; ++i) {
fill_addrs(addrs, vma.start + i * addrs.size() * PAGE_SIZE);
locator.clear_status_count();
err = locator.move_pages(addrs, true);
if (err) {
perror("move_pages");
return err;
}
locator.calc_status_count();
// only show when it's bigger than 1G
if (nr_slots != 1)
dump_node_percent(fmt, i);
locator.add_status_count_to(status_sum);
}
return err;
}
int VMAInspect::dump_task_nodes(pid_t i, Formatter* m)
{
ProcMaps proc_maps;
int err = 0;
MovePagesStatusCount status_sum;
auto maps = proc_maps.load(i);
pid = i;
for (auto &vma: maps) {
err = dump_vma_nodes(m, true, vma, status_sum);
if (err)
break;
}
if (!err) {
m->print("\nAnonymous page distribution across NUMA nodes in pid %d:\n", pid);
locator.set_pid(pid);
locator.show_status_count(m, status_sum);
}
return err;
}
int VMAInspect::calc_memory_state(pid_t i,
unsigned long &total_kb,
unsigned long &total_dram_kb,
unsigned long &total_pmem_kb)
{
ProcMaps proc_maps;
int err = 0;
MovePagesStatusCount status_sum;
auto maps = proc_maps.load(i);
pid = i;
for (auto &vma: maps) {
err = dump_vma_nodes(NULL, false, vma, status_sum);
if (err)
break;
}
if (!err) {
locator.set_pid(pid);
locator.calc_memory_state(status_sum,
total_kb,
total_dram_kb,
total_pmem_kb);
}
return err;
}