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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
/* Print vm statistics
Copyright (C) 1996 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include <stddef.h>
#include <argp.h>
#include <error.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <version.h>
#include <mach.h>
#include <mach/vm_statistics.h>
#include <mach/default_pager.h>
#include <hurd.h>
char *argp_program_version = STANDARD_HURD_VERSION (vmstat);
static const struct argp_option options[] = {
{"terse", 't', 0, 0, "Use short one-line output format", 1 },
{"no-header", 'H', 0, 0, "Don't print a descriptive header line"},
{"prefix", 'p', 0, 0, "Always display a description before stats"},
{"no-prefix", 'P', 0, 0, "Never display a description before stats"},
{"pages", 'v', 0, 0, "Display sizes in pages"},
{"kilobytes", 'k', 0, 0, "Display sizes in 1024 byte blocks"},
{"bytes", 'b', 0, 0, "Display sizes in bytes"},
/* A header for all the individual field options. */
{ 0,0,0,0, "Selecting which statistics to show:", 2},
{0}
};
static const char *args_doc = "[PERIOD [COUNT [HEADER_INTERVAL]]]";
static const char *doc = "Show system virtual memory statistics"
"\vIf PERIOD is supplied, then terse mode is"
" selected, and the output repeated every PERIOD seconds, with cumulative"
" fields given the difference from the last output. If COUNT is given"
" and non-zero, only that many lines are output. HEADER_INTERVAL"
" defaults to 23, and if not zero, is the number of repeats after which a"
" blank line and the header will be reprinted (as well as the totals for"
" cumulative fields).";
/* We use this one type to represent all values printed by this program. It
should be signed, and hopefully large enough (it may need to be larger
than what the system returns values in, as we represent some quantities as
bytes instead of pages)! */
typedef long long val_t;
/* What a given number describes. */
enum val_type
{
COUNT, /* As-is. */
SIZE, /* Use the most convenient unit, with suffix */
PAGESZ, /* Like SIZE, but never changed to PAGES. */
PCENT, /* Append `%'. */
};
/* Print a number of type TYPE. If SIZE_UNITS is non-zero, then values of
type SIZE are divided by that amount and printed without a suffix. FWIDTH
is the width of the field to print it in, right-justified. If SIGN is
true, the value is always printed with a sign, even if it's positive. */
static void
print_val (val_t val, enum val_type type,
size_t size_units, int fwidth, int sign)
{
if (type == PCENT)
printf (sign ? "%+*d%%" : "%*d%%", fwidth - 1, val);
else if ((type == SIZE || type == PAGESZ) && size_units == 0)
{
float fval = val;
char *units = " KMGT", *u = units;
while (fval > 1024)
{
fval /= 1024;
u++;
}
printf ((fval >= 1000
? (sign ? "%+*.0f%c" : "%*.0f%c")
: (sign ? "%+*.3g%c" : "%*.3g%c")),
fwidth - 1, fval, *u);
}
else
{
if ((type == SIZE || type == PAGESZ) && size_units > 0)
val /= size_units;
printf (sign ? "%+*d" : "%*d", fwidth, val);
}
}
/* How this field changes with time. */
enum field_change_type
{
VARY, /* Can go up or down. */
CONST, /* Always the same. */
CUMUL, /* Monotonic increasing. */
};
struct vm_state; /* fwd */
struct field
{
/* Name of the field. */
char *name;
/* Terse header used for the columnar style output. */
char *hdr;
/* A description of this field (for user help). */
char *doc;
/* Type of this field. */
enum field_change_type change_type;
/* How to display the number associated with this field.
If this is anything but `DIMLESS', then it can be overriden by the
user. */
enum val_type type;
/* True if we display this field by default (user can always override). */
int standard :1;
/* Offset of the integer_t field in a vm_statistics structure. -1 if a
computed field (in which case the COMPUTE field should be filled in). */
int offs;
/* How to compute this field. If 0, get_vmstats_value is used. This
function should return a negative number if there's some problem getting
the field. */
val_t (*compute)(struct vm_state *state, const struct field *field);
};
/* State about system vm from which we compute the above defined fields. */
struct vm_state
{
/* General vm statistics. */
struct vm_statistics vmstats;
/* default pager port (must be privileged to fetch this). */
mach_port_t def_pager;
struct default_pager_info def_pager_info;
};
static error_t
vm_state_refresh (struct vm_state *state)
{
error_t err = vm_statistics (mach_task_self (), &state->vmstats);
if (err)
return err;
/* Mark the info as invalid, but leave DEF_PAGER alone. */
bzero (&state->def_pager_info, sizeof state->def_pager_info);
return 0;
}
static val_t
get_vmstats_field (struct vm_state *state, const struct field *field)
{
val_t val =
(val_t)(*(integer_t *)((char *)&state->vmstats + field->offs));
if (field->type == SIZE)
val *= state->vmstats.pagesize;
return val;
}
static val_t
get_size (struct vm_state *state, const struct field *field)
{
return
(state->vmstats.free_count + state->vmstats.active_count
+ state->vmstats.inactive_count + state->vmstats.wire_count)
* state->vmstats.pagesize;
}
static val_t
vm_state_get_field (struct vm_state *state, const struct field *field)
{
return (field->compute ?: get_vmstats_field) (state, field);
}
static val_t
get_cache_hit_ratio (struct vm_state *state, const struct field *field)
{
return state->vmstats.hits * 100 / state->vmstats.lookups;
}
/* Makes sure STATE contains a default pager port and associated info, and
returns 0 if not (after printing an error). */
static int
ensure_def_pager_info (struct vm_state *state)
{
error_t err;
if (state->def_pager == MACH_PORT_NULL)
{
mach_port_t host;
err = get_privileged_ports (&host, 0);
if (err)
{
error (0, err, "get_privileged_ports");
return 0;
}
err = vm_set_default_memory_manager (host, &state->def_pager);
mach_port_deallocate (mach_task_self (), host);
if (err)
{
error (0, err, "vm_set_default_memory_manager");
return 0;
}
}
err = default_pager_info (state->def_pager, &state->def_pager_info);
if (err)
error (0, err, "default_pager_info");
return (err == 0);
}
static val_t
get_swap_size (struct vm_state *state, const struct field *field)
{
return
ensure_def_pager_info (state) ? state->def_pager_info.dpi_total_space : -1;
}
static val_t
get_swap_free (struct vm_state *state, const struct field *field)
{
return
ensure_def_pager_info (state) ? state->def_pager_info.dpi_free_space : -1;
}
static val_t
get_swap_page_size (struct vm_state *state, const struct field *field)
{
return
ensure_def_pager_info (state) ? state->def_pager_info.dpi_page_size : -1;
}
static val_t
get_swap_active (struct vm_state *state, const struct field *field)
{
return
ensure_def_pager_info (state)
? (state->def_pager_info.dpi_total_space
- state->def_pager_info.dpi_free_space)
: -1;
}
/* Returns the byte offset of the field FIELD in a vm_statistics structure. */
#define _F(field_name) offsetof (struct vm_statistics, field_name)
/* vm_statistics fields we know about. */
static const struct field fields[] =
{
{"pagesize", " pgsz", "System pagesize",
CONST,PAGESZ, 1,_F(pagesize)},
{"size", " size", "Usable physical memory",
CONST,SIZE, 1,0,get_size},
{"free", " free", "Unused physical memory",
VARY, SIZE, 1,_F(free_count)},
{"active", " actv", "Physical memory in active use",
VARY, SIZE, 1,_F(active_count)},
{"inactive", "inact", "Physical memory in the inactive queue",
VARY, SIZE, 1,_F(inactive_count)},
{"wired", "wired", "Unpageable physical memory",
VARY, SIZE, 1,_F(wire_count)},
{"zero filled", "zeroed","Cumulative zero-filled pages",
CUMUL,SIZE, 1,_F(zero_fill_count)},
{"reactivated", "react", "Cumulative reactivated inactive pages",
CUMUL,SIZE, 1,_F(reactivations)},
{"pageins", "pgins", "Cumulative pages paged in",
CUMUL,SIZE, 1,_F(pageins)},
{"pageouts", "pgouts","Cumulative pages paged out",
CUMUL,SIZE, 1,_F(pageouts)},
{"page faults", "pfaults","Cumulative page faults",
CUMUL,COUNT,1,_F(faults)},
{"cow faults", "cowpfs", "Cumulative copy-on-write page faults",
CUMUL,COUNT,1,_F(cow_faults)},
{"cache lookups","clkups", "...",
CUMUL,COUNT,0,_F(lookups)},
{"cache hits", "chits", "...",
CUMUL,COUNT,0,_F(hits)},
{"cache hit ratio","chrat","...",
VARY,PCENT,1,-1,get_cache_hit_ratio},
{"swap size", "swsize", "Size of the default-pager swap area",
CONST,SIZE, 1,0,get_swap_size},
{"swap active", "swactv", "Default-pager swap area in use",
VARY, SIZE, 0,0,get_swap_active},
{"swap free", "swfree", "Default-pager swap area available for swapping",
VARY, SIZE, 1,0,get_swap_free},
{"swap pagesize","swpgsz", "Units used for swapping to the default pager",
CONST,PAGESZ, 0,0,get_swap_page_size},
{0}
};
#undef _F
/* Convert a field name to the corresponding user-option. */
static char *name_to_option (const char *name)
{
char *opt = strdup (name), *p;
if (! opt)
for (p = opt; *p; p++)
if (*p == ' ')
*p = '-';
return opt;
}
int
main (int argc, char **argv)
{
error_t err;
const struct field *field;
struct vm_state state;
int num_fields = 0; /* Number of vm_fields known. */
unsigned long output_fields = 0; /* A bit per field, from 0. */
int count = 1; /* Number of repeats. */
unsigned period = 0; /* Seconds between terse mode repeats. */
unsigned hdr_interval = 22; /* */
ssize_t size_units = 0; /* -1 means `pages' */
int terse = 0, print_heading = 1, print_prefix = -1;
/* Parse our options... */
error_t parse_opt (int key, char *arg, struct argp_state *state)
{
if (key < 0)
/* A field option. */
output_fields |= (1 << (-1 - key));
else
switch (key)
{
case 't': terse = 1; break;
case 'p': print_prefix = 1; break;
case 'P': print_prefix = 0; break;
case 'H': print_heading = 0; break;
case 'b': size_units = 1; break;
case 'v': size_units = -1; break;
case 'k': size_units = 1024; break;
case ARGP_KEY_ARG:
terse = 1;
switch (state->arg_num)
{
case 0:
period = atoi (arg); count = 0; break;
case 1:
count = atoi (arg); break;
case 2:
hdr_interval = atoi (arg); break;
default:
return ARGP_ERR_UNKNOWN;
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
struct argp_option *field_opts;
int field_opts_size;
struct argp field_argp = { 0, parse_opt };
const struct argp *parents[] = { &field_argp, 0 };
const struct argp argp = { options, parse_opt, args_doc, doc, parents };
/* See how many fields we know about. */
for (field = fields; field->name; field++)
num_fields++;
/* Construct an options vector for them. */
field_opts_size = ((num_fields + 1) * sizeof (struct argp_option));
field_opts = alloca (field_opts_size);
bzero (field_opts, field_opts_size);
for (field = fields; field->name; field++)
{
int which = field - fields;
struct argp_option *opt = &field_opts[which];
opt->name = name_to_option (field->name);
opt->key = -1 - which; /* options are numbered -1 ... -(N - 1). */
opt->doc = field->doc;
opt->group = 2;
}
/* No need to terminate FIELD_OPTS because the bzero above's done so. */
field_argp.options = field_opts;
/* Parse our arguments. */
argp_parse (&argp, argc, argv, 0, 0, 0);
if (output_fields == 0)
/* Show default fields. */
for (field = fields; field->name; field++)
if (field->standard)
output_fields |= (1 << (field - fields));
/* Returns an appropiate SIZE_UNITS for printing FIELD. */
#define SIZE_UNITS(field) \
(size_units >= 0 \
? size_units \
: ((field)->type == PAGESZ ? 0 : state.vmstats.pagesize))
/* Prints SEP if the variable FIRST is 0, otherwise, prints START (if
it's non-zero), and sets first to 0. */
#define PSEP(sep, start) \
(first ? (first = 0, (start && fputs (start, stdout))) : fputs (sep, stdout))
#define PVAL(val, field, width, sign) \
print_val (val, (field)->type, SIZE_UNITS (field), width, sign)
/* Actually fetch the statistics. */
bzero (&state, sizeof (state)); /* Initialize STATE. */
err = vm_state_refresh (&state);
if (err)
error (2, err, "vm_state_refresh");
if (terse)
/* Terse (one-line) output mode. */
{
int first_hdr = 1, first, repeats;
struct vm_state prev_state;
int const_fields = 0;
if (count == 0)
count = -1;
/* We only show const fields once per page, so find out which ones
those are. */
for (field = fields; field->name; field++)
if ((output_fields & (1 << (field - fields)))
&& field->change_type == CONST)
const_fields |= (1 << (field - fields));
output_fields &= ~const_fields;
if (const_fields)
hdr_interval--; /* Allow room for the constant fields. */
do
{
if (first_hdr)
first_hdr = 0;
else
putchar ('\n');
if (const_fields)
/* Output constant fields on a line preceding the header. */
{
for (field = fields, first = 1; field->name; field++)
if (const_fields & (1 << (field - fields)))
{
val_t val = vm_state_get_field (&state, field);
if (val < 0)
/* Couldn't fetch this field, don't try again. */
const_fields &= ~(1 << (field - fields));
else
{
PSEP (", ", "(");
printf ("%s: ", field->name);
PVAL (val, field, 0, 0);
}
}
if (! first)
puts (")");
}
if (print_heading)
{
for (field = fields, first = 1; field->name; field++)
if (output_fields & (1 << (field - fields)))
{
PSEP (" ", 0);
fputs (field->hdr, stdout);
}
putchar ('\n');
}
prev_state = state;
for (repeats = 0
; count && repeats < hdr_interval && count
; repeats++, count--)
{
/* Output the fields. */
for (field = fields, first = 1; field->name; field++)
if (output_fields & (1 << (field - fields)))
{
int sign = 0;
int width = strlen (field->hdr);
val_t val = vm_state_get_field (&state, field);
if (repeats && field->change_type == CUMUL)
{
sign = 1;
val -= vm_state_get_field (&prev_state, field);
}
PSEP (" ", 0);
PVAL (val, field, width, sign);
}
putchar ('\n');
prev_state = state;
if (period)
{
sleep (period);
err = vm_state_refresh (&state);
if (err)
error (2, err, "vm_state_refresh");
}
}
}
while (count);
}
else
/* Verbose output. */
{
int max_name_width = 0;
if (print_prefix < 0)
/* By default, only print a prefix if there are multiple fields. */
print_prefix = (output_fields & (output_fields - 1));
if (print_prefix)
/* Find the widest description string, so we can align the output. */
for (field = fields; field->name; field++)
if (output_fields & (1 << (field - fields)))
{
int name_len = strlen (field->name);
if (name_len > max_name_width)
max_name_width = name_len;
}
for (field = fields; field->name; field++)
if (output_fields & (1 << (field - fields)))
{
val_t val = vm_state_get_field (&state, field);
int fwidth = 0;
if (print_prefix)
{
printf ("%s:", field->name);
fwidth = max_name_width + 5 - strlen (field->name);
}
PVAL (val, field, fwidth, 0);
putchar ('\n');
}
}
exit (0);
}
|