This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
b2g-info.cpp
396 lines (327 loc) · 9.49 KB
/
b2g-info.cpp
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright (C) 2013 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* b2g-info prints information about B2G processes on a phone to stdout.
*
* To build this file without rebuilding your whole tree, run
*
* ./build.sh b2g-info
*
* Then push the executable from out/target/product/<NAME>/system/bin/b2g-info
* into /system/bin.
*/
// Enable assertions.
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "table.h"
#include "process.h"
#include "processlist.h"
#include "utils.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <string>
#include <sstream>
using namespace std;
/**
* The name of the file being executed; i.e., argv[0].
*/
static const char* cmd_name;
/**
* Prints the pids of B2G processes.
*/
void
print_b2g_pids(bool main_process_only, bool child_processes_only)
{
assert(!(main_process_only && child_processes_only));
if (!child_processes_only) {
printf("%d ", ProcessList::singleton().main_process()->pid());
}
if (!main_process_only) {
for (vector<Process*>::const_iterator it =
ProcessList::singleton().child_processes().begin();
it != ProcessList::singleton().child_processes().end(); ++it) {
printf("%d ", (*it)->pid());
}
}
putchar('\n');
}
string
read_whole_file(const char* filename)
{
char buf[1024];
int fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
if (fd == -1) {
return "";
}
ssize_t total_read = 0;
ssize_t num_remaining = sizeof(buf) - 1;
while (true) {
// No more room in the buffer; we're done.
if (num_remaining <= 0) {
break;
}
ssize_t nread = TEMP_FAILURE_RETRY(read(fd, buf + total_read, num_remaining));
if (nread == 0 || nread == -1) {
break;
}
num_remaining -= nread;
total_read += nread;
}
buf[total_read] = '\0';
return buf;
}
void print_system_meminfo()
{
// We can't use sysinfo() here because iit doesn't tell us how much cached
// memory we're using. (On B2G, this is often upwards of 30mb.)
//
// Instead, we have to parse /proc/meminfo.
FILE* meminfo = fopen("/proc/meminfo", "r");
if (!meminfo) {
perror("Couldn't open /proc/meminfo");
return;
}
// These are all in kb.
int total = -1;
int free = -1;
int buffers = -1;
int cached = -1;
int swap_total = -1;
int swap_free = -1;
int swap_cached = -1;
char line[256];
while(fgets(line, sizeof(line), meminfo)) {
int val;
if (sscanf(line, "MemTotal: %d kB", &val) == 1) {
total = val;
} else if (sscanf(line, "MemFree: %d kB", &val) == 1) {
free = val;
} else if (sscanf(line, "Buffers: %d kB", &val) == 1) {
buffers = val;
} else if (sscanf(line, "Cached: %d kB", &val) == 1) {
cached = val;
} else if (sscanf(line, "SwapTotal: %d kB", &val) == 1) {
swap_total = val;
} else if (sscanf(line, "SwapFree: %d kB", &val) == 1) {
swap_free = val;
} else if (sscanf(line, "SwapCached: %d kB", &val) == 1) {
swap_cached = val;
}
}
fclose(meminfo);
if (total == -1 || free == -1 || buffers == -1 || cached == -1 ||
swap_total == -1 || swap_free == -1 || swap_cached == -1) {
fprintf(stderr, "Unable to parse /proc/meminfo.\n");
return;
}
int actually_used = total - free - buffers - cached - swap_cached;
puts("System memory info:\n");
Table t;
t.start_row();
t.add("Total");
t.add_fmt("%0.1f MB", kb_to_mb(total));
t.start_row();
t.add("SwapTotal");
t.add_fmt("%0.1f MB", kb_to_mb(swap_total));
t.start_row();
t.add("Used - cache");
t.add_fmt("%0.1f MB", kb_to_mb(total - free - buffers - cached - swap_cached));
t.start_row();
t.add("B2G procs (PSS)");
int b2g_mem_kb = 0;
for (vector<Process*>::const_iterator it = ProcessList::singleton().b2g_processes().begin();
it != ProcessList::singleton().b2g_processes().end(); ++it) {
b2g_mem_kb += (*it)->pss_kb();
}
t.add_fmt("%0.1f MB", b2g_mem_kb / 1024.0);
t.start_row();
t.add("Non-B2G procs");
t.add_fmt("%0.1f MB", kb_to_mb(total - free - buffers - cached - b2g_mem_kb - swap_cached));
t.start_row();
t.add("Free + cache");
t.add_fmt("%0.1f MB", kb_to_mb(free + buffers + cached + swap_cached));
t.start_row();
t.add("Free");
t.add_fmt("%0.1f MB", kb_to_mb(free));
t.start_row();
t.add("Cache");
t.add_fmt("%0.1f MB", kb_to_mb(buffers + cached + swap_cached));
t.start_row();
t.add("SwapFree");
t.add_fmt("%0.1f MB", kb_to_mb(swap_free));
t.print_with_indent(2);
}
void print_lmk_params()
{
#define LMK_DIR "/sys/module/lowmemorykiller/parameters/"
puts("Low-memory killer parameters:\n");
int notify_pages = str_to_int(read_whole_file(LMK_DIR "notify_trigger"), -1);
printf(" notify_trigger %d KB\n", pages_to_kb(notify_pages));
putchar('\n');
vector<int> oom_adjs;
{
stringstream ss(read_whole_file(LMK_DIR "adj"));
string item;
while (getline(ss, item, ',')) {
oom_adjs.push_back(str_to_int(item, -1));
}
}
vector<int> minfrees;
{
stringstream ss(read_whole_file(LMK_DIR "minfree"));
string item;
while (getline(ss, item, ',')) {
minfrees.push_back(pages_to_kb(str_to_int(item, -1)));
}
}
Table t;
t.start_row();
t.add("oom_adj");
t.add("min_free", Table::ALIGN_LEFT);
for (size_t i = 0; i < max(oom_adjs.size(), minfrees.size()); i++) {
t.start_row();
if (i < oom_adjs.size()) {
t.add(oom_adjs[i]);
} else {
t.add("");
}
if (i < minfrees.size()) {
t.add_fmt("%d KB", minfrees[i]);
} else {
t.add("");
}
}
t.print_with_indent(2);
#undef LMK_DIR
}
void
b2g_ps_add_table_headers(Table& t, bool show_threads)
{
t.start_row();
t.add("NAME");
t.add(show_threads ? "TID" : "PID");
t.add("PPID");
t.add("CPU(s)");
t.add("NICE");
t.add("USS");
t.add("PSS");
t.add("RSS");
t.add("SWAP");
t.add("VSIZE");
t.add("OOM_ADJ");
t.add("USER", Table::ALIGN_LEFT);
}
int
print_b2g_info(bool show_threads)
{
// TODO: switch between kb and mb for RSS etc.
// TODO: Sort processes?
Table t;
// This sits atop USS/PSS/RSS/VSIZE.
t.multi_col_header("megabytes", 3, 7);
if (!show_threads) {
b2g_ps_add_table_headers(t, /* show_threads */ false);
}
for (vector<Process*>::const_iterator it =
ProcessList::singleton().b2g_processes().begin();
it != ProcessList::singleton().b2g_processes().end(); ++it) {
if (show_threads) {
b2g_ps_add_table_headers(t, /* show_threads */ true);
}
Process* p = *it;
t.start_row();
t.add(p->name());
t.add(p->pid());
t.add(p->ppid());
t.add_fmt("%0.1f", p->stime_s() + p->utime_s());
t.add(p->nice());
t.add_fmt("%0.1f", p->uss_mb());
t.add_fmt("%0.1f", p->pss_mb());
t.add_fmt("%0.1f", p->rss_mb());
t.add_fmt("%0.1f", p->swap_mb());
t.add_fmt("%0.1f", p->vsize_mb());
t.add(p->oom_adj());
t.add(p->user(), Table::ALIGN_LEFT);
if (show_threads) {
for (vector<Thread*>::const_iterator thread_it =
p->threads().begin();
thread_it != p->threads().end(); ++thread_it) {
t.start_row();
Thread* thread = *thread_it;
t.add(thread->name());
t.add(thread->tid());
t.add(thread->nice());
}
if (it + 1 != ProcessList::singleton().b2g_processes().end()) {
t.add_delimiter();
}
}
}
t.print();
putchar('\n');
print_system_meminfo();
putchar('\n');
print_lmk_params();
return 0;
}
void usage()
{
printf("usage: %s [args]\n", cmd_name);
printf("\n");
printf("Options:\n");
printf(" -t, --threads Display information about threads.\n");
printf(" -p, --pids Print a list of all B2G PIDs.\n");
printf(" -m, --main-pid Print only the main B2G process's PID.\n");
printf(" -c, --child-pids Print only the child B2G processes' PIDs.\n");
printf(" -h, --help Display this message.\n");
printf("\n");
printf("Note that all of these options are mutually-exclusive.\n");
}
int main(int argc, const char** argv)
{
cmd_name = argv[0];
// We could use an option-parsing library, but this is easier for now.
if (argc > 2) {
fputs("Too many arguments.\n", stderr);
usage();
return 1;
}
bool threads = false;
bool pids_only = false;
bool main_pid_only = false;
bool child_pids_only = false;
if (argc > 1) {
if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "help")) {
usage();
return 0;
}
if (!(threads = !strcmp(argv[1], "-t") || !strcmp(argv[1], "--threads")) &&
!(pids_only = !strcmp(argv[1], "-p") || !strcmp(argv[1], "--pids")) &&
!(main_pid_only = !strcmp(argv[1], "-m") || !strcmp(argv[1], "--main-pid")) &&
!(child_pids_only = !strcmp(argv[1], "-c") || !strcmp(argv[1], "--child-pids"))) {
fprintf(stderr, "Unknown argument %s.\n", argv[1]);
usage();
return 1;
}
}
if (pids_only || main_pid_only || child_pids_only) {
print_b2g_pids(main_pid_only, child_pids_only);
return 0;
}
return print_b2g_info(threads);
}