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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <libintl.h>
#include <locale.h>
#include "rcapd.h"
#include "utils.h"
#include "rcapd_stat.h"
#include "statcommon.h"
static char mode[RC_MODE_LEN];
static rcapd_stat_hdr_t hdr;
static int global;
static int unformatted;
static time_t stat_mod = 0;
static uint_t timestamp_fmt = NODATE;
typedef struct col {
rcid_t col_id;
char col_name[LC_NAME_LEN];
uint64_t col_nproc;
uint64_t col_vmsize;
uint64_t col_rsssize;
uint64_t col_rsslimit;
uint64_t col_paged_eff;
uint64_t col_paged_eff_old;
uint64_t col_paged_eff_avg;
uint64_t col_paged_att;
uint64_t col_paged_att_old;
uint64_t col_paged_att_avg;
uint64_t col_count;
int col_fresh;
struct col *col_next;
struct col *col_prev;
lcollection_stat_t col_src_stat;
lcollection_stat_t col_old_stat;
} col_t;
static col_t *col_head;
static int ncol;
#define RCAPD_NA "rcapd is not active (try zonememstat)\n"
static col_t *
col_find(rcid_t id)
{
col_t *col;
for (col = col_head; col != NULL; col = col->col_next)
if (col->col_id.rcid_type == id.rcid_type &&
col->col_id.rcid_val == id.rcid_val)
return (col);
return (NULL);
}
static col_t *
col_insert(rcid_t id)
{
col_t *new_col;
new_col = malloc(sizeof (col_t));
if (new_col == NULL) {
(void) fprintf(stderr, gettext("rcapstat: malloc() failed\n"));
exit(E_ERROR);
}
(void) memset(new_col, 0, sizeof (col_t));
new_col->col_next = col_head;
new_col->col_id = id;
if (col_head != NULL)
col_head->col_prev = new_col;
col_head = new_col;
ncol++;
return (new_col);
}
static void
col_remove(col_t *col)
{
if (col->col_prev != NULL)
col->col_prev->col_next = col->col_next;
if (col->col_next != NULL)
col->col_next->col_prev = col->col_prev;
if (col_head == col)
col_head = col->col_next;
ncol--;
free(col);
}
static void
usage()
{
(void) fprintf(stderr,
gettext("usage: rcapstat [-g] [-p | -z] [-T d|u] "
"[interval [count]]\n"));
exit(E_USAGE);
}
static void
format_size(char *str, uint64_t size, int length)
{
char tag = 'K';
if (size >= 10000) {
size = (size + 512) / 1024;
tag = 'M';
if (size >= 10000) {
size = (size + 512) / 1024;
tag = 'G';
}
}
(void) snprintf(str, length, "%4lld%c", size, tag);
}
static int
read_stats(rcid_type_t stat_type)
{
int fd;
int proc_fd;
char procfile[20];
uint64_t pid;
col_t *col, *col_next;
lcollection_report_t report;
struct stat st;
if ((fd = open(STAT_FILE_DEFAULT, O_RDONLY)) < 0) {
warn(gettext(RCAPD_NA));
return (E_ERROR);
}
if (fstat(fd, &st) == 0)
stat_mod = st.st_mtime;
if (read(fd, &hdr, sizeof (hdr)) != sizeof (hdr)) {
(void) fprintf(stderr,
gettext("rcapstat: can't read stat file header: %s\n"),
strerror(errno));
(void) close(fd);
return (E_ERROR);
}
/*
* Check if rcapd is running
*/
pid = hdr.rs_pid;
(void) snprintf(procfile, 20, "/proc/%lld/psinfo", pid);
if ((proc_fd = open(procfile, O_RDONLY)) < 0) {
warn(gettext(RCAPD_NA));
(void) close(fd);
return (E_ERROR);
}
(void) close(proc_fd);
(void) strncpy(mode, hdr.rs_mode, RC_MODE_LEN);
for (col = col_head; col != NULL; col = col->col_next) {
col->col_fresh = 0;
col->col_paged_eff = 0;
col->col_paged_att = 0;
}
while (read(fd, &report, sizeof (report)) == sizeof (report)) {
if (report.lcol_id.rcid_type != stat_type)
continue;
col = col_find(report.lcol_id);
if (col == NULL) {
col = col_insert(report.lcol_id);
col->col_paged_eff_old = col->col_paged_eff =
report.lcol_stat.lcols_pg_eff;
col->col_paged_att_old = col->col_paged_att =
report.lcol_stat.lcols_pg_att;
col->col_count = 0;
}
(void) strncpy(col->col_name, report.lcol_name, LC_NAME_LEN);
col->col_vmsize = report.lcol_image_size;
col->col_rsssize = report.lcol_rss;
col->col_rsslimit = report.lcol_rss_cap;
col->col_fresh = 1;
if (report.lcol_stat.lcols_pg_eff > col->col_paged_eff_old) {
col->col_paged_eff =
report.lcol_stat.lcols_pg_eff -
col->col_paged_eff_old;
if (report.lcol_stat.lcols_scan_count > col->col_count)
col->col_paged_eff_avg =
col->col_paged_eff /
(report.lcol_stat.lcols_scan_count -
col->col_count);
} else {
col->col_paged_eff_avg = 0;
}
if (report.lcol_stat.lcols_pg_att > col->col_paged_att_old) {
col->col_paged_att =
report.lcol_stat.lcols_pg_att -
col->col_paged_att_old;
if (report.lcol_stat.lcols_scan_count > col->col_count)
col->col_paged_att_avg =
col->col_paged_att /
(report.lcol_stat.lcols_scan_count -
col->col_count);
} else {
col->col_paged_att_avg = 0;
}
col->col_paged_eff_old = report.lcol_stat.lcols_pg_eff;
col->col_paged_att_old = report.lcol_stat.lcols_pg_att;
col->col_nproc =
report.lcol_stat.lcols_proc_in -
report.lcol_stat.lcols_proc_out;
col->col_count = report.lcol_stat.lcols_scan_count;
col->col_src_stat = report.lcol_stat;
}
/*
* Remove stale data
*/
col = col_head;
while (col != NULL) {
col_next = col->col_next;
if (col->col_fresh == 0)
col_remove(col);
col = col_next;
}
(void) close(fd);
return (E_SUCCESS);
}
/*
* Print each collection's interval statistics.
*/
/*ARGSUSED*/
static void
print_unformatted_stats(void)
{
col_t *col;
#define DELTA(field) \
(col->col_src_stat.field - col->col_old_stat.field)
col = col_head;
while (col != NULL) {
if (bcmp(&col->col_src_stat, &col->col_old_stat,
sizeof (col->col_src_stat)) == 0) {
col = col->col_next;
continue;
}
(void) printf("%s %s status: succeeded/attempted (k): "
"%llu/%llu, ineffective/scans/unenforced/samplings: "
"%llu/%llu/%llu/%llu, RSS min/max (k): %llu/%llu, cap %llu "
"kB, processes/thpt: %llu/%llu, %llu scans over %lld ms\n",
mode, col->col_name, DELTA(lcols_pg_eff),
DELTA(lcols_pg_att), DELTA(lcols_scan_ineffective),
DELTA(lcols_scan), DELTA(lcols_unenforced_cap),
DELTA(lcols_rss_sample), col->col_src_stat.lcols_min_rss,
col->col_src_stat.lcols_max_rss, col->col_rsslimit,
(col->col_src_stat.lcols_proc_in -
col->col_old_stat.lcols_proc_out), DELTA(lcols_proc_out),
DELTA(lcols_scan_count),
NSEC2MSEC(DELTA(lcols_scan_time_complete)));
col->col_old_stat = col->col_src_stat;
col = col->col_next;
}
if (global)
(void) printf(gettext("physical memory utilization: %3u%% "
"cap enforcement threshold: %3u%%\n"), hdr.rs_pressure_cur,
hdr.rs_pressure_cap);
#undef DELTA
}
static void
print_stats(rcid_type_t stat_type)
{
col_t *col;
char size[6];
char limit[6];
char rss[6];
char nproc[6];
char paged_att[6];
char paged_eff[6];
char paged_att_avg[6];
char paged_eff_avg[6];
static int count = 0;
/*
* Print a header once every 20 times if we're only displaying reports
* for one collection (10 times if -g is used). Print a header every
* interval otherwise.
*/
if (count == 0 || ncol != 1)
(void) printf("%6s %-15s %5s %5s %5s %5s %5s %5s %5s %5s\n",
"id", (stat_type == RCIDT_PROJECT ? "project" : "zone"),
"nproc", "vm", "rss", "cap",
"at", "avgat", "pg", "avgpg");
if (++count >= 20 || (count >= 10 && global != 0) || ncol != 1)
count = 0;
for (col = col_head; col != NULL; col = col->col_next) {
if (col->col_id.rcid_type != stat_type)
continue;
if (col->col_paged_att == 0)
(void) strlcpy(nproc, "-", sizeof (nproc));
else
(void) snprintf(nproc, sizeof (nproc), "%lld",
col->col_nproc);
format_size(size, col->col_vmsize, 6);
format_size(rss, col->col_rsssize, 6);
format_size(limit, col->col_rsslimit, 6);
format_size(paged_att, col->col_paged_att, 6);
format_size(paged_eff, col->col_paged_eff, 6);
format_size(paged_att_avg, col->col_paged_att_avg, 6);
format_size(paged_eff_avg, col->col_paged_eff_avg, 6);
(void) printf("%6lld %-15s %5s %5s %5s %5s %5s %5s %5s %5s\n",
col->col_id.rcid_val, col->col_name,
nproc,
size, rss, limit,
paged_att, paged_att_avg,
paged_eff, paged_eff_avg);
}
if (global)
(void) printf(gettext("physical memory utilization: %3u%% "
"cap enforcement threshold: %3u%%\n"), hdr.rs_pressure_cur,
hdr.rs_pressure_cap);
}
int
main(int argc, char *argv[])
{
int interval = 5;
int count;
int always = 1;
int opt;
int projects = 0;
int zones = 0;
/* project reporting is the default if no option is specified */
rcid_type_t stat_type = RCIDT_PROJECT;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
(void) setpname("rcapstat");
global = unformatted = 0;
while ((opt = getopt(argc, argv, "gpuzT:")) != (int)EOF) {
switch (opt) {
case 'g':
global = 1;
break;
case 'p':
projects = 1;
stat_type = RCIDT_PROJECT;
break;
case 'u':
unformatted = 1;
break;
case 'z':
stat_type = RCIDT_ZONE;
zones = 1;
break;
case 'T':
if (optarg) {
if (*optarg == 'u')
timestamp_fmt = UDATE;
else if (*optarg == 'd')
timestamp_fmt = DDATE;
else
usage();
} else {
usage();
}
break;
default:
usage();
}
}
if (argc > optind)
if ((interval = xatoi(argv[optind++])) <= 0)
die(gettext("invalid interval specified\n"));
if (argc > optind) {
if ((count = xatoi(argv[optind++])) <= 0)
die(gettext("invalid count specified\n"));
always = 0;
}
if (argc > optind || (projects > 0 && zones > 0))
usage();
while (always || count-- > 0) {
if (read_stats(stat_type) != E_SUCCESS)
return (E_ERROR);
if (timestamp_fmt != NODATE)
print_timestamp(timestamp_fmt);
if (!unformatted) {
print_stats(stat_type);
(void) fflush(stdout);
if (count || always)
(void) sleep(interval);
} else {
struct stat st;
print_unformatted_stats();
(void) fflush(stdout);
while (stat(STAT_FILE_DEFAULT, &st) == 0 &&
st.st_mtime == stat_mod)
(void) usleep((useconds_t)(0.2 * MICROSEC));
}
}
return (E_SUCCESS);
}
|