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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* acctcms [-a] [-c] [-j] [-n] [-s] [-p] [-o] [-t] [file...]
* summarize per-process accounting
* -a output in ascii, rather than [pt]cms.h format
* -c sort by total cpu, rather than total kcore-minutes
* -j anything used only once -> ***other
* -n sort by number of processes
* -s any following files already in pcms.h format
* -p output prime time command summary (only with -a)
* -o output non-prime time (offshift) command summary (only
* with -a option)
* -t process records in total (old) style (tcms.h) format
* file file in [pt]cms.h (if -s seen already) or acct.h (if not)
* expected use:
* acctcms /var/adm/pacct? > today; acctcms -s old today >new
* cp new old; rm new
* acctcms -a today; acctcms -a old
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include "acctdef.h"
#include <ctype.h>
#include <string.h>
#include <sys/acct.h>
#include <stdlib.h>
int csize = CSIZE;
/*
* Total cms records format
*/
struct tcms {
char tcm_comm[8]; /* command name */
long tcm_pc; /* number of processes */
float tcm_cpu; /* cpu time(min) */
float tcm_real; /* real time(min) */
float tcm_kcore; /* kcore-minutes */
ulong_t tcm_io; /* chars transferred */
ulong_t tcm_rw; /* blocks read */
} ;
struct tcms *tcm;
/*
* prime/nonprime CMS record format
*/
struct pcms {
char pcm_comm[8]; /* command name */
long pcm_pc[2]; /* number of processes */
float pcm_cpu[2]; /* cpu time(min) */
float pcm_real[2]; /* real time(min) */
float pcm_kcore[2]; /* kcore-minutes */
float pcm_io[2]; /* chars transferred */
float pcm_rw[2]; /* blocks read */
} ;
struct pcms *pcm;
struct tcms tcmtmp = {{'*','*','*','o','t','h','e','r'}};
struct pcms pcmtmp = {{'*','*','*','o','t','h','e','r'}};
int aflg;
int cflg;
int jflg;
int nflg;
int sflg;
int pflg;
int oflg;
int tflg;
int errflg;
#ifdef uts
float expand();
#else
ulong_t expand();
#endif
void outputc(void);
void totprnt(struct pcms *);
void pprint(struct pcms *);
void prnt(struct pcms *, int);
void print(struct pcms *);
void outputa(void);
void toutptc(void);
void tprint(struct tcms *);
void toutpta(void);
int ncmp(struct pcms *, struct pcms *);
int tncmp(struct tcms *, struct tcms *);
int tccmp(struct tcms *, struct tcms *);
int tkcmp(struct tcms *, struct tcms *);
int ccmp(struct pcms *, struct pcms *);
int kcmp(struct pcms *, struct pcms *);
void tdofile(char *);
void dofile(char *);
void tfixjunk(void);
void fixjunk(void);
void tcmadd(struct tcms *, struct tcms *);
void pcmadd(struct pcms *, struct pcms *);
void tsqueeze(void);
void squeeze(void);
/* Format specification for ASCII printing */
char *fmtcmd = "%-8.8s",
*fmtcnt = "%8ld",
*fmtkcore = " %11.2f",
*fmtcpu = " %9.2f",
*fmtreal = " %12.2f",
*fmtmsz = " %7.2f",
*fmtmcpu = " %6.2f",
*fmthog = " %5.2f",
*fmtcharx = " %12.0f",
*fmtblkx = " %10.0f" ;
int
main(int argc, char **argv)
{
int c;
while((c = getopt(argc, argv, "acjnspot")) != EOF)
switch(c) {
case 'a':
aflg++;
continue;
case 'c':
cflg++;
continue;
case 'j':
jflg++;
continue;
case 'n':
nflg++;
continue;
case 's':
sflg++;
continue;
case 'p':
pflg++;
continue;
case 'o':
oflg++;
continue;
case 't':
tflg++;
continue;
default:
errflg++;
continue;
}
if(errflg){
fprintf(stderr, "Usage: %s [-acjnspot] [file ...]\n", argv[0]);
exit(1);
}
if(tflg) {
if( (tcm = (struct tcms *)calloc(CSIZE, sizeof(struct tcms))) == NULL) {
fprintf(stderr, "%s: Cannot allocate memory\n", argv[0]);
exit(5);
}
for(; optind < argc; optind++)
tdofile(argv[optind]);
if (jflg)
tfixjunk();
tsqueeze();
qsort(tcm, csize, sizeof(tcm[0]),
(int (*)(const void *, const void *))
( nflg ? tncmp: (cflg? tccmp: tkcmp)));
if (aflg)
toutpta();
else
toutptc();
} else {
if( (pcm = (struct pcms *)calloc(CSIZE, sizeof(struct pcms))) == NULL) {
fprintf(stderr, "%s: Cannot allocate memory\n", argv[0]);
exit(6);
}
for(; optind < argc; optind++)
dofile(argv[optind]);
if (jflg)
fixjunk();
squeeze();
qsort(pcm, csize, sizeof(pcm[0]),
(int (*)(const void *, const void *))
(nflg? ncmp: (cflg? ccmp: kcmp)));
if (aflg)
outputa();
else
outputc();
}
exit(0);
}
void
tdofile(char *fname)
{
struct tcms cmt;
union {
struct acct ab; /* SVR4 acct structure */
struct o_acct oab; /* SVR3 acct structure */
} acct;
int ver = 0;
ulong_t mem;
ulong_t cpu;
ulong_t real;
if (freopen(fname, "r", stdin) == NULL) {
fprintf(stderr, "acctcms: cannot open %s\n", fname);
return;
}
if (sflg)
while (fread(&cmt, sizeof(cmt), 1, stdin) == 1)
tenter(&cmt);
else {
if (fread(&acct.ab, sizeof(acct.ab), 1, stdin) == 1)
/* check for expanded account structure flag */
if (acct.ab.ac_flag & AEXPND)
ver = 2; /* 4.0 acct file */
else
ver = 1; /* SVR3.x acct file */
rewind(stdin); /* reset file pointer */
switch(ver) {
default:
/* this can't happen */
fprintf(stderr, "acctcms: encountered bad version number\n");
return;
case 1 :
while (fread(&acct.oab, sizeof(acct.oab), 1, stdin) == 1) {
CPYN(cmt.tcm_comm, acct.oab.ac_comm);
cmt.tcm_pc = 1;
cpu = expand(acct.oab.ac_stime)+
expand(acct.oab.ac_utime);
cmt.tcm_cpu = MINT(cpu);
real = expand(acct.oab.ac_etime);
cmt.tcm_real = MINT(real);
mem = expand(acct.oab.ac_mem);
cmt.tcm_kcore = MINT(KCORE(mem));
cmt.tcm_io = expand(acct.oab.ac_io);
cmt.tcm_rw = expand(acct.oab.ac_rw);
tenter(&cmt);
}
break;
case 2 :
while (fread(&acct.ab, sizeof(acct.ab), 1, stdin) == 1) {
CPYN(cmt.tcm_comm, acct.ab.ac_comm);
cmt.tcm_pc = 1;
cpu = expand(acct.oab.ac_stime)+
expand(acct.oab.ac_utime);
cmt.tcm_cpu = MINT(cpu);
real = expand(acct.ab.ac_etime);
cmt.tcm_real = MINT(real);
mem = expand(acct.ab.ac_mem);
cmt.tcm_kcore = MINT(KCORE(mem));
cmt.tcm_io = expand(acct.ab.ac_io);
cmt.tcm_rw = expand(acct.ab.ac_rw);
tenter(&cmt);
}
break;
}
}
}
void
dofile(char *fname)
{
union {
struct acct ab;
struct o_acct oab;
} acct;
struct pcms pcmt;
double ratio;
long elaps[2];
ulong_t etime;
double dtmp;
unsigned long ltmp;
ulong_t mem;
ulong_t cpu;
ulong_t real;
if (freopen(fname, "r", stdin) == NULL) {
fprintf(stderr, "acctcms: cannot open %s\n", fname);
return;
}
if (sflg)
while (fread(&pcmt, sizeof(pcmt), 1, stdin) == 1)
enter(&pcmt);
else {
int ver = 0;
if (fread(&acct.ab, sizeof(acct.ab), 1, stdin) == 1)
/* check for expanded account structure flag */
if (acct.ab.ac_flag & AEXPND)
ver = 2; /* 4.0 acct file */
else
ver = 1; /* SVR3.x acct file */
rewind(stdin); /* reset file pointer */
switch(ver) {
default :
/* this can't happen */
fprintf(stderr, "acctcms: encountered bad version number\n");
return;
case 1 :
while (fread(&acct.oab, sizeof(acct.oab), 1, stdin) == 1) {
CPYN(pcmt.pcm_comm, acct.oab.ac_comm);
/*
** Approximate P/NP split as same as elapsed time
*/
if((etime = SECS(expand(acct.oab.ac_etime))) == 0)
etime = 1;
if (pnpsplit(acct.oab.ac_btime, etime, elaps)
== 0) {
(void) fprintf(stderr, "acctcms: could "
"not calculate prime/non-prime "
"hours\n");
exit(1);
}
ratio = (double)elaps[PRIME]/(double)etime;
if(elaps[PRIME] > elaps[NONPRIME]) {
pcmt.pcm_pc[PRIME] = 1;
pcmt.pcm_pc[NONPRIME] = 0;
} else {
pcmt.pcm_pc[PRIME] = 0;
pcmt.pcm_pc[NONPRIME] = 1;
}
cpu = expand(acct.oab.ac_stime)+
expand(acct.oab.ac_utime);
dtmp = MINT(cpu);
pcmt.pcm_cpu[PRIME] = dtmp * ratio;
pcmt.pcm_cpu[NONPRIME] = (ratio == 1.0) ? 0.0 :
(dtmp - pcmt.pcm_cpu[PRIME]);
real = expand(acct.oab.ac_etime);
dtmp = MINT(real);
pcmt.pcm_real[PRIME] = dtmp * ratio;
pcmt.pcm_real[NONPRIME] = (ratio == 1.0) ? 0.0 :
(dtmp - pcmt.pcm_real[PRIME]);
mem = expand(acct.oab.ac_mem);
dtmp = MINT(KCORE(mem));
pcmt.pcm_kcore[PRIME] = dtmp * ratio;
pcmt.pcm_kcore[NONPRIME] = (ratio == 1.0) ? 0.0 :
(dtmp - pcmt.pcm_kcore[PRIME]);
ltmp = expand(acct.oab.ac_io);
pcmt.pcm_io[PRIME] = (double)ltmp * ratio;
pcmt.pcm_io[NONPRIME] = (ratio == 1.0) ? 0.0 :
((double)ltmp - pcmt.pcm_io[PRIME]);
ltmp = expand(acct.oab.ac_rw);
pcmt.pcm_rw[PRIME] = (double)ltmp * ratio;
pcmt.pcm_rw[NONPRIME] = (ratio == 1.0) ? 0.0 :
((double)ltmp - pcmt.pcm_rw[PRIME]);
enter(&pcmt);
}
break;
case 2 :
while (fread(&acct.ab, sizeof(acct.ab), 1, stdin) == 1) {
CPYN(pcmt.pcm_comm, acct.ab.ac_comm);
/*
** Approximate P/NP split as same as elapsed time
*/
if((etime = SECS(expand(acct.ab.ac_etime))) == 0)
etime = 1;
if(pnpsplit(acct.ab.ac_btime, etime, elaps) == 0) {
fprintf(stderr, "acctcms: could not calculate prime/non-prime hours\n");
exit(1);
}
ratio = (double)elaps[PRIME]/(double)etime;
if(elaps[PRIME] > elaps[NONPRIME]) {
pcmt.pcm_pc[PRIME] = 1;
pcmt.pcm_pc[NONPRIME] = 0;
} else {
pcmt.pcm_pc[PRIME] = 0;
pcmt.pcm_pc[NONPRIME] = 1;
}
cpu = expand(acct.ab.ac_stime)+
expand(acct.ab.ac_utime);
dtmp = MINT(cpu);
pcmt.pcm_cpu[PRIME] = dtmp * ratio;
pcmt.pcm_cpu[NONPRIME] = (ratio == 1.0) ? 0.0 :
(dtmp - pcmt.pcm_cpu[PRIME]);
real = expand(acct.ab.ac_etime);
dtmp = MINT(real);
pcmt.pcm_real[PRIME] = dtmp * ratio;
pcmt.pcm_real[NONPRIME] = (ratio == 1.0) ? 0.0 :
(dtmp - pcmt.pcm_real[PRIME]);
mem = expand(acct.ab.ac_mem);
dtmp = MINT(KCORE(mem));
pcmt.pcm_kcore[PRIME] = dtmp * ratio;
pcmt.pcm_kcore[NONPRIME] = (ratio == 1.0) ? 0.0 :
(dtmp - pcmt.pcm_kcore[PRIME]);
ltmp = expand(acct.ab.ac_io);
pcmt.pcm_io[PRIME] = (double)ltmp * ratio;
pcmt.pcm_io[NONPRIME] = (ratio == 1.0) ? 0.0 :
((double)ltmp - pcmt.pcm_io[PRIME]);
ltmp = expand(acct.ab.ac_rw);
pcmt.pcm_rw[PRIME] = (double)ltmp * ratio;
pcmt.pcm_rw[NONPRIME] = (ratio == 1.0) ? 0.0 :
((double)ltmp - pcmt.pcm_rw[PRIME]);
enter(&pcmt);
}
break;
}
}
}
int
tenter(struct tcms *p)
{
int i;
int j;
struct tcms *ntcm;
for (i = j = 0; j < sizeof(p->tcm_comm); j++) {
if (p->tcm_comm[j] && p->tcm_comm[j] <= 037)
p->tcm_comm[j] = '?';
i = i*7 + p->tcm_comm[j]; /* hash function */
}
if (i < 0)
i = -i;
for (i %= csize, j = 0; tcm[i].tcm_comm[0] && j != csize; i = (i+1)%csize, j++)
if (EQN(p->tcm_comm, tcm[i].tcm_comm))
break;
if(j == csize) {
if ((ntcm = (struct tcms *) realloc(tcm,
(csize + CSIZE - 1) * sizeof (struct tcms))) == NULL) {
fprintf(stderr,
"acctcms: Cannot reallocate memory (tcm)\n");
return(-1);
} else {
memset(&ntcm[csize], 0, CSIZE - 1);
tcm = ntcm;
if (!EQN(p->tcm_comm, tcm[i].tcm_comm))
i = csize;
csize = csize + CSIZE - 1;
}
}
if (tcm[i].tcm_comm[0] == 0)
CPYN(tcm[i].tcm_comm, p->tcm_comm);
tcmadd(&tcm[i], p);
return(i);
}
int
enter(struct pcms *p)
{
int i;
int j;
struct pcms *npcm;
for (i = j = 0; j < sizeof(p->pcm_comm); j++) {
if (p->pcm_comm[j] && p->pcm_comm[j] <= 037)
p->pcm_comm[j] = '?';
i = i*7 + p->pcm_comm[j]; /* hash function */
}
if (i < 0)
i = -i;
for (i %= csize, j = 0; pcm[i].pcm_comm[0] && j != csize; i = (i+1)%csize, j++)
if (EQN(p->pcm_comm, pcm[i].pcm_comm))
break;
if(j == csize) {
if ((npcm = (struct pcms *) realloc(pcm,
(csize + CSIZE - 1) * sizeof (struct pcms))) == NULL) {
fprintf(stderr,
"acctcms: Cannot reallocate memory (pcm)\n");
return(-1);
} else {
memset(&npcm[csize], 0, CSIZE - 1);
pcm = npcm;
if (!EQN(p->pcm_comm, pcm[i].pcm_comm))
i = csize;
csize = csize + CSIZE - 1;
}
}
if (pcm[i].pcm_comm[0] == 0)
CPYN(pcm[i].pcm_comm, p->pcm_comm);
pcmadd(&pcm[i], p);
return(i);
}
void
tfixjunk(void) /* combine commands used only once */
{
int i, j;
j = tenter(&tcmtmp);
for (i = 0; i < csize; i++)
if (i != j && tcm[i].tcm_comm[0] && tcm[i].tcm_pc <= 1) {
tcmadd(&tcm[j], &tcm[i]);
tcm[i].tcm_comm[0] = 0;
}
}
void
fixjunk(void) /* combine commands used only once */
{
int i, j;
j = enter(&pcmtmp);
for (i = 0; i < csize; i++)
if (i != j && pcm[i].pcm_comm[0] && (pcm[i].pcm_pc[PRIME] + pcm[i].pcm_pc[NONPRIME]) <= 1) {
pcmadd(&pcm[j], &pcm[i]);
pcm[i].pcm_comm[0] = 0;
}
}
void
tcmadd(struct tcms *p1, struct tcms *p2)
{
p1->tcm_pc += p2->tcm_pc;
p1->tcm_cpu = p1->tcm_cpu + p2->tcm_cpu;
p1->tcm_real = p1->tcm_real + p2->tcm_real;
p1->tcm_kcore = p1->tcm_kcore + p2->tcm_kcore;
p1->tcm_io += p2->tcm_io;
p1->tcm_rw += p2->tcm_rw;
}
void
pcmadd(struct pcms *p1, struct pcms *p2)
{
p1->pcm_pc[PRIME] += p2->pcm_pc[PRIME];
p1->pcm_pc[NONPRIME] += p2->pcm_pc[NONPRIME];
p1->pcm_cpu[PRIME] += p2->pcm_cpu[PRIME];
p1->pcm_cpu[NONPRIME] += p2->pcm_cpu[NONPRIME];
p1->pcm_real[PRIME] += p2->pcm_real[PRIME];
p1->pcm_real[NONPRIME] += p2->pcm_real[NONPRIME];
p1->pcm_kcore[PRIME] += p2->pcm_kcore[PRIME];
p1->pcm_kcore[NONPRIME] += p2->pcm_kcore[NONPRIME];
p1->pcm_io[PRIME] += p2->pcm_io[PRIME];
p1->pcm_io[NONPRIME] += p2->pcm_io[NONPRIME];
p1->pcm_rw[PRIME] += p2->pcm_rw[PRIME];
p1->pcm_rw[NONPRIME] += p2->pcm_rw[NONPRIME];
}
void
tsqueeze(void) /* get rid of holes in hash table */
{
int i, k;
for (i = k = 0; i < csize; i++)
if (tcm[i].tcm_comm[0]) {
CPYN(tcm[k].tcm_comm, tcm[i].tcm_comm);
tcm[k].tcm_pc = tcm[i].tcm_pc;
tcm[k].tcm_cpu = tcm[i].tcm_cpu;
tcm[k].tcm_real = tcm[i].tcm_real;
tcm[k].tcm_kcore = tcm[i].tcm_kcore;
tcm[k].tcm_io = tcm[i].tcm_io;
tcm[k].tcm_rw = tcm[i].tcm_rw;
k++;
}
csize = k;
}
void
squeeze(void) /* get rid of holes in hash table */
{
int i, k;
for (i = k = 0; i < csize; i++)
if (pcm[i].pcm_comm[0]) {
CPYN(pcm[k].pcm_comm, pcm[i].pcm_comm);
pcm[k].pcm_pc[PRIME] = pcm[i].pcm_pc[PRIME];
pcm[k].pcm_pc[NONPRIME] = pcm[i].pcm_pc[NONPRIME];
pcm[k].pcm_cpu[PRIME] = pcm[i].pcm_cpu[PRIME];
pcm[k].pcm_cpu[NONPRIME] = pcm[i].pcm_cpu[NONPRIME];
pcm[k].pcm_real[PRIME] = pcm[i].pcm_real[PRIME];
pcm[k].pcm_real[NONPRIME] = pcm[i].pcm_real[NONPRIME];
pcm[k].pcm_kcore[PRIME] = pcm[i].pcm_kcore[PRIME];
pcm[k].pcm_kcore[NONPRIME] = pcm[i].pcm_kcore[NONPRIME];
pcm[k].pcm_io[PRIME] = pcm[i].pcm_io[PRIME];
pcm[k].pcm_io[NONPRIME] = pcm[i].pcm_io[NONPRIME];
pcm[k].pcm_rw[PRIME] = pcm[i].pcm_rw[PRIME];
pcm[k].pcm_rw[NONPRIME] = pcm[i].pcm_rw[NONPRIME];
k++;
}
csize = k;
}
int
tccmp(struct tcms *p1, struct tcms *p2)
{
if (p1->tcm_cpu == p2->tcm_cpu)
return(0);
return ((p2->tcm_cpu > p1->tcm_cpu)? 1 : -1);
}
int
ccmp(struct pcms *p1, struct pcms *p2)
{
int index;
if( (pflg && oflg) || (!pflg && !oflg) ) {
if (p1->pcm_cpu[PRIME] + p1->pcm_cpu[NONPRIME] == p2->pcm_cpu[PRIME] + p2->pcm_cpu[NONPRIME])
return(0);
return ((p2->pcm_cpu[PRIME] + p2->pcm_cpu[NONPRIME] > p1->pcm_cpu[PRIME] + p1->pcm_cpu[NONPRIME])? 1 : -1);
}
index = pflg ? PRIME : NONPRIME;
if (p1->pcm_cpu[index] == p2->pcm_cpu[index])
return(0);
return ((p2->pcm_cpu[index] > p1->pcm_cpu[index])? 1 : -1);
}
int
tkcmp(struct tcms *p1, struct tcms *p2)
{
if (p1->tcm_kcore == p2->tcm_kcore)
return(0);
return ((p2->tcm_kcore > p1->tcm_kcore)? 1 : -1);
}
int
kcmp(struct pcms *p1, struct pcms *p2)
{
int index;
if( (pflg && oflg) || (!pflg && !pflg) ){
if (p1->pcm_kcore[PRIME] + p1->pcm_kcore[NONPRIME] == p2->pcm_kcore[PRIME] + p2->pcm_kcore[NONPRIME])
return(0);
return ((p2->pcm_kcore[PRIME] + p2->pcm_kcore[NONPRIME] > p1->pcm_kcore[PRIME] + p1->pcm_kcore[NONPRIME])? 1 : -1);
}
index = pflg ? PRIME : NONPRIME;
if (p1->pcm_kcore[index] == p2->pcm_kcore[index])
return(0);
return ((p2->pcm_kcore[index] > p1->pcm_kcore[index])? 1 : -1);
}
int
tncmp(struct tcms *p1, struct tcms *p2)
{
if (p1->tcm_pc == p2->tcm_pc)
return(0);
return ((p2->tcm_pc > p1->tcm_pc)? 1 : -1);
}
int
ncmp(struct pcms *p1, struct pcms *p2)
{
int index;
if( (pflg && oflg) || (!pflg && !oflg) ) {
if (p1->pcm_pc[PRIME] + p1->pcm_pc[NONPRIME] == p2->pcm_pc[PRIME] + p2->pcm_pc[NONPRIME])
return(0);
return ((p2->pcm_pc[PRIME] + p2->pcm_pc[NONPRIME] > p1->pcm_pc[PRIME] + p1->pcm_pc[NONPRIME])? 1 : -1);
}
index = pflg ? PRIME : NONPRIME;
if (p1->pcm_pc[index] == p2->pcm_pc[index])
return(0);
return ((p2->pcm_pc[index] > p1->pcm_pc[index])? 1 : -1);
}
char thd1[] =
"COMMAND NUMBER TOTAL TOTAL TOTAL MEAN MEAN HOG CHARS BLOCKS\n";
char thd2[] =
"NAME CMDS KCOREMIN CPU-MIN REAL-MIN SIZE-K CPU-MIN FACTOR TRNSFD READ\n";
void
toutpta(void)
{
int i;
printf(thd1);
printf(thd2);
printf("\n");
for (i = 0; i < csize; i++)
tcmadd(&tcmtmp, &tcm[i]);
CPYN(tcmtmp.tcm_comm, "TOTALS");
tprint(&tcmtmp);
printf("\n");
for (i = 0; i < csize; i++)
tprint(&tcm[i]);
}
void
tprint(struct tcms *p)
{
printf("%-8.8s", p->tcm_comm);
printf(" %7ld", p->tcm_pc);
printf(" %11.2f", p->tcm_kcore);
printf(" %10.2f", p->tcm_cpu);
printf(" %12.2f", p->tcm_real);
if(p->tcm_cpu == 0) p->tcm_cpu = 1;
printf(" %6.2f", p->tcm_kcore/p->tcm_cpu);
if(p->tcm_pc == 0) p->tcm_pc = 1;
printf(" %7.2f", p->tcm_cpu/p->tcm_pc);
if (p->tcm_real == 0)
p->tcm_real = 1;
printf(" %8.2f", p->tcm_cpu/p->tcm_real);
printf(" %11lu", p->tcm_io);
printf(" %11lu\n", p->tcm_rw);
}
void
toutptc(void)
{
int i;
for (i = 0; i < csize; i++)
fwrite(&tcm[i], sizeof(tcm[i]), 1, stdout);
}
char hd1[] =
"COMMAND NUMBER TOTAL TOTAL TOTAL MEAN MEAN HOG CHARS BLOCKS\n";
char hd2[] =
"NAME CMDS KCOREMIN CPU-MIN REAL-MIN SIZE-K CPU-MIN FACTOR TRNSFD READ\n";
char hd3[] =
"COMMAND NUMBER TOTAL CPU-MIN REAL-MIN MEAN MEAN HOG CHARS BLOCKS\n";
char hd4[] =
"NAME (P) (NP) KCOREMIN (P) (NP) (P) (NP) SIZE-K CPU-MIN FACTOR TRNSFD READ\n";
char hdprime[] =
" PRIME TIME COMMAND SUMMARY\n";
char hdnonprime[] =
" NON-PRIME TIME COMMAND SUMMARY\n";
char hdtot[] =
" TOTAL COMMAND SUMMARY\n";
char hdp[] =
" PRIME/NON-PRIME TIME COMMAND SUMMARY\n";
void
outputa(void)
{
int i;
if( pflg && oflg ) printf(hdp);
else if(pflg) printf(hdprime);
else if(oflg) printf(hdnonprime);
else printf(hdtot);
if( (!pflg && !oflg) || (pflg ^ oflg)) {
printf(hd1);
printf(hd2);
}
else {
printf(hd3);
printf(hd4);
}
printf("\n");
for (i = 0; i < csize; i++)
pcmadd(&pcmtmp, &pcm[i]);
CPYN(pcmtmp.pcm_comm, "TOTALS");
print(&pcmtmp);
printf("\n");
for (i = 0; i < csize; i++)
print(&pcm[i]);
}
void
print(struct pcms *p)
{
if(pflg && oflg) pprint(p);
else if(pflg || oflg) prnt(p, pflg ? PRIME : NONPRIME);
else totprnt(p);
}
void
prnt(struct pcms *p, int hr)
{
if(p->pcm_pc[hr] == 0) return;
printf(fmtcmd, p->pcm_comm);
printf(fmtcnt, p->pcm_pc[hr]);
printf(fmtkcore, p->pcm_kcore[hr]);
printf(fmtcpu, p->pcm_cpu[hr]);
printf(fmtreal, p->pcm_real[hr]);
if(p->pcm_cpu[hr] == 0) p->pcm_cpu[hr] = 1;
printf(fmtmsz, p->pcm_kcore[hr]/p->pcm_cpu[hr]);
if(p->pcm_pc[hr] == 0) p->pcm_pc[hr] = 1;
printf(fmtmcpu, p->pcm_cpu[hr]/p->pcm_pc[hr]);
if (p->pcm_real[hr] == 0)
p->pcm_real[hr] = 1;
printf(fmthog, p->pcm_cpu[hr]/p->pcm_real[hr]);
printf(fmtcharx,p->pcm_io[hr]);
printf(fmtblkx,p->pcm_rw[hr]);
printf("\n");
}
void
pprint(struct pcms *p)
{
printf(fmtcmd, p->pcm_comm);
printf(fmtcnt, p->pcm_pc[PRIME]);
printf(fmtcnt, p->pcm_pc[NONPRIME]);
printf(fmtkcore, TOTAL(p->pcm_kcore));
printf(fmtcpu, p->pcm_cpu[PRIME]);
printf(fmtcpu, p->pcm_cpu[NONPRIME]);
printf(fmtreal, p->pcm_real[PRIME]);
printf(fmtreal, p->pcm_real[NONPRIME]);
if(TOTAL(p->pcm_cpu) == 0) p->pcm_cpu[PRIME] = 1;
printf(fmtmsz, TOTAL(p->pcm_kcore)/TOTAL(p->pcm_cpu));
if(TOTAL(p->pcm_pc) == 0) p->pcm_pc[PRIME] = 1;
printf(fmtmcpu, TOTAL(p->pcm_cpu)/TOTAL(p->pcm_pc));
if ( TOTAL(p->pcm_real) == 0)
p->pcm_real[PRIME] = 1;
printf(fmthog, TOTAL(p->pcm_cpu)/TOTAL(p->pcm_real));
printf(fmtcharx,TOTAL(p->pcm_io));
printf(fmtblkx, TOTAL(p->pcm_rw));
printf("\n");
}
void
totprnt(struct pcms *p)
{
printf(fmtcmd, p->pcm_comm);
printf(fmtcnt, TOTAL(p->pcm_pc));
printf(fmtkcore, TOTAL(p->pcm_kcore));
printf(fmtcpu, TOTAL(p->pcm_cpu));
printf(fmtreal, TOTAL(p->pcm_real));
if(TOTAL(p->pcm_cpu) == 0) p->pcm_cpu[PRIME] = 1;
printf(fmtmsz, TOTAL(p->pcm_kcore)/TOTAL(p->pcm_cpu));
if(TOTAL(p->pcm_pc) == 0) p->pcm_pc[PRIME] = 1;
printf(fmtmcpu, TOTAL(p->pcm_cpu)/TOTAL(p->pcm_pc));
if (TOTAL(p->pcm_real) == 0)
p->pcm_real[PRIME] = 1;
printf(fmthog, TOTAL(p->pcm_cpu)/TOTAL(p->pcm_real));
printf(fmtcharx,TOTAL(p->pcm_io));
printf(fmtblkx,TOTAL(p->pcm_rw));
printf("\n");
}
void
outputc(void)
{
int i;
for (i = 0; i < csize; i++)
fwrite(&pcm[i], sizeof(pcm[i]), 1, stdout);
}
|