1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  *	acctcms [-a] [-c] [-j] [-n] [-s] [-p] [-o] [-t] [file...]
35*0Sstevel@tonic-gate  *	summarize per-process accounting
36*0Sstevel@tonic-gate  *	-a	output in ascii, rather than [pt]cms.h format
37*0Sstevel@tonic-gate  *	-c	sort by total cpu, rather than total kcore-minutes
38*0Sstevel@tonic-gate  *	-j	anything used only once -> ***other
39*0Sstevel@tonic-gate  *	-n	sort by number of processes
40*0Sstevel@tonic-gate  *	-s	any following files already in pcms.h format
41*0Sstevel@tonic-gate  *      -p      output prime time command summary (only with -a)
42*0Sstevel@tonic-gate  *      -o      output non-prime time (offshift) command summary (only
43*0Sstevel@tonic-gate  *		with -a option)
44*0Sstevel@tonic-gate  *	-t	process records in total (old) style (tcms.h) format
45*0Sstevel@tonic-gate  *	file	file in [pt]cms.h (if -s seen already) or acct.h (if not)
46*0Sstevel@tonic-gate  *	expected use:
47*0Sstevel@tonic-gate  *	acctcms /var/adm/pacct? > today; acctcms -s old today >new
48*0Sstevel@tonic-gate  *	cp new old; rm new
49*0Sstevel@tonic-gate  *	acctcms -a today; acctcms -a old
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate #include <stdio.h>
52*0Sstevel@tonic-gate #include <sys/types.h>
53*0Sstevel@tonic-gate #include <sys/param.h>
54*0Sstevel@tonic-gate #include "acctdef.h"
55*0Sstevel@tonic-gate #include <ctype.h>
56*0Sstevel@tonic-gate #include <sys/acct.h>
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate int	csize = CSIZE;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  *  Total cms records format
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate struct tcms {
64*0Sstevel@tonic-gate 	char	tcm_comm[8];	/* command name */
65*0Sstevel@tonic-gate 	long	tcm_pc;		/* number of processes */
66*0Sstevel@tonic-gate 	float	tcm_cpu;	/* cpu time(min) */
67*0Sstevel@tonic-gate 	float	tcm_real;	/* real time(min) */
68*0Sstevel@tonic-gate 	float	tcm_kcore;	/* kcore-minutes */
69*0Sstevel@tonic-gate 	ulong_t	tcm_io;		/* chars transferred */
70*0Sstevel@tonic-gate 	ulong_t	tcm_rw;		/* blocks read */
71*0Sstevel@tonic-gate } ;
72*0Sstevel@tonic-gate struct tcms	*tcm;
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * prime/nonprime CMS record format
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate struct pcms {
77*0Sstevel@tonic-gate 	char	pcm_comm[8];	/* command name */
78*0Sstevel@tonic-gate 	long	pcm_pc[2];	/* number of processes */
79*0Sstevel@tonic-gate 	float	pcm_cpu[2];	/* cpu time(min) */
80*0Sstevel@tonic-gate 	float	pcm_real[2];	/* real time(min) */
81*0Sstevel@tonic-gate 	float	pcm_kcore[2];	/* kcore-minutes */
82*0Sstevel@tonic-gate 	float	pcm_io[2];	/* chars transferred */
83*0Sstevel@tonic-gate 	float	pcm_rw[2];	/* blocks read */
84*0Sstevel@tonic-gate } ;
85*0Sstevel@tonic-gate struct pcms	*pcm;
86*0Sstevel@tonic-gate struct  tcms    tcmtmp  = {{'*','*','*','o','t','h','e','r'}};
87*0Sstevel@tonic-gate struct  pcms    pcmtmp  = {{'*','*','*','o','t','h','e','r'}};
88*0Sstevel@tonic-gate int	aflg;
89*0Sstevel@tonic-gate int	cflg;
90*0Sstevel@tonic-gate int	jflg;
91*0Sstevel@tonic-gate int	nflg;
92*0Sstevel@tonic-gate int	sflg;
93*0Sstevel@tonic-gate int	pflg;
94*0Sstevel@tonic-gate int	oflg;
95*0Sstevel@tonic-gate int	tflg;
96*0Sstevel@tonic-gate int	errflg;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate int	ccmp(), kcmp(), ncmp();
99*0Sstevel@tonic-gate int	tccmp(), tkcmp(), tncmp();
100*0Sstevel@tonic-gate #ifdef uts
101*0Sstevel@tonic-gate float   expand();
102*0Sstevel@tonic-gate #else
103*0Sstevel@tonic-gate ulong_t	expand();
104*0Sstevel@tonic-gate #endif
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate char	*strncpy();
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate /*  Format specification for ASCII printing */
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate char	*fmtcmd =	"%-8.8s",
111*0Sstevel@tonic-gate 	*fmtcnt =	"%8ld",
112*0Sstevel@tonic-gate 	*fmtkcore =	" %11.2f",
113*0Sstevel@tonic-gate 	*fmtcpu =	" %9.2f",
114*0Sstevel@tonic-gate 	*fmtreal =	" %12.2f",
115*0Sstevel@tonic-gate 	*fmtmsz =	" %7.2f",
116*0Sstevel@tonic-gate 	*fmtmcpu =	" %6.2f",
117*0Sstevel@tonic-gate 	*fmthog =	" %5.2f",
118*0Sstevel@tonic-gate 	*fmtcharx =	" %12.0f",
119*0Sstevel@tonic-gate 	*fmtblkx =	" %10.0f" ;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate main(argc, argv)
122*0Sstevel@tonic-gate char **argv;
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate 	int	c;
125*0Sstevel@tonic-gate 	extern	int	optind;
126*0Sstevel@tonic-gate 	extern	char	*optarg;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	while((c = getopt(argc, argv, "acjnspot")) != EOF)
129*0Sstevel@tonic-gate 	switch(c) {
130*0Sstevel@tonic-gate 		case 'a':
131*0Sstevel@tonic-gate 			aflg++;
132*0Sstevel@tonic-gate 			continue;
133*0Sstevel@tonic-gate 		case 'c':
134*0Sstevel@tonic-gate 			cflg++;
135*0Sstevel@tonic-gate 			continue;
136*0Sstevel@tonic-gate 		case 'j':
137*0Sstevel@tonic-gate 			jflg++;
138*0Sstevel@tonic-gate 			continue;
139*0Sstevel@tonic-gate 		case 'n':
140*0Sstevel@tonic-gate 			nflg++;
141*0Sstevel@tonic-gate 			continue;
142*0Sstevel@tonic-gate 		case 's':
143*0Sstevel@tonic-gate 			sflg++;
144*0Sstevel@tonic-gate 			continue;
145*0Sstevel@tonic-gate 		case 'p':
146*0Sstevel@tonic-gate 			pflg++;
147*0Sstevel@tonic-gate 			continue;
148*0Sstevel@tonic-gate 		case 'o':
149*0Sstevel@tonic-gate 			oflg++;
150*0Sstevel@tonic-gate 			continue;
151*0Sstevel@tonic-gate 		case 't':
152*0Sstevel@tonic-gate 			tflg++;
153*0Sstevel@tonic-gate 			continue;
154*0Sstevel@tonic-gate 		default:
155*0Sstevel@tonic-gate 			errflg++;
156*0Sstevel@tonic-gate 			continue;
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 	if(errflg){
159*0Sstevel@tonic-gate 		fprintf(stderr, "Usage: %s [-acjnspot] [file ...]\n", argv[0]);
160*0Sstevel@tonic-gate 		exit(1);
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 	if(tflg) {
163*0Sstevel@tonic-gate 		if( (tcm = (struct tcms *)calloc(CSIZE, sizeof(struct tcms))) == NULL) {
164*0Sstevel@tonic-gate 			fprintf(stderr, "%s: Cannot allocate memory\n", argv[0]);
165*0Sstevel@tonic-gate 			exit(5);
166*0Sstevel@tonic-gate 		}
167*0Sstevel@tonic-gate 		for(; optind < argc; optind++)
168*0Sstevel@tonic-gate 			tdofile(argv[optind]);
169*0Sstevel@tonic-gate 		if (jflg)
170*0Sstevel@tonic-gate 			tfixjunk();
171*0Sstevel@tonic-gate 		tsqueeze();
172*0Sstevel@tonic-gate 		qsort(tcm, csize, sizeof(tcm[0]), nflg? tncmp: (cflg? tccmp: tkcmp));
173*0Sstevel@tonic-gate 		if (aflg)
174*0Sstevel@tonic-gate 			toutpta();
175*0Sstevel@tonic-gate 		else
176*0Sstevel@tonic-gate 			toutptc();
177*0Sstevel@tonic-gate 	} else {
178*0Sstevel@tonic-gate 		if( (pcm = (struct pcms *)calloc(CSIZE, sizeof(struct pcms))) == NULL) {
179*0Sstevel@tonic-gate 			fprintf(stderr, "%s: Cannot allocate memory\n", argv[0]);
180*0Sstevel@tonic-gate 			exit(6);
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 		for(; optind < argc; optind++)
183*0Sstevel@tonic-gate 			dofile(argv[optind]);
184*0Sstevel@tonic-gate 		if (jflg)
185*0Sstevel@tonic-gate 			fixjunk();
186*0Sstevel@tonic-gate 		squeeze();
187*0Sstevel@tonic-gate 		qsort(pcm, csize, sizeof(pcm[0]), nflg? ncmp: (cflg? ccmp: kcmp));
188*0Sstevel@tonic-gate 		if (aflg)
189*0Sstevel@tonic-gate 			outputa();
190*0Sstevel@tonic-gate 		else
191*0Sstevel@tonic-gate 			outputc();
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 	exit(0);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate tdofile(fname)
198*0Sstevel@tonic-gate char *fname;
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate 	struct tcms cmt;
201*0Sstevel@tonic-gate 	union {
202*0Sstevel@tonic-gate 		struct acct ab;		/* SVR4 acct structure */
203*0Sstevel@tonic-gate 		struct o_acct oab;	/* SVR3 acct structure */
204*0Sstevel@tonic-gate 	} acct;
205*0Sstevel@tonic-gate 	int ver = 0;
206*0Sstevel@tonic-gate 	ulong_t	mem;
207*0Sstevel@tonic-gate 	ulong_t	cpu;
208*0Sstevel@tonic-gate 	ulong_t	real;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	if (freopen(fname, "r", stdin) == NULL) {
211*0Sstevel@tonic-gate 		fprintf(stderr,  "acctcms: cannot open %s\n", fname);
212*0Sstevel@tonic-gate 		return;
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	if (sflg)
216*0Sstevel@tonic-gate 		while (fread(&cmt, sizeof(cmt), 1, stdin) == 1)
217*0Sstevel@tonic-gate 			tenter(&cmt);
218*0Sstevel@tonic-gate 	else {
219*0Sstevel@tonic-gate 		if (fread(&acct.ab, sizeof(acct.ab), 1, stdin) == 1)
220*0Sstevel@tonic-gate 			/* check for expanded account structure flag */
221*0Sstevel@tonic-gate 			if (acct.ab.ac_flag & AEXPND)
222*0Sstevel@tonic-gate 				ver = 2;		/* 4.0 acct file */
223*0Sstevel@tonic-gate 			else
224*0Sstevel@tonic-gate 				ver = 1;		/* SVR3.x acct file */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		rewind(stdin);	/* reset file pointer */
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate  		switch(ver) {
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		default:
231*0Sstevel@tonic-gate 				/* this can't happen */
232*0Sstevel@tonic-gate 			fprintf(stderr, "acctcms: encountered bad version number\n");
233*0Sstevel@tonic-gate 			return;
234*0Sstevel@tonic-gate 		case 1 :
235*0Sstevel@tonic-gate 			while (fread(&acct.oab, sizeof(acct.oab), 1, stdin) == 1) {
236*0Sstevel@tonic-gate 				CPYN(cmt.tcm_comm, acct.oab.ac_comm);
237*0Sstevel@tonic-gate 				cmt.tcm_pc = 1;
238*0Sstevel@tonic-gate 				cpu = expand(acct.oab.ac_stime)+
239*0Sstevel@tonic-gate 					expand(acct.oab.ac_utime);
240*0Sstevel@tonic-gate 				cmt.tcm_cpu = MINT(cpu);
241*0Sstevel@tonic-gate 				real = expand(acct.oab.ac_etime);
242*0Sstevel@tonic-gate 				cmt.tcm_real = MINT(real);
243*0Sstevel@tonic-gate 				mem = expand(acct.oab.ac_mem);
244*0Sstevel@tonic-gate 				cmt.tcm_kcore = MINT(KCORE(mem));
245*0Sstevel@tonic-gate 				cmt.tcm_io = expand(acct.oab.ac_io);
246*0Sstevel@tonic-gate 				cmt.tcm_rw = expand(acct.oab.ac_rw);
247*0Sstevel@tonic-gate 				tenter(&cmt);
248*0Sstevel@tonic-gate 			}
249*0Sstevel@tonic-gate 			break;
250*0Sstevel@tonic-gate 		case 2 :
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 			while (fread(&acct.ab, sizeof(acct.ab), 1, stdin) == 1) {
253*0Sstevel@tonic-gate 				CPYN(cmt.tcm_comm, acct.ab.ac_comm);
254*0Sstevel@tonic-gate 				cmt.tcm_pc = 1;
255*0Sstevel@tonic-gate 				cpu = expand(acct.oab.ac_stime)+
256*0Sstevel@tonic-gate 					expand(acct.oab.ac_utime);
257*0Sstevel@tonic-gate 				cmt.tcm_cpu = MINT(cpu);
258*0Sstevel@tonic-gate 				real = expand(acct.ab.ac_etime);
259*0Sstevel@tonic-gate 				cmt.tcm_real = MINT(real);
260*0Sstevel@tonic-gate 				mem = expand(acct.ab.ac_mem);
261*0Sstevel@tonic-gate 				cmt.tcm_kcore = MINT(KCORE(mem));
262*0Sstevel@tonic-gate 				cmt.tcm_io = expand(acct.ab.ac_io);
263*0Sstevel@tonic-gate 				cmt.tcm_rw = expand(acct.ab.ac_rw);
264*0Sstevel@tonic-gate 				tenter(&cmt);
265*0Sstevel@tonic-gate 			}
266*0Sstevel@tonic-gate 			break;
267*0Sstevel@tonic-gate 		}
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate }
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate dofile(fname)
272*0Sstevel@tonic-gate char *fname;
273*0Sstevel@tonic-gate {
274*0Sstevel@tonic-gate 	union {
275*0Sstevel@tonic-gate 		struct acct ab;
276*0Sstevel@tonic-gate 		struct o_acct oab;
277*0Sstevel@tonic-gate 	} acct;
278*0Sstevel@tonic-gate 	struct pcms 	pcmt;
279*0Sstevel@tonic-gate 	double		ratio;
280*0Sstevel@tonic-gate 	long		elaps[2];
281*0Sstevel@tonic-gate 	ulong_t		etime;
282*0Sstevel@tonic-gate 	double	dtmp;
283*0Sstevel@tonic-gate 	unsigned long	ltmp;
284*0Sstevel@tonic-gate 	ulong_t	mem;
285*0Sstevel@tonic-gate 	ulong_t	cpu;
286*0Sstevel@tonic-gate 	ulong_t	real;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	if (freopen(fname, "r", stdin) == NULL) {
289*0Sstevel@tonic-gate 		fprintf(stderr,  "acctcms: cannot open %s\n", fname);
290*0Sstevel@tonic-gate 		return;
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	if (sflg)
294*0Sstevel@tonic-gate 		while (fread(&pcmt, sizeof(pcmt), 1, stdin) == 1)
295*0Sstevel@tonic-gate 			enter(&pcmt);
296*0Sstevel@tonic-gate 	else {
297*0Sstevel@tonic-gate 		int ver = 0;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 		if (fread(&acct.ab, sizeof(acct.ab), 1, stdin) == 1)
300*0Sstevel@tonic-gate 			/* check for expanded account structure flag */
301*0Sstevel@tonic-gate 			if (acct.ab.ac_flag & AEXPND)
302*0Sstevel@tonic-gate 				ver = 2;		/* 4.0 acct file */
303*0Sstevel@tonic-gate 			else
304*0Sstevel@tonic-gate 				ver = 1;		/* SVR3.x acct file */
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 		rewind(stdin);	/* reset file pointer */
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 		switch(ver) {
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 		default :
311*0Sstevel@tonic-gate  				/* this can't happen */
312*0Sstevel@tonic-gate 			fprintf(stderr, "acctcms: encountered bad version number\n");
313*0Sstevel@tonic-gate 			return;
314*0Sstevel@tonic-gate 		case 1 :
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 			while (fread(&acct.oab, sizeof(acct.oab), 1, stdin) == 1) {
317*0Sstevel@tonic-gate 				CPYN(pcmt.pcm_comm, acct.oab.ac_comm);
318*0Sstevel@tonic-gate 			/*
319*0Sstevel@tonic-gate 			** Approximate P/NP split as same as elapsed time
320*0Sstevel@tonic-gate 		 	*/
321*0Sstevel@tonic-gate 				if((etime = SECS(expand(acct.oab.ac_etime))) == 0)
322*0Sstevel@tonic-gate 					etime = 1;
323*0Sstevel@tonic-gate 				if (pnpsplit(acct.oab.ac_btime, etime, elaps)
324*0Sstevel@tonic-gate 				    == 0) {
325*0Sstevel@tonic-gate 					(void) fprintf(stderr, "acctcms: could "
326*0Sstevel@tonic-gate 					    "not calculate prime/non-prime "
327*0Sstevel@tonic-gate 					    "hours\n");
328*0Sstevel@tonic-gate 					exit(1);
329*0Sstevel@tonic-gate 				}
330*0Sstevel@tonic-gate 				ratio = (double)elaps[PRIME]/(double)etime;
331*0Sstevel@tonic-gate 				if(elaps[PRIME] > elaps[NONPRIME]) {
332*0Sstevel@tonic-gate 					pcmt.pcm_pc[PRIME] = 1;
333*0Sstevel@tonic-gate 					pcmt.pcm_pc[NONPRIME] = 0;
334*0Sstevel@tonic-gate 				} else {
335*0Sstevel@tonic-gate 					pcmt.pcm_pc[PRIME] = 0;
336*0Sstevel@tonic-gate 					pcmt.pcm_pc[NONPRIME] = 1;
337*0Sstevel@tonic-gate 				}
338*0Sstevel@tonic-gate 				cpu = expand(acct.oab.ac_stime)+
339*0Sstevel@tonic-gate 					expand(acct.oab.ac_utime);
340*0Sstevel@tonic-gate 				dtmp = MINT(cpu);
341*0Sstevel@tonic-gate 				pcmt.pcm_cpu[PRIME] = dtmp * ratio;
342*0Sstevel@tonic-gate 				pcmt.pcm_cpu[NONPRIME] = (ratio == 1.0) ? 0.0 :
343*0Sstevel@tonic-gate 					(dtmp - pcmt.pcm_cpu[PRIME]);
344*0Sstevel@tonic-gate 				real = expand(acct.oab.ac_etime);
345*0Sstevel@tonic-gate 				dtmp = MINT(real);
346*0Sstevel@tonic-gate 				pcmt.pcm_real[PRIME] = dtmp * ratio;
347*0Sstevel@tonic-gate 				pcmt.pcm_real[NONPRIME] = (ratio == 1.0) ? 0.0 :
348*0Sstevel@tonic-gate 					(dtmp - pcmt.pcm_real[PRIME]);
349*0Sstevel@tonic-gate 				mem = expand(acct.oab.ac_mem);
350*0Sstevel@tonic-gate 				dtmp = MINT(KCORE(mem));
351*0Sstevel@tonic-gate 				pcmt.pcm_kcore[PRIME] = dtmp * ratio;
352*0Sstevel@tonic-gate 				pcmt.pcm_kcore[NONPRIME] = (ratio == 1.0) ? 0.0 :
353*0Sstevel@tonic-gate 					(dtmp - pcmt.pcm_kcore[PRIME]);
354*0Sstevel@tonic-gate 				ltmp = expand(acct.oab.ac_io);
355*0Sstevel@tonic-gate 				pcmt.pcm_io[PRIME] = (double)ltmp * ratio;
356*0Sstevel@tonic-gate 				pcmt.pcm_io[NONPRIME] = (ratio == 1.0) ? 0.0 :
357*0Sstevel@tonic-gate 					((double)ltmp - pcmt.pcm_io[PRIME]);
358*0Sstevel@tonic-gate 				ltmp = expand(acct.oab.ac_rw);
359*0Sstevel@tonic-gate 				pcmt.pcm_rw[PRIME] = (double)ltmp * ratio;
360*0Sstevel@tonic-gate 				pcmt.pcm_rw[NONPRIME] = (ratio == 1.0) ? 0.0 :
361*0Sstevel@tonic-gate 					((double)ltmp - pcmt.pcm_rw[PRIME]);
362*0Sstevel@tonic-gate 				enter(&pcmt);
363*0Sstevel@tonic-gate 			}
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 			break;
366*0Sstevel@tonic-gate 		case 2 :
367*0Sstevel@tonic-gate 			while (fread(&acct.ab, sizeof(acct.ab), 1, stdin) == 1) {
368*0Sstevel@tonic-gate 				CPYN(pcmt.pcm_comm, acct.ab.ac_comm);
369*0Sstevel@tonic-gate 				/*
370*0Sstevel@tonic-gate 				** Approximate P/NP split as same as elapsed time
371*0Sstevel@tonic-gate 		 		*/
372*0Sstevel@tonic-gate 				if((etime = SECS(expand(acct.ab.ac_etime))) == 0)
373*0Sstevel@tonic-gate 					etime = 1;
374*0Sstevel@tonic-gate 				if(pnpsplit(acct.ab.ac_btime, etime, elaps) == 0) {
375*0Sstevel@tonic-gate 					fprintf(stderr, "acctcms: could not calculate prime/non-prime hours\n");
376*0Sstevel@tonic-gate 					exit(1);
377*0Sstevel@tonic-gate 				}
378*0Sstevel@tonic-gate 				ratio = (double)elaps[PRIME]/(double)etime;
379*0Sstevel@tonic-gate 				if(elaps[PRIME] > elaps[NONPRIME]) {
380*0Sstevel@tonic-gate 					pcmt.pcm_pc[PRIME] = 1;
381*0Sstevel@tonic-gate 					pcmt.pcm_pc[NONPRIME] = 0;
382*0Sstevel@tonic-gate 				} else {
383*0Sstevel@tonic-gate 					pcmt.pcm_pc[PRIME] = 0;
384*0Sstevel@tonic-gate 					pcmt.pcm_pc[NONPRIME] = 1;
385*0Sstevel@tonic-gate 				}
386*0Sstevel@tonic-gate 				cpu = expand(acct.ab.ac_stime)+
387*0Sstevel@tonic-gate 					expand(acct.ab.ac_utime);
388*0Sstevel@tonic-gate 				dtmp = MINT(cpu);
389*0Sstevel@tonic-gate 				pcmt.pcm_cpu[PRIME] = dtmp * ratio;
390*0Sstevel@tonic-gate 				pcmt.pcm_cpu[NONPRIME] = (ratio == 1.0) ? 0.0 :
391*0Sstevel@tonic-gate 					(dtmp - pcmt.pcm_cpu[PRIME]);
392*0Sstevel@tonic-gate 				real = expand(acct.ab.ac_etime);
393*0Sstevel@tonic-gate 				dtmp = MINT(real);
394*0Sstevel@tonic-gate 				pcmt.pcm_real[PRIME] = dtmp * ratio;
395*0Sstevel@tonic-gate 				pcmt.pcm_real[NONPRIME] = (ratio == 1.0) ? 0.0 :
396*0Sstevel@tonic-gate 					(dtmp - pcmt.pcm_real[PRIME]);
397*0Sstevel@tonic-gate 				mem = expand(acct.ab.ac_mem);
398*0Sstevel@tonic-gate 				dtmp = MINT(KCORE(mem));
399*0Sstevel@tonic-gate 				pcmt.pcm_kcore[PRIME] = dtmp * ratio;
400*0Sstevel@tonic-gate 				pcmt.pcm_kcore[NONPRIME] = (ratio == 1.0) ? 0.0 :
401*0Sstevel@tonic-gate 					(dtmp - pcmt.pcm_kcore[PRIME]);
402*0Sstevel@tonic-gate 				ltmp = expand(acct.ab.ac_io);
403*0Sstevel@tonic-gate 				pcmt.pcm_io[PRIME] = (double)ltmp * ratio;
404*0Sstevel@tonic-gate 				pcmt.pcm_io[NONPRIME] = (ratio == 1.0) ? 0.0 :
405*0Sstevel@tonic-gate 					((double)ltmp - pcmt.pcm_io[PRIME]);
406*0Sstevel@tonic-gate 				ltmp = expand(acct.ab.ac_rw);
407*0Sstevel@tonic-gate 				pcmt.pcm_rw[PRIME] = (double)ltmp * ratio;
408*0Sstevel@tonic-gate 				pcmt.pcm_rw[NONPRIME] = (ratio == 1.0) ? 0.0 :
409*0Sstevel@tonic-gate 					((double)ltmp - pcmt.pcm_rw[PRIME]);
410*0Sstevel@tonic-gate 				enter(&pcmt);
411*0Sstevel@tonic-gate 			}
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 			break;
414*0Sstevel@tonic-gate 		}
415*0Sstevel@tonic-gate 	}
416*0Sstevel@tonic-gate }
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate tenter(p)
419*0Sstevel@tonic-gate register struct tcms *p;
420*0Sstevel@tonic-gate {
421*0Sstevel@tonic-gate 	register i;
422*0Sstevel@tonic-gate 	register j;
423*0Sstevel@tonic-gate 	register struct tcms *ntcm;
424*0Sstevel@tonic-gate 	for (i = j = 0; j < sizeof(p->tcm_comm); j++) {
425*0Sstevel@tonic-gate 		if (p->tcm_comm[j] && p->tcm_comm[j] <= 037)
426*0Sstevel@tonic-gate 			p->tcm_comm[j] = '?';
427*0Sstevel@tonic-gate 		i = i*7 + p->tcm_comm[j];	/* hash function */
428*0Sstevel@tonic-gate 	}
429*0Sstevel@tonic-gate 	if (i < 0)
430*0Sstevel@tonic-gate 		i = -i;
431*0Sstevel@tonic-gate 	for (i %= csize, j = 0; tcm[i].tcm_comm[0] && j != csize; i = (i+1)%csize, j++)
432*0Sstevel@tonic-gate 		if (EQN(p->tcm_comm, tcm[i].tcm_comm))
433*0Sstevel@tonic-gate 			break;
434*0Sstevel@tonic-gate 	if(j == csize) {
435*0Sstevel@tonic-gate 		if ((ntcm = (struct tcms *) realloc(tcm,
436*0Sstevel@tonic-gate 			(csize + CSIZE - 1) * sizeof (struct tcms))) == NULL) {
437*0Sstevel@tonic-gate 			fprintf(stderr,
438*0Sstevel@tonic-gate 				"acctcms: Cannot reallocate memory (tcm)\n");
439*0Sstevel@tonic-gate 			return(-1);
440*0Sstevel@tonic-gate 		} else {
441*0Sstevel@tonic-gate 			memset(&ntcm[csize], 0, CSIZE - 1);
442*0Sstevel@tonic-gate 			tcm = ntcm;
443*0Sstevel@tonic-gate 			if (!EQN(p->tcm_comm, tcm[i].tcm_comm))
444*0Sstevel@tonic-gate 				i = csize;
445*0Sstevel@tonic-gate 			csize = csize + CSIZE - 1;
446*0Sstevel@tonic-gate 		}
447*0Sstevel@tonic-gate 	}
448*0Sstevel@tonic-gate 	if (tcm[i].tcm_comm[0] == 0)
449*0Sstevel@tonic-gate 		CPYN(tcm[i].tcm_comm, p->tcm_comm);
450*0Sstevel@tonic-gate 	tcmadd(&tcm[i], p);
451*0Sstevel@tonic-gate 	return(i);
452*0Sstevel@tonic-gate }
453*0Sstevel@tonic-gate enter(p)
454*0Sstevel@tonic-gate register struct pcms *p;
455*0Sstevel@tonic-gate {
456*0Sstevel@tonic-gate 	register i;
457*0Sstevel@tonic-gate 	register j;
458*0Sstevel@tonic-gate 	register struct pcms *npcm;
459*0Sstevel@tonic-gate 	for (i = j = 0; j < sizeof(p->pcm_comm); j++) {
460*0Sstevel@tonic-gate 		if (p->pcm_comm[j] && p->pcm_comm[j] <= 037)
461*0Sstevel@tonic-gate 			p->pcm_comm[j] = '?';
462*0Sstevel@tonic-gate 		i = i*7 + p->pcm_comm[j];	/* hash function */
463*0Sstevel@tonic-gate 	}
464*0Sstevel@tonic-gate 	if (i < 0)
465*0Sstevel@tonic-gate 		i = -i;
466*0Sstevel@tonic-gate 	for (i %= csize, j = 0; pcm[i].pcm_comm[0] && j != csize; i = (i+1)%csize, j++)
467*0Sstevel@tonic-gate 		if (EQN(p->pcm_comm, pcm[i].pcm_comm))
468*0Sstevel@tonic-gate 			break;
469*0Sstevel@tonic-gate 	if(j == csize) {
470*0Sstevel@tonic-gate 		if ((npcm = (struct pcms *) realloc(pcm,
471*0Sstevel@tonic-gate 			(csize + CSIZE - 1) * sizeof (struct pcms))) == NULL) {
472*0Sstevel@tonic-gate 			fprintf(stderr,
473*0Sstevel@tonic-gate 				"acctcms: Cannot reallocate memory (pcm)\n");
474*0Sstevel@tonic-gate 			return(-1);
475*0Sstevel@tonic-gate 		} else {
476*0Sstevel@tonic-gate 			memset(&npcm[csize], 0, CSIZE - 1);
477*0Sstevel@tonic-gate 			pcm = npcm;
478*0Sstevel@tonic-gate 			if (!EQN(p->pcm_comm, pcm[i].pcm_comm))
479*0Sstevel@tonic-gate 				i = csize;
480*0Sstevel@tonic-gate 			csize = csize + CSIZE - 1;
481*0Sstevel@tonic-gate 		}
482*0Sstevel@tonic-gate 	}
483*0Sstevel@tonic-gate 	if (pcm[i].pcm_comm[0] == 0)
484*0Sstevel@tonic-gate 		CPYN(pcm[i].pcm_comm, p->pcm_comm);
485*0Sstevel@tonic-gate 	pcmadd(&pcm[i], p);
486*0Sstevel@tonic-gate 	return(i);
487*0Sstevel@tonic-gate }
488*0Sstevel@tonic-gate tfixjunk()	/* combine commands used only once */
489*0Sstevel@tonic-gate {
490*0Sstevel@tonic-gate 	register i, j;
491*0Sstevel@tonic-gate 	j = enter(&tcmtmp);
492*0Sstevel@tonic-gate 	for (i = 0; i < csize; i++)
493*0Sstevel@tonic-gate 		if (i != j && tcm[i].tcm_comm[0] && tcm[i].tcm_pc <= 1) {
494*0Sstevel@tonic-gate 			tcmadd(&tcm[j], &tcm[i]);
495*0Sstevel@tonic-gate 			tcm[i].tcm_comm[0] = 0;
496*0Sstevel@tonic-gate 		}
497*0Sstevel@tonic-gate }
498*0Sstevel@tonic-gate fixjunk()	/* combine commands used only once */
499*0Sstevel@tonic-gate {
500*0Sstevel@tonic-gate 	register i, j;
501*0Sstevel@tonic-gate 	j = enter(&pcmtmp);
502*0Sstevel@tonic-gate 	for (i = 0; i < csize; i++)
503*0Sstevel@tonic-gate 		if (i != j && pcm[i].pcm_comm[0] && (pcm[i].pcm_pc[PRIME] + pcm[i].pcm_pc[NONPRIME]) <= 1) {
504*0Sstevel@tonic-gate 			pcmadd(&pcm[j], &pcm[i]);
505*0Sstevel@tonic-gate 			pcm[i].pcm_comm[0] = 0;
506*0Sstevel@tonic-gate 		}
507*0Sstevel@tonic-gate }
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate tcmadd(p1, p2)
510*0Sstevel@tonic-gate register struct tcms *p1, *p2;
511*0Sstevel@tonic-gate {
512*0Sstevel@tonic-gate 	p1->tcm_pc += p2->tcm_pc;
513*0Sstevel@tonic-gate 	p1->tcm_cpu = p1->tcm_cpu + p2->tcm_cpu;
514*0Sstevel@tonic-gate 	p1->tcm_real = p1->tcm_real + p2->tcm_real;
515*0Sstevel@tonic-gate 	p1->tcm_kcore = p1->tcm_kcore + p2->tcm_kcore;
516*0Sstevel@tonic-gate 	p1->tcm_io += p2->tcm_io;
517*0Sstevel@tonic-gate 	p1->tcm_rw += p2->tcm_rw;
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate pcmadd(p1, p2)
520*0Sstevel@tonic-gate register struct pcms *p1, *p2;
521*0Sstevel@tonic-gate {
522*0Sstevel@tonic-gate 	p1->pcm_pc[PRIME] += p2->pcm_pc[PRIME];
523*0Sstevel@tonic-gate 	p1->pcm_pc[NONPRIME] += p2->pcm_pc[NONPRIME];
524*0Sstevel@tonic-gate 	p1->pcm_cpu[PRIME] += p2->pcm_cpu[PRIME];
525*0Sstevel@tonic-gate 	p1->pcm_cpu[NONPRIME] += p2->pcm_cpu[NONPRIME];
526*0Sstevel@tonic-gate 	p1->pcm_real[PRIME] += p2->pcm_real[PRIME];
527*0Sstevel@tonic-gate 	p1->pcm_real[NONPRIME] += p2->pcm_real[NONPRIME];
528*0Sstevel@tonic-gate 	p1->pcm_kcore[PRIME] += p2->pcm_kcore[PRIME];
529*0Sstevel@tonic-gate 	p1->pcm_kcore[NONPRIME] += p2->pcm_kcore[NONPRIME];
530*0Sstevel@tonic-gate 	p1->pcm_io[PRIME] += p2->pcm_io[PRIME];
531*0Sstevel@tonic-gate 	p1->pcm_io[NONPRIME] += p2->pcm_io[NONPRIME];
532*0Sstevel@tonic-gate 	p1->pcm_rw[PRIME] += p2->pcm_rw[PRIME];
533*0Sstevel@tonic-gate 	p1->pcm_rw[NONPRIME] += p2->pcm_rw[NONPRIME];
534*0Sstevel@tonic-gate }
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate tsqueeze()	/* get rid of holes in hash table */
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate 	register i, k;
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	for (i = k = 0; i < csize; i++)
541*0Sstevel@tonic-gate 		if (tcm[i].tcm_comm[0]) {
542*0Sstevel@tonic-gate 			CPYN(tcm[k].tcm_comm, tcm[i].tcm_comm);
543*0Sstevel@tonic-gate 			tcm[k].tcm_pc = tcm[i].tcm_pc;
544*0Sstevel@tonic-gate 			tcm[k].tcm_cpu = tcm[i].tcm_cpu;
545*0Sstevel@tonic-gate 			tcm[k].tcm_real = tcm[i].tcm_real;
546*0Sstevel@tonic-gate 			tcm[k].tcm_kcore = tcm[i].tcm_kcore;
547*0Sstevel@tonic-gate 			tcm[k].tcm_io = tcm[i].tcm_io;
548*0Sstevel@tonic-gate 			tcm[k].tcm_rw = tcm[i].tcm_rw;
549*0Sstevel@tonic-gate 			k++;
550*0Sstevel@tonic-gate 		}
551*0Sstevel@tonic-gate 	csize = k;
552*0Sstevel@tonic-gate }
553*0Sstevel@tonic-gate squeeze()	/* get rid of holes in hash table */
554*0Sstevel@tonic-gate {
555*0Sstevel@tonic-gate 	register i, k;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	for (i = k = 0; i < csize; i++)
558*0Sstevel@tonic-gate 		if (pcm[i].pcm_comm[0]) {
559*0Sstevel@tonic-gate 			CPYN(pcm[k].pcm_comm, pcm[i].pcm_comm);
560*0Sstevel@tonic-gate 			pcm[k].pcm_pc[PRIME] = pcm[i].pcm_pc[PRIME];
561*0Sstevel@tonic-gate 			pcm[k].pcm_pc[NONPRIME] = pcm[i].pcm_pc[NONPRIME];
562*0Sstevel@tonic-gate 			pcm[k].pcm_cpu[PRIME] = pcm[i].pcm_cpu[PRIME];
563*0Sstevel@tonic-gate 			pcm[k].pcm_cpu[NONPRIME] = pcm[i].pcm_cpu[NONPRIME];
564*0Sstevel@tonic-gate 			pcm[k].pcm_real[PRIME] = pcm[i].pcm_real[PRIME];
565*0Sstevel@tonic-gate 			pcm[k].pcm_real[NONPRIME] = pcm[i].pcm_real[NONPRIME];
566*0Sstevel@tonic-gate 			pcm[k].pcm_kcore[PRIME] = pcm[i].pcm_kcore[PRIME];
567*0Sstevel@tonic-gate 			pcm[k].pcm_kcore[NONPRIME] = pcm[i].pcm_kcore[NONPRIME];
568*0Sstevel@tonic-gate 			pcm[k].pcm_io[PRIME] = pcm[i].pcm_io[PRIME];
569*0Sstevel@tonic-gate 			pcm[k].pcm_io[NONPRIME] = pcm[i].pcm_io[NONPRIME];
570*0Sstevel@tonic-gate 			pcm[k].pcm_rw[PRIME] = pcm[i].pcm_rw[PRIME];
571*0Sstevel@tonic-gate 			pcm[k].pcm_rw[NONPRIME] = pcm[i].pcm_rw[NONPRIME];
572*0Sstevel@tonic-gate 			k++;
573*0Sstevel@tonic-gate 		}
574*0Sstevel@tonic-gate 	csize = k;
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate tccmp(p1, p2)
578*0Sstevel@tonic-gate register struct tcms *p1, *p2;
579*0Sstevel@tonic-gate {
580*0Sstevel@tonic-gate 	if (p1->tcm_cpu == p2->tcm_cpu)
581*0Sstevel@tonic-gate 		return(0);
582*0Sstevel@tonic-gate 	return ((p2->tcm_cpu > p1->tcm_cpu)? 1 : -1);
583*0Sstevel@tonic-gate }
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate ccmp(p1, p2)
586*0Sstevel@tonic-gate register struct pcms *p1, *p2;
587*0Sstevel@tonic-gate {
588*0Sstevel@tonic-gate 	register int	index;
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	if( (pflg && oflg) || (!pflg && !oflg) ) {
591*0Sstevel@tonic-gate 		if (p1->pcm_cpu[PRIME] + p1->pcm_cpu[NONPRIME] == p2->pcm_cpu[PRIME] + p2->pcm_cpu[NONPRIME])
592*0Sstevel@tonic-gate 			return(0);
593*0Sstevel@tonic-gate 		return ((p2->pcm_cpu[PRIME] + p2->pcm_cpu[NONPRIME] > p1->pcm_cpu[PRIME] + p1->pcm_cpu[NONPRIME])? 1 : -1);
594*0Sstevel@tonic-gate 	}
595*0Sstevel@tonic-gate 	index = pflg ? PRIME : NONPRIME;
596*0Sstevel@tonic-gate 	if (p1->pcm_cpu[index] == p2->pcm_cpu[index])
597*0Sstevel@tonic-gate 		return(0);
598*0Sstevel@tonic-gate 	return ((p2->pcm_cpu[index] > p1->pcm_cpu[index])? 1 : -1);
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate tkcmp(p1, p2)
602*0Sstevel@tonic-gate register struct tcms *p1, *p2;
603*0Sstevel@tonic-gate {
604*0Sstevel@tonic-gate 	if (p1->tcm_kcore == p2->tcm_kcore)
605*0Sstevel@tonic-gate 		return(0);
606*0Sstevel@tonic-gate 	return ((p2->tcm_kcore > p1->tcm_kcore)? 1 : -1);
607*0Sstevel@tonic-gate }
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate kcmp(p1, p2)
610*0Sstevel@tonic-gate register struct pcms *p1, *p2;
611*0Sstevel@tonic-gate {
612*0Sstevel@tonic-gate 	register int	index;
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	if( (pflg && oflg) || (!pflg && !pflg) ){
615*0Sstevel@tonic-gate 		if (p1->pcm_kcore[PRIME] + p1->pcm_kcore[NONPRIME] == p2->pcm_kcore[PRIME] + p2->pcm_kcore[NONPRIME])
616*0Sstevel@tonic-gate 			return(0);
617*0Sstevel@tonic-gate 		return ((p2->pcm_kcore[PRIME] + p2->pcm_kcore[NONPRIME] > p1->pcm_kcore[PRIME] + p1->pcm_kcore[NONPRIME])? 1 : -1);
618*0Sstevel@tonic-gate 	}
619*0Sstevel@tonic-gate 	index = pflg ? PRIME : NONPRIME;
620*0Sstevel@tonic-gate 	if (p1->pcm_kcore[index] == p2->pcm_kcore[index])
621*0Sstevel@tonic-gate 		return(0);
622*0Sstevel@tonic-gate 	return ((p2->pcm_kcore[index] > p1->pcm_kcore[index])? 1 : -1);
623*0Sstevel@tonic-gate }
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate tncmp(p1, p2)
626*0Sstevel@tonic-gate register struct tcms *p1, *p2;
627*0Sstevel@tonic-gate {
628*0Sstevel@tonic-gate 	if (p1->tcm_pc == p2->tcm_pc)
629*0Sstevel@tonic-gate 		return(0);
630*0Sstevel@tonic-gate 	return ((p2->tcm_pc > p1->tcm_pc)? 1 : -1);
631*0Sstevel@tonic-gate }
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate ncmp(p1, p2)
634*0Sstevel@tonic-gate register struct pcms *p1, *p2;
635*0Sstevel@tonic-gate {
636*0Sstevel@tonic-gate 	register int	index;
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	if( (pflg && oflg) || (!pflg && !oflg) ) {
639*0Sstevel@tonic-gate 		if (p1->pcm_pc[PRIME] + p1->pcm_pc[NONPRIME] == p2->pcm_pc[PRIME] + p2->pcm_pc[NONPRIME])
640*0Sstevel@tonic-gate 			return(0);
641*0Sstevel@tonic-gate 		return ((p2->pcm_pc[PRIME] + p2->pcm_pc[NONPRIME] > p1->pcm_pc[PRIME] + p1->pcm_pc[NONPRIME])? 1 : -1);
642*0Sstevel@tonic-gate 	}
643*0Sstevel@tonic-gate 	index =  pflg ? PRIME : NONPRIME;
644*0Sstevel@tonic-gate 	if (p1->pcm_pc[index] == p2->pcm_pc[index])
645*0Sstevel@tonic-gate 		return(0);
646*0Sstevel@tonic-gate 	return ((p2->pcm_pc[index] > p1->pcm_pc[index])? 1 : -1);
647*0Sstevel@tonic-gate }
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate char	thd1[] =
650*0Sstevel@tonic-gate "COMMAND   NUMBER      TOTAL       TOTAL       TOTAL   MEAN     MEAN     HOG      CHARS        BLOCKS\n";
651*0Sstevel@tonic-gate char	thd2[] =
652*0Sstevel@tonic-gate "NAME        CMDS    KCOREMIN     CPU-MIN     REAL-MIN SIZE-K  CPU-MIN  FACTOR   TRNSFD         READ\n";
653*0Sstevel@tonic-gate toutpta()
654*0Sstevel@tonic-gate {
655*0Sstevel@tonic-gate 	register i;
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 	printf(thd1);
658*0Sstevel@tonic-gate 	printf(thd2);
659*0Sstevel@tonic-gate 	printf("\n");
660*0Sstevel@tonic-gate 	for (i = 0; i < csize; i++)
661*0Sstevel@tonic-gate 		tcmadd(&tcmtmp, &tcm[i]);
662*0Sstevel@tonic-gate 	CPYN(tcmtmp.tcm_comm, "TOTALS");
663*0Sstevel@tonic-gate 	tprint(&tcmtmp);
664*0Sstevel@tonic-gate 	printf("\n");
665*0Sstevel@tonic-gate 	for (i = 0; i < csize; i++)
666*0Sstevel@tonic-gate 		tprint(&tcm[i]);
667*0Sstevel@tonic-gate }
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate tprint(p)
670*0Sstevel@tonic-gate register struct tcms *p;
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	printf("%-8.8s", p->tcm_comm);
673*0Sstevel@tonic-gate 	printf(" %7ld", p->tcm_pc);
674*0Sstevel@tonic-gate 	printf(" %11.2f", p->tcm_kcore);
675*0Sstevel@tonic-gate 	printf(" %10.2f", p->tcm_cpu);
676*0Sstevel@tonic-gate 	printf(" %12.2f", p->tcm_real);
677*0Sstevel@tonic-gate 	if(p->tcm_cpu == 0)  p->tcm_cpu = 1;
678*0Sstevel@tonic-gate 	printf(" %6.2f", p->tcm_kcore/p->tcm_cpu);
679*0Sstevel@tonic-gate 	if(p->tcm_pc == 0)  p->tcm_pc = 1;
680*0Sstevel@tonic-gate 	printf(" %7.2f", p->tcm_cpu/p->tcm_pc);
681*0Sstevel@tonic-gate 	if (p->tcm_real == 0)
682*0Sstevel@tonic-gate 		p->tcm_real = 1;
683*0Sstevel@tonic-gate 	printf(" %8.2f", p->tcm_cpu/p->tcm_real);
684*0Sstevel@tonic-gate 	printf(" %11lu", p->tcm_io);
685*0Sstevel@tonic-gate 	printf(" %11lu\n", p->tcm_rw);
686*0Sstevel@tonic-gate }
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate toutptc()
689*0Sstevel@tonic-gate {
690*0Sstevel@tonic-gate 	register i;
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	for (i = 0; i < csize; i++)
693*0Sstevel@tonic-gate 		fwrite(&tcm[i], sizeof(tcm[i]), 1, stdout);
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate char	hd1[] =
697*0Sstevel@tonic-gate "COMMAND   NUMBER      TOTAL       TOTAL       TOTAL   MEAN    MEAN     HOG         CHARS     BLOCKS\n";
698*0Sstevel@tonic-gate char	hd2[] =
699*0Sstevel@tonic-gate "NAME        CMDS    KCOREMIN     CPU-MIN   REAL-MIN  SIZE-K  CPU-MIN  FACTOR      TRNSFD      READ\n";
700*0Sstevel@tonic-gate char	hd3[] =
701*0Sstevel@tonic-gate "COMMAND        NUMBER         TOTAL          CPU-MIN                 REAL-MIN        MEAN    MEAN      HOG       CHARS       BLOCKS\n";
702*0Sstevel@tonic-gate char	hd4[] =
703*0Sstevel@tonic-gate "NAME         (P)    (NP)   KCOREMIN       (P)      (NP)          (P)         (NP)  SIZE-K  CPU-MIN   FACTOR     TRNSFD        READ\n";
704*0Sstevel@tonic-gate char	hdprime[] =
705*0Sstevel@tonic-gate "                                   PRIME TIME COMMAND SUMMARY\n";
706*0Sstevel@tonic-gate char	hdnonprime[] =
707*0Sstevel@tonic-gate "                                  NON-PRIME TIME COMMAND SUMMARY\n";
708*0Sstevel@tonic-gate char	hdtot[] =
709*0Sstevel@tonic-gate "                                     TOTAL COMMAND SUMMARY\n";
710*0Sstevel@tonic-gate char	hdp[] =
711*0Sstevel@tonic-gate "                                PRIME/NON-PRIME TIME COMMAND SUMMARY\n";
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate outputa()
714*0Sstevel@tonic-gate {
715*0Sstevel@tonic-gate 	register i;
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	if( pflg && oflg ) printf(hdp);
718*0Sstevel@tonic-gate 	else if(pflg) printf(hdprime);
719*0Sstevel@tonic-gate 	else if(oflg) printf(hdnonprime);
720*0Sstevel@tonic-gate 	else printf(hdtot);
721*0Sstevel@tonic-gate 	if( (!pflg && !oflg) || (pflg ^ oflg)) {
722*0Sstevel@tonic-gate 		printf(hd1);
723*0Sstevel@tonic-gate 		printf(hd2);
724*0Sstevel@tonic-gate 	}
725*0Sstevel@tonic-gate 	else {
726*0Sstevel@tonic-gate 		printf(hd3);
727*0Sstevel@tonic-gate 		printf(hd4);
728*0Sstevel@tonic-gate 	}
729*0Sstevel@tonic-gate 	printf("\n");
730*0Sstevel@tonic-gate 	for (i = 0; i < csize; i++)
731*0Sstevel@tonic-gate 		pcmadd(&pcmtmp, &pcm[i]);
732*0Sstevel@tonic-gate 	CPYN(pcmtmp.pcm_comm, "TOTALS");
733*0Sstevel@tonic-gate 	print(&pcmtmp);
734*0Sstevel@tonic-gate 	printf("\n");
735*0Sstevel@tonic-gate 	for (i = 0; i < csize; i++)
736*0Sstevel@tonic-gate 		print(&pcm[i]);
737*0Sstevel@tonic-gate }
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate print(p)
740*0Sstevel@tonic-gate register struct pcms *p;
741*0Sstevel@tonic-gate {
742*0Sstevel@tonic-gate 	if(pflg && oflg) pprint(p);
743*0Sstevel@tonic-gate 	else if(pflg || oflg) prnt(p, pflg ? PRIME : NONPRIME);
744*0Sstevel@tonic-gate 	else totprnt(p);
745*0Sstevel@tonic-gate }
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate prnt(p, hr)
748*0Sstevel@tonic-gate register struct pcms *p;
749*0Sstevel@tonic-gate register int	hr;
750*0Sstevel@tonic-gate {
751*0Sstevel@tonic-gate 	if(p->pcm_pc[hr] == 0) return;
752*0Sstevel@tonic-gate 	printf(fmtcmd, p->pcm_comm);
753*0Sstevel@tonic-gate 	printf(fmtcnt, p->pcm_pc[hr]);
754*0Sstevel@tonic-gate 	printf(fmtkcore, p->pcm_kcore[hr]);
755*0Sstevel@tonic-gate 	printf(fmtcpu, p->pcm_cpu[hr]);
756*0Sstevel@tonic-gate 	printf(fmtreal, p->pcm_real[hr]);
757*0Sstevel@tonic-gate 	if(p->pcm_cpu[hr] == 0)  p->pcm_cpu[hr] = 1;
758*0Sstevel@tonic-gate 	printf(fmtmsz, p->pcm_kcore[hr]/p->pcm_cpu[hr]);
759*0Sstevel@tonic-gate 	if(p->pcm_pc[hr] == 0)  p->pcm_pc[hr] = 1;
760*0Sstevel@tonic-gate 	printf(fmtmcpu, p->pcm_cpu[hr]/p->pcm_pc[hr]);
761*0Sstevel@tonic-gate 	if (p->pcm_real[hr] == 0)
762*0Sstevel@tonic-gate 		p->pcm_real[hr] = 1;
763*0Sstevel@tonic-gate 	printf(fmthog, p->pcm_cpu[hr]/p->pcm_real[hr]);
764*0Sstevel@tonic-gate 	printf(fmtcharx,p->pcm_io[hr]);
765*0Sstevel@tonic-gate 	printf(fmtblkx,p->pcm_rw[hr]);
766*0Sstevel@tonic-gate 	printf("\n");
767*0Sstevel@tonic-gate }
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate pprint(p)
770*0Sstevel@tonic-gate register struct pcms *p;
771*0Sstevel@tonic-gate {
772*0Sstevel@tonic-gate 	printf(fmtcmd, p->pcm_comm);
773*0Sstevel@tonic-gate 	printf(fmtcnt, p->pcm_pc[PRIME]);
774*0Sstevel@tonic-gate 	printf(fmtcnt, p->pcm_pc[NONPRIME]);
775*0Sstevel@tonic-gate 	printf(fmtkcore, TOTAL(p->pcm_kcore));
776*0Sstevel@tonic-gate 	printf(fmtcpu, p->pcm_cpu[PRIME]);
777*0Sstevel@tonic-gate 	printf(fmtcpu, p->pcm_cpu[NONPRIME]);
778*0Sstevel@tonic-gate 	printf(fmtreal, p->pcm_real[PRIME]);
779*0Sstevel@tonic-gate 	printf(fmtreal, p->pcm_real[NONPRIME]);
780*0Sstevel@tonic-gate 	if(TOTAL(p->pcm_cpu) == 0)  p->pcm_cpu[PRIME] = 1;
781*0Sstevel@tonic-gate 	printf(fmtmsz, TOTAL(p->pcm_kcore)/TOTAL(p->pcm_cpu));
782*0Sstevel@tonic-gate 	if(TOTAL(p->pcm_pc) == 0)  p->pcm_pc[PRIME] = 1;
783*0Sstevel@tonic-gate 	printf(fmtmcpu, TOTAL(p->pcm_cpu)/TOTAL(p->pcm_pc));
784*0Sstevel@tonic-gate 	if ( TOTAL(p->pcm_real) == 0)
785*0Sstevel@tonic-gate 		p->pcm_real[PRIME] = 1;
786*0Sstevel@tonic-gate 	printf(fmthog, TOTAL(p->pcm_cpu)/TOTAL(p->pcm_real));
787*0Sstevel@tonic-gate 	printf(fmtcharx,TOTAL(p->pcm_io));
788*0Sstevel@tonic-gate 	printf(fmtblkx, TOTAL(p->pcm_rw));
789*0Sstevel@tonic-gate 	printf("\n");
790*0Sstevel@tonic-gate }
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate totprnt(p)
793*0Sstevel@tonic-gate register struct pcms *p;
794*0Sstevel@tonic-gate {
795*0Sstevel@tonic-gate 	printf(fmtcmd, p->pcm_comm);
796*0Sstevel@tonic-gate 	printf(fmtcnt, TOTAL(p->pcm_pc));
797*0Sstevel@tonic-gate 	printf(fmtkcore, TOTAL(p->pcm_kcore));
798*0Sstevel@tonic-gate 	printf(fmtcpu, TOTAL(p->pcm_cpu));
799*0Sstevel@tonic-gate 	printf(fmtreal, TOTAL(p->pcm_real));
800*0Sstevel@tonic-gate 	if(TOTAL(p->pcm_cpu) == 0)  p->pcm_cpu[PRIME] = 1;
801*0Sstevel@tonic-gate 	printf(fmtmsz, TOTAL(p->pcm_kcore)/TOTAL(p->pcm_cpu));
802*0Sstevel@tonic-gate 	if(TOTAL(p->pcm_pc) == 0)  p->pcm_pc[PRIME] = 1;
803*0Sstevel@tonic-gate 	printf(fmtmcpu, TOTAL(p->pcm_cpu)/TOTAL(p->pcm_pc));
804*0Sstevel@tonic-gate 	if (TOTAL(p->pcm_real) == 0)
805*0Sstevel@tonic-gate 		p->pcm_real[PRIME] = 1;
806*0Sstevel@tonic-gate 	printf(fmthog, TOTAL(p->pcm_cpu)/TOTAL(p->pcm_real));
807*0Sstevel@tonic-gate 	printf(fmtcharx,TOTAL(p->pcm_io));
808*0Sstevel@tonic-gate 	printf(fmtblkx,TOTAL(p->pcm_rw));
809*0Sstevel@tonic-gate 	printf("\n");
810*0Sstevel@tonic-gate }
811*0Sstevel@tonic-gate outputc()
812*0Sstevel@tonic-gate {
813*0Sstevel@tonic-gate 	register i;
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 	for (i = 0; i < csize; i++)
816*0Sstevel@tonic-gate 		fwrite(&pcm[i], sizeof(pcm[i]), 1, stdout);
817*0Sstevel@tonic-gate }
818