-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfaultstat.c
1781 lines (1552 loc) · 38.8 KB
/
faultstat.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
/*
* Copyright (C) 2014-2021 Canonical, Ltd.
* Copyright (C) 2021-2025 Colin Ian King.
*
* 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 even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Author: Colin Ian King <[email protected]>
*/
#define _GNU_SOURCE
#define _XOPEN_SOURCE_EXTENDED
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <ctype.h>
#include <pwd.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <signal.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <ncurses.h>
#include <math.h>
#include <locale.h>
#define UNAME_HASH_TABLE_SIZE (521)
#define PROC_HASH_TABLE_SIZE (503)
#define OPT_CMD_SHORT (0x00000001)
#define OPT_CMD_LONG (0x00000002)
#define OPT_CMD_COMM (0x00000004)
#define OPT_CMD_ALL (OPT_CMD_SHORT | OPT_CMD_LONG | OPT_CMD_COMM)
#define OPT_DIRNAME_STRIP (0x00000008)
#define OPT_TOP (0x00000010)
#define OPT_TOP_TOTAL (0x00000020)
#define OPT_ARROW (0x00000040)
#define SORT_MAJOR_MINOR (0x00)
#define SORT_MAJOR (0x01)
#define SORT_MINOR (0x02)
#define SORT_D_MAJOR_MINOR (0x03)
#define SORT_D_MAJOR (0x04)
#define SORT_D_MINOR (0x05)
#define SORT_SWAP (0x06)
#define SORT_END (0x07)
#define ATTR_MAJOR (0x00)
#define ATTR_MINOR (0x01)
#define ATTR_D_MAJOR (0x02)
#define ATTR_D_MINOR (0x03)
#define ATTR_SWAP (0x04)
#define ATTR_MAX (0x05)
#define SIZEOF_ARRAY(a) (sizeof(a) / sizeof(a[0]))
typedef struct {
bool attr[ATTR_MAX];
} attr_vals_t;
/* process specific information */
typedef struct proc_info {
struct proc_info *next; /* next in hash */
char *cmdline; /* Process name from cmdline */
pid_t pid; /* PID */
bool kernel_thread; /* true if process is kernel thread */
} proc_info_t;
/* UID cache */
typedef struct uname_cache_t {
struct uname_cache_t *next;
char * name; /* User name */
uid_t uid; /* User UID */
} uname_cache_t;
/* page fault information per process */
typedef struct fault_info_t {
pid_t pid; /* process id */
uid_t uid; /* process' UID */
proc_info_t *proc; /* cached process info */
uname_cache_t *uname; /* cached uname info */
int64_t min_fault; /* minor page faults */
int64_t maj_fault; /* major page faults */
int64_t vm_swap; /* pages swapped */
int64_t d_min_fault; /* delta in minor page faults */
int64_t d_maj_fault; /* delta in major page faults */
struct fault_info_t *d_next; /* sorted deltas by total */
struct fault_info_t *s_next; /* sorted by total */
struct fault_info_t *next; /* for free list */
bool alive; /* true if proc is alive */
} fault_info_t;
typedef struct pid_list {
struct pid_list *next; /* next in list */
char *name; /* process name */
pid_t pid; /* process id */
} pid_list_t;
typedef struct {
void (*df_setup)(void); /* display setup */
void (*df_endwin)(void); /* display end */
void (*df_clear)(void); /* display clear */
void (*df_refresh)(void); /* display refresh */
void (*df_winsize)(const bool redo); /* display get size */
void (*df_printf)(const char *str, ...) __attribute__((format(printf, 1, 2)));
void (*df_attrset)(const int attr); /* display attribute */
} display_funcs_t;
static uname_cache_t *uname_cache[UNAME_HASH_TABLE_SIZE];
static proc_info_t *proc_cache_hash[PROC_HASH_TABLE_SIZE];
static const char *const app_name = "faultstat";
static bool stop_faultstat = false; /* set by sighandler */
static unsigned int opt_flags; /* options */
static fault_info_t *fault_info_cache; /* cache of fault infos */
static pid_list_t *pids; /* PIDs to check against */
static display_funcs_t df; /* display functions */
static bool resized; /* true when SIGWINCH occurs */
static int rows = 25; /* display rows */
static int cols = 80; /* display columns */
static int cury = 0; /* current display y position */
static int sort_by = SORT_MAJOR_MINOR; /* sort order */
static void faultstat_top_printf(const char *fmt, ...) \
__attribute__((format(printf, 1, 2)));
static void faultstat_normal_printf(const char *fmt, ...) \
__attribute__((format(printf, 1, 2)));
/*
* sort_by to column attribute highlighting mappings
*/
static const attr_vals_t attr_vals[] = {
/* Major Minor dMajor dMinor Swap */
{ { true, true, false, false, false } }, /* SORT_MAJOR_MINOR */
{ { true, false, false, false, false } }, /* SORT_MAJOR */
{ { false, true, false, false, false } }, /* SORT_MINOR */
{ { false, false, true, true, false } }, /* SORT_D_MAJOR_MINOR */
{ { false, false, true, false, false } }, /* SORT_D_MAJOR */
{ { false, false, false, true, false } }, /* SORT_D_MINOR */
{ { false, false, false, false, true } }, /* SORT_SWAP */
};
/*
* Attempt to catch a range of signals so
* we can clean
*/
static const int signals[] = {
/* POSIX.1-1990 */
#ifdef SIGHUP
SIGHUP,
#endif
#ifdef SIGINT
SIGINT,
#endif
#ifdef SIGQUIT
SIGQUIT,
#endif
#ifdef SIGFPE
SIGFPE,
#endif
#ifdef SIGTERM
SIGTERM,
#endif
#ifdef SIGUSR1
SIGUSR1,
#endif
#ifdef SIGUSR2
SIGUSR2,
/* POSIX.1-2001 */
#endif
#ifdef SIGXCPU
SIGXCPU,
#endif
#ifdef SIGXFSZ
SIGXFSZ,
#endif
/* Linux various */
#ifdef SIGIOT
SIGIOT,
#endif
#ifdef SIGSTKFLT
SIGSTKFLT,
#endif
#ifdef SIGPWR
SIGPWR,
#endif
#ifdef SIGINFO
SIGINFO,
#endif
#ifdef SIGVTALRM
SIGVTALRM,
#endif
-1,
};
/*
* pid_max_digits()
* determine (or guess) maximum digits of pids
*/
static int pid_max_digits(void)
{
static int max_digits;
ssize_t n;
int fd;
const int default_digits = 6;
const int min_digits = 6;
char buf[32];
if (max_digits)
goto ret;
max_digits = default_digits;
fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
if (fd < 0)
goto ret;
n = read(fd, buf, sizeof(buf) - 1);
(void)close(fd);
if (n < 0)
goto ret;
buf[n] = '\0';
max_digits = 0;
while ((max_digits < n) && (buf[max_digits] >= '0') && (buf[max_digits] <= '9'))
max_digits++;
if (max_digits < min_digits)
max_digits = min_digits;
ret:
return max_digits;
}
/*
* getattr()
* get attribute for a specific column, index
* is the index into the attr fields that maps
* to a specific column.
*/
static int getattr(const int index)
{
if (sort_by < 0 || sort_by >= SORT_END)
return A_NORMAL;
if (index < 0 || index >= ATTR_MAX)
return A_NORMAL;
return attr_vals[sort_by].attr[index] ? A_UNDERLINE : A_NORMAL;
}
/*
* handle_sigwinch()
* flag window resize on SIGWINCH
*/
static void handle_sigwinch(int sig)
{
(void)sig;
resized = true;
}
/*
* faultstat_noop()
* no-operation display handler
*/
static void faultstat_noop(void)
{
}
/*
* faultstat_top_setup()
* setup display for ncurses top mode
*/
static void faultstat_top_setup(void)
{
(void)initscr();
(void)cbreak();
(void)noecho();
(void)nodelay(stdscr, 1);
(void)keypad(stdscr, 1);
(void)curs_set(0);
}
/*
* faultstat_top_endwin()
* end display for ncurses top mode
*/
static void faultstat_top_endwin(void)
{
df.df_winsize(true);
(void)resizeterm(rows, cols);
(void)refresh();
resized = false;
(void)clear();
(void)endwin();
}
/*
* faultstat_top_clear()
* clear display for ncurses top mode
*/
static void faultstat_top_clear(void)
{
(void)clear();
}
/*
* faultstat_top_refresh()
* refresh display for ncurses top mode
*/
static void faultstat_top_refresh(void)
{
(void)refresh();
}
/*
* faultstat_generic_winsize()
* get tty size in all modes
*/
static void faultstat_generic_winsize(const bool redo)
{
if (redo) {
struct winsize ws;
(void)memset(&ws, 0, sizeof(ws));
if ((ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)) {
rows = ws.ws_row;
cols = ws.ws_col;
} else {
rows = 25;
cols = 80;
}
}
}
/*
* faultstat_top_winsize()
* get tty size in top mode
*/
static void faultstat_top_winsize(const bool redo)
{
(void)redo;
faultstat_generic_winsize(true);
(void)resizeterm(rows, cols);
}
/*
* faultstat_top_printf()
* print text to display width in top mode
*/
static void faultstat_top_printf(const char *fmt, ...)
{
va_list ap;
char buf[256];
int sz = sizeof(buf) - 1;
char *ptr;
if (cury >= rows)
return;
if (cols < sz)
sz = cols;
va_start(ap, fmt);
(void)vsnprintf(buf, sizeof(buf), fmt, ap);
buf[sz] = '\0';
(void)printw("%s", buf);
for (ptr = buf; *ptr; ptr++)
if (*ptr == '\n')
cury++;
va_end(ap);
}
/*
* faultstat_normal_printf()
* normal tty printf
*/
static void faultstat_normal_printf(const char *fmt, ...)
{
va_list ap;
char buf[256];
va_start(ap, fmt);
(void)vsnprintf(buf, sizeof(buf), fmt, ap);
(void)fputs(buf, stdout);
va_end(ap);
}
/*
* faultstat_top_attrset()
* set attributes for ncurses top mode
*/
static void faultstat_top_attrset(const int attr)
{
attrset(attr);
}
/*
* faultstat_normal_attrset
* set attribites for tty printf (ignored)
*/
static void faultstat_normal_attrset(const int attr)
{
(void)attr;
}
/* ncurses based "top" mode display functions */
static const display_funcs_t df_top = {
faultstat_top_setup,
faultstat_top_endwin,
faultstat_top_clear,
faultstat_top_refresh,
faultstat_top_winsize,
faultstat_top_printf,
faultstat_top_attrset,
};
/* normal tty mode display functions */
static const display_funcs_t df_normal = {
faultstat_noop,
faultstat_noop,
faultstat_noop,
faultstat_noop,
faultstat_generic_winsize,
faultstat_normal_printf,
faultstat_normal_attrset,
};
/*
* display_restore()
* restore display back to normal tty
*/
static void display_restore(void)
{
df.df_endwin();
df = df_normal;
}
/*
* out_of_memory()
* report out of memory condition
*/
static void out_of_memory(const char *msg)
{
display_restore();
(void)fprintf(stderr, "Out of memory: %s.\n", msg);
}
/*
* uname_name()
* fetch name from uname, handle
* unknown NULL unames too
*/
static inline const char *uname_name(const uname_cache_t * const uname)
{
return uname ? uname->name : "<unknown>";
}
/*
* count_bits()
*/
#if defined(__GNUC__)
/*
* use GCC built-in
*/
static inline unsigned int count_bits(const unsigned int val)
{
return __builtin_popcount(val);
}
#else
/*
* count bits set, from C Programming Language 2nd Ed
*/
static inline unsigned int OPTIMIZE3 HOT count_bits(const unsigned int val)
{
register unsigned int c, n = val;
for (c = 0; n; c++)
n &= n - 1;
return c;
}
#endif
/*
* procnamecmp()
* compare process names up to the end of string or ' '
*/
static int procnamecmp(const char *s1, const char *s2)
{
register char c1, c2;
do {
c1 = (unsigned char)*s1++;
c2 = (unsigned char)*s2++;
if (c1 == 0 || c1 == ' ')
return 0;
} while (c1 == c2);
return c1 - c2;
}
/*
* int64_to_str()
* report int64 values in different units
*/
static void int64_to_str(int64_t val, char *buf, const size_t buflen)
{
double s;
const int64_t pos_val = val < 0 ? 0 : val;
const double v = (double)pos_val;
char unit;
(void)memset(buf, 0, buflen);
if (pos_val < 1000000LL) {
s = v;
unit = ' ';
} else if (pos_val < 1000000000LL) {
s = v / 1000.0;
unit = 'k';
} else if (pos_val < 1000000000000LL) {
s = v / 1000000.0;
unit = 'M';
} else {
s = v / 1000000000.0;
unit = 'G';
}
(void)snprintf(buf, buflen, "%6.0f%c", s, unit);
}
/*
* get_pid_comm
* get comm name of a pid
*/
static char *get_pid_comm(const pid_t pid)
{
char buffer[4096];
int fd;
ssize_t ret;
(void)snprintf(buffer, sizeof(buffer), "/proc/%i/comm", pid);
if ((fd = open(buffer, O_RDONLY)) < 0)
return NULL;
if ((ret = read(fd, buffer, sizeof(buffer))) <= 0) {
(void)close(fd);
return NULL;
}
(void)close(fd);
buffer[ret - 1] = '\0';
return strdup(buffer);
}
/*
* get_pid_cmdline
* get process's /proc/pid/cmdline
*/
static char *get_pid_cmdline(const pid_t pid)
{
char buffer[4096];
char *ptr;
int fd;
ssize_t ret;
(void)snprintf(buffer, sizeof(buffer), "/proc/%i/cmdline", pid);
if ((fd = open(buffer, O_RDONLY)) < 0)
return NULL;
if ((ret = read(fd, buffer, sizeof(buffer))) <= 0) {
(void)close(fd);
return NULL;
}
(void)close(fd);
if (ret >= (ssize_t)sizeof(buffer))
ret = sizeof(buffer) - 1;
buffer[ret] = '\0';
/*
* OPT_CMD_LONG option we get the full cmdline args
*/
if (opt_flags & OPT_CMD_LONG) {
for (ptr = buffer; ptr < buffer + ret - 1; ptr++) {
if (*ptr == '\0')
*ptr = ' ';
}
*ptr = '\0';
}
/*
* OPT_CMD_SHORT option we discard anything after a space
*/
if (opt_flags & OPT_CMD_SHORT) {
for (ptr = buffer; *ptr && (ptr < buffer + ret); ptr++) {
if (*ptr == ' ')
*ptr = '\0';
}
}
if (opt_flags & OPT_DIRNAME_STRIP) {
char *base = buffer;
for (ptr = buffer; *ptr; ptr++) {
if (isblank(*ptr))
break;
if (*ptr == '/')
base = ptr + 1;
}
return strdup(base);
}
return strdup(buffer);
}
/*
* pid_exists()
* true if given process with given pid exists
*/
static bool pid_exists(const pid_t pid)
{
char path[PATH_MAX];
struct stat statbuf;
(void)snprintf(path, sizeof(path), "/proc/%i", pid);
return stat(path, &statbuf) == 0;
}
/*
* proc_cache_hash_pid()
* hash a process id
*/
static inline unsigned long proc_cache_hash_pid(const pid_t pid)
{
const unsigned long h = (unsigned long)pid;
return h % PROC_HASH_TABLE_SIZE;
}
/*
* proc_cache_add_at_hash_index()
* helper function to add proc info to the proc cache and list
*/
static proc_info_t *proc_cache_add_at_hash_index(
const unsigned long h,
const pid_t pid)
{
proc_info_t *p;
if ((p = calloc(1, sizeof(*p))) == NULL) {
out_of_memory("allocating proc cache");
return NULL;
}
p->pid = pid;
p->cmdline = get_pid_cmdline(pid);
if (p->cmdline == NULL)
p->kernel_thread = true;
if ((p->cmdline == NULL) || (opt_flags & OPT_CMD_COMM)) {
if (p->cmdline)
free(p->cmdline);
p->cmdline = get_pid_comm(pid);
}
p->next = proc_cache_hash[h];
proc_cache_hash[h] = p;
return p;
}
/*
* proc_cache_find_by_pid()
* find process info by the process id, if it is not found
* and it is a traceable process then cache it
*/
static proc_info_t *proc_cache_find_by_pid(const pid_t pid)
{
const unsigned long h = proc_cache_hash_pid(pid);
proc_info_t *p;
for (p = proc_cache_hash[h]; p; p = p->next)
if (p->pid == pid)
return p;
/*
* Not found, so add it and return it if it is a legitimate
* process to trace
*/
if (!pid_exists(pid))
return NULL;
return proc_cache_add_at_hash_index(h, pid);
}
/*
* proc_cache_cleanup()
* free up proc cache hash table
*/
static void proc_cache_cleanup(void)
{
size_t i;
for (i = 0; i < PROC_HASH_TABLE_SIZE; i++) {
proc_info_t *p = proc_cache_hash[i];
while (p) {
proc_info_t *next = p->next;
free(p->cmdline);
free(p);
p = next;
}
}
}
/*
* timeval_to_double
* timeval to a double
*/
static inline double timeval_to_double(const struct timeval * const tv)
{
return (double)tv->tv_sec + ((double)tv->tv_usec / 1000000.0);
}
/*
* double_to_timeval
* seconds in double to timeval
*/
static inline void double_to_timeval(
const double val,
struct timeval * const tv)
{
tv->tv_sec = val;
tv->tv_usec = (val - (time_t)val) * 1000000.0;
}
/*
* gettime_to_double()
* get time as a double
*/
static double gettime_to_double(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) {
display_restore();
(void)fprintf(stderr, "gettimeofday failed: errno=%d (%s)\n",
errno, strerror(errno));
exit(EXIT_FAILURE);
}
return timeval_to_double(&tv);
}
/*
* hash_uid()
* hash a uid
*/
static inline unsigned long hash_uid(const uid_t uid)
{
const unsigned long h = (unsigned long)uid;
return h % UNAME_HASH_TABLE_SIZE;
}
/*
* uname_cache_find()
* lookup uname info on uid and cache data
*/
static uname_cache_t *uname_cache_find(const uid_t uid)
{
struct passwd *pw;
uname_cache_t *uname;
const unsigned long h = hash_uid(uid);
for (uname = uname_cache[h]; uname; uname = uname->next) {
if (uname->uid == uid)
return uname;
}
if ((uname = calloc(1, sizeof(*uname))) == NULL) {
out_of_memory("allocating pwd cache item");
return NULL;
}
if ((pw = getpwuid(uid)) == NULL) {
char buf[16];
(void)snprintf(buf, sizeof(buf), "%i", uid);
uname->name = strdup(buf);
} else {
uname->name = strdup(pw->pw_name);
}
if (uname->name == NULL) {
out_of_memory("allocating pwd cache item");
free(uname);
return NULL;
}
uname->uid = uid;
uname->next = uname_cache[h];
uname_cache[h] = uname;
return uname;
}
/*
* uname_cache_cleanup()
* free cache
*/
static void uname_cache_cleanup(void)
{
size_t i;
for (i = 0; i < UNAME_HASH_TABLE_SIZE; i++) {
uname_cache_t *u = uname_cache[i];
while (u) {
uname_cache_t *next = u->next;
free(u->name);
free(u);
u = next;
}
}
}
/*
* fault_cache_alloc()
* allocate a fault_info_t, first try the cache of
* unused fault_info's, if none available fall back
* to calloc
*/
static fault_info_t *fault_cache_alloc(void)
{
fault_info_t *fault_info;
if (fault_info_cache) {
fault_info = fault_info_cache;
fault_info_cache = fault_info_cache->next;
(void)memset(fault_info, 0, sizeof(*fault_info));
return fault_info;
}
if ((fault_info = calloc(1, sizeof(*fault_info))) == NULL) {
out_of_memory("allocating page fault tracking information");
return NULL;
}
return fault_info;
}
/*
* fault_cache_free()
* free a fault_info_t by just adding it to the
* fault_info_cache free list
*/
static inline void fault_cache_free(fault_info_t * const fault_info)
{
fault_info->next = fault_info_cache;
fault_info_cache = fault_info;
}
/*
* fault_cache_free_list()
* free up a list of fault_info_t items by
* adding them to the fault_info_cache free list
*/
static void fault_cache_free_list(fault_info_t *fault_info)
{
while (fault_info) {
fault_info_t *next = fault_info->next;
fault_cache_free(fault_info);
fault_info = next;
}
}
/*
* fault_cache_prealloc()
* create some spare fault_info_t items on
* the free list so that we don't keep on
* hitting the heap during the run
*/
static void fault_cache_prealloc(const size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
fault_info_t *fault_info;
if ((fault_info = calloc(1, sizeof(*fault_info))) != NULL)
fault_cache_free_list(fault_info);
}
}
/*
* fault_cache_cleanup()
* free the fault_info_cache free list
*/
static void fault_cache_cleanup(void)
{
while (fault_info_cache) {
fault_info_t *next = fault_info_cache->next;
free(fault_info_cache);
fault_info_cache = next;
}
}
/*
* get_proc_self_stat_field()
* find nth field of /proc/$PID/stat data. This works around
* the problem that the comm field can contain spaces and
* multiple ) so sscanf on this field won't work. The returned
* pointer is the start of the Nth field and it is up to the
* caller to determine the end of the field
*/
static const char *get_proc_self_stat_field(const char *buf, const int num)
{
const char *ptr, *comm_end;
int n;
if (num < 1 || !buf || !*buf)
return NULL;
if (num == 1)
return buf;
if (num == 2)
return strstr(buf, "(");
comm_end = NULL;
for (ptr = buf; *ptr; ptr++) {
if (*ptr == ')')
comm_end = ptr;
}
if (!comm_end)
return NULL;
comm_end++;
n = num - 2;
ptr = comm_end;
while (*ptr) {
while (*ptr == ' ')
ptr++;
n--;
if (n <= 0)
break;
while (*ptr && *ptr != ' ')
ptr++;
}
return ptr;
}
/*
* fault_get_by_proc()
* get page fault info for a specific proc
*/
static int fault_get_by_proc(const pid_t pid, fault_info_t ** const fault_info)
{
FILE *fp;
fault_info_t *new_fault_info;
proc_info_t *proc;
unsigned long min_fault, maj_fault, vm_swap;
int n;
char buffer[4096];
char path[PATH_MAX];
const char *ptr;
int got_fields = 0;
if (getpgid(pid) == 0)
return 0; /* Kernel thread */