forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidstat.c
2879 lines (2493 loc) · 74.3 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-2024 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 <sys/wait.h>
#include <regex.h>
#ifdef HAVE_LINUX_SCHED_H
#include <linux/sched.h>
#endif
#include "version.h"
#include "pidstat.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
extern int __env;
#endif
unsigned long long tot_jiffies[3] = {0, 0, 0};
unsigned long long uptime_cs[3] = {0, 0, 0};
struct st_pid *pid_list = NULL;
struct tm ps_tstamp[3];
char commstr[MAX_COMM_LEN];
char userstr[MAX_USER_LEN];
char procstr[MAX_COMM_LEN];
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 status = 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 int_handler(int sig)
{
int status_code;
signal_caught = TRUE;
if (sig == SIGCHLD) {
/*
* SIGCHLD tells the parent process that it can now
* get the exit status of the child via wait().
*/
wait(&status_code);
status = WEXITSTATUS(status_code);
}
}
/*
***************************************************************************
* Free unused PID structures.
*
* IN
* @plist Pointer address on the start of the linked list.
* @force Set to TRUE if all PID structures shall be freed.
***************************************************************************
*/
void sfree_pid(struct st_pid **plist, int force)
{
int i;
struct st_pid *p;
while (*plist != NULL) {
p = *plist;
if (!p->exist || force) {
*plist = p->next;
for (i = 0; i < 3; i++) {
if (p->pstats[i]) {
free(p->pstats[i]);
}
}
free(p);
}
else {
plist = &(p->next);
}
}
}
/*
***************************************************************************
* Set every PID in list to nonexistent status.
*
* IN:
* @plist Pointer on the start of the linked list.
***************************************************************************
*/
void set_pid_nonexistent(struct st_pid *plist)
{
while (plist != NULL) {
plist->exist = FALSE;
plist = plist->next;
}
}
/*
***************************************************************************
* 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)) {
/*
* If no PIDs nor -p ALL entered on the command line then
* only active PIDs will be displayed.
*/
pidflag |= P_D_ACTIVE_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 and store it if necessary.
* PID -> PID -> TGID -> TID -> TID -> TID -> PID -> NULL
* Eg.: 1234 -> 1289 -> 1356 -> 1356 -> 1361 -> 4678 -> 1376 -> NULL
*
* IN:
* @plist Pointer address on the start of the linked list.
* @pid PID number.
* @tgid If PID is a TID then @tgid is its TGID number. 0 otherwise.
*
* RETURNS:
* Pointer on the st_pid structure in the list where the PID is located
* (whether it was already in the list or if it has been added).
* NULL if the PID is 0 or it is a TID and its TGID has not been found in
* list.
***************************************************************************
*/
struct st_pid *add_list_pid(struct st_pid **plist, pid_t pid, pid_t tgid)
{
struct st_pid *p, *ps, *tgid_p = NULL;
int i;
if (!pid)
return NULL;
if (!tgid) {
/*
* Add a true PID to the list.
* Add it in ascending order, not taking into account
* other TIDs.
*/
while (*plist != NULL) {
p = *plist;
if (!p->tgid && (p->pid == pid))
/* PID found in list */
return p;
if (!p->tgid && (p->pid > pid))
/* Stop now to insert PID in list */
break;
plist = &(p->next);
}
}
else {
int tgid_found = FALSE;
/*
* PID is a TID.
* It will be inserted in ascending order immediately
* following its TGID.
*/
while (*plist != NULL) {
p = *plist;
if (p->pid == tgid) {
/* TGID found in list */
tgid_found = TRUE;
tgid_p = p;
break;
}
plist = &(p->next);
}
if (!tgid_found)
/* TGID not found: Stop now */
return NULL;
plist = &(p->next);
while (*plist != NULL) {
p = *plist;
if ((p->tgid == tgid_p) && (p->pid == pid))
/* TID found in list */
return p;
if ((p->tgid == tgid_p) && (p->pid > pid))
/* Stop now to insert TID in list */
break;
if (p->tgid != tgid_p)
/* End of TID list: insert TID here */
break;
plist = &(p->next);
}
}
/* PID not found */
ps = *plist;
/* Add PID to the list */
if ((*plist = (struct st_pid *) malloc(sizeof(struct st_pid))) == NULL) {
perror("malloc");
exit(4);
}
memset(*plist, 0, sizeof(struct st_pid));
p = *plist;
for (i = 0; i < 3; i++) {
if ((p->pstats[i] = (struct pid_stats *) malloc(sizeof(struct pid_stats))) == NULL) {
perror("malloc");
exit(4);
}
memset(p->pstats[i], 0, PID_STATS_SIZE);
}
p->pid = pid;
p->next = ps;
if (tgid_p) {
p->tgid = tgid_p;
}
return p;
}
/*
***************************************************************************
* 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:
* @plist Pointer address on the start of the linked list.
***************************************************************************
*/
char *get_tcmd(struct st_pid *plist)
{
if (DISPLAY_CMDLINE(pidflag) && strlen(plist->cmdline) && !plist->tgid)
/* Option "-l" used */
return plist->cmdline;
else
return plist->comm;
}
/*
***************************************************************************
* Display process command name or command line.
*
* IN:
* @plist Pointer address on the start of the linked list.
***************************************************************************
*/
void print_comm(struct st_pid *plist)
{
char *p;
/* Get pointer on task's command string */
p = get_tcmd(plist);
if (plist->tgid) {
if (IS_PID_DISPLAYED(plist->tgid->flags)) {
cprintf_s(IS_ZERO, " |__%s\n", p);
}
else {
/* Its TGID has not been displayed */
cprintf_s(IS_STR, " (%s)", plist->tgid->comm);
cprintf_s(IS_ZERO, "__%s\n", p);
/* We can now consider this has been the case */
plist->tgid->flags |= F_PID_DISPLAYED;
}
}
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.
* @plist Pointer on the linked list where PID is saved.
* @tgid If !=0, thread whose stats are to be read.
* @curr Index in array for current sample statistics.
*
* OUT:
* @thread_nr Number of threads of the process.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
***************************************************************************
*/
int read_proc_pid_stat(pid_t pid, struct st_pid *plist,
unsigned int *thread_nr, pid_t tgid, int curr)
{
int fd, sz, rc, commsz;
char filename[128];
static char buffer[1024 + 1];
char *start, *end;
struct pid_stats *pst = plist->pstats[curr];
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(plist->comm, start, commsz);
plist->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);
return 0;
}
/*
***************************************************************************
* Read stats from /proc/#[/task/##]/schedstat.
*
* IN:
* @pid Process whose stats are to be read.
* @plist Pointer on the linked list where PID is saved.
* @tgid If != 0, thread whose stats are to be read.
* @curr Index in array for current sample statistics.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
***************************************************************************
*/
int read_proc_pid_sched(pid_t pid, struct st_pid *plist, pid_t tgid, int curr)
{
int fd, rc = 0;
char filename[128];
static char buffer[1024 + 1];
unsigned long long wtime = 0;
struct pid_stats *pst = plist->pstats[curr];
if (tgid) {
sprintf(filename, TASK_SCHED, tgid, pid);
}
else {
sprintf(filename, PID_SCHED, pid);
}
if ((fd = open(filename, O_RDONLY)) >= 0) {
int sz;
/* 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;
if (rc < 1)
return 1;
return 0;
}
/*
*****************************************************************************
* Read stats from /proc/#[/task/##]/status.
*
* IN:
* @pid Process whose stats are to be read.
* @plist Pointer on the linked list where PID is saved.
* @tgid If != 0, thread whose stats are to be read.
* @curr Index in array for current sample statistics.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
*****************************************************************************
*/
int read_proc_pid_status(pid_t pid, struct st_pid *plist, pid_t tgid, int curr)
{
FILE *fp;
char filename[128], line[256];
struct pid_stats *pst = plist->pstats[curr];
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", &plist->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);
return 0;
}
/*
*****************************************************************************
* Read information from /proc/#[/task/##}/smaps.
*
* @pid Process whose stats are to be read.
* @plist Pointer on the linked list where PID is saved.
* @tgid If != 0, thread whose stats are to be read.
* @curr Index in array for current sample statistics.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
*****************************************************************************
*/
int read_proc_pid_smap(pid_t pid, struct st_pid *plist, pid_t tgid, int curr)
{
FILE *fp;
char filename[128], line[256];
int state = 0;
struct pid_stats *pst = plist->pstats[curr];
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);
return 0;
}
/*
*****************************************************************************
* Read process command line from /proc/#[/task/##]/cmdline.
*
* IN:
* @pid Process whose command line is to be read.
* @plist Pointer on the linked list where PID is saved.
* @tgid If != 0, thread whose stats are 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(pid_t pid, struct st_pid *plist, pid_t tgid)
{
FILE *fp;
char filename[128], line[MAX_CMDLINE_LEN];
size_t len;
int found = FALSE;
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, sizeof(line) - 1, fp);
fclose(fp);
if (len) {
int i;
for (i = len - 2; i >= 0; i--) {
if (line[i]) {
found = TRUE;
}
else if (found) {
line[i] = ' ';
}
}
strncpy(plist->cmdline, line, sizeof(plist->cmdline) - 1);
plist->cmdline[sizeof(plist->cmdline) - 1] = '\0';
}
else {
/* proc/.../cmdline was empty */
plist->cmdline[0] = '\0';
}
return 0;
}
/*
***************************************************************************
* Read stats from /proc/#[/task/##]/io.
*
* IN:
* @pid Process whose stats are to be read.
* @plist Pointer on the linked list where PID is saved.
* @tgid If != 0, thread whose stats are to be read.
* @curr Index in array for current sample statistics.
*
* 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(pid_t pid, struct st_pid *plist, pid_t tgid, int curr)
{
FILE *fp;
char filename[128], line[256];
struct pid_stats *pst = plist->pstats[curr];
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! */
plist->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);
plist->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.
* @plist Pointer on the linked list where PID is saved.
* @tgid If != 0, thread whose stats are to be read.
* @curr Index in array for current sample statistics.
*
* 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(pid_t pid, struct st_pid *plist, pid_t tgid, int curr)
{
DIR *dir;
struct dirent *drp;
char filename[128];
struct pid_stats *pst = plist->pstats[curr];
if (tgid) {
sprintf(filename, TASK_FD, tgid, pid);
}
else {
sprintf(filename, PID_FD, pid);
}
if ((dir = opendir(filename)) == NULL) {
/* Cannot read fd directory */
plist->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);
plist->flags &= ~F_NO_PID_FD;
return 0;
}
/*
***************************************************************************
* Read various stats for given PID.
*
* IN:
* @pid Process whose stats are to be read.
* @plist Pointer on the linked list where PID is saved.
* @tgid If !=0, thread whose stats are to be read.
* @curr Index in array for current sample statistics.
*
* OUT:
* @thread_nr Number of threads of the process.
*
* RETURNS:
* 0 if stats have been successfully read, and 1 otherwise.
***************************************************************************
*/
int read_pid_stats(pid_t pid, struct st_pid *plist, unsigned int *thread_nr,
pid_t tgid, int curr)
{
if (read_proc_pid_stat(pid, plist, thread_nr, tgid, curr))
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, plist, tgid, curr);
if (DISPLAY_CMDLINE(pidflag) && !plist->cmdline[0]) {
if (read_proc_pid_cmdline(pid, plist, tgid))
return 1;
}
if (read_proc_pid_status(pid, plist, tgid, curr))
return 1;
if (DISPLAY_STACK(actflag)) {
if (read_proc_pid_smap(pid, plist, tgid, curr))
return 1;
}
if (DISPLAY_KTAB(actflag)) {
if (read_proc_pid_fd(pid, plist, tgid, curr))
return 1;
}
if (DISPLAY_IO(actflag))
/* Assume that /proc/#/task/#/io exists! */
return (read_proc_pid_io(pid, plist, tgid, curr));
return 0;
}
/*
***************************************************************************
* Read stats for threads in /proc/#/task directory.
*
* IN:
* @pid Process number whose threads stats are to be read.
* @plist Pointer on the linked list where PID is saved.
* @curr Index in array for current sample statistics.
***************************************************************************
*/
void read_task_stats(pid_t pid, struct st_pid *plist, int curr)
{
DIR *dir;
pid_t tid;
struct dirent *drp;
char filename[128];
unsigned int thr_nr;
struct st_pid *tlist;
/* 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;
}
tid = atoi(drp->d_name);
tlist = add_list_pid(&pid_list, tid, pid);
if (!tlist)
continue;
tlist->exist = TRUE;
if (read_pid_stats(tid, tlist, &thr_nr, pid, curr)) {
/* Thread doesn't exist */
tlist->exist = FALSE;
}
}
__closedir(dir);
}
/*
***************************************************************************
* Read various stats.
*
* IN:
* @curr Index in array for current sample statistics.
***************************************************************************
*/
void read_stats(int curr)
{
unsigned int thr_nr;
pid_t pid;
struct st_pid *plist;
struct stats_cpu *st_cpu;
/*
* Allocate two structures for CPU statistics.
* No need to init them (done by read_stat_cpu() function).
*/
if ((st_cpu = (struct stats_cpu *) malloc(STATS_CPU_SIZE * 2)) == NULL) {
perror("malloc");
exit(4);
}
/* Read statistics for CPUs "all" */
read_stat_cpu(st_cpu, 1);
/*
* Compute the total number of jiffies spent by all processors.
* NB: Don't add cpu_guest/cpu_guest_nice because cpu_user/cpu_nice
* already include them.
*/
tot_jiffies[curr] = st_cpu->cpu_user + st_cpu->cpu_nice +
st_cpu->cpu_sys + st_cpu->cpu_idle +
st_cpu->cpu_iowait + st_cpu->cpu_hardirq +
st_cpu->cpu_steal + st_cpu->cpu_softirq;
free(st_cpu);
if (DISPLAY_ALL_PID(pidflag)) {
DIR *dir;
struct dirent *drp;
/* 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])) {
continue;
}
pid = atoi(drp->d_name);
plist = add_list_pid(&pid_list, pid, 0);
if (!plist)
continue;
plist->exist = TRUE;
if (read_pid_stats(pid, plist, &thr_nr, 0, curr)) {
/* PID has terminated */
plist->exist = FALSE;
} else if (DISPLAY_TID(pidflag)) {
/* Read stats for threads in task subdirectory */
read_task_stats(pid, plist, curr);