-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlmon16j.c
8583 lines (8109 loc) · 262 KB
/
lmon16j.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
/*
* lmon.c -- Curses based Performance Monitor for Linux
* with saving performance stats to a CSV file mode.
* Developer: Nigel Griffiths.
* (C) Copyright 2009 Nigel Griffiths
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 3 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, see <http://www.gnu.org/licenses/>.
*/
/*
* Use the following Makefile (for Linux on POWER)
CFLAGS=-g -Wall -D POWER
LDFLAGS=-lcurses -lm
nmon: lmon.o
* end of Makefile
*/
/* Other #ifdef's for specific features or platforms
Platforms: POWER MAINFRAME X86 ARM - Mandatory one of these at a time
Specific Linux versions: RHEL7 SLES113 SLES12
Specific feature: NVIDIA_GPU
Bug / missing feature workarounds:
REREAD - for RHEL3
LSBLK_NO_TYPE - SLES11.3 has not lsblk disk TYPE option
Options which should always but switched on:
SMALLMEM - removes huge memory, dirty, whritebak, mapped, slab, pagethreads as not in older kernels
PRE_KERNEL_2_6_18 1 kernel levels before removed the following to the disk stats
pi_num_threads,
pi_rt_priority,
pi_policy,
pi_delayacct_blkio_ticks
*/
/* note: RAW assumes you are using the index "i" to select the CPU */
#define RAW(member) (long)((long)(p->cpuN[i].member) - (long)(q->cpuN[i].member))
#define RAWTOTAL(member) (long)((long)(p->cpu_total.member) - (long)(q->cpu_total.member))
#define VERSION "16j"
char version[] = VERSION;
static char *SccsId = "nmon " VERSION;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <ncurses.h>
#include <signal.h>
#include <pwd.h>
#include <fcntl.h>
#include <math.h>
#include <time.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/wait.h>
/* Windows moved here so they can be cleared when the screen mode changes */
WINDOW *padwelcome = NULL;
WINDOW *padtop = NULL;
WINDOW *padmem = NULL;
WINDOW *padlarge = NULL;
WINDOW *padpage = NULL;
WINDOW *padker = NULL;
WINDOW *padnet = NULL;
WINDOW *padneterr = NULL;
WINDOW *padnfs = NULL;
WINDOW *padres = NULL;
WINDOW *padsmp = NULL;
WINDOW *padutil = NULL;
WINDOW *padwide = NULL;
WINDOW *padgpu = NULL;
WINDOW *padmhz = NULL;
WINDOW *padlong = NULL;
WINDOW *paddisk = NULL;
WINDOW *paddg = NULL;
WINDOW *padmap = NULL;
WINDOW *padjfs = NULL;
#ifdef POWER
WINDOW *padlpar = NULL;
#endif
WINDOW *padverb = NULL;
WINDOW *padhelp = NULL;
/* for Disk Busy rain style output covering 100's of diskss on one screen */
const char disk_busy_map_ch[] =
"_____.....----------++++++++++oooooooooo0000000000OOOOOOOOOO8888888888XXXXXXXXXX##########@@@@@@@@@@*";
/*"00000555551111111111222222222233333333334444444444555555555566666666667777777777888888888899999999991"*/
int extended_disk = 0; /* report additional data from /proc/diskstats to spreadsheet output */
#define FLIP(variable) if(variable) variable=0; else variable=1;
#ifdef MALLOC_DEBUG
#define MALLOC(argument) mymalloc(argument,__LINE__)
#define FREE(argument) myfree(argument,__LINE__)
#define REALLOC(argument1,argument2) myrealloc(argument1,argument2,__LINE__)
void *mymalloc(int size, int line)
{
void *ptr;
ptr = malloc(size);
fprintf(stderr, "0x%x = malloc(%d) at line=%d\n", ptr, size, line);
return ptr;
}
void myfree(void *ptr, int line)
{
fprintf(stderr, "free(0x%x) at line=%d\n", ptr, line);
free(ptr);
}
void *myrealloc(void *oldptr, int size, int line)
{
void *ptr;
ptr = realloc(oldptr, size);
fprintf(stderr, "0x%x = realloc(0x%x, %d) at line=%d\n", ptr, oldptr,
size, line);
return ptr;
}
#else
#define MALLOC(argument) malloc(argument)
#define FREE(argument) free(argument)
#define REALLOC(argument1,argument2) realloc(argument1,argument2)
#endif /* MALLOC STUFF */
#ifdef NVIDIA_GPU
typedef int nvmlReturn_t;
#define NVML_SUCCESS 0
typedef struct nvmlUtilization_st {
unsigned int gpu;
unsigned int memory;
} nvmlUtilization_t;
struct nvmlDevice_st;
typedef struct nvmlDevice_st *nvmlDevice_t;
nvmlReturn_t nvmlInit(void);
nvmlReturn_t nvmlShutdown(void);
nvmlReturn_t nvmlDeviceGetCount(unsigned int *count);
nvmlReturn_t nvmlDeviceGetHandleByIndex(unsigned int index,
nvmlDevice_t * device);
nvmlReturn_t nvmlDeviceGetUtilizationRates(nvmlDevice_t device,
nvmlUtilization_t *
utilization);
nvmlReturn_t nvmlSystemGetDriverVersion(char *version, int count);
nvmlReturn_t nvmlSystemGetNVMLVersion(char *version, int count);
nvmlReturn_t nvmlDeviceGetName(nvmlDevice_t device, char *name, int count);
nvmlReturn_t nvmlDeviceGetTemperature(nvmlDevice_t device, int type,
unsigned int *temp);
nvmlReturn_t nvmlDeviceGetPowerUsage(nvmlDevice_t device,
unsigned int *watts);
nvmlReturn_t nvmlDeviceGetClockInfo(nvmlDevice_t device, int type,
unsigned int *mhz);
#define NVML_TEMPERATURE_GPU 0
#define NVML_CLOCK_GRAPHICS 0
nvmlDevice_t gpu_device[4];
nvmlUtilization_t gpu_util[4];
unsigned int gpu_devices;
char gpu_name[4][1024];
unsigned int gpu_temp[4];
unsigned int gpu_watts[4];
unsigned int gpu_clock[4];
char gpu_driver_version[1024];
char gpu_nvml_version[1024];
int first_time_gpu = 1;
void gpu_init()
{
int i;
nvmlReturn_t nvres;
if ((nvres = nvmlInit()) != NVML_SUCCESS) {
printf("nvmlInit failed %d\n", nvres);
return;
}
if ((nvres =
nvmlSystemGetDriverVersion(&gpu_driver_version[0],
1024)) != NVML_SUCCESS) {
printf("nvmlSystemGetDriverVersion failed %d\n", nvres);
return;
}
if ((nvres =
nvmlSystemGetNVMLVersion(&gpu_nvml_version[0],
1024)) != NVML_SUCCESS) {
printf("nvmlSystemGetDriverVersion failed %d\n", nvres);
return;
}
if ((nvres = nvmlDeviceGetCount(&gpu_devices)) != NVML_SUCCESS) {
printf("nvmlDeviceGetCount failed %d\n", nvres);
return;
}
if (gpu_devices > 4)
gpu_devices = 4;
for (i = 0; i < gpu_devices; i++) {
if (nvmlDeviceGetHandleByIndex(i, &gpu_device[i]) != NVML_SUCCESS) {
printf("nvmlDeviceGetHandleByIndex(%d) failed %d\n", i, nvres);
return;
}
}
}
#endif /* NVIDIA_GPU */
#define P_CPUINFO 0
#define P_STAT 1
#define P_VERSION 2
#define P_MEMINFO 3
#define P_UPTIME 4
#define P_LOADAVG 5
#define P_NFS 6
#define P_NFSD 7
#define P_VMSTAT 8 /* new in 13h */
#define P_NUMBER 9 /* one more than the max */
char *month[12] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
};
/* Cut of everything after the first space in callback
* Delete any '&' just before the space
*/
char *check_call_string(char *callback, const char *name)
{
char *tmp_ptr = callback;
if (strlen(callback) > 256) {
fprintf(stderr, "ERROR nmon: ignoring %s - too long\n", name);
return (char *) NULL;
}
for (; *tmp_ptr != '\0' && *tmp_ptr != ' ' && *tmp_ptr != '&';
++tmp_ptr);
*tmp_ptr = '\0';
if (tmp_ptr == callback)
return (char *) NULL;
else
return callback;
}
/* Remove error output to this buffer and display it if NMONDEBUG=1 */
char errorstr[70];
int error_on = 0;
void error(char *err)
{
strncpy(errorstr, err, 69);
errorstr[69] = 0;
}
/*
* lscpu command output save
*/
int lscpu_available = 0;
struct {
char *arch;
char *byte_order;
#define ORDER_UNKNOWN 0
#define ORDER_LITTLE 1
#define ORDER_BIG 2
int order;
int cpus;
char *cpu_online;
char *cpu_offline;
int threads;
int cores;
int sockets;
int numa_nodes;
char *model;
char *model_name;
int mhz;
int mhz_min;
int mhz_max;
} lscpu;
void lscpu_init()
{
FILE *pop;
int len;
int data_col = 21;
#define LSCPU_STRLEN 512
char tmpstr[LSCPU_STRLEN + 1];
if (lscpu_available == 1)
return;
pop = popen("/usr/bin/lscpu 2>/dev/null", "r");
if (pop != NULL) {
lscpu_available = 1;
tmpstr[0] = 0;
while (fgets(tmpstr, LSCPU_STRLEN, pop) != NULL) {
tmpstr[strlen(tmpstr) - 1] = 0; /* remove newline */
if (strncmp("Architecture:", tmpstr, strlen("Architecture:")) == 0) {
/* Architecture: SOMETHING OR OTHER
0123456789012345678901
|-> ^ */
/* start from char after the :<space> looking for some leters or numbers */
len = strlen(tmpstr);
for(data_col=14;data_col<len;data_col++) {
if(isalnum(tmpstr[data_col]))
break;
}
lscpu.arch = MALLOC(strlen(&tmpstr[data_col]) + 1);
strcpy(lscpu.arch, &tmpstr[data_col]);
}
if (strncmp("Byte Order:", tmpstr, strlen("Byte Order:")) == 0) {
lscpu.byte_order = MALLOC(strlen(&tmpstr[data_col]) + 1);
strcpy(lscpu.byte_order, &tmpstr[data_col]);
if (strncmp
("Little Endian", lscpu.byte_order,
strlen("Little Endian")) == 0) {
lscpu.order = ORDER_LITTLE;
}
if (strncmp
("Big Endian", lscpu.byte_order,
strlen("Big Endian")) == 0) {
lscpu.order = ORDER_BIG;
}
}
if (strncmp("CPU(s):", tmpstr, strlen("CPU(s):")) == 0) {
sscanf(&tmpstr[data_col], "%d", &lscpu.cpus);
}
if (strncmp
("On-line CPU(s) list:", tmpstr,
strlen("On-line CPU(s) list:")) == 0) {
lscpu.cpu_online = MALLOC(strlen(&tmpstr[data_col]) + 1);
strcpy(lscpu.cpu_online, &tmpstr[data_col]);
}
if (strncmp
("Off-line CPU(s) list:", tmpstr,
strlen("Off-line CPU(s) list:")) == 0) {
lscpu.cpu_offline =
MALLOC(strlen(&tmpstr[data_col]) + 1);
strcpy(lscpu.cpu_offline, &tmpstr[data_col]);
}
if (strncmp("Model:", tmpstr, strlen("Model:")) == 0) {
lscpu.model = MALLOC(strlen(&tmpstr[data_col]) + 1);
strcpy(lscpu.model, &tmpstr[data_col]);
}
if (strncmp("Model name:", tmpstr, strlen("Model name:")) == 0) {
lscpu.model_name = MALLOC(strlen(&tmpstr[data_col]) + 1);
strcpy(lscpu.model_name, &tmpstr[data_col]);
}
if (strncmp
("Thread(s) per core:", tmpstr,
strlen("Thread(s) per core:")) == 0) {
sscanf(&tmpstr[data_col], "%d", &lscpu.threads);
}
if (strncmp
("Core(s) per socket:", tmpstr,
strlen("Core(s) per socket:")) == 0) {
sscanf(&tmpstr[data_col], "%d", &lscpu.cores);
}
if (strncmp("Socket(s):", tmpstr, strlen("Socket(s):")) == 0) {
sscanf(&tmpstr[data_col], "%d", &lscpu.sockets);
}
if (strncmp("NUMA node(s):", tmpstr, strlen("NUMA node(s):"))
== 0) {
sscanf(&tmpstr[data_col], "%d", &lscpu.numa_nodes);
}
if (strncmp("CPU MHz:", tmpstr, strlen("CPU MHz:")) == 0) {
sscanf(&tmpstr[data_col], "%d", &lscpu.mhz);
}
if (strncmp("CPU max MHz:", tmpstr, strlen("CPU max MHz:")) ==
0) {
sscanf(&tmpstr[data_col], "%d", &lscpu.mhz_max);
}
if (strncmp("CPU min MHz:", tmpstr, strlen("CPU min MHz:")) ==
0) {
sscanf(&tmpstr[data_col], "%d", &lscpu.mhz_min);
}
}
pclose(pop);
}
}
/* Maximum number of lines in /proc files */
/* Intel already has 26 (so here 30) per Hypterthread CPU (max 128*2 CPUs here) */
/* POWER has only 6 to 7 lines but gets 1536 SMT threads soon */
/* Erring on the saf side below */
#define PROC_MAXLINES (16*1024) /*MAGIC COOKIE WARNING */
int proc_cpu_done = 0; /* Flag if we have run function proc_cpu() already in this interval */
int reread = 0;
struct {
FILE *fp;
char *filename;
int size;
int lines;
char *line[PROC_MAXLINES];
char *buf;
int read_this_interval; /* track updates for each update to stop double data collection */
} proc[P_NUMBER];
void proc_init()
{
proc[P_CPUINFO].filename = "/proc/cpuinfo";
proc[P_STAT].filename = "/proc/stat";
proc[P_VERSION].filename = "/proc/version";
proc[P_MEMINFO].filename = "/proc/meminfo";
proc[P_UPTIME].filename = "/proc/uptime";
proc[P_LOADAVG].filename = "/proc/loadavg";
proc[P_NFS].filename = "/proc/net/rpc/nfs";
proc[P_NFSD].filename = "/proc/net/rpc/nfsd";
proc[P_VMSTAT].filename = "/proc/vmstat";
}
void proc_read(int num)
{
int i;
int size;
int found;
char buf[1024];
if (proc[num].read_this_interval == 1)
return;
if (proc[num].fp == 0) {
if ((proc[num].fp = fopen(proc[num].filename, "r")) == NULL) {
snprintf(buf, 1024,"failed to open file %s", proc[num].filename);
error(buf);
proc[num].fp = 0;
return;
}
}
rewind(proc[num].fp);
/* We re-read P_STAT, now flag proc_cpu() that it has to re-process that data */
if (num == P_STAT)
proc_cpu_done = 0;
if (proc[num].size == 0) {
/* first time so allocate initial now */
proc[num].buf = MALLOC(512);
proc[num].size = 512;
}
for (i = 0; i < 4096; i++) { /* MAGIC COOKIE WARNING POWER8 default install can have 2655 processes */
size = fread(proc[num].buf, 1, proc[num].size - 1, proc[num].fp);
if (size < proc[num].size - 1)
break;
proc[num].size += 512;
proc[num].buf = REALLOC(proc[num].buf, proc[num].size);
rewind(proc[num].fp);
}
proc[num].buf[size] = 0;
proc[num].lines = 0;
proc[num].line[0] = &proc[num].buf[0];
if (num == P_VERSION) {
found = 0;
for (i = 0; i < size; i++) { /* remove some weird stuff found the hard way in various Linux versions and device drivers */
/* place ") (" on two lines */
if (found == 0 &&
proc[num].buf[i] == ')' &&
proc[num].buf[i + 1] == ' ' &&
proc[num].buf[i + 2] == '(') {
proc[num].buf[i + 1] = '\n';
found = 1;
} else {
/* place ") #" on two lines */
if (proc[num].buf[i] == ')' &&
proc[num].buf[i + 1] == ' ' &&
proc[num].buf[i + 2] == '#') {
proc[num].buf[i + 1] = '\n';
}
/* place "#1" on two lines */
if (proc[num].buf[i] == '#' && proc[num].buf[i + 2] == '1') {
proc[num].buf[i] = '\n';
}
}
}
}
for (i = 0; i < size; i++) {
/* replace Tab characters with space */
if (proc[num].buf[i] == '\t') {
proc[num].buf[i] = ' ';
} else if (proc[num].buf[i] == '\n') {
/* replace newline characters with null */
proc[num].lines++;
proc[num].buf[i] = '\0';
proc[num].line[proc[num].lines] = &proc[num].buf[i + 1];
}
if (proc[num].lines == PROC_MAXLINES - 1)
break;
}
if (reread) {
fclose(proc[num].fp);
proc[num].fp = 0;
}
/* Set flag so we do not re-read the data even if called multiple times in same interval */
proc[num].read_this_interval = 1;
}
#include <dirent.h>
struct procsinfo {
int pi_pid;
char pi_comm[64];
char pi_state;
int pi_ppid;
int pi_pgrp;
int pi_session;
int pi_tty_nr;
int pi_tty_pgrp;
unsigned long pi_flags;
unsigned long pi_minflt;
unsigned long pi_cmin_flt;
unsigned long pi_majflt;
unsigned long pi_cmaj_flt;
unsigned long pi_utime;
unsigned long pi_stime;
long pi_cutime;
long pi_cstime;
long pi_pri;
long pi_nice;
#ifdef PRE_KERNEL_2_6_18
long junk /* removed */ ;
#else
long pi_num_threads;
#endif
long pi_it_real_value;
unsigned long pi_start_time;
unsigned long pi_vsize;
long pi_rss; /* - 3 */
unsigned long pi_rlim_cur;
unsigned long pi_start_code;
unsigned long pi_end_code;
unsigned long pi_start_stack;
unsigned long pi_esp;
unsigned long pi_eip;
/* The signal information here is obsolete. */
unsigned long pi_pending_signal;
unsigned long pi_blocked_sig;
unsigned long pi_sigign;
unsigned long pi_sigcatch;
unsigned long pi_wchan;
unsigned long pi_nswap;
unsigned long pi_cnswap;
int pi_exit_signal;
int pi_cpu;
#ifndef PRE_KERNEL_2_6_18
unsigned long pi_rt_priority;
unsigned long pi_policy;
unsigned long long pi_delayacct_blkio_ticks;
#endif
unsigned long statm_size; /* total program size */
unsigned long statm_resident; /* resident set size */
unsigned long statm_share; /* shared pages */
unsigned long statm_trs; /* text (code) */
unsigned long statm_drs; /* data/stack */
unsigned long statm_lrs; /* library */
unsigned long statm_dt; /* dirty pages */
unsigned long long read_io; /* storage read bytes */
unsigned long long write_io; /* storage write bytes */
};
int isroot = 0;
#include <mntent.h>
#include <fstab.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <net/if.h>
int debug = 0;
time_t timer; /* used to work out the hour/min/second */
/* Counts of resources */
int cpus = 1; /* number of CPUs in system (lets hope its more than zero!) */
#if X86 || ARM
int cores = 0;
int siblings = 0;
int processorchips = 0;
int hyperthreads = 0;
char *vendor_ptr = "not-set";
char *model_ptr = "not-set";
char *mhz_ptr = "not-set";
char *bogo_ptr = "not-set";
#endif
int old_cpus = 1; /* Number of CPU seen in previuos interval */
int max_cpus = 1; /* highest number of CPUs in DLPAR */
int networks = 0; /* number of networks in system */
int partitions = 0; /* number of partitions in system */
int partitions_short = 0; /* partitions file data short form (i.e. data missing) */
int disks = 0; /* number of disks in system */
int seconds = -1; /* pause interval */
int maxloops = -1; /* stop after this number of updates */
char hostname[256];
char run_name[256];
int run_name_set = 0;
char fullhostname[256];
int loop;
#define DPL 150 /* Disks per line for file output to ensure it
does not overflow the spreadsheet input line max */
int disks_per_line = DPL;
#define NEWDISKGROUP(disk) ( (disk) % disks_per_line == 0)
/* Mode of output variables */
int show_aaa = 1;
int show_para = 1;
int show_headings = 1;
int show_res = 0;
int show_smp = 0;
int show_util = 0;
int first_util = 1;
int show_wide = 0;
int show_gpu = 0;
int show_mhz = 0;
int show_longterm = 0;
int show_disk = 0;
#define SHOW_DISK_NONE 0
#define SHOW_DISK_STATS 1
#define SHOW_DISK_GRAPH 2
int show_diskmap = 0;
int show_memory = 0;
int show_large = 0;
int show_kernel = 0;
int show_nfs = 0;
int show_net = 0;
int show_neterror = 0;
int show_help = 0;
int show_top = 0;
int show_topmode = 1;
#define ARGS_NONE 0
#define ARGS_ONLY 1
int show_args = 0;
int show_all = 1; /* 1=all procs& disk 0=only if 1% or more busy */
int show_verbose = 0;
int show_jfs = 0;
int show_jfs_minimum = 0;
int flash_on = 0;
int first_huge = 1;
int first_steal = 1;
long huge_peak = 0;
int welcome = 1;
int dotline = 0;
int show_rrd = 0;
int show_lpar = 0;
int show_vm = 0;
int show_dgroup = 0; /* disk groups */
int auto_dgroup = 0; /* disk groups defined via -g auto */
int disk_only_mode = 0; /* disk stats shows disks only if user used -g auto */
int dgroup_loaded = 0; /* 0 = no, 1=needed, 2=loaded */
#define RRD if(show_rrd)
double ignore_procdisk_threshold = 0.1;
double ignore_io_threshold = 0.1;
/* Curses support */
#define CURSE if(cursed) /* Only use this for single line curses calls */
#define COLOUR if(colour) /* Only use this for single line colour curses calls */
int cursed = 1; /* 1 = using curses and
0 = loging output for a spreadsheet */
int colour = 1; /* 1 = using colour curses and
0 = using black and white curses (see -b flag) */
#define MVPRINTW(row,col,string) {move((row),(col)); \
attron(A_STANDOUT); \
printw(string); \
attroff(A_STANDOUT); }
FILE *fp; /* filepointer for spreadsheet output */
char *timestamp(int loop, time_t eon)
{
static char string[64];
if (show_rrd)
snprintf(string, 64, "%ld", (long) eon);
else
snprintf(string, 64, "T%04d", loop);
return string;
}
#define LOOP timestamp(loop,timer)
char *easy[5] = { "not found", 0, 0, 0, 0 };
char *lsb_release[5] = { "not found", 0, 0, 0, 0 };
void find_release()
{
FILE *pop;
int i;
char tmpstr[1024+1];
#if defined(SLES12) || defined(SLES15)
pop = popen("cat /etc/os-release 2>/dev/null", "r");
#else
pop = popen("cat /etc/*ease 2>/dev/null", "r");
#endif /* SLES12 */
if (pop != NULL) {
tmpstr[0] = 0;
for (i = 0; i < 4; i++) {
if (fgets(tmpstr, 1024, pop) == NULL)
break;
tmpstr[strlen(tmpstr) - 1] = 0; /* remove newline */
easy[i] = MALLOC(strlen(tmpstr) + 1);
strcpy(easy[i], tmpstr);
}
pclose(pop);
}
pop = popen("/usr/bin/lsb_release -idrc 2>/dev/null", "r");
if (pop != NULL) {
tmpstr[0] = 0;
for (i = 0; i < 4; i++) {
if (fgets(tmpstr, 70, pop) == NULL)
break;
tmpstr[strlen(tmpstr) - 1] = 0; /* remove newline */
lsb_release[i] = MALLOC(strlen(tmpstr) + 1);
strcpy(lsb_release[i], tmpstr);
}
pclose(pop);
}
}
/* Full Args Mode stuff here */
#define ARGSMAX 1024*8
#define CMDLEN 4096
struct {
int pid;
char *args;
} arglist[ARGSMAX];
void args_output(int pid, int loop, char *progname)
{
FILE *pop;
int i, j, n;
char tmpstr[CMDLEN];
static int arg_first_time = 1;
if (pid == 0)
return; /* ignore init */
for (i = 0; i < ARGSMAX - 1; i++) { /* clear data out */
if (arglist[i].pid == pid) {
return;
}
if (arglist[i].pid == 0) /* got to empty slot */
break;
}
snprintf(tmpstr, CMDLEN, "ps -p %d -o args 2>/dev/null", pid);
pop = popen(tmpstr, "r");
if (pop == NULL) {
return;
} else {
if (fgets(tmpstr, CMDLEN, pop) == NULL) { /* throw away header */
pclose(pop);
return;
}
tmpstr[0] = 0;
if (fgets(tmpstr, CMDLEN, pop) == NULL) {
pclose(pop);
return;
}
tmpstr[strlen(tmpstr) - 1] = 0;
if (tmpstr[strlen(tmpstr) - 1] == ' ')
tmpstr[strlen(tmpstr) - 1] = 0;
arglist[i].pid = pid;
if (arg_first_time) {
fprintf(fp, "UARG,+Time,PID,ProgName,FullCommand\n");
arg_first_time = 0;
}
n = strlen(tmpstr);
for (i = 0; i < n; i++) {
/*strip out stuff that confused Excel i.e. starting with maths symbol */
if (tmpstr[i] == ',' &&
((tmpstr[i + 1] == '-') || tmpstr[i + 1] == '+'))
tmpstr[i + 1] = '_';
/*strip out double spaces */
if (tmpstr[i] == ' ' && tmpstr[i + 1] == ' ') {
for (j = 0; j < n - i; j++)
tmpstr[i + j] = tmpstr[i + j + 1];
i--; /* rescan to remove triple space etc */
}
}
fprintf(fp, "UARG,%s,%07d,%s,%s\n", LOOP, pid, progname, tmpstr);
pclose(pop);
return;
}
}
void args_load()
{
FILE *pop;
int i;
char tmpstr[CMDLEN];
for (i = 0; i < ARGSMAX; i++) { /* clear data out */
if (arglist[i].pid == -1)
break;
if (arglist[i].pid != 0) {
arglist[i].pid = -1;
free(arglist[i].args);
}
}
pop = popen("ps -eo pid,args 2>/dev/null", "r");
if (pop == NULL) {
return;
} else {
if (fgets(tmpstr, CMDLEN, pop) == NULL) { /* throw away header */
pclose(pop);
return;
}
for (i = 0; i < ARGSMAX; i++) {
tmpstr[0] = 0;
if (fgets(tmpstr, CMDLEN, pop) == NULL) {
pclose(pop);
return;
}
tmpstr[strlen(tmpstr) - 1] = 0;
if (tmpstr[strlen(tmpstr) - 1] == ' ')
tmpstr[strlen(tmpstr) - 1] = 0;
arglist[i].pid = atoi(tmpstr);
arglist[i].args = MALLOC(strlen(tmpstr) + 1);
strcpy(arglist[i].args, &tmpstr[6]);
}
pclose(pop);
}
}
char *args_lookup(int pid, char *progname)
{
int i;
for (i = 0; i < ARGSMAX; i++) {
if (arglist[i].pid == pid)
return arglist[i].args;
if (arglist[i].pid == -1)
return progname;
}
return progname;
}
/* end args mode stuff here */
void linux_bbbp(char *name, char *cmd, char *err)
{
int i;
int j;
int len;
#define STRLEN 4096
char str[STRLEN];
FILE *pop;
static int lineno = 0;
pop = popen(cmd, "r");
if (pop == NULL) {
fprintf(fp, "BBBP,%03d,%s failed to run %s\n", lineno++, cmd, err);
} else {
fprintf(fp, "BBBP,%03d,%s\n", lineno++, name);
for (i = 0; i < 2048 && (fgets(str, STRLEN, pop) != NULL); i++) { /* 2048=sanity check only */
len = strlen(str);
if (len > STRLEN)
len = STRLEN;
if (str[len - 1] == '\n') /*strip off the newline */
str[len - 1] = 0;
/* fix text starting characters that confuses spread sheets */
if (str[0] == '+')
str[0] = 'p';
if (str[0] == '*')
str[0] = 'm';
if (str[0] == '-')
str[0] = 'n';
if (str[0] == '/')
str[0] = 'd';
if (str[0] == '=')
str[0] = 'e';
/* remove double quotes as it confuses spread sheets */
for (j = 0; str[j] != 0; j++)
if (str[j] == '"')
str[j] = 'Q';
fprintf(fp, "BBBP,%03d,%s,\"%s\"\n", lineno++, name, str);
}
pclose(pop);
}
}
#define WARNING "needs root permission or file not present"
/* Global name of programme for printing it */
char *progname;
/* Seconds since epoc and the sting version */
long long boottime = 0;
char boottime_str[64] = "not found";
/* Main data structure for collected stats.
* Two versions are previous and current data.
* Often its the difference that is printed.
* The pointers are swaped i.e. current becomes the previous
* and the previous over written rather than moving data around.
*/
struct cpu_stat { /* changed the order here to match this years kernel (man 5 /proc/stat) */
long long user;
long long nice;
long long sys;
long long idle;
long long wait; /* for IO */
long long irq;
long long softirq;
long long steal;
long long guest;
long long guest_nice;
/* below are non-cpu based numbers in the same file */
long long intr;
long long ctxt;
long long procs;
long long running;
long long blocked;
float uptime;
float idletime;
float mins1;
float mins5;
float mins15;
};
#define ulong unsigned long
struct dsk_stat {
char dk_name[32];
int dk_major;
int dk_minor;
long dk_noinfo;
ulong dk_reads;
ulong dk_rmerge;
ulong dk_rmsec;
ulong dk_rkb;
ulong dk_writes;
ulong dk_wmerge;
ulong dk_wmsec;
ulong dk_wkb;
ulong dk_xfers;
ulong dk_bsize;
ulong dk_time;
ulong dk_inflight;
ulong dk_backlog;
ulong dk_partition;
ulong dk_blocks; /* in /proc/partitions only */
ulong dk_use;
ulong dk_aveq;
};
struct mem_stat {
long memtotal;
long memfree;
long memshared;
long buffers;
long cached;
long swapcached;
long active;
long inactive;
long hightotal;
long highfree;
long lowtotal;
long lowfree;
long swaptotal;
long swapfree;
#ifndef SMALLMEM
long dirty;
long writeback;
long mapped;
long slab;
long committed_as;
long pagetables;
long hugetotal;
long hugefree;
long hugesize;
#else
long bigfree;
#endif /*SMALLMEM*/
};