-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn2stat-linux.c
More file actions
1264 lines (1119 loc) · 29.3 KB
/
n2stat-linux.c
File metadata and controls
1264 lines (1119 loc) · 29.3 KB
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
#include "n2stat.h"
#include "n2config.h"
#include "n2args.h"
#include "n2encoding.h"
#include "tproc.h"
#include "datatypes.h"
#include "n2malloc.h"
#include <sys/time.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <pwd.h>
#include <stdlib.h>
#include <utmp.h>
#include <syslog.h>
#include <sys/utsname.h>
#include <sys/mount.h>
#include <fcntl.h>
#ifdef DEBUG
#define dprintf printf
#else
#define dprintf //
#endif
/* ------------------------------------------------------------------------- *\
* Internal datatypes *
\* ------------------------------------------------------------------------- */
typedef struct
{
time_t lastrun;
unsigned long long total_cpu;
unsigned long long net_in;
unsigned long long net_out;
unsigned long long io_blk;
unsigned long long io_wait;
unsigned short ports[65536][3];
procrun procs;
} linuxgather_global;
static const char *afterlast (const char *in, char match)
{
const char *res = in;
const char *t;
while (t = strchr (res, match)) res = t+1;
return res;
}
/* ------------------------------------------------------------------------- *\
* Internal globals *
\* ------------------------------------------------------------------------- */
linuxgather_global GLOB;
int KMEMTOTAL;
portlist *getports (void)
{
return &GLOB.ports;
}
procrun *getprocs (void)
{
return &GLOB.procs;
}
/* ------------------------------------------------------------------------- *\
* Internal function prototypes *
\* ------------------------------------------------------------------------- */
int volume_promille_used (const char *);
void gather_mounts_getmount (n2arglist *, netload_info *, unsigned short *, int *);
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_init (void) *
* --------------------------- *
* Initializes globals. *
\* ------------------------------------------------------------------------- */
void gather_init (void)
{
FILE *F;
char buf[256];
GLOB.net_in = 0;
GLOB.net_out = 0;
GLOB.io_blk = 0;
GLOB.io_wait = 0;
GLOB.total_cpu = 0;
GLOB.lastrun = time (NULL);
procrun_init (&GLOB.procs);
GLOB.procs.ncpu = 0;
F = fopen ("/proc/cpuinfo","r");
if (F)
{
while (! feof (F))
{
buf[0] = 0;
fgets (buf, 255, F);
if (strncasecmp (buf, "processor", 9) == 0)
{
GLOB.procs.ncpu++;
}
}
fclose (F);
}
else
{
GLOB.procs.ncpu = 1;
}
if (! GLOB.procs.ncpu) GLOB.procs.ncpu = 1;
}
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_io (info) *
\* ------------------------------------------------------------------------- */
void gather_io (netload_info *inf)
{
n2arglist *split;
FILE *F;
time_t ti;
char buf[256];
unsigned long long totalblk = 0;
unsigned long long delta;
unsigned long long cpudelta;
unsigned long long totalcpu;
unsigned long long totalwait = 0;
F = fopen ("/proc/diskstats", "r");
if (F)
{
while (! feof (F))
{
buf[0] = 0;
fgets (buf, 255, F);
if (! strlen (buf)) continue;
split = make_args (buf);
if (split->argc < 14)
{
destroy_args (split);
continue;
}
totalblk += atoll (split->argv[5]);
totalblk += atoll (split->argv[9]);
destroy_args (split);
}
fclose (F);
}
else
{
F = fopen ("/proc/partitions", "r");
if (F)
{
while (! feof (F))
{
buf[0] = 0;
fgets (buf, 255, F);
if (! strlen (buf)) continue;
if (strchr (buf, '#') != NULL) continue;
split = make_args (buf);
if (split->argc < 15)
{
destroy_args (split);
continue;
}
totalblk += atoll (split->argv[6]);
totalblk += atoll (split->argv[10]);
destroy_args (split);
}
fclose (F);
}
}
F = fopen ("/proc/stat","r");
if (F)
{
fgets (buf, 255, F);
split = make_args (buf);
totalwait = atoll (split->argv[5]);
totalcpu = atoll (split->argv[1]) + atoll (split->argv[2]) +
atoll (split->argv[3]) + atoll (split->argv[4]);
destroy_args (split);
fclose (F);
}
delta = totalblk - GLOB.io_blk;
ti = time (NULL);
if (ti == GLOB.lastrun) GLOB.lastrun--;
if (GLOB.io_blk)
{
inf->diskio = delta / (ti - GLOB.lastrun);
}
else
{
inf->diskio = 0;
}
delta = totalwait - GLOB.io_wait;
cpudelta = totalcpu - GLOB.total_cpu;
if (GLOB.io_wait)
{
/* delta is in 100Hz ticks, so this works out */
inf->iowait = (100*delta) / cpudelta;
if (inf->iowait > 100) inf->iowait = 100;
}
GLOB.io_blk = totalblk;
GLOB.io_wait = totalwait;
GLOB.total_cpu = totalcpu;
GLOB.lastrun = ti;
}
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_hostdat *
* ----------------------- *
* Fill in host operating system data. *
\* ------------------------------------------------------------------------- */
void gather_hostdat (netload_info *inf)
{
FILE *F;
char buf[256];
struct utsname unamebuf;
strncpy (inf->hostname, CONF.hostname, 31);
inf->hostname[31] = 0;
inf->hosttime = time (NULL);
inf->ostype = MY_OSTYPE;
uname(&unamebuf);
inf->hwtype = HW_OTHER;
if (!strcmp(unamebuf.machine, "i386") || !strcmp(unamebuf.machine, "i686"))
{
inf->hwtype = HW_IA32;
}
else if (!strcmp(unamebuf.machine, "x86_64"))
{
inf->hwtype = HW_IA64;
}
else if (!strcmp(unamebuf.machine, "mips"))
{
inf->hwtype = HW_MIPS;
}
F = fopen ("/proc/uptime", "r");
if (F)
{
fgets (buf, 255, F);
fclose (F);
inf->uptime = atoi (buf);
}
else
{
inf->uptime = 0;
}
}
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_load (info) *
* --------------------------- *
* Fill in the loadaverage data. *
\* ------------------------------------------------------------------------- */
void gather_load (netload_info *inf)
{
FILE *F;
char buf[256];
n2arglist *arg;
char *slash;
double td;
F = fopen ("/proc/loadavg", "r");
if (F)
{
fgets (buf, 255, F);
fclose (F);
arg = make_args (buf);
td = atof (arg->argv[0]);
inf->load1 = (int) (td * (double) 100);
inf->nrun = atoi (arg->argv[3]);
slash = strchr (arg->argv[3], '/');
if (slash)
{
inf->nproc = atoi (slash+1);
}
destroy_args (arg);
}
}
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_meminfo (info) *
* ------------------------------ *
* Fill in memory/swap usage *
\* ------------------------------------------------------------------------- */
void gather_meminfo (netload_info *inf)
{
FILE *F;
char buf[256];
F = fopen ("/proc/meminfo", "r");
if (F)
{
while (! feof (F))
{
*buf = 0;
fgets (buf, 255, F);
if (strncmp (buf, "MemTotal:", 9) == 0)
{
inf->kmemtotal = KMEMTOTAL = atoi (buf+9);
}
else if (strncmp (buf, "MemFree:", 8) == 0)
{
inf->kmemfree = atoi (buf+8);
}
else if (strncmp (buf, "Buffers:", 8) == 0)
{
inf->kmemfree += atoi (buf+8);
}
else if (strncmp (buf, "Cached:", 7) == 0)
{
inf->kmemfree += atoi (buf+7);
}
else if (strncmp (buf, "SwapFree:", 9) == 0)
{
inf->kswapfree = atoi (buf+9);
}
}
fclose (F);
}
}
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_netinfo (info) *
* ------------------------------ *
* Set network interface statistics *
\* ------------------------------------------------------------------------- */
void gather_netinfo (netload_info *inf)
{
FILE *F;
char buf[256];
unsigned long long totalin;
unsigned long long totalout;
long long diffin;
long long diffout;
n2arglist *args;
char *colon;
time_t ti;
totalin = 0;
totalout = 0;
F = fopen ("/proc/net/dev", "r");
if (F)
{
fgets (buf, 255, F);
while (! feof (F))
{
*buf = 0;
fgets (buf, 255, F);
if ((*buf) && (colon = strchr (buf, ':')))
{
*colon = '\0';
colon++;
args = make_args (colon);
colon = buf;
while (isspace (*colon)) colon++;
if (! interface_configured (colon))
{
destroy_args (args);
continue;
}
totalin = totalin + (atoll (args->argv[0]) >> 7);
totalout = totalout + (atoll (args->argv[8]) >> 7);
destroy_args (args);
}
}
fclose (F);
ti = time (NULL);
if (ti == GLOB.lastrun) GLOB.lastrun--;
if (GLOB.net_in)
{
diffin = totalin - GLOB.net_in;
if (diffin < 0) diffin = 0;
diffout = totalout - GLOB.net_out;
if (diffout < 0) diffout = 0;
inf->netin = (unsigned int) ((diffin / (ti - GLOB.lastrun)) & 0x7fffffff);
inf->netout = (unsigned int) ((diffout / (ti - GLOB.lastrun)) & 0x7fffffff);
#define DELTAT (ti - GLOB.lastrun)
GLOB.net_in = totalin;
GLOB.net_out = totalout;
}
else
{
GLOB.net_in = totalin;
GLOB.net_out = totalout;
}
}
else
{
}
}
/*int diskdevice_size_in_gb (const char *devname)
{
unsigned long long bytes = 0;
int fd;
fd = open (devname, O_RDONLY);
if (fd<0) return 0;
ioctl(fd, BLKGETSIZE64, &bytes);
close (fd);
return (bytes >> 30);
}*/
int diskdevice_size_in_gb (const char *devname)
{
struct stat st;
n2arglist *args;
int iminor, imajor;
FILE *F;
char buf[256];
unsigned long long sizebytes;
if (stat (devname, &st)) return 0;
iminor = minor (st.st_rdev);
imajor = major (st.st_rdev);
F = fopen ("/proc/partitions","r");
if (!F) return 0;
while (! feof (F))
{
fgets (buf, 255, F);
buf[255] = 0;
if (*buf) buf[strlen(buf)-1] = 0;
if (! (*buf)) continue;
args = make_args (buf);
if (args->argc < 3)
{
destroy_args (args);
continue;
}
if (imajor == atoi (args->argv[0]))
{
if (iminor == atoi (args->argv[1]))
{
sizebytes = atoll (args->argv[2]);
destroy_args (args);
fclose (F);
return sizebytes/(1024*1024);
}
}
destroy_args (args);
}
fclose (F);
return 0;
}
/* ------------------------------------------------------------------------- *\
* FUNCTION volume_promille_used *
* ----------------------------- *
* A utility function that safely gets the usage information out of a *
* mounted volume, with a timeout for the eventuality that the mount 'hangs' *
* Returns a number from 0 to 1000 depicting the 0.1% step usage of the *
* volume's filesystem. 1001 indicates an error condition. *
\* ------------------------------------------------------------------------- */
int volume_promille_used (const char *volume)
{
struct statfs sfs;
int retval;
int resval;
struct timeval tv;
pid_t cpid;
pid_t rpid;
retval = 250;
resval = 1001;
/* Fork a background process to softly poke VFS */
switch ((cpid = fork()))
{
case -1:
/* uh oh */
return 1001;
case 0:
/* ANALPROBE Mk2 launched */
if (statfs (volume, &sfs) == 0)
{
retval = ((((unsigned long long)(sfs.f_blocks - sfs.f_bfree))*249) / (unsigned long long) (sfs.f_blocks+1));
exit (retval);
}
exit ((int) 250); /* Unlikely failure but hey */
default:
break;
}
tv.tv_sec = 0;
tv.tv_usec = 50000;
/* Wait for a little bit */
select (0, NULL, NULL, NULL, &tv);
/* Collect the exit value if the child is done */
rpid = waitpid (cpid, &retval, WNOHANG);
if (rpid)
{
if (WIFEXITED(retval))
{
resval = WEXITSTATUS(retval);
if (resval == 250) resval=1001;
else resval *= 4;
}
}
else /* It seems it wasn't quite done */
{
retval = 250;
/* Grant it two more seconds */
tv.tv_sec = 2;
tv.tv_usec = 0;
select (0, NULL, NULL, NULL, &tv);
rpid = waitpid (cpid, &retval, WNOHANG);
if (rpid)
{
if (WIFEXITED(retval))
{
resval = WEXITSTATUS(retval);
if (resval == 250) resval=1001;
else resval *= 4;
}
}
else /* Still no juice? */
{
resval = 1001;
/* Give it a mercy shot */
kill (cpid, 9);
/* Collect the damage */
rpid = waitpid (cpid, NULL, WNOHANG);
}
}
waitpid (-1, NULL, WNOHANG);
return resval;
}
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_mounts (info) *
* ----------------------------- *
* Get a list of mounts out of /proc/mtab and fill in the 4 most *
* heavily used volumes to return into the netload_info structure. *
\* ------------------------------------------------------------------------- */
void gather_mounts (netload_info *inf)
{
FILE *F;
char buf[256];
struct statfs sfs;
pid_t cpid;
int retval;
unsigned short usage;
unsigned short lowusage;
int lowusageidx;
const char *mountpoint;
const char *fstype;
int nmounts;
int i;
n2arglist *args;
lowusage = 1001;
inf->nmounts = 0;
F = fopen ("/proc/mounts", "r");
if (F)
{
while (!feof (F))
{
*buf = 0;
fgets (buf, 255, F);
if (*buf)
{
args = make_args (buf);
if (args->argc > 3)
{
/* only rw volumes */
if ((! strncmp (args->argv[3], "rw", 2)) &&
(strcmp (args->argv[2], "rootfs")) &&
(strcmp (args->argv[2], "proc")) &&
(strcmp (args->argv[2], "usbdevfs")) &&
(strcmp (args->argv[2], "devfs")) &&
(strcmp (args->argv[2], "tmpfs")) &&
(strcmp (args->argv[2], "usbfs")) &&
(strcmp (args->argv[2], "autofs")) &&
(strncmp (args->argv[2], "nfs", 3)) &&
(strncmp (args->argv[2], "sys", 3)) &&
(strcmp (args->argv[2], "devpts")) &&
(strncmp (args->argv[2], "binfmt_", 7)) &&
(strncmp (args->argv[2], "rpc_", 4)) &&
(strcmp (args->argv[0], "none")) &&
(strncmp (args->argv[1], "/sys", 4)) &&
(strncmp (args->argv[1], "/proc", 5)))
{
gather_mounts_getmount (args, inf, &lowusage,
&lowusageidx);
}
}
destroy_args (args);
}
}
fclose (F);
}
}
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_mounts_getmount (args, info, lowusage, lowidx) *
* -------------------------------------------------------------- *
* Checks out a specific mount (with an n2arglist containing the fields *
* as they came out of the /proc entry) and adds it into the info *
* structure if it has more priority than sitting entries or if there *
* is still an entry free. *
\* ------------------------------------------------------------------------- */
void gather_mounts_getmount (n2arglist *args, netload_info *inf,
unsigned short *lowusage, int *lowusageidx)
{
const char *origindevice;
const char *mountpoint;
const char *fstype;
int i;
unsigned short usage;
int sizegb;
int nmounts;
fstype = args->argv[2];
if (strcmp (fstype, "proc") &&
strncmp (fstype, "dev", 3) &&
strncmp (fstype, "mqueue", 7) &&
strncmp (fstype, "hugetlbfs", 10)) /* not procfs or devfsen */
{
origindevice = args->argv[0];
mountpoint = args->argv[1];
sizegb = diskdevice_size_in_gb (origindevice);
usage = volume_promille_used (mountpoint);
if (usage == 1001) return;
nmounts = inf->nmounts;
for (i=0; i<nmounts; ++i)
{
if (! strcmp (inf->mounts[i].device, origindevice)) return;
}
if (nmounts < 4)
{
#define tmnt inf->mounts[nmounts]
strncpy (tmnt.mountpoint, mountpoint, 47);
tmnt.mountpoint[47] = 0;
strncpy (tmnt.device, origindevice, 63);
tmnt.device[63] = 0;
strncpy (tmnt.fstype, fstype, 11);
tmnt.fstype[11] = 0;
tmnt.usage = usage;
tmnt.size = sizegb;
if (usage < *lowusage)
{
*lowusage = usage;
*lowusageidx = nmounts;
}
#undef tmnt
inf->nmounts++;
}
else
{
if (usage > *lowusage)
{
#define tmnt inf->mounts[*lowusageidx]
strncpy (tmnt.mountpoint, mountpoint, 47);
tmnt.mountpoint[47] = 0;
strncpy (tmnt.device, origindevice, 63);
tmnt.device[63] = 0;
strncpy (tmnt.fstype, fstype, 11);
tmnt.fstype[11] = 0;
tmnt.usage = usage;
tmnt.size = sizegb;
*lowusage = 1001;
for (i=0; i<4; ++i)
{
if (inf->mounts[i].usage < *lowusage)
{
*lowusage = inf->mounts[i].usage;
*lowusageidx = i;
}
}
#undef tmnt
}
}
}
}
/* ------------------------------------------------------------------------- *\
* FUNCTION sample_tprocs (info) *
* ----------------------------- *
* Does a single sample run of the processes found inside /proc and tracks *
* their usage. *
\* ------------------------------------------------------------------------- */
void sample_tprocs (netload_info *inf)
{
struct dirent *de;
DIR *D;
FILE *F;
pid_t pid;
unsigned long utime;
unsigned long stime;
uid_t tuid;
gid_t tgid;
struct stat st;
char buf[256];
char *c;
n2arglist *args;
struct timeval tv;
int pausetimer;
long long rss;
long long tpmem;
int pmem;
procrun_initsample (&GLOB.procs);
D = opendir ("/proc");
if (!D) return;
pausetimer = 0;
while ( (de = readdir (D)) )
{
pid = atoi (de->d_name);
pausetimer++;
if (pausetimer & 15)
{
tv.tv_sec = 0;
tv.tv_usec = 64;
select (0, NULL, NULL, NULL, &tv);
}
if (pid>0)
{
sprintf (buf, "/proc/%d", pid);
if (stat (buf, &st) == 0)
{
tuid = st.st_uid;
tgid = st.st_gid;
}
else
{
tuid = 0;
tgid = 0;
}
sprintf (buf, "/proc/%d/stat", pid);
if ( (stat(buf, &st) == 0) && (F = fopen (buf, "r")) )
{
fgets (buf, 255, F);
buf[255] = 0;
c = buf;
while (strchr (c, ')'))
c = strchr (c, ')') + 1;
/* 11=u 12=s */
args = make_args (c);
if (args->argc > 12)
{
utime = atol (args->argv[11]);
stime = atol (args->argv[12]);
}
fclose (F);
destroy_args (args);
sprintf (buf, "/proc/%d/statm", pid);
if ((F = fopen (buf, "r")))
{
memset (buf, 0, 255);
fgets (buf, 255, F);
c = strchr (buf, ' ');
if (c)
{
++c;
rss = 4 * atoll (c);
tpmem = rss;
tpmem *= 10000;
tpmem /= KMEMTOTAL;
pmem = (int) (tpmem & 0xffffffff);
}
fclose (F);
}
else
{
rss = 128000;
}
sprintf (buf, "/proc/%d/cmdline", pid);
if ((F = fopen (buf, "r")))
{
memset (buf, 0, 255);
fread (buf, 0, 255, F);
fclose (F);
if (*buf == 0)
{
sprintf (buf, "/proc/%d/status", pid);
if ((F = fopen (buf, "r")))
{
memset (buf, 0, 255);
fgets (buf, 255, F);
c = strchr (buf, ':');
if (! c)
{
fclose (F);
continue;
}
++c;
while (*c <= ' ') ++c;
if (strlen (c))
{
c[strlen(c)-1] = 0;
}
fclose (F);
procrun_setproc (&GLOB.procs, pid, utime, stime,
tuid, tgid, c, pmem);
}
}
else
{
procrun_setproc (&GLOB.procs, pid, utime, stime,
tuid, tgid, buf, pmem);
}
}
}
}
}
closedir (D);
}
/* ------------------------------------------------------------------------- *\
* FUNCTION make_top_hole (info, position) *
* --------------------------------------- *
* Internal subroutine to insert a top entry into a specific slot of the *
* tprocs array, moving other entries to the bottom. *
\* ------------------------------------------------------------------------- */
void make_top_hole (netload_info *inf, int pos)
{
int tailsz;
if (pos>(MAX_NTOP-2)) return;
tailsz = ((MAX_NTOP-1) - pos);
memmove (inf->tprocs + pos + 1,
inf->tprocs + pos,
tailsz * sizeof (netload_topentry));
if (inf->ntop < MAX_NTOP) ++inf->ntop;
}
/* ------------------------------------------------------------------------- *\
* FUNCTION gather_tprocs *
* ---------------------- *
* Gathers the statistics from the tproc sample rounds and puts them into *
* the netload_info structure. *
\* ------------------------------------------------------------------------- */
void gather_tprocs (netload_info *inf)
{
int i;
int j;
int inserted;
struct passwd *pwd;
sample_tprocs (inf);
procrun_calc (&GLOB.procs);
inf->ntop = 0;
inf->cpu = GLOB.procs.pcpu;
/* Walk through the process table */
for (i=0; i<GLOB.procs.count; ++i)
{
inserted = 0;
/* Skip reaped process slots */
if (GLOB.procs.array[i].pid)
{
/* Find a suitable spot to insert */
for (j=0; j<inf->ntop; ++j)
{
/* bloat(this) > bloat(that) ==> insert */
if (((3*inf->tprocs[j].pcpu) + (inf->tprocs[j].pmem/2)) <
((3*GLOB.procs.array[i].pcpu) + (GLOB.procs.array[i].pmem/2)))
{
// Skip if only memory is visible and < 5%
if ((GLOB.procs.array[i].pcpu == 0) &&
(GLOB.procs.array[i].pmem < 501)) continue;
make_top_hole (inf, j);
pwd = getpwuid (GLOB.procs.array[i].uid);
if (pwd)
{
strncpy (inf->tprocs[j].username, pwd->pw_name, 15);
inf->tprocs[j].username[15] = 0;
}
else
{
sprintf (inf->tprocs[j].username, "#%d",
GLOB.procs.array[i].uid);
}
inf->tprocs[j].pid = GLOB.procs.array[i].pid;
inf->tprocs[j].pcpu = GLOB.procs.array[i].pcpu;
inf->tprocs[j].pmem = GLOB.procs.array[i].pmem;
inf->tprocs[j].secrun = 0;
strncpy (inf->tprocs[j].ptitle,
GLOB.procs.array[i].ptitle, 47);
inf->tprocs[j].ptitle[47] = 0;
inserted = 1;
j = inf->ntop;
}
}
/* If no inserts have been done but there's still space, add
the entry to the bottom */
if ((! inserted) && (inf->ntop < (MAX_NTOP-2)) &&
(GLOB.procs.array[i].pcpu || (GLOB.procs.array[i].pmem > 500)))
{
dprintf ("insert pcpu=%i pmem=%i\n", GLOB.procs.array[i].pcpu, GLOB.procs.array[i].pmem);
j = inf->ntop;
inf->ntop++;
pwd = getpwuid (GLOB.procs.array[i].uid);
if (pwd)
{
strncpy (inf->tprocs[j].username, pwd->pw_name, 8);
inf->tprocs[j].username[8] = 0;
}
else
{
sprintf (inf->tprocs[j].username, "#%d",
GLOB.procs.array[i].uid);
}
inf->tprocs[j].pid = GLOB.procs.array[i].pid;
inf->tprocs[j].pcpu = GLOB.procs.array[i].pcpu;
inf->tprocs[j].pmem = GLOB.procs.array[i].pmem;
inf->tprocs[j].secrun = 0;
strncpy (inf->tprocs[j].ptitle,
GLOB.procs.array[i].ptitle, 30);
inf->tprocs[j].ptitle[31] = 0;
inserted = 1;
}
}
}
procrun_newround (&GLOB.procs);