-
Notifications
You must be signed in to change notification settings - Fork 0
/
showgeneric.c
2199 lines (1897 loc) · 48.9 KB
/
showgeneric.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
/*
** ATOP - System & Process Monitor
**
** The program 'atop' offers the possibility to view the activity of
** the system on system-level as well as process-level.
**
** This source-file contains the print-functions to visualize the calculated
** figures.
** ==========================================================================
** Author: Gerlof Langeveld
** E-mail: [email protected]
** Date: November 1996
** LINUX-port: June 2000
** --------------------------------------------------------------------------
** Copyright (C) 2000-2010 Gerlof Langeveld
**
** 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, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
** --------------------------------------------------------------------------
**
** $Log: showgeneric.c,v $
** Revision 1.71 2010/10/25 19:08:32 gerlof
** When the number of lines is too small for the system-level
** lines, limit the number of variable resources automatically
** to a minimum.
**
** Revision 1.70 2010/10/23 14:04:05 gerlof
** Counters for total number of running and sleep threads (JC van Winkel).
**
** Revision 1.69 2010/04/23 12:19:35 gerlof
** Modified mail-address in header.
**
** Revision 1.68 2010/04/23 09:58:11 gerlof
** Version (flag -V) handled earlier after startup.
**
** Revision 1.67 2010/04/23 07:57:32 gerlof
** Proper sorting of processes when switching from single process view
** to cumulative view (key 'u' or 'p') and vice versa.
**
** Revision 1.66 2010/04/17 17:20:26 gerlof
** Allow modifying the layout of the columns in the system lines.
**
** Revision 1.65 2010/03/16 21:13:38 gerlof
** Program and user selection can be combined with program and user
** accumulation.
**
** Revision 1.64 2010/03/16 20:18:46 gerlof
** Show in header-line if user selections and program selection are active.
**
** Revision 1.63 2010/03/16 20:08:51 gerlof
** Performance improvement: only sort system-resources once per interval.
**
** Revision 1.62 2010/03/04 10:53:01 gerlof
** Support I/O-statistics on logical volumes and MD devices.
**
** Revision 1.61 2009/12/17 10:55:07 gerlof
** *** empty log message ***
**
** Revision 1.60 2009/12/17 10:50:30 gerlof
** Allow own defined process line with key 'o' and a definition
** in the atoprc file.
**
** Revision 1.59 2009/12/17 09:03:26 gerlof
** Center message "....since boot" in status line on first screen.
**
** Revision 1.58 2009/12/17 08:55:15 gerlof
** Show messages on status line in color to draw attention.
**
** Revision 1.57 2009/12/17 08:16:14 gerlof
** Introduce branch-key to go to specific time in raw file.
**
** Revision 1.56 2009/12/12 09:06:39 gerlof
** Corrected cumulated disk I/O per user/program (JC van Winkel).
**
** Revision 1.55 2009/12/10 13:34:44 gerlof
** Show which toggle-keys are active in the header line.
**
** Revision 1.54 2009/12/10 11:55:03 gerlof
** Introduce system-wide /etc/atoprc
**
** Revision 1.53 2009/12/10 09:53:08 gerlof
** Improved display of header-line (JC van Winkel).
**
** Revision 1.52 2008/03/06 10:14:01 gerlof
** Modified help-messages.
**
** Revision 1.51 2008/02/25 13:47:21 gerlof
** Bug-solution: segmentation-fault in case of invalid regular expression.
**
** Revision 1.50 2008/01/07 11:33:58 gerlof
** Cosmetic changes.
**
** Revision 1.49 2008/01/07 10:18:24 gerlof
** Implement possibility to make summaries with atopsar.
**
** Revision 1.48 2007/03/22 10:12:17 gerlof
** Support for io counters (>= kernel 2.6.20).
**
** Revision 1.47 2007/03/21 14:22:34 gerlof
** Handle io counters maintained from 2.6.20
**
** Revision 1.46 2007/03/20 11:13:15 gerlof
** Cosmetic changes.
**
** Revision 1.45 2007/03/09 12:39:59 gerlof
** Do not allow 'N' and 'D' when kernel-patch is not installed.
**
** Revision 1.44 2007/02/13 10:31:53 gerlof
** Removal of external declarations.
**
** Revision 1.43 2007/01/18 10:41:45 gerlof
** Add support for colors.
** Add support for automatic determination of most critical resource.
**
** Revision 1.42 2006/11/13 13:48:36 gerlof
** Implement load-average counters, context-switches and interrupts.
**
** Revision 1.41 2006/04/03 05:42:35 gerlof
** *** empty log message ***
**
** Revision 1.40 2006/02/07 08:28:26 gerlof
** Improve screen-handling (less flashing) by exchanging clear()
** by werase() (contribution Folkert van Heusden).
**
** Revision 1.39 2005/11/04 14:16:16 gerlof
** Minor bug-solutions.
**
** Revision 1.38 2005/10/28 09:52:03 gerlof
** All flags/subcommands are defined as macro's.
** Subcommand 'p' has been changed to 'z' (pause).
**
** Revision 1.37 2005/10/24 06:12:17 gerlof
** Flag -L modified into -l.
**
** Revision 1.36 2005/10/21 09:50:46 gerlof
** Per-user accumulation of resource consumption.
** Possibility to send signal to process.
**
** Revision 1.35 2004/12/14 15:06:41 gerlof
** Implementation of patch-recognition for disk and network-statistics.
**
** Revision 1.34 2004/09/27 11:01:13 gerlof
** Corrected usage-info as suggested by Edelhard Becker.
**
** Revision 1.33 2004/09/13 09:19:14 gerlof
** Modify subcommands (former 's' -> 'v', 'v' -> 'V', new 's').
**
** Revision 1.32 2004/08/31 09:52:47 root
** information about underlying threads.
**
** Revision 1.31 2004/06/01 11:57:58 gerlof
** Regular expressions for selections on process-name and user-name.
**
** Revision 1.30 2003/07/07 09:27:24 gerlof
** Cleanup code (-Wall proof).
**
** Revision 1.29 2003/07/03 11:16:42 gerlof
** Implemented subcommand `r' (reset).
**
** Revision 1.28 2003/06/30 11:29:49 gerlof
** Handle configuration file ~/.atoprc
**
** Revision 1.27 2003/06/24 06:21:57 gerlof
** Limit number of system resource lines.
**
** Revision 1.26 2003/02/07 10:19:18 gerlof
** Possibility to show the version number and date.
**
** Revision 1.25 2003/01/17 07:32:16 gerlof
** Show the full command-line per process (option 'c').
**
** Revision 1.24 2002/10/30 13:47:20 gerlof
** Generate notification for statistics since boot.
**
** Revision 1.23 2002/10/08 12:00:30 gerlof
** *** empty log message ***
**
** Revision 1.22 2002/09/26 14:17:39 gerlof
** No beep when resizing the window.
**
** Revision 1.21 2002/09/26 13:52:26 gerlof
** Limit header lines by not showing disks.
**
** Revision 1.20 2002/09/18 07:15:59 gerlof
** Modified viewflag to rawreadflag.
**
** Revision 1.19 2002/09/17 13:17:39 gerlof
** Allow key 'T' to be pressed to view previous sample in raw file.
**
** Revision 1.18 2002/08/30 07:11:50 gerlof
** Minor changes to support viewing of raw atop data.
**
** Revision 1.17 2002/08/27 12:10:12 gerlof
** Allow raw data file to be written and to be read (with compression).
**
** Revision 1.16 2002/07/24 11:12:46 gerlof
** Changed to ease porting to other UNIX-platforms.
**
** Revision 1.15 2002/07/11 09:12:05 root
** Some minor updates.
**
** Revision 1.14 2002/07/10 05:00:37 root
** Counters pin/pout renamed to swin/swout (Linux conventions).
**
** Revision 1.13 2002/07/08 09:29:49 root
** Limitation for username and groupname (8 characters truncate).
**
** Revision 1.12 2002/07/02 07:14:02 gerlof
** More positions for the name of the disk-unit in the DSK-line.
**
** Revision 1.11 2002/01/22 13:40:42 gerlof
** Support for number of cpu's.
** Check if the window is large enough for the system-statistics.
**
** Revision 1.10 2001/11/30 09:09:36 gerlof
** Cosmetic chnage.
**
** Revision 1.9 2001/11/29 10:41:44 gerlof
** *** empty log message ***
**
** Revision 1.8 2001/11/29 10:38:16 gerlof
** Exit-code correctly printed.
**
** Revision 1.7 2001/11/26 11:18:45 gerlof
** Modified generic output in case that the kernel-patch is not installed.
**
** Revision 1.6 2001/11/13 08:24:50 gerlof
** Show blank columns for sockets and disk I/O when no kernel-patch installed.
**
** Revision 1.5 2001/11/07 09:19:28 gerlof
** Use /proc instead of /dev/kmem for process-level statistics.
**
** Revision 1.4 2001/10/05 13:46:32 gerlof
** Implemented paging through the process-list
**
** Revision 1.3 2001/10/04 08:47:27 gerlof
** Improved handling of error-messages
**
** Revision 1.2 2001/10/03 08:57:53 gerlof
** Improved help-screen shown in scrollable window
**
** Revision 1.1 2001/10/02 10:43:34 gerlof
** Initial revision
**
*/
static const char rcsid[] = "$Id: showgeneric.c,v 1.71 2010/10/25 19:08:32 gerlof Exp $";
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <signal.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termio.h>
#include <unistd.h>
#include <stdarg.h>
#include <curses.h>
#include <pwd.h>
#include <grp.h>
#include <regex.h>
#include "atop.h"
#include "photoproc.h"
#include "photosyst.h"
#include "showgeneric.h"
#include "showlinux.h"
struct selection procsel = {"", {USERSTUB, }, "", 0, { 0, }};
static void showhelp(int);
static int paused; /* boolean: currently in pause-mode */
static int fixedhead; /* boolean: fixate header-lines */
static int usecolors=1; /* boolean: colors for high occupation */
static int avgval; /* boolean: average values i.s.o. total */
static char showtype = MPROCGEN;
static char showorder = MSORTCPU;
static int maxcpulines = 999; /* maximum cpu lines */
static int maxdsklines = 999; /* maximum disk lines */
static int maxmddlines = 999; /* maximum MDD lines */
static int maxlvmlines = 999; /* maximum LVM lines */
static int maxintlines = 999; /* maximum interface lines */
static int cumusers(struct pstat *, struct pstat *, int);
static int cumprocs(struct pstat *, struct pstat *, int);
static void limitedlines(void);
static long getnumval(char *, long, int);
static int (*procsort[])(const void *, const void *) = {
[MSORTCPU&0x1f]=compcpu,
[MSORTMEM&0x1f]=compmem,
[MSORTDSK&0x1f]=compdsk,
[MSORTNET&0x1f]=compnet,
};
extern proc_printpair ownprocs[];
/*
** print the deviation-counters on process- and system-level
*/
char
generic_samp(time_t curtime, int nsecs,
struct sstat *sstat, struct pstat *pstat,
int nact, int nproc, int ntrun, int ntslpi, int ntslpu, int nzomb,
int nexit, char flag)
{
static int callnr = 0;
register int i, curline, statline;
int firstproc = 0, plistsz, alistsz, killpid, killsig;
int lastchar;
char format1[16], format2[16], hhmm[16];
char *statmsg = NULL, lastorder=0, curorder, autoorder;
char buf[33];
struct passwd *pwd;
struct syscap syscap;
struct selection *cursel = &procsel;
static struct selection emptysel = {"", {USERSTUB, }, "", 0, { 0, }};
struct pstat *save_pstat = NULL;
int save_nact = 0;
if (callnr == 0) /* first call ? */
{
/*
** check if default sort order and/or showtype are overruled
** by commando-line flags
*/
for (i=0; flaglist[i]; i++)
{
switch (flaglist[i])
{
case MSORTAUTO:
showorder = MSORTAUTO;
break;
case MSORTCPU:
showorder = MSORTCPU;
break;
case MSORTMEM:
showorder = MSORTMEM;
break;
case MSORTDSK:
showorder = MSORTDSK;
break;
case MSORTNET:
showorder = MSORTNET;
break;
case MPROCGEN:
showtype = MPROCGEN;
showorder = MSORTCPU;
break;
case MPROCMEM:
showtype = MPROCMEM;
showorder = MSORTMEM;
break;
case MPROCSCH:
showtype = MPROCSCH;
showorder = MSORTCPU;
break;
case MPROCDSK:
if ( !(supportflags & (PATCHSTAT|IOSTAT)) )
{
fprintf(stderr,
"No disk-activity figures "
"available; request ignored\n");
sleep(3);
break;
}
showtype = MPROCDSK;
showorder = MSORTDSK;
break;
case MPROCNET:
if ( !(supportflags & PATCHSTAT) )
{
fprintf(stderr,
"No kernel-patch installed "
"(no network-statistics)\n");
sleep(3);
break;
}
showtype = MPROCNET;
showorder = MSORTNET;
break;
case MPROCVAR:
showtype = MPROCVAR;
break;
case MPROCARG:
showtype = MPROCARG;
break;
case MPROCOWN:
showtype = MPROCOWN;
break;
case MAVGVAL:
if (avgval)
avgval=0;
else
avgval=1;
break;
case MCUMUSER:
showtype = MCUMUSER;
break;
case MCUMPROC:
showtype = MCUMPROC;
break;
case MSYSFIXED:
if (fixedhead)
fixedhead=0;
else
fixedhead=1;
break;
case MCOLORS:
if (usecolors)
usecolors=0;
else
usecolors=1;
break;
case MSYSLIMIT:
limitedlines();
break;
default:
prusage("atop");
}
}
/*
** set stdout output on line-basis
*/
setvbuf(stdout, (char *)0, _IOLBF, BUFSIZ);
/*
** check if STDOUT is related to a tty or
** something else (file, pipe)
*/
if ( isatty(1) )
screen = 1;
else
screen = 0;
/*
** install catch-routine to finish in a controlled way
** and activate cbreak-mode
*/
if (screen)
{
/*
** initialize screen-handling via curses
*/
initscr();
cbreak();
noecho();
if (COLS < 30)
{
printw("Not enough columns "
"(need at least %d columns)\n", 30);
refresh();
sleep(3);
cleanstop(1);
}
if (has_colors())
{
use_default_colors();
start_color();
init_pair(COLORLOW, COLOR_GREEN, -1);
init_pair(COLORMED, COLOR_CYAN, -1);
init_pair(COLORHIGH, COLOR_RED, -1);
}
else
{
usecolors = 0;
}
}
signal(SIGINT, cleanstop);
signal(SIGTERM, cleanstop);
}
callnr++;
/*
** compute the total capacity of this system for the
** four main resources
*/
totalcap(&syscap, sstat, pstat, nact);
/*
** sort per-cpu statistics on busy percentage
** sort per-logical-volume statistics on busy percentage
** sort per-multiple-device statistics on busy percentage
** sort per-disk statistics on busy percentage
** sort per-interface statistics on busy percentage (if known)
*/
if (sstat->cpu.nrcpu > 1 && maxcpulines > 0)
qsort(sstat->cpu.cpu, sstat->cpu.nrcpu,
sizeof sstat->cpu.cpu[0], cpucompar);
if (sstat->dsk.nlvm > 1 && maxlvmlines > 0)
qsort(sstat->dsk.lvm, sstat->dsk.nlvm,
sizeof sstat->dsk.lvm[0], diskcompar);
if (sstat->dsk.nmdd > 1 && maxmddlines > 0)
qsort(sstat->dsk.mdd, sstat->dsk.nmdd,
sizeof sstat->dsk.mdd[0], diskcompar);
if (sstat->dsk.ndsk > 1 && maxdsklines > 0)
qsort(sstat->dsk.dsk, sstat->dsk.ndsk,
sizeof sstat->dsk.dsk[0], diskcompar);
if (sstat->intf.nrintf > 1 && maxintlines > 0)
qsort(sstat->intf.intf, sstat->intf.nrintf,
sizeof sstat->intf.intf[0], intfcompar);
/*
** loop in which the system resources and the list of active
** processes is shown; the loop will be preempted by receiving
** a timer-signal or when the trigger-button is pressed.
*/
while (1)
{
curline = 1;
/*
** prepare screen or file output for new sample
*/
if (screen)
werase(stdscr);
else
printf("\n\n");
/*
** print general headerlines
*/
convdate(curtime, format1); /* date to ascii string */
convtime(curtime, format2); /* time to ascii string */
if (screen)
attron(A_REVERSE);
int seclen = val2elapstr(nsecs, buf);
int lenavail = (screen ? COLS : linelen) -
41 - seclen - utsnodenamelen;
int len1 = lenavail / 3;
int len2 = lenavail - len1 - len1;
printg("ATOP - %s%*s%s %s%*s%c%c%c%c%c%c%*s%s elapsed",
utsname.nodename, len1, "",
format1, format2, len1, "",
fixedhead ? 'f' : '-',
deviatonly ? '-' : 'a',
usecolors ? '-' : 'x',
avgval ? '1' : '-',
procsel.userid[0] != USERSTUB ? 'U' : '-',
procsel.procnamesz ? 'P' : '-',
len2, "", buf);
if (screen)
{
attroff(A_REVERSE);
attroff(A_REVERSE);
}
else
{
printg("\n");
}
/*
** print cumulative system- and user-time for all processes
*/
pricumproc(pstat, sstat, nact, nproc, ntrun, ntslpi, ntslpu,
nzomb, nexit, avgval, nsecs);
curline=2;
/*
** print other lines of system-wide statistics
*/
if (showorder == MSORTAUTO)
autoorder = MSORTCPU;
else
autoorder = showorder;
curline = prisyst(sstat, curline, nsecs, avgval,
fixedhead, usecolors, &autoorder,
maxcpulines, maxdsklines, maxmddlines,
maxlvmlines, maxintlines);
/*
** if system-wide statistics do not fit,
** limit the number of variable resource lines
** and try again
*/
if (screen && curline+2 > LINES)
{
curline = 2;
move(curline, 0);
clrtobot();
move(curline, 0);
limitedlines();
curline = prisyst(sstat, curline, nsecs, avgval,
fixedhead, usecolors, &autoorder,
maxcpulines, maxdsklines, maxmddlines,
maxlvmlines, maxintlines);
/*
** if system-wide statistics still do not fit,
** the window is really to small
*/
if (curline+2 > LINES)
{
move(0, 0);
clrtobot();
move(0, 0);
printw("Not enough screen-lines available "
"(need at least %d lines)\n", curline+2);
move(1, 0);
printw("Please resize window....");
refresh();
sleep(4);
cleanstop(1);
}
else
{
statmsg = "Number of variable resources"
" limited to fit number of lines";
}
}
statline = curline;
if (screen)
move(curline, 0);
if (statmsg)
{
clrtoeol();
if (usecolors)
attron(COLOR_PAIR(COLORLOW));
printg(statmsg);
if (usecolors)
attroff(COLOR_PAIR(COLORLOW));
statmsg = NULL;
}
else
{
if (flag&RRBOOT)
{
if (screen)
{
if (usecolors)
attron(COLOR_PAIR(COLORLOW));
attron(A_BLINK);
printg("%*s", (COLS-45)/2, " ");
}
else
{
printg(" ");
}
printg("*** system and process activity "
"since boot ***");
if (screen)
{
if (usecolors)
attroff(COLOR_PAIR(COLORLOW));
attroff(A_BLINK);
}
}
}
if (screen)
plistsz = LINES-curline-2;
else
plistsz = nact;
/*
** if cumulative figures required, save the current
** list of processes and accumulate resource consumption
** of all processes in the current list
*/
if (showtype == MCUMUSER || showtype == MCUMPROC)
{
/*
** remember info based on individual processes
*/
save_pstat = pstat;
save_nact = nact;
cursel = &emptysel;
/*
** allocate space for new (temporary) list with
** one entry per user/program (list has worst-case size)
*/
pstat = calloc(sizeof(struct pstat), nact);
/*
** accumulate all processes
** return-value nact contains number of new entries
*/
switch (showtype)
{
case MCUMUSER:
nact = cumusers(save_pstat, pstat, save_nact);
break;
case MCUMPROC:
nact = cumprocs(save_pstat, pstat, save_nact);
break;
}
lastorder = 0; /* force new sort */
if (screen)
plistsz = LINES-curline-2;
else
plistsz = nact;
}
/*
** sort the list in required order
** (default CPU-consumption) and print the list
*/
if (showorder == MSORTAUTO)
curorder = autoorder;
else
curorder = showorder;
if (nact > 0 && plistsz > 0)
{
if (lastorder != curorder)
{
qsort(pstat, nact, sizeof(struct pstat),
procsort[(int)curorder&0x1f]);
lastorder = curorder;
}
if (screen) {
attron(A_REVERSE);
move(curline+1, 0);
}
/*
** print the header
** first determine the column-header for the current
** sorting order of processes
*/
priphead(firstproc/plistsz+1, (nact-1)/plistsz+1,
showtype, curorder,
showorder == MSORTAUTO ? 1 : 0);
if (screen)
{
attroff(A_REVERSE);
clrtobot();
}
/*
** print the list
*/
priproc(pstat, firstproc, nact, curline+2,
firstproc/plistsz+1, (nact-1)/plistsz+1,
showtype, curorder, &syscap, cursel,
nsecs, avgval);
}
alistsz = nact; /* preserve size of active list */
/*
** if cumulative figures per user shown,
** release temporary space and restore per-process values
*/
if (showtype == MCUMUSER || showtype == MCUMPROC)
{
free(pstat);
pstat = save_pstat;
nact = save_nact;
cursel = &procsel;
}
/*
** in case of writing to a terminal, the user can also enter
** a character to switch options, etc
*/
if (screen)
{
/*
** show blinking pause-indication if necessary
*/
if (paused)
{
move(statline, COLS-6);
attron(A_BLINK);
attron(A_REVERSE);
printw("PAUSED");
attroff(A_REVERSE);
attroff(A_BLINK);
}
/*
** await input-character or interval-timer expiration
*/
switch ( (lastchar = mvgetch(statline, 0)) )
{
/*
** timer expired
*/
case ERR:
case 0:
return lastchar;
/*
** stop it
*/
case MQUIT:
move(LINES-1, 0);
clrtoeol();
refresh();
cleanstop(0);
/*
** manual trigger for next sample
*/
case MSAMPNEXT:
if (paused)
break;
getalarm(0);
return lastchar;
/*
** manual trigger for previous sample
*/
case MSAMPPREV:
if (!rawreadflag)
{
statmsg = "Only allowed when viewing "
"raw file!";
beep();
break;
}
if (paused)
break;
return lastchar;
/*
** branch to certain time stamp
*/
case MSAMPBRANCH:
if (!rawreadflag)
{
statmsg = "Only allowed when viewing "
"raw file!";
beep();
break;
}
if (paused)
break;
echo();
move(statline, 0);
clrtoeol();
printw("Enter new time (format hh:mm): ");
hhmm[0] = '\0';
scanw("%15s\n", hhmm);
noecho();
if ( !hhmm2secs(hhmm, &begintime) )
{
move(statline, 0);
clrtoeol();
statmsg = "Wrong time format!";
beep();
begintime = 0;
break;
}
return lastchar;
/*
** sort order automatically depending on
** most busy resource
*/
case MSORTAUTO:
showorder = MSORTAUTO;
firstproc = 0;
break;
/*
** sort in cpu-activity order
*/
case MSORTCPU:
showorder = MSORTCPU;
firstproc = 0;
break;
/*
** sort in memory-consumption order
*/
case MSORTMEM:
showorder = MSORTMEM;
firstproc = 0;
break;
/*
** sort in disk-activity order
*/
case MSORTDSK:
if ( !(supportflags & (PATCHSTAT|IOSTAT)) )
{
statmsg = "No disk-activity figures "
"available; request ignored!";
break;
}
showorder = MSORTDSK;
firstproc = 0;
break;
/*
** sort in network-activity order
*/
case MSORTNET:
if ( !(supportflags & PATCHSTAT) )
{
statmsg = "No kernel-patch installed; "
"request ignored!";
break;
}
showorder = MSORTNET;
firstproc = 0;
break;
/*
** general figures per process
*/
case MPROCGEN:
if (showtype ==MCUMUSER || showtype ==MCUMPROC)
lastorder = 0; /* force new sort */
showtype = MPROCGEN;
if (showorder != MSORTAUTO)
showorder = MSORTCPU;