forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidstat.c
2945 lines (2576 loc) · 75.8 KB
/
pidstat.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
/*
* pidstat: Report statistics for Linux tasks
* (C) 2007-2019 by Sebastien GODARD (sysstat <at> orange.fr)
*
***************************************************************************
* 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 of the License, or (at your *
* option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without 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., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA *
***************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/utsname.h>
#include <regex.h>
#ifdef HAVE_LINUX_SCHED_H
#include <linux/sched.h>
#endif
#include "version.h"
#include "pidstat.h"
#include "common.h"
#include "rd_stats.h"
#include "count.h"
#ifdef USE_NLS
#include <locale.h>
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif
#ifdef USE_SCCSID
#define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
char *sccsid(void) { return (SCCSID); }
#endif
#ifdef TEST
void int_handler(int n) { return; }
#endif
unsigned long long tot_jiffies[3] = {0, 0, 0};
unsigned long long uptime_cs[3] = {0, 0, 0};
struct pid_stats *st_pid_list[3] = {NULL, NULL, NULL};
unsigned int *pid_array = NULL;
struct pid_stats st_pid_null;
struct tm ps_tstamp[3];
char commstr[MAX_COMM_LEN];
char userstr[MAX_USER_LEN];
char procstr[MAX_COMM_LEN];
int show_threads = FALSE;
unsigned int pid_nr = 0; /* Nb of PID to display */
int cpu_nr = 0; /* Nb of processors on the machine */
unsigned long tlmkb; /* Total memory in kB */
long interval = -1;
long count = 0;
unsigned int pidflag = 0; /* General flags */
unsigned int tskflag = 0; /* TASK/CHILD stats */
unsigned int actflag = 0; /* Activity flag */
struct sigaction alrm_act, int_act, chld_act;
int signal_caught = 0;
int dplaces_nr = -1; /* Number of decimal places */
/*
***************************************************************************
* Print usage and exit.
*
* IN:
* @progname Name of sysstat command
***************************************************************************
*/
void usage(char *progname)
{
fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ] [ -e <program> <args> ]\n"),
progname);
fprintf(stderr, _("Options are:\n"
"[ -d ] [ -H ] [ -h ] [ -I ] [ -l ] [ -R ] [ -r ] [ -s ] [ -t ] [ -U [ <username> ] ]\n"
"[ -u ] [ -V ] [ -v ] [ -w ] [ -C <command> ] [ -G <process_name> ]\n"
"[ -p { <pid> [,...] | SELF | ALL } ] [ -T { TASK | CHILD | ALL } ]\n"
"[ --dec={ 0 | 1 | 2 } ] [ --human ]\n"));
exit(1);
}
/*
***************************************************************************
* SIGALRM signal handler. No need to reset the handler here.
*
* IN:
* @sig Signal number.
***************************************************************************
*/
void alarm_handler(int sig)
{
alarm(interval);
}
/*
***************************************************************************
* SIGINT and SIGCHLD signals handler.
*
* IN:
* @sig Signal number.
***************************************************************************
*/
void sig_handler(int sig)
{
signal_caught = 1;
}
/*
***************************************************************************
* Initialize uptime variables.
***************************************************************************
*/
void init_stats(void)
{
memset(&st_pid_null, 0, PID_STATS_SIZE);
}
/*
***************************************************************************
* Allocate structures for PIDs entered on command line.
*
* IN:
* @len Number of PIDs entered on the command line.
***************************************************************************
*/
void salloc_pid_array(unsigned int len)
{
if ((pid_array = (unsigned int *) malloc(sizeof(unsigned int) * len)) == NULL) {
perror("malloc");
exit(4);
}
memset(pid_array, 0, sizeof(int) * len);
}
/*
***************************************************************************
* Allocate structures for PIDs to read.
*
* IN:
* @len Number of PIDs (and TIDs) on the system.
***************************************************************************
*/
void salloc_pid(unsigned int len)
{
short i;
for (i = 0; i < 3; i++) {
if ((st_pid_list[i] = (struct pid_stats *) calloc(len, PID_STATS_SIZE)) == NULL) {
perror("calloc");
exit(4);
}
}
}
/*
***************************************************************************
* Reallocate structures for PIDs to read.
***************************************************************************
*/
void realloc_pid(void)
{
short i;
unsigned int new_size = 2 * pid_nr;
for (i = 0; i < 3; i++) {
if ((st_pid_list[i] = (struct pid_stats *) realloc(st_pid_list[i], PID_STATS_SIZE * new_size)) == NULL) {
perror("realloc");
exit(4);
}
memset(st_pid_list[i] + pid_nr, 0, PID_STATS_SIZE * (new_size - pid_nr));
}
pid_nr = new_size;
}
/*
***************************************************************************
* Free PID list structures.
***************************************************************************
*/
void sfree_pid(void)
{
int i;
for (i = 0; i < 3; i++) {
free(st_pid_list[i]);
}
}
/*
***************************************************************************
* Check flags and set default values.
***************************************************************************
*/
void check_flags(void)
{
unsigned int act = 0;
/* Display CPU usage for active tasks by default */
if (!actflag) {
actflag |= P_A_CPU;
}
if (!DISPLAY_PID(pidflag)) {
pidflag |= P_D_ACTIVE_PID + P_D_PID + P_D_ALL_PID;
}
if (!tskflag) {
tskflag |= P_TASK;
}
/* Check that requested activities are available */
if (DISPLAY_TASK_STATS(tskflag)) {
act |= P_A_CPU + P_A_MEM + P_A_IO + P_A_CTXSW
+ P_A_STACK + P_A_KTAB + P_A_RT;
}
if (DISPLAY_CHILD_STATS(tskflag)) {
act |= P_A_CPU + P_A_MEM;
}
actflag &= act;
if (!actflag) {
fprintf(stderr, _("Requested activities not available\n"));
exit(1);
}
}
/*
***************************************************************************
* Look for the PID in the list of PIDs entered on the command line, and
* store it if necessary.
*
* IN:
* @pid_array_nr Length of the PID list.
* @pid PID to search.
*
* OUT:
* @pid_array_nr New length of the PID list.
*
* RETURNS:
* Returns the position of the PID in the list.
***************************************************************************
*/
int update_pid_array(unsigned int *pid_array_nr, unsigned int pid)
{
unsigned int i;
for (i = 0; i < *pid_array_nr; i++) {
if (pid_array[i] == pid)
break;
}
if (i == *pid_array_nr) {
/* PID not found: Store it */
(*pid_array_nr)++;
pid_array[i] = pid;
}
return i;
}
/*
***************************************************************************
* Get pointer on task's command string.
* If this is a thread then return the short command name so that threads
* can still be identified.
*
* IN:
* @pst Pointer on structure with process stats and command line.
***************************************************************************
*/
char *get_tcmd(struct pid_stats *pst)
{
if (DISPLAY_CMDLINE(pidflag) && strlen(pst->cmdline) && !pst->tgid)
/* Option "-l" used */
return pst->cmdline;
else
return pst->comm;
}
/*
***************************************************************************
* Display process command name or command line.
*
* IN:
* @pst Pointer on structure with process stats and command line.
***************************************************************************
*/
void print_comm(struct pid_stats *pst)
{
char *p;
/* Get pointer on task's command string */
p = get_tcmd(pst);
if (pst->tgid) {
cprintf_s(IS_ZERO, " |__%s\n", p);
}
else {
cprintf_s(IS_STR, " %s\n", p);
}
}
/*
***************************************************************************
* Read /proc/meminfo.
***************************************************************************
*/
void read_proc_meminfo(void)
{
struct stats_memory st_mem;
memset(&st_mem, 0, STATS_MEMORY_SIZE);
read_meminfo(&st_mem);
tlmkb = st_mem.tlmkb;
}
/*
***************************************************************************
* Read stats from /proc/#[/task/##]/stat.
*
* IN:
* @pid Process whose stats are to be read.
* @pst Pointer on structure where stats will be saved.
* @tgid If !=0, thread whose stats are to be read.
*
* OUT:
* @pst Pointer on structure where stats have been saved.
* @thread_nr Number of threads of the process.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
***************************************************************************
*/
int read_proc_pid_stat(unsigned int pid, struct pid_stats *pst,
unsigned int *thread_nr, unsigned int tgid)
{
int fd, sz, rc, commsz;
char filename[128];
static char buffer[1024 + 1];
char *start, *end;
if (tgid) {
sprintf(filename, TASK_STAT, tgid, pid);
}
else {
sprintf(filename, PID_STAT, pid);
}
if ((fd = open(filename, O_RDONLY)) < 0)
/* No such process */
return 1;
sz = read(fd, buffer, 1024);
close(fd);
if (sz <= 0)
return 1;
buffer[sz] = '\0';
if ((start = strchr(buffer, '(')) == NULL)
return 1;
start += 1;
if ((end = strrchr(start, ')')) == NULL)
return 1;
commsz = end - start;
if (commsz >= MAX_COMM_LEN)
return 1;
memcpy(pst->comm, start, commsz);
pst->comm[commsz] = '\0';
start = end + 2;
rc = sscanf(start,
"%*s %*d %*d %*d %*d %*d %*u %llu %llu"
" %llu %llu %llu %llu %lld %lld %*d %*d %u %*u %*d %llu %llu"
" %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u"
" %*u %u %u %u %llu %llu %lld\n",
&pst->minflt, &pst->cminflt, &pst->majflt, &pst->cmajflt,
&pst->utime, &pst->stime, &pst->cutime, &pst->cstime,
thread_nr, &pst->vsz, &pst->rss, &pst->processor,
&pst->priority, &pst->policy,
&pst->blkio_swapin_delays, &pst->gtime, &pst->cgtime);
if (rc < 15)
return 1;
if (rc < 17) {
/* gtime and cgtime fields are unavailable in file */
pst->gtime = pst->cgtime = 0;
}
/* Convert to kB */
pst->vsz >>= 10;
pst->rss = PG_TO_KB(pst->rss);
pst->pid = pid;
pst->tgid = tgid;
return 0;
}
/*
***************************************************************************
* Read stats from /proc/#[/task/##]/schedstat.
*
* IN:
* @pid Process whose stats are to be read.
* @pst Pointer on structure where stats will be saved.
* @tgid If != 0, thread whose stats are to be read.
*
* OUT:
* @pst Pointer on structure where stats have been saved.
* @thread_nr Number of threads of the process.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
***************************************************************************
*/
int read_proc_pid_sched(unsigned int pid, struct pid_stats *pst,
unsigned int *thread_nr, unsigned int tgid)
{
int fd, sz, rc = 0;
char filename[128];
static char buffer[1024 + 1];
unsigned long long wtime = 0;
if (tgid) {
sprintf(filename, TASK_SCHED, tgid, pid);
}
else {
sprintf(filename, PID_SCHED, pid);
}
if ((fd = open(filename, O_RDONLY)) >= 0) {
/* schedstat file found for process */
sz = read(fd, buffer, 1024);
close(fd);
if (sz > 0) {
buffer[sz] = '\0';
rc = sscanf(buffer, "%*u %llu %*d\n", &wtime);
}
}
/* Convert ns to jiffies */
pst->wtime = wtime * HZ / 1000000000;
pst->pid = pid;
pst->tgid = tgid;
if (rc < 1)
return 1;
return 0;
}
/*
*****************************************************************************
* Read stats from /proc/#[/task/##]/status.
*
* IN:
* @pid Process whose stats are to be read.
* @pst Pointer on structure where stats will be saved.
* @tgid If !=0, thread whose stats are to be read.
*
* OUT:
* @pst Pointer on structure where stats have been saved.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
*****************************************************************************
*/
int read_proc_pid_status(unsigned int pid, struct pid_stats *pst,
unsigned int tgid)
{
FILE *fp;
char filename[128], line[256];
if (tgid) {
sprintf(filename, TASK_STATUS, tgid, pid);
}
else {
sprintf(filename, PID_STATUS, pid);
}
if ((fp = fopen(filename, "r")) == NULL)
/* No such process */
return 1;
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "Uid:", 4)) {
sscanf(line + 5, "%u", &pst->uid);
}
else if (!strncmp(line, "Threads:", 8)) {
sscanf(line + 9, "%u", &pst->threads);
}
else if (!strncmp(line, "voluntary_ctxt_switches:", 24)) {
sscanf(line + 25, "%lu", &pst->nvcsw);
}
else if (!strncmp(line, "nonvoluntary_ctxt_switches:", 27)) {
sscanf(line + 28, "%lu", &pst->nivcsw);
}
}
fclose(fp);
pst->pid = pid;
pst->tgid = tgid;
return 0;
}
/*
*****************************************************************************
* Read information from /proc/#[/task/##}/smaps.
*
* @pid Process whose stats are to be read.
* @pst Pointer on structure where stats will be saved.
* @tgid If !=0, thread whose stats are to be read.
*
* OUT:
* @pst Pointer on structure where stats have been saved.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
*****************************************************************************
*/
int read_proc_pid_smap(unsigned int pid, struct pid_stats *pst, unsigned int tgid)
{
FILE *fp;
char filename[128], line[256];
int state = 0;
if (tgid) {
sprintf(filename, TASK_SMAP, tgid, pid);
}
else {
sprintf(filename, PID_SMAP, pid);
}
if ((fp = fopen(filename, "rt")) == NULL)
/* No such process */
return 1;
while ((state < 3) && (fgets(line, sizeof(line), fp) != NULL)) {
switch (state) {
case 0:
if (strstr(line, "[stack]")) {
state = 1;
}
break;
case 1:
if (strstr(line, "Size:")) {
sscanf(line + sizeof("Size:"), "%lu", &pst->stack_size);
state = 2;
}
break;
case 2:
if (strstr(line, "Referenced:")) {
sscanf(line + sizeof("Referenced:"), "%lu", &pst->stack_ref);
state = 3;
}
break;
}
}
fclose(fp);
pst->pid = pid;
pst->tgid = tgid;
return 0;
}
/*
*****************************************************************************
* Read process command line from /proc/#[/task/##]/cmdline.
*
* IN:
* @pid Process whose command line is to be read.
* @pst Pointer on structure where command line will be saved.
* @tgid If !=0, thread whose command line is to be read.
*
* OUT:
* @pst Pointer on structure where command line has been saved.
*
* RETURNS:
* 0 if command line has been successfully read (even if the /proc/.../cmdline
* is just empty), and 1 otherwise (the process has terminated).
*****************************************************************************
*/
int read_proc_pid_cmdline(unsigned int pid, struct pid_stats *pst,
unsigned int tgid)
{
FILE *fp;
char filename[128], line[MAX_CMDLINE_LEN];
size_t len;
int i;
if (tgid) {
sprintf(filename, TASK_CMDLINE, tgid, pid);
}
else {
sprintf(filename, PID_CMDLINE, pid);
}
if ((fp = fopen(filename, "r")) == NULL)
/* No such process */
return 1;
memset(line, 0, MAX_CMDLINE_LEN);
len = fread(line, 1, MAX_CMDLINE_LEN - 1, fp);
fclose(fp);
for (i = 0; i < len; i++) {
if (line[i] == '\0') {
line[i] = ' ';
}
}
if (len) {
strncpy(pst->cmdline, line, MAX_CMDLINE_LEN - 1);
pst->cmdline[MAX_CMDLINE_LEN - 1] = '\0';
}
else {
/* proc/.../cmdline was empty */
pst->cmdline[0] = '\0';
}
return 0;
}
/*
***************************************************************************
* Read stats from /proc/#[/task/##]/io.
*
* IN:
* @pid Process whose stats are to be read.
* @pst Pointer on structure where stats will be saved.
* @tgid If !=0, thread whose stats are to be read.
*
* OUT:
* @pst Pointer on structure where stats have been saved.
*
* RETURNS:
* 0 if stats have been successfully read.
* Also returns 0 if current process has terminated or if its io file
* doesn't exist, but in this case, set process' F_NO_PID_IO flag to
* indicate that I/O stats should no longer be read for it.
***************************************************************************
*/
int read_proc_pid_io(unsigned int pid, struct pid_stats *pst,
unsigned int tgid)
{
FILE *fp;
char filename[128], line[256];
if (tgid) {
sprintf(filename, TASK_IO, tgid, pid);
}
else {
sprintf(filename, PID_IO, pid);
}
if ((fp = fopen(filename, "r")) == NULL) {
/* No such process... or file non existent! */
pst->flags |= F_NO_PID_IO;
/*
* Also returns 0 since io stats file doesn't necessarily exist,
* depending on the kernel version used.
*/
return 0;
}
while (fgets(line, sizeof(line), fp) != NULL) {
if (!strncmp(line, "read_bytes:", 11)) {
sscanf(line + 12, "%llu", &pst->read_bytes);
}
else if (!strncmp(line, "write_bytes:", 12)) {
sscanf(line + 13, "%llu", &pst->write_bytes);
}
else if (!strncmp(line, "cancelled_write_bytes:", 22)) {
sscanf(line + 23, "%llu", &pst->cancelled_write_bytes);
}
}
fclose(fp);
pst->pid = pid;
pst->tgid = tgid;
pst->flags &= ~F_NO_PID_IO;
return 0;
}
/*
***************************************************************************
* Count number of file descriptors in /proc/#[/task/##]/fd directory.
*
* IN:
* @pid Process whose stats are to be read.
* @pst Pointer on structure where stats will be saved.
* @tgid If !=0, thread whose stats are to be read.
*
* OUT:
* @pst Pointer on structure where stats have been saved.
*
* RETURNS:
* 0 if stats have been successfully read.
* Also returns 0 if current process has terminated or if we cannot read its
* fd directory, but in this case, set process' F_NO_PID_FD flag to
* indicate that fd directory couldn't be read.
***************************************************************************
*/
int read_proc_pid_fd(unsigned int pid, struct pid_stats *pst,
unsigned int tgid)
{
DIR *dir;
struct dirent *drp;
char filename[128];
if (tgid) {
sprintf(filename, TASK_FD, tgid, pid);
}
else {
sprintf(filename, PID_FD, pid);
}
if ((dir = opendir(filename)) == NULL) {
/* Cannot read fd directory */
pst->flags |= F_NO_PID_FD;
return 0;
}
pst->fd_nr = 0;
/* Count number of entries if fd directory */
while ((drp = readdir(dir)) != NULL) {
if (isdigit(drp->d_name[0])) {
(pst->fd_nr)++;
}
}
closedir(dir);
pst->pid = pid;
pst->tgid = tgid;
pst->flags &= ~F_NO_PID_FD;
return 0;
}
/*
***************************************************************************
* Read various stats for given PID.
*
* IN:
* @pid Process whose stats are to be read.
* @pst Pointer on structure where stats will be saved.
* @tgid If !=0, thread whose stats are to be read.
*
* OUT:
* @pst Pointer on structure where stats have been saved.
* @thread_nr Number of threads of the process.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
***************************************************************************
*/
int read_pid_stats(unsigned int pid, struct pid_stats *pst,
unsigned int *thread_nr, unsigned int tgid)
{
if (read_proc_pid_stat(pid, pst, thread_nr, tgid))
return 1;
/*
* No need to test the return code here: Not finding
* the schedstat files shouldn't make pidstat stop.
*/
read_proc_pid_sched(pid, pst, thread_nr, tgid);
if (DISPLAY_CMDLINE(pidflag)) {
if (read_proc_pid_cmdline(pid, pst, tgid))
return 1;
}
if (read_proc_pid_status(pid, pst, tgid))
return 1;
if (DISPLAY_STACK(actflag)) {
if (read_proc_pid_smap(pid, pst, tgid))
return 1;
}
if (DISPLAY_KTAB(actflag)) {
if (read_proc_pid_fd(pid, pst, tgid))
return 1;
}
if (DISPLAY_IO(actflag))
/* Assume that /proc/#/task/#/io exists! */
return (read_proc_pid_io(pid, pst, tgid));
return 0;
}
/*
***************************************************************************
* Count number of threads in /proc/#/task directory, including the leader
* one.
*
* IN:
* @pid Process number for which the number of threads are to be counted.
*
* RETURNS:
* Number of threads for the given process (min value is 1).
* A value of 0 indicates that the process has terminated.
***************************************************************************
*/
unsigned int count_tid(unsigned int pid)
{
struct pid_stats pst;
unsigned int thread_nr;
if (read_proc_pid_stat(pid, &pst, &thread_nr, 0) != 0)
/* Task no longer exists */
return 0;
return thread_nr;
}
/*
***************************************************************************
* Count number of processes (and threads).
*
* RETURNS:
* Number of processes (and threads if requested).
***************************************************************************
*/
unsigned int count_pid(void)
{
DIR *dir;
struct dirent *drp;
unsigned int pid = 0;
/* Open /proc directory */
if ((dir = opendir(PROC)) == NULL) {
perror("opendir");
exit(4);
}
/* Get directory entries */
while ((drp = readdir(dir)) != NULL) {
if (isdigit(drp->d_name[0])) {
/* There is at least the TGID */
pid++;
if (DISPLAY_TID(pidflag)) {
pid += count_tid(atoi(drp->d_name));
}
}
}
/* Close /proc directory */
closedir(dir);
return pid;
}
/*
***************************************************************************
* Count number of threads associated with the tasks entered on the command
* line.
*
* IN:
* @pid_array_nr Length of the PID list.
*
* RETURNS:
* Number of threads (including the leading one) associated with every task
* entered on the command line.
***************************************************************************
*/
unsigned int count_tid_in_list(unsigned int pid_array_nr)
{
unsigned int p, tid, pid = 0;
for (p = 0; p < pid_array_nr; p++) {
tid = count_tid(pid_array[p]);
if (!tid) {
/* PID no longer exists */
pid_array[p] = 0;
}
else {
/* <tid_value> TIDs + 1 TGID */
pid += tid + 1;
}
}
return pid;
}
/*
***************************************************************************
* Allocate and init structures according to system state.
*
* IN:
* @pid_array_nr Length of the PID list.
***************************************************************************
*/
void pid_sys_init(unsigned int pid_array_nr)
{
/* Init stat common counters */
init_stats();
/* Count nb of proc */
cpu_nr = get_cpu_nr(~0, FALSE);
if (DISPLAY_ALL_PID(pidflag)) {
/* Count PIDs and allocate structures */
pid_nr = count_pid() + NR_PID_PREALLOC;
salloc_pid(pid_nr);
}
else if (DISPLAY_TID(pidflag)) {
/* Count total number of threads associated with tasks in list */
pid_nr = count_tid_in_list(pid_array_nr) + NR_PID_PREALLOC;
salloc_pid(pid_nr);
}
else {
pid_nr = pid_array_nr;
salloc_pid(pid_nr);
}
}
/*
***************************************************************************
* Read stats for threads in /proc/#/task directory.
*
* IN:
* @curr Index in array for current sample statistics.
* @pid Process number whose threads stats are to be read.
* @index Index in process list where stats will be saved.
*
* OUT:
* @index Index in process list where next stats will be saved.
***************************************************************************
*/
void read_task_stats(int curr, unsigned int pid, unsigned int *index)
{
DIR *dir;
struct dirent *drp;
char filename[128];
struct pid_stats *pst;
unsigned int thr_nr;
/* Open /proc/#/task directory */
sprintf(filename, PROC_TASK, pid);
if ((dir = opendir(filename)) == NULL)
return;
while ((drp = readdir(dir)) != NULL) {
if (!isdigit(drp->d_name[0])) {
continue;
}
pst = st_pid_list[curr] + (*index)++;
if (read_pid_stats(atoi(drp->d_name), pst, &thr_nr, pid)) {
/* Thread no longer exists */
pst->pid = 0;
}
if (*index >= pid_nr) {
realloc_pid();
}
}
closedir(dir);