-
Notifications
You must be signed in to change notification settings - Fork 752
/
prstat.c
1723 lines (1595 loc) · 44.8 KB
/
prstat.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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) 2013 Gary Mills
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Portions Copyright 2009 Chad Mynhier
* Copyright 2018 Joyent, Inc. All rights reserved.
*/
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/loadavg.h>
#include <sys/time.h>
#include <sys/pset.h>
#include <sys/vm_usage.h>
#include <zone.h>
#include <libzonecfg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <time.h>
#include <project.h>
#include <langinfo.h>
#include <libintl.h>
#include <locale.h>
#include "prstat.h"
#include "prutil.h"
#include "prtable.h"
#include "prsort.h"
#include "prfile.h"
/*
* x86 <sys/regs.h> ERR conflicts with <curses.h> ERR. For the purposes
* of this file, we care about the curses.h ERR so include that last.
*/
#if defined(ERR)
#undef ERR
#endif
#ifndef TEXT_DOMAIN /* should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* use this only if it wasn't */
#endif
#include <curses.h>
#include <term.h>
#define LOGIN_WIDTH 8
#define ZONE_WIDTH 28
#define PROJECT_WIDTH 28
#define PSINFO_HEADER_PROC \
" PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP "
#define PSINFO_HEADER_PROC_LGRP \
" PID USERNAME SIZE RSS STATE PRI NICE TIME CPU LGRP PROCESS/NLWP "
#define PSINFO_HEADER_LWP \
" PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/LWP "
#define PSINFO_HEADER_LWP_LGRP \
" PID USERNAME SIZE RSS STATE PRI NICE TIME CPU LGRP PROCESS/LWP "
#define USAGE_HEADER_PROC \
" PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROCESS/NLWP "
#define USAGE_HEADER_LWP \
" PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROCESS/LWP "
#define USER_HEADER_PROC \
" NPROC USERNAME SWAP RSS MEMORY TIME CPU "
#define USER_HEADER_LWP \
" NLWP USERNAME SWAP RSS MEMORY TIME CPU "
#define TASK_HEADER_PROC \
"TASKID NPROC SWAP RSS MEMORY TIME CPU PROJECT "
#define TASK_HEADER_LWP \
"TASKID NLWP SWAP RSS MEMORY TIME CPU PROJECT "
#define PROJECT_HEADER_PROC \
"PROJID NPROC SWAP RSS MEMORY TIME CPU PROJECT "
#define PROJECT_HEADER_LWP \
"PROJID NLWP SWAP RSS MEMORY TIME CPU PROJECT "
#define ZONE_HEADER_PROC \
"ZONEID NPROC SWAP RSS MEMORY TIME CPU ZONE "
#define ZONE_HEADER_LWP \
"ZONEID NLWP SWAP RSS MEMORY TIME CPU ZONE "
#define PSINFO_LINE \
"%6d %-8s %5s %5s %-6s %3s %3s %9s %3.3s%% %s"
#define PSINFO_LINE_LGRP \
"%6d %-8s %5s %5s %-6s %3s %3s %9s %3.3s%% %4d %s"
#define USAGE_LINE \
"%6d %-8s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s "\
"%3.3s %3.3s %s"
#define USER_LINE \
"%6d %-8s %5.5s %5.5s %3.3s%% %9s %3.3s%%"
#define TASK_LINE \
"%6d %8d %5s %5s %3.3s%% %9s %3.3s%% %28s"
#define PROJECT_LINE \
"%6d %8d %5s %5s %3.3s%% %9s %3.3s%% %28s"
#define ZONE_LINE \
"%6d %8d %5s %5s %3.3s%% %9s %3.3s%% %28s"
#define TOTAL_LINE \
"Total: %d processes, %d lwps, load averages: %3.2f, %3.2f, %3.2f"
/* global variables */
static char *t_ulon; /* termcap: start underline */
static char *t_uloff; /* termcap: end underline */
static char *t_up; /* termcap: cursor 1 line up */
static char *t_eol; /* termcap: clear end of line */
static char *t_smcup; /* termcap: cursor mvcap on */
static char *t_rmcup; /* termcap: cursor mvcap off */
static char *t_home; /* termcap: move cursor home */
static char *movecur = NULL; /* termcap: move up string */
static char *empty_string = "\0"; /* termcap: empty string */
static uint_t print_movecur = FALSE; /* print movecur or not */
static int is_curses_on = FALSE; /* current curses state */
static table_t pid_tbl = {0, 0, NULL}; /* selected processes */
static table_t cpu_tbl = {0, 0, NULL}; /* selected processors */
static table_t set_tbl = {0, 0, NULL}; /* selected processor sets */
static table_t prj_tbl = {0, 0, NULL}; /* selected projects */
static table_t tsk_tbl = {0, 0, NULL}; /* selected tasks */
static table_t lgr_tbl = {0, 0, NULL}; /* selected lgroups */
static zonetbl_t zone_tbl = {0, 0, NULL}; /* selected zones */
static uidtbl_t euid_tbl = {0, 0, NULL}; /* selected effective users */
static uidtbl_t ruid_tbl = {0, 0, NULL}; /* selected real users */
static uint_t total_procs; /* total number of procs */
static uint_t total_lwps; /* total number of lwps */
static float total_cpu; /* total cpu usage */
static float total_mem; /* total memory usage */
static list_t lwps; /* list of lwps/processes */
static list_t users; /* list of users */
static list_t tasks; /* list of tasks */
static list_t projects; /* list of projects */
static list_t zones; /* list of zones */
static list_t lgroups; /* list of lgroups */
static volatile uint_t sigwinch = 0;
static volatile uint_t sigtstp = 0;
static volatile uint_t sigterm = 0;
static long pagesize;
/* default settings */
optdesc_t opts = {
5, /* interval between updates, seconds */
15, /* number of lines in top part */
5, /* number of lines in bottom part */
-1, /* number of iterations; infinitely */
OPT_PSINFO | OPT_FULLSCREEN | OPT_USEHOME | OPT_TERMCAP,
-1 /* sort in decreasing order */
};
/*
* Print timestamp as decimal reprentation of time_t value (-d u was specified)
* or the standard date format (-d d was specified).
*/
static void
print_timestamp(void)
{
time_t t = time(NULL);
static char *fmt = NULL;
/* We only need to retrieve this once per invocation */
if (fmt == NULL)
fmt = nl_langinfo(_DATE_FMT);
if (opts.o_outpmode & OPT_UDATE) {
(void) printf("%ld", t);
} else if (opts.o_outpmode & OPT_DDATE) {
char dstr[64];
int len;
len = strftime(dstr, sizeof (dstr), fmt, localtime(&t));
if (len > 0)
(void) printf("%s", dstr);
}
(void) putp(t_eol);
(void) putchar('\n');
}
static void
psetloadavg(long psetid, void *ptr)
{
double psetloadavg[3];
double *loadavg = ptr;
if (pset_getloadavg((psetid_t)psetid, psetloadavg, 3) != -1) {
*loadavg++ += psetloadavg[0];
*loadavg++ += psetloadavg[1];
*loadavg += psetloadavg[2];
}
}
/*
* Queries the memory virtual and rss size for each member of a list.
* This will override the values computed by /proc aggregation.
*/
static void
list_getsize(list_t *list)
{
id_info_t *id;
vmusage_t *results, *next;
vmusage_t *match;
size_t nres = 0;
size_t i;
uint_t flags = 0;
int ret;
size_t physmem = sysconf(_SC_PHYS_PAGES) * pagesize;
/*
* Determine what swap/rss results to calculate. getvmusage() will
* prune results returned to non-global zones automatically, so
* there is no need to pass different flags when calling from a
* non-global zone.
*
* Currently list_getsize() is only called with a single flag. This
* is because -Z, -J, -T, and -a are mutually exclusive. Regardless
* of this, we handle multiple flags.
*/
if (opts.o_outpmode & OPT_USERS) {
/*
* Gather rss for all users in all zones. Treat the same
* uid in different zones as the same user.
*/
flags |= VMUSAGE_COL_RUSERS;
} else if (opts.o_outpmode & OPT_TASKS) {
/* Gather rss for all tasks in all zones */
flags |= VMUSAGE_ALL_TASKS;
} else if (opts.o_outpmode & OPT_PROJECTS) {
/*
* Gather rss for all projects in all zones. Treat the same
* projid in diffrent zones as the same project.
*/
flags |= VMUSAGE_COL_PROJECTS;
} else if (opts.o_outpmode & OPT_ZONES) {
/* Gather rss for all zones */
flags |= VMUSAGE_ALL_ZONES;
} else {
Die(gettext(
"Cannot determine rss flags for output options %x\n"),
opts.o_outpmode);
}
/*
* getvmusage() returns an array of result structures. One for
* each zone, project, task, or user on the system, depending on
* flags.
*
* If getvmusage() fails, prstat will use the size already gathered
* from psinfo
*/
if (getvmusage(flags, opts.o_interval, NULL, &nres) != 0)
return;
results = (vmusage_t *)Malloc(sizeof (vmusage_t) * nres);
for (;;) {
ret = getvmusage(flags, opts.o_interval, results, &nres);
if (ret == 0)
break;
if (errno == EOVERFLOW) {
results = (vmusage_t *)Realloc(results,
sizeof (vmusage_t) * nres);
continue;
}
/*
* Failure for some other reason. Prstat will use the size
* already gathered from psinfo.
*/
free(results);
return;
}
for (id = list->l_head; id != NULL; id = id->id_next) {
match = NULL;
next = results;
for (i = 0; i < nres; i++, next++) {
switch (flags) {
case VMUSAGE_COL_RUSERS:
if (next->vmu_id == id->id_uid)
match = next;
break;
case VMUSAGE_ALL_TASKS:
if (next->vmu_id == id->id_taskid)
match = next;
break;
case VMUSAGE_COL_PROJECTS:
if (next->vmu_id == id->id_projid)
match = next;
break;
case VMUSAGE_ALL_ZONES:
if (next->vmu_id == id->id_zoneid)
match = next;
break;
default:
Die(gettext(
"Unknown vmusage flags %d\n"), flags);
}
}
if (match != NULL) {
id->id_size = match->vmu_swap_all / 1024;
id->id_rssize = match->vmu_rss_all / 1024;
id->id_pctmem = (100.0 * (float)match->vmu_rss_all) /
(float)physmem;
/* Output using data from getvmusage() */
id->id_sizematch = B_TRUE;
}
/*
* If no match is found, prstat will use the size already
* gathered from psinfo.
*/
}
free(results);
}
/*
* A routine to display the contents of the list on the screen
*/
static void
list_print(list_t *list)
{
lwp_info_t *lwp;
id_info_t *id;
char usr[4], sys[4], trp[4], tfl[4];
char dfl[4], lck[4], slp[4], lat[4];
char vcx[4], icx[4], scl[4], sig[4];
char psize[6], prssize[6], pmem[6], pcpu[6], ptime[12];
char pstate[7], pnice[4], ppri[4];
char pname[LOGNAME_MAX+1];
char name[PRFNSZ + THREAD_NAME_MAX + 2];
char projname[PROJNAME_MAX+1];
char zonename[ZONENAME_MAX+1];
float cpu, mem;
double loadavg[3] = {0, 0, 0};
int i, n;
if (list->l_size == 0)
return;
if (foreach_element(&set_tbl, &loadavg, psetloadavg) == 0) {
/*
* If processor sets aren't specified, we display system-wide
* load averages.
*/
(void) getloadavg(loadavg, 3);
}
if (((opts.o_outpmode & OPT_UDATE) || (opts.o_outpmode & OPT_DDATE)) &&
((list->l_type == LT_LWPS) || !(opts.o_outpmode & OPT_SPLIT)))
print_timestamp();
if (opts.o_outpmode & OPT_TTY)
(void) putchar('\r');
(void) putp(t_ulon);
n = opts.o_cols;
switch (list->l_type) {
case LT_PROJECTS:
if (opts.o_outpmode & OPT_LWPS)
n = printf(PROJECT_HEADER_LWP);
else
n = printf(PROJECT_HEADER_PROC);
break;
case LT_TASKS:
if (opts.o_outpmode & OPT_LWPS)
n = printf(TASK_HEADER_LWP);
else
n = printf(TASK_HEADER_PROC);
break;
case LT_ZONES:
if (opts.o_outpmode & OPT_LWPS)
n = printf(ZONE_HEADER_LWP);
else
n = printf(ZONE_HEADER_PROC);
break;
case LT_USERS:
if (opts.o_outpmode & OPT_LWPS)
n = printf(USER_HEADER_LWP);
else
n = printf(USER_HEADER_PROC);
break;
case LT_LWPS:
if (opts.o_outpmode & OPT_LWPS) {
if (opts.o_outpmode & OPT_PSINFO) {
if (opts.o_outpmode & OPT_LGRP)
n = printf(PSINFO_HEADER_LWP_LGRP);
else
n = printf(PSINFO_HEADER_LWP);
}
if (opts.o_outpmode & OPT_MSACCT)
n = printf(USAGE_HEADER_LWP);
} else {
if (opts.o_outpmode & OPT_PSINFO) {
if (opts.o_outpmode & OPT_LGRP)
n = printf(PSINFO_HEADER_PROC_LGRP);
else
n = printf(PSINFO_HEADER_PROC);
}
if (opts.o_outpmode & OPT_MSACCT)
n = printf(USAGE_HEADER_PROC);
}
break;
}
/* Pad out the header line so the underline spans the whole width */
if ((opts.o_outpmode & OPT_TERMCAP) && n < opts.o_cols)
(void) printf("%*s", (int)(opts.o_cols - n), "");
(void) putp(t_uloff);
(void) putp(t_eol);
(void) putchar('\n');
for (i = 0; i < list->l_used; i++) {
switch (list->l_type) {
case LT_PROJECTS:
case LT_TASKS:
case LT_USERS:
case LT_ZONES:
id = list->l_ptrs[i];
/*
* CPU usage and memory usage normalization
*/
if (total_cpu >= 100)
cpu = (100 * id->id_pctcpu) / total_cpu;
else
cpu = id->id_pctcpu;
if (id->id_sizematch == B_FALSE && total_mem >= 100)
mem = (100 * id->id_pctmem) / total_mem;
else
mem = id->id_pctmem;
if (list->l_type == LT_USERS) {
pwd_getname(id->id_uid, pname, sizeof (pname),
opts.o_outpmode & OPT_NORESOLVE,
opts.o_outpmode & (OPT_TERMCAP|OPT_TRUNC),
LOGIN_WIDTH);
} else if (list->l_type == LT_ZONES) {
getzonename(id->id_zoneid, zonename,
sizeof (zonename),
opts.o_outpmode & (OPT_TERMCAP|OPT_TRUNC),
ZONE_WIDTH);
} else {
getprojname(id->id_projid, projname,
sizeof (projname),
opts.o_outpmode & OPT_NORESOLVE,
opts.o_outpmode & (OPT_TERMCAP|OPT_TRUNC),
PROJECT_WIDTH);
}
Format_size(psize, id->id_size, 6);
Format_size(prssize, id->id_rssize, 6);
Format_pct(pmem, mem, 4);
Format_pct(pcpu, cpu, 4);
Format_time(ptime, id->id_time, 10);
if (opts.o_outpmode & OPT_TTY)
(void) putchar('\r');
if (list->l_type == LT_PROJECTS)
(void) printf(PROJECT_LINE, (int)id->id_projid,
id->id_nproc, psize, prssize, pmem, ptime,
pcpu, projname);
else if (list->l_type == LT_TASKS)
(void) printf(TASK_LINE, (int)id->id_taskid,
id->id_nproc, psize, prssize, pmem, ptime,
pcpu, projname);
else if (list->l_type == LT_ZONES)
(void) printf(ZONE_LINE, (int)id->id_zoneid,
id->id_nproc, psize, prssize, pmem, ptime,
pcpu, zonename);
else
(void) printf(USER_LINE, id->id_nproc, pname,
psize, prssize, pmem, ptime, pcpu);
(void) putp(t_eol);
(void) putchar('\n');
break;
case LT_LWPS:
lwp = list->l_ptrs[i];
format_name(lwp, name, sizeof (name));
pwd_getname(lwp->li_info.pr_uid, pname, sizeof (pname),
opts.o_outpmode & OPT_NORESOLVE,
opts.o_outpmode & (OPT_TERMCAP|OPT_TRUNC),
LOGIN_WIDTH);
if (opts.o_outpmode & OPT_PSINFO) {
Format_size(psize, lwp->li_info.pr_size, 6);
Format_size(prssize, lwp->li_info.pr_rssize, 6);
Format_state(pstate,
lwp->li_info.pr_lwp.pr_sname,
lwp->li_info.pr_lwp.pr_onpro, 7);
if (strcmp(lwp->li_info.pr_lwp.pr_clname,
"RT") == 0 ||
strcmp(lwp->li_info.pr_lwp.pr_clname,
"SYS") == 0 ||
lwp->li_info.pr_lwp.pr_sname == 'Z')
(void) strcpy(pnice, " -");
else
Format_num(pnice,
lwp->li_info.pr_lwp.pr_nice - NZERO,
4);
Format_num(ppri, lwp->li_info.pr_lwp.pr_pri, 4);
Format_pct(pcpu,
FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu), 4);
if (opts.o_outpmode & OPT_LWPS)
Format_time(ptime,
lwp->li_info.pr_lwp.pr_time.tv_sec,
10);
else
Format_time(ptime,
lwp->li_info.pr_time.tv_sec, 10);
if (opts.o_outpmode & OPT_TTY)
(void) putchar('\r');
if (opts.o_outpmode & OPT_LGRP) {
(void) printf(PSINFO_LINE_LGRP,
(int)lwp->li_info.pr_pid, pname,
psize, prssize, pstate,
ppri, pnice, ptime, pcpu,
lwp->li_info.pr_lwp.pr_lgrp, name);
} else {
(void) printf(PSINFO_LINE,
(int)lwp->li_info.pr_pid, pname,
psize, prssize, pstate, ppri, pnice,
ptime, pcpu, name);
}
(void) putp(t_eol);
(void) putchar('\n');
}
if (opts.o_outpmode & OPT_MSACCT) {
Format_pct(usr, lwp->li_usr, 4);
Format_pct(sys, lwp->li_sys, 4);
Format_pct(slp, lwp->li_slp, 4);
Format_num(vcx, lwp->li_vcx, 4);
Format_num(icx, lwp->li_icx, 4);
Format_num(scl, lwp->li_scl, 4);
Format_num(sig, lwp->li_sig, 4);
Format_pct(trp, lwp->li_trp, 4);
Format_pct(tfl, lwp->li_tfl, 4);
Format_pct(dfl, lwp->li_dfl, 4);
Format_pct(lck, lwp->li_lck, 4);
Format_pct(lat, lwp->li_lat, 4);
if (opts.o_outpmode & OPT_TTY)
(void) putchar('\r');
(void) printf(USAGE_LINE,
(int)lwp->li_info.pr_pid, pname,
usr, sys, trp, tfl, dfl, lck,
slp, lat, vcx, icx, scl, sig,
name);
(void) putp(t_eol);
(void) putchar('\n');
}
break;
}
}
if (opts.o_outpmode & OPT_TTY)
(void) putchar('\r');
if (opts.o_outpmode & OPT_TERMCAP) {
switch (list->l_type) {
case LT_PROJECTS:
case LT_USERS:
case LT_TASKS:
case LT_ZONES:
while (i++ < opts.o_nbottom) {
(void) putp(t_eol);
(void) putchar('\n');
}
break;
case LT_LWPS:
while (i++ < opts.o_ntop) {
(void) putp(t_eol);
(void) putchar('\n');
}
}
}
if (opts.o_outpmode & OPT_TTY)
(void) putchar('\r');
if ((opts.o_outpmode & OPT_SPLIT) && list->l_type == LT_LWPS)
return;
(void) printf(TOTAL_LINE, total_procs, total_lwps,
loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN],
loadavg[LOADAVG_15MIN]);
(void) putp(t_eol);
(void) putchar('\n');
if (opts.o_outpmode & OPT_TTY)
(void) putchar('\r');
(void) putp(t_eol);
(void) fflush(stdout);
}
static lwp_info_t *
list_add_lwp(list_t *list, pid_t pid, id_t lwpid)
{
lwp_info_t *lwp;
if (list->l_head == NULL) {
list->l_head = list->l_tail = lwp = Zalloc(sizeof (lwp_info_t));
} else {
lwp = Zalloc(sizeof (lwp_info_t));
lwp->li_prev = list->l_tail;
((lwp_info_t *)list->l_tail)->li_next = lwp;
list->l_tail = lwp;
}
lwp->li_info.pr_pid = pid;
lwp->li_info.pr_lwp.pr_lwpid = lwpid;
lwpid_add(lwp, pid, lwpid);
list->l_count++;
return (lwp);
}
static void
list_remove_lwp(list_t *list, lwp_info_t *lwp)
{
if (lwp->li_prev)
lwp->li_prev->li_next = lwp->li_next;
else
list->l_head = lwp->li_next; /* removing the head */
if (lwp->li_next)
lwp->li_next->li_prev = lwp->li_prev;
else
list->l_tail = lwp->li_prev; /* removing the tail */
lwpid_del(lwp->li_info.pr_pid, lwp->li_info.pr_lwp.pr_lwpid);
if (lwpid_pidcheck(lwp->li_info.pr_pid) == 0)
fds_rm(lwp->li_info.pr_pid);
list->l_count--;
free(lwp);
}
static void
list_clear(list_t *list)
{
if (list->l_type == LT_LWPS) {
lwp_info_t *lwp = list->l_tail;
lwp_info_t *lwp_tmp;
fd_closeall();
while (lwp) {
lwp_tmp = lwp;
lwp = lwp->li_prev;
list_remove_lwp(&lwps, lwp_tmp);
}
} else {
id_info_t *id = list->l_head;
id_info_t *nextid;
while (id) {
nextid = id->id_next;
free(id);
id = nextid;
}
list->l_count = 0;
list->l_head = list->l_tail = NULL;
}
}
static void
list_update(list_t *list, lwp_info_t *lwp)
{
id_info_t *id;
if (list->l_head == NULL) { /* first element */
list->l_head = list->l_tail = id = Zalloc(sizeof (id_info_t));
goto update;
}
for (id = list->l_head; id; id = id->id_next) {
if ((list->l_type == LT_USERS) &&
(id->id_uid != lwp->li_info.pr_uid))
continue;
if ((list->l_type == LT_TASKS) &&
(id->id_taskid != lwp->li_info.pr_taskid))
continue;
if ((list->l_type == LT_PROJECTS) &&
(id->id_projid != lwp->li_info.pr_projid))
continue;
if ((list->l_type == LT_ZONES) &&
(id->id_zoneid != lwp->li_info.pr_zoneid))
continue;
if ((list->l_type == LT_LGRPS) &&
(id->id_lgroup != lwp->li_info.pr_lwp.pr_lgrp))
continue;
id->id_nproc++;
id->id_taskid = lwp->li_info.pr_taskid;
id->id_projid = lwp->li_info.pr_projid;
id->id_zoneid = lwp->li_info.pr_zoneid;
id->id_lgroup = lwp->li_info.pr_lwp.pr_lgrp;
if (lwp->li_flags & LWP_REPRESENT) {
id->id_size += lwp->li_info.pr_size;
id->id_rssize += lwp->li_info.pr_rssize;
}
id->id_pctcpu += FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);
if (opts.o_outpmode & OPT_LWPS)
id->id_time += TIME2SEC(lwp->li_info.pr_lwp.pr_time);
else
id->id_time += TIME2SEC(lwp->li_info.pr_time);
id->id_pctmem += FRC2PCT(lwp->li_info.pr_pctmem);
id->id_key += lwp->li_key;
total_cpu += FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);
total_mem += FRC2PCT(lwp->li_info.pr_pctmem);
return;
}
id = list->l_tail;
id->id_next = Zalloc(sizeof (id_info_t));
id->id_next->id_prev = list->l_tail;
id->id_next->id_next = NULL;
list->l_tail = id->id_next;
id = list->l_tail;
update:
id->id_uid = lwp->li_info.pr_uid;
id->id_projid = lwp->li_info.pr_projid;
id->id_taskid = lwp->li_info.pr_taskid;
id->id_zoneid = lwp->li_info.pr_zoneid;
id->id_lgroup = lwp->li_info.pr_lwp.pr_lgrp;
id->id_nproc++;
id->id_sizematch = B_FALSE;
if (lwp->li_flags & LWP_REPRESENT) {
id->id_size = lwp->li_info.pr_size;
id->id_rssize = lwp->li_info.pr_rssize;
}
id->id_pctcpu = FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);
if (opts.o_outpmode & OPT_LWPS)
id->id_time = TIME2SEC(lwp->li_info.pr_lwp.pr_time);
else
id->id_time = TIME2SEC(lwp->li_info.pr_time);
id->id_pctmem = FRC2PCT(lwp->li_info.pr_pctmem);
id->id_key = lwp->li_key;
total_cpu += id->id_pctcpu;
total_mem += id->id_pctmem;
list->l_count++;
}
static void
lwp_update(lwp_info_t *lwp, pid_t pid, id_t lwpid, struct prusage *usage)
{
float period;
if (!lwpid_is_active(pid, lwpid)) {
/*
* If we are reading cpu times for the first time then
* calculate average cpu times based on whole process
* execution time.
*/
(void) memcpy(&lwp->li_usage, usage, sizeof (prusage_t));
period = TIME2NSEC(usage->pr_rtime);
period = period/(float)100;
if (period == 0) { /* zombie */
period = 1;
lwp->li_usr = 0;
lwp->li_sys = 0;
lwp->li_slp = 0;
} else {
lwp->li_usr = TIME2NSEC(usage->pr_utime)/period;
lwp->li_sys = TIME2NSEC(usage->pr_stime)/period;
lwp->li_slp = TIME2NSEC(usage->pr_slptime)/period;
}
lwp->li_trp = TIME2NSEC(usage->pr_ttime)/period;
lwp->li_tfl = TIME2NSEC(usage->pr_tftime)/period;
lwp->li_dfl = TIME2NSEC(usage->pr_dftime)/period;
lwp->li_lck = TIME2NSEC(usage->pr_ltime)/period;
lwp->li_lat = TIME2NSEC(usage->pr_wtime)/period;
period = (period / NANOSEC)*(float)100; /* now in seconds */
lwp->li_vcx = (ulong_t)
(opts.o_interval * (usage->pr_vctx/period));
lwp->li_icx = (ulong_t)
(opts.o_interval * (usage->pr_ictx/period));
lwp->li_scl = (ulong_t)
(opts.o_interval * (usage->pr_sysc/period));
lwp->li_sig = (ulong_t)
(opts.o_interval * (usage->pr_sigs/period));
(void) lwpid_set_active(pid, lwpid);
} else {
/*
* If this is not a first time we are reading a process's
* CPU times then recalculate CPU times based on fresh data
* obtained from procfs and previous CPU time usage values.
*/
period = TIME2NSEC(usage->pr_rtime)-
TIME2NSEC(lwp->li_usage.pr_rtime);
period = period/(float)100;
if (period == 0) { /* zombie */
period = 1;
lwp->li_usr = 0;
lwp->li_sys = 0;
lwp->li_slp = 0;
} else {
lwp->li_usr = (TIME2NSEC(usage->pr_utime)-
TIME2NSEC(lwp->li_usage.pr_utime))/period;
lwp->li_sys = (TIME2NSEC(usage->pr_stime) -
TIME2NSEC(lwp->li_usage.pr_stime))/period;
lwp->li_slp = (TIME2NSEC(usage->pr_slptime) -
TIME2NSEC(lwp->li_usage.pr_slptime))/period;
}
lwp->li_trp = (TIME2NSEC(usage->pr_ttime) -
TIME2NSEC(lwp->li_usage.pr_ttime))/period;
lwp->li_tfl = (TIME2NSEC(usage->pr_tftime) -
TIME2NSEC(lwp->li_usage.pr_tftime))/period;
lwp->li_dfl = (TIME2NSEC(usage->pr_dftime) -
TIME2NSEC(lwp->li_usage.pr_dftime))/period;
lwp->li_lck = (TIME2NSEC(usage->pr_ltime) -
TIME2NSEC(lwp->li_usage.pr_ltime))/period;
lwp->li_lat = (TIME2NSEC(usage->pr_wtime) -
TIME2NSEC(lwp->li_usage.pr_wtime))/period;
lwp->li_vcx = usage->pr_vctx - lwp->li_usage.pr_vctx;
lwp->li_icx = usage->pr_ictx - lwp->li_usage.pr_ictx;
lwp->li_scl = usage->pr_sysc - lwp->li_usage.pr_sysc;
lwp->li_sig = usage->pr_sigs - lwp->li_usage.pr_sigs;
(void) memcpy(&lwp->li_usage, usage, sizeof (prusage_t));
}
}
static int
read_procfile(fd_t **fd, char *pidstr, char *file, void *buf, size_t bufsize)
{
char procfile[MAX_PROCFS_PATH];
(void) snprintf(procfile, MAX_PROCFS_PATH,
"/proc/%s/%s", pidstr, file);
if ((*fd = fd_open(procfile, O_RDONLY, *fd)) == NULL)
return (1);
if (pread(fd_getfd(*fd), buf, bufsize, 0) != bufsize) {
fd_close(*fd);
return (1);
}
return (0);
}
static void
add_proc(psinfo_t *psinfo)
{
lwp_info_t *lwp;
id_t lwpid;
pid_t pid = psinfo->pr_pid;
lwpid = psinfo->pr_lwp.pr_lwpid;
if ((lwp = lwpid_get(pid, lwpid)) == NULL)
lwp = list_add_lwp(&lwps, pid, lwpid);
lwp->li_flags |= LWP_ALIVE | LWP_REPRESENT;
(void) memcpy(&lwp->li_info, psinfo, sizeof (psinfo_t));
lwp->li_info.pr_lwp.pr_pctcpu = lwp->li_info.pr_pctcpu;
}
static void
get_lwpname(pid_t pid, id_t lwpid, char *buf, size_t bufsize)
{
char *path = NULL;
int fd;
buf[0] = '\0';
if (asprintf(&path, "/proc/%d/lwp/%d/lwpname",
(int)pid, (int)lwpid) == -1)
return;
if ((fd = open(path, O_RDONLY)) != -1) {
(void) read(fd, buf, bufsize);
buf[bufsize - 1] = '\0';
(void) close(fd);
}
free(path);
}
static void
add_lwp(psinfo_t *psinfo, lwpsinfo_t *lwpsinfo, int flags)
{
lwp_info_t *lwp;
pid_t pid = psinfo->pr_pid;
id_t lwpid = lwpsinfo->pr_lwpid;
if ((lwp = lwpid_get(pid, lwpid)) == NULL)
lwp = list_add_lwp(&lwps, pid, lwpid);
lwp->li_flags &= ~LWP_REPRESENT;
lwp->li_flags |= LWP_ALIVE;
lwp->li_flags |= flags;
(void) memcpy(&lwp->li_info, psinfo,
sizeof (psinfo_t) - sizeof (lwpsinfo_t));
(void) memcpy(&lwp->li_info.pr_lwp, lwpsinfo, sizeof (lwpsinfo_t));
get_lwpname(pid, lwpid, lwp->li_lwpname, sizeof (lwp->li_lwpname));
}
static void
prstat_scandir(DIR *procdir)
{
char *pidstr;
pid_t pid;
id_t lwpid;
size_t entsz;
long nlwps, nent, i;
char *buf, *ptr;
fds_t *fds;
lwp_info_t *lwp;
dirent_t *direntp;
prheader_t header;
psinfo_t psinfo;
prusage_t usage;
lwpsinfo_t *lwpsinfo;
prusage_t *lwpusage;
total_procs = 0;
total_lwps = 0;
total_cpu = 0;
total_mem = 0;
convert_zone(&zone_tbl);
for (rewinddir(procdir); (direntp = readdir(procdir)); ) {
pidstr = direntp->d_name;
if (pidstr[0] == '.') /* skip "." and ".." */
continue;
pid = atoi(pidstr);
if (pid == 0 || pid == 2 || pid == 3)
continue; /* skip sched, pageout and fsflush */
if (has_element(&pid_tbl, pid) == 0)
continue; /* check if we really want this pid */
fds = fds_get(pid); /* get ptr to file descriptors */
if (read_procfile(&fds->fds_psinfo, pidstr,
"psinfo", &psinfo, sizeof (psinfo_t)) != 0)
continue;
if (!has_uid(&ruid_tbl, psinfo.pr_uid) ||
!has_uid(&euid_tbl, psinfo.pr_euid) ||
!has_element(&prj_tbl, psinfo.pr_projid) ||
!has_element(&tsk_tbl, psinfo.pr_taskid) ||
!has_zone(&zone_tbl, psinfo.pr_zoneid)) {
fd_close(fds->fds_psinfo);
continue;
}
nlwps = psinfo.pr_nlwp + psinfo.pr_nzomb;
if (nlwps > 1 && (opts.o_outpmode & (OPT_LWPS | OPT_PSETS))) {
int rep_lwp = 0;
if (read_procfile(&fds->fds_lpsinfo, pidstr, "lpsinfo",
&header, sizeof (prheader_t)) != 0) {
fd_close(fds->fds_psinfo);
continue;
}
nent = header.pr_nent;
entsz = header.pr_entsize * nent;
ptr = buf = Malloc(entsz);
if (pread(fd_getfd(fds->fds_lpsinfo), buf,
entsz, sizeof (struct prheader)) != entsz) {
fd_close(fds->fds_lpsinfo);
fd_close(fds->fds_psinfo);
free(buf);
continue;
}
nlwps = 0;
for (i = 0; i < nent; i++, ptr += header.pr_entsize) {
/*LINTED ALIGNMENT*/
lwpsinfo = (lwpsinfo_t *)ptr;
if (!has_element(&cpu_tbl,
lwpsinfo->pr_onpro) ||
!has_element(&set_tbl,
lwpsinfo->pr_bindpset) ||
!has_element(&lgr_tbl, lwpsinfo->pr_lgrp))
continue;
nlwps++;
if ((opts.o_outpmode & (OPT_PSETS | OPT_LWPS))