-
Notifications
You must be signed in to change notification settings - Fork 28
/
wss-v1.c
282 lines (251 loc) · 7.81 KB
/
wss-v1.c
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
/*
* wss-v1.c Estimate the working set size (WSS) for a process on Linux.
* Version 1: suited for small processes.
*
* This is a proof of concept that uses idle page tracking from Linux 4.3+,
* for a page-based WSS estimation. This version walks page structures one by
* one, and is suited for small processes. See wss-v2.c which snapshots page
* data, and can be over 50x faster for large processes, although slower for
* small processes. There is also wss.pl, which can be over 500x faster and
* works on older Linux, however, uses the referenced page flag instead and has
* its own caveats. These tools can be found here:
*
* http://www.brendangregg.com/wss.pl
*
* Currently written for x86_64 and default page size only. Early version:
* probably has bugs.
*
* COMPILE: gcc -o wss-v1 wss-v1.c
*
* REQUIREMENTS: Linux 4.3+
*
* USAGE: wss PID duration(s)
*
* COLUMNS:
* - Est(s): Estimated WSS measurement duration: this accounts for delays
* with setting and reading pagemap data, which inflates the
* intended sleep duration.
* - Ref(MB): Referenced (Mbytes) during the specified duration.
* This is the working set size metric.
*
* WARNING: This tool sets and reads process page flags, which for large
* processes (> 100 Gbytes) can take several minutes (use wss-v2 for those
* instead). During that time, this tool consumes one CPU, and the application
* may experience slightly higher latency (eg, 5%). Consider these overheads.
* Also, this is activating some new kernel code added in Linux 4.3 that you
* may have never executed before. As is the case for any such code, there is
* the risk of undiscovered kernel panics (I have no specific reason to worry,
* just being paranoid). Test in a lab environment for your kernel versions,
* and consider this experimental: use at your own risk.
*
* Copyright 2018 Netflix, Inc.
* Licensed under the Apache License, Version 2.0 (the "License")
*
* 13-Jan-2018 Brendan Gregg Created this.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
// see Documentation/vm/pagemap.txt:
#define PFN_MASK (~(0x1ffLLU << 55))
#define PATHSIZE 128
#define LINESIZE 256
#define PAGEMAP_CHUNK_SIZE 8
// from mm/page_idle.c:
#ifndef BITMAP_CHUNK_SIZE
#define BITMAP_CHUNK_SIZE 8
#endif
#ifndef PAGE_OFFSET
#define PAGE_OFFSET 0xffff880000000000LLU
#endif
enum {
READIDLE = 0,
SETIDLE
};
// globals
int g_debug = 0; // 1 == some, 2 == verbose
int g_activepages = 0;
int g_walkedpages = 0;
int mapidle(pid_t pid, unsigned long long mapstart, unsigned long long mapend, int action)
{
char pagepath[PATHSIZE];
char *idlepath = "/sys/kernel/mm/page_idle/bitmap";
int idlefd, pagefd;
char *line;
int pagesize = getpagesize();
unsigned long long p, pagemapp, pfn, idlemapp, idlebits;
int flags;
int err = 0;
// open pagemap for virtual to PFN translation
if (sprintf(pagepath, "/proc/%d/pagemap", pid) < 0) {
printf("Can't allocate memory. Exiting.");
exit(1);
}
if ((pagefd = open(pagepath, O_RDONLY)) < 0) {
perror("Can't read pagemap file");
return 2;
}
// open idlemap for WSS estimation
flags = O_RDONLY;
if (action == SETIDLE)
flags = O_WRONLY;
if ((idlefd = open(idlepath, flags)) < 0) {
perror("Can't read idlemap file");
close(pagefd);
return 2;
}
// walk pagemap to get PFN, then operate on PFN from idlemap
for (p = mapstart; p < mapend; p += pagesize) {
pagemapp = PAGEMAP_CHUNK_SIZE * p / pagesize;
/*
* The following involves a lot of syscalls for setting and
* reading bits. This is why this program is slow. This should
* be optimized to read by chunks. Or to use mmap, however, I
* don't think the kernel files involved support an mmap
* interface. Perhaps a later version of Linux will provide a
* /proc/PID/clear_idle interface with an entry in
* /proc/PID/smaps, which would make this much faster.
*/
// convert virtual address p to physical PFN
if (lseek(pagefd, pagemapp, SEEK_SET) < 0) {
printf("Can't seek pagemap file\n");
goto out;
}
if (read(pagefd, &pfn, sizeof (pfn)) < 0) {
printf("Can't read pagemap file\n");
goto out;
}
pfn = pfn & PFN_MASK;
if (pfn == 0)
continue;
// locate idle map byte
idlemapp = (pfn / 64) * BITMAP_CHUNK_SIZE;
if (lseek(idlefd, idlemapp, SEEK_SET) < 0) {
printf("Can't seek idlemap file\n");
goto out;
}
if (g_debug > 1) {
printf("%s: p %llx pfn %llx idlebits %llx\n",
action == READIDLE ? "R" : "W", p, pfn, idlebits);
}
/*
* At the time of writing, I could find no example code that
* used idle page tracking. This is based on the description in
* Documentation/vm/idle_page_tracking.txt.
*/
// read idle bit
if (action == READIDLE) {
if (read(idlefd, &idlebits, sizeof (idlebits)) <= 0) {
perror("Can't read idlemap file");
goto out;
}
if (!(idlebits & (1ULL << (pfn % 64)))) {
// XXX: optimize
g_activepages++;
}
g_walkedpages++;
// set idle bit
} else /* SETIDLE */ {
idlebits = ~0ULL;
if (write(idlefd, &idlebits, sizeof (idlebits)) <= 0) {
perror("Can't write idlemap file");
goto out;
}
}
}
out:
close(pagefd);
close(idlefd);
return err;
}
int walkmaps(pid_t pid, int action)
{
FILE *mapsfile;
char mapspath[PATHSIZE];
char line[LINESIZE];
unsigned long long mapstart, mapend;
// read virtual mappings
if (sprintf(mapspath, "/proc/%d/maps", pid) < 0) {
printf("Can't allocate memory. Exiting.");
exit(1);
}
if ((mapsfile = fopen(mapspath, "r")) == NULL) {
perror("Can't read maps file");
exit(2);
}
while (fgets(line, sizeof (line), mapsfile) != NULL) {
sscanf(line, "%llx-%llx", &mapstart, &mapend);
if (g_debug)
printf("MAP %llx-%llx\n", mapstart, mapend);
if (mapstart > PAGE_OFFSET)
continue; // page idle tracking is user mem only
if (mapidle(pid, mapstart, mapend, action)) {
printf("Error setting map %llx-%llx. Exiting.\n",
mapstart, mapend);
}
}
fclose(mapsfile);
return 0;
}
int main(int argc, char *argv[])
{
pid_t pid;
double duration, mbytes;
static struct timeval ts1, ts2, ts3, ts4;
unsigned long long set_us, read_us, dur_us, slp_us, est_us;
// options
if (argc < 3) {
printf("USAGE: wss PID duration(s)\n");
exit(0);
}
pid = atoi(argv[1]);
duration = atof(argv[2]);
if (duration < 0.01) {
printf("Interval too short. Exiting.\n");
return 1;
}
printf("Watching PID %d page references during %.2f seconds...\n",
pid, duration);
// set idle flags
gettimeofday(&ts1, NULL);
walkmaps(pid, SETIDLE);
// sleep
gettimeofday(&ts2, NULL);
usleep((int)(duration * 1000000));
gettimeofday(&ts3, NULL);
// read idle flags
walkmaps(pid, READIDLE);
gettimeofday(&ts4, NULL);
// calculate times
set_us = 1000000 * (ts2.tv_sec - ts1.tv_sec) +
(ts2.tv_usec - ts1.tv_usec);
slp_us = 1000000 * (ts3.tv_sec - ts2.tv_sec) +
(ts3.tv_usec - ts2.tv_usec);
read_us = 1000000 * (ts4.tv_sec - ts3.tv_sec) +
(ts4.tv_usec - ts3.tv_usec);
dur_us = 1000000 * (ts4.tv_sec - ts1.tv_sec) +
(ts4.tv_usec - ts1.tv_usec);
est_us = dur_us - (set_us / 2) - (read_us / 2);
if (g_debug) {
printf("set time : %.3f s\n", (double)set_us / 1000000);
printf("sleep time: %.3f s\n", (double)slp_us / 1000000);
printf("read time : %.3f s\n", (double)read_us / 1000000);
printf("dur time : %.3f s\n", (double)dur_us / 1000000);
// assume getpagesize() sized pages:
printf("referenced: %d pages, %d Kbytes\n", g_activepages,
g_activepages * getpagesize());
printf("walked : %d pages, %d Kbytes\n", g_walkedpages,
g_walkedpages * getpagesize());
}
// assume getpagesize() sized pages:
mbytes = (g_activepages * getpagesize()) / (1024 * 1024);
printf("%-7s %10s\n", "Est(s)", "Ref(MB)");
printf("%-7.3f %10.2f", (double)est_us / 1000000, mbytes);
return 0;
}