xref: /onnv-gate/usr/src/cmd/acct/acctmerg.c (revision 428:a261479c3dbd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
25*428Ssl108498 /*
26*428Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27*428Ssl108498  * Use is subject to license terms.
28*428Ssl108498  */
29*428Ssl108498 #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *	acctmerg [-a] [-i] [-p] [-t] [-u] [-v] [file...]
330Sstevel@tonic-gate  *	-a	output in tacct.h/ascii (instead of tacct.h)
340Sstevel@tonic-gate  *	-i	input is in tacct.h/ascii (instead of tacct.h)
350Sstevel@tonic-gate  *	-p	print input files with no processing
360Sstevel@tonic-gate  *	-t	output single record that totals all input
370Sstevel@tonic-gate  *	-u	summarize by uid, rather than uid/name
380Sstevel@tonic-gate  *	-v	output in verbose tacct.h/ascii
390Sstevel@tonic-gate  *	reads std input and 0-NFILE files, all in tacct.h format,
400Sstevel@tonic-gate  *	sorted by uid/name.
410Sstevel@tonic-gate  *	merge/adds all records with same uid/name (or same uid if -u,
420Sstevel@tonic-gate  *	or all records if -t], writes to std. output
430Sstevel@tonic-gate  *	(still in tacct.h format)
440Sstevel@tonic-gate  *	note that this can be used to summarize the std input
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <stdio.h>
480Sstevel@tonic-gate #include <sys/types.h>
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include "acctdef.h"
51*428Ssl108498 #include <stdlib.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int	nfile;			/* index of last used in fl */
540Sstevel@tonic-gate FILE	*fl[NFILE]	= {stdin};
550Sstevel@tonic-gate 
560Sstevel@tonic-gate struct	tacct tb[NFILE];	/* current record from each file */
570Sstevel@tonic-gate struct	tacct	tt = {
580Sstevel@tonic-gate 	0,
590Sstevel@tonic-gate 	"TOTAL"
600Sstevel@tonic-gate };
610Sstevel@tonic-gate int	asciiout;
620Sstevel@tonic-gate int	asciiinp;
630Sstevel@tonic-gate int	printonly;
640Sstevel@tonic-gate int	totalonly;
650Sstevel@tonic-gate int	uidsum;
660Sstevel@tonic-gate int	verbose;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate int 	exitcode = 0;
690Sstevel@tonic-gate 
70*428Ssl108498 void prtacct(struct tacct *);
71*428Ssl108498 struct tacct *getleast(void);
72*428Ssl108498 void output(struct tacct *);
73*428Ssl108498 void tacctadd(struct tacct *, struct tacct *);
74*428Ssl108498 void sumcurr(struct tacct *);
75*428Ssl108498 
76*428Ssl108498 int
main(int argc,char ** argv)77*428Ssl108498 main(int argc, char **argv)
780Sstevel@tonic-gate {
79*428Ssl108498 	int i;
80*428Ssl108498 	struct tacct *tp;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	while (--argc > 0) {
830Sstevel@tonic-gate 		if (**++argv == '-')
840Sstevel@tonic-gate 			switch (*++*argv) {
850Sstevel@tonic-gate 			case 'a':
860Sstevel@tonic-gate 				asciiout++;
870Sstevel@tonic-gate 				continue;
880Sstevel@tonic-gate 			case 'i':
890Sstevel@tonic-gate 				asciiinp++;
900Sstevel@tonic-gate 				continue;
910Sstevel@tonic-gate 			case 'p':
920Sstevel@tonic-gate 				printonly++;
930Sstevel@tonic-gate 				continue;
940Sstevel@tonic-gate 			case 't':
950Sstevel@tonic-gate 				totalonly++;
960Sstevel@tonic-gate 				continue;
970Sstevel@tonic-gate 			case 'u':
980Sstevel@tonic-gate 				uidsum++;
990Sstevel@tonic-gate 				continue;
1000Sstevel@tonic-gate 			case 'v':
1010Sstevel@tonic-gate 				verbose++;
1020Sstevel@tonic-gate 				asciiout++;
1030Sstevel@tonic-gate 				continue;
1040Sstevel@tonic-gate 			}
1050Sstevel@tonic-gate 		else {
1060Sstevel@tonic-gate 			if (++nfile >= NFILE) {
1070Sstevel@tonic-gate 				fprintf(stderr, "acctmerg: >%d files\n", NFILE);
1080Sstevel@tonic-gate 				exit(1);
1090Sstevel@tonic-gate 			}
1100Sstevel@tonic-gate 			if ((fl[nfile] = fopen(*argv, "r")) == NULL) {
1110Sstevel@tonic-gate 				fprintf(stderr, "acctmerg: can't open %s\n", *argv);
1120Sstevel@tonic-gate 				exitcode = 1;
1130Sstevel@tonic-gate 				/*	exit(1); 	*/
1140Sstevel@tonic-gate 			}
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (printonly) {
1190Sstevel@tonic-gate 		for (i = 0; i <= nfile; i++)
1200Sstevel@tonic-gate 			while (getnext(i))
1210Sstevel@tonic-gate 				prtacct(&tb[i]);
1220Sstevel@tonic-gate 		exit(exitcode);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	for (i = 0; i <= nfile; i++)
1260Sstevel@tonic-gate 		if(getnext(i) == 0) {
1270Sstevel@tonic-gate 			fprintf(stderr,"acctmerg: read error file %d.  File may be empty.\n", i);
1280Sstevel@tonic-gate 			exitcode = 2;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	while ((tp = getleast()) != NULL)	/* get least uid of all files, */
1330Sstevel@tonic-gate 		sumcurr(tp);			/* sum all entries for that uid, */
1340Sstevel@tonic-gate 	if (totalonly)				/* and write the 'summed' record */
1350Sstevel@tonic-gate 		output(&tt);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	exit(exitcode);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  *	getleast returns ptr to least (lowest uid)  element of current
1420Sstevel@tonic-gate  *	avail, NULL if none left; always returns 1st of equals
1430Sstevel@tonic-gate  */
144*428Ssl108498 struct tacct *
getleast(void)145*428Ssl108498 getleast(void)
1460Sstevel@tonic-gate {
147*428Ssl108498 	struct tacct *tp, *least;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	least = NULL;
1500Sstevel@tonic-gate 	for (tp = tb; tp <= &tb[nfile]; tp++) {
1510Sstevel@tonic-gate 		if (tp->ta_name[0] == '\0')
1520Sstevel@tonic-gate 			continue;
1530Sstevel@tonic-gate 		if (least == NULL ||
1540Sstevel@tonic-gate 			tp->ta_uid < least->ta_uid ||
1550Sstevel@tonic-gate 			((tp->ta_uid == least->ta_uid) &&
1560Sstevel@tonic-gate 			!uidsum &&
1570Sstevel@tonic-gate 			(strncmp(tp->ta_name, least->ta_name, NSZ) < 0)))
1580Sstevel@tonic-gate 			least = tp;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	return(least);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  *	sumcurr sums all entries with same uid/name (into tp->tacct record)
1650Sstevel@tonic-gate  *	writes it out, gets new entry
1660Sstevel@tonic-gate  */
167*428Ssl108498 void
sumcurr(struct tacct * tp)168*428Ssl108498 sumcurr(struct tacct *tp)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	struct tacct tc;
1710Sstevel@tonic-gate 	char *memcpy();
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	memcpy(&tc, tp, sizeof(struct tacct));
1740Sstevel@tonic-gate 	tacctadd(&tt, tp);	/* gets total of all uids */
1750Sstevel@tonic-gate 	getnext(tp-&tb[0]);	/* get next one in same file */
1760Sstevel@tonic-gate 	while (tp <= &tb[nfile])
1770Sstevel@tonic-gate 		if (tp->ta_name[0] != '\0' &&
1780Sstevel@tonic-gate 			tp->ta_uid == tc.ta_uid &&
1790Sstevel@tonic-gate 			(uidsum || EQN(tp->ta_name, tc.ta_name))) {
1800Sstevel@tonic-gate 			tacctadd(&tc, tp);
1810Sstevel@tonic-gate 			tacctadd(&tt, tp);
1820Sstevel@tonic-gate 			getnext(tp-&tb[0]);
1830Sstevel@tonic-gate 		} else
1840Sstevel@tonic-gate 			tp++;	/* look at next file */
1850Sstevel@tonic-gate 	if (!totalonly)
1860Sstevel@tonic-gate 		output(&tc);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
189*428Ssl108498 void
tacctadd(struct tacct * t1,struct tacct * t2)190*428Ssl108498 tacctadd(struct tacct *t1, struct tacct *t2)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	t1->ta_cpu[0] = t1->ta_cpu[0] + t2->ta_cpu[0];
1930Sstevel@tonic-gate 	t1->ta_cpu[1] = t1->ta_cpu[1] + t2->ta_cpu[1];
1940Sstevel@tonic-gate 	t1->ta_kcore[0] = t1->ta_kcore[0] + t2->ta_kcore[0];
1950Sstevel@tonic-gate 	t1->ta_kcore[1] = t1->ta_kcore[1] + t2->ta_kcore[1];
1960Sstevel@tonic-gate 	t1->ta_con[0] = t1->ta_con[0] + t2->ta_con[0];
1970Sstevel@tonic-gate 	t1->ta_con[1] = t1->ta_con[1] + t2->ta_con[1];
1980Sstevel@tonic-gate 	t1->ta_du = t1->ta_du + t2->ta_du;
1990Sstevel@tonic-gate 	t1->ta_pc += t2->ta_pc;
2000Sstevel@tonic-gate 	t1->ta_sc += t2->ta_sc;
2010Sstevel@tonic-gate 	t1->ta_dc += t2->ta_dc;
2020Sstevel@tonic-gate 	t1->ta_fee += t2->ta_fee;
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
205*428Ssl108498 void
output(struct tacct * tp)206*428Ssl108498 output(struct tacct *tp)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	if (asciiout)
2090Sstevel@tonic-gate 		prtacct(tp);
2100Sstevel@tonic-gate 	else
2110Sstevel@tonic-gate 		fwrite(tp, sizeof(*tp), 1, stdout);
2120Sstevel@tonic-gate }
213*428Ssl108498 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  *	getnext reads next record from stream i, returns 1 if one existed
2160Sstevel@tonic-gate  */
217*428Ssl108498 int
getnext(int i)218*428Ssl108498 getnext(int i)
2190Sstevel@tonic-gate {
220*428Ssl108498 	struct tacct *tp;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	tp = &tb[i];
2230Sstevel@tonic-gate 	tp->ta_name[0] = '\0';
2240Sstevel@tonic-gate 	if (fl[i] == NULL)
2250Sstevel@tonic-gate 		return(0);
2260Sstevel@tonic-gate 	if (asciiinp) {
2270Sstevel@tonic-gate 		if (fscanf(fl[i],
2280Sstevel@tonic-gate 			"%ld\t%s\t%e %e %e %e %e %e %e %lu\t%hu\t%hu\t%hu",
2290Sstevel@tonic-gate 			&tp->ta_uid,
2300Sstevel@tonic-gate 			tp->ta_name,
2310Sstevel@tonic-gate 			&tp->ta_cpu[0], &tp->ta_cpu[1],
2320Sstevel@tonic-gate 			&tp->ta_kcore[0], &tp->ta_kcore[1],
2330Sstevel@tonic-gate 			&tp->ta_con[0], &tp->ta_con[1],
2340Sstevel@tonic-gate 			&tp->ta_du,
2350Sstevel@tonic-gate 			&tp->ta_pc,
2360Sstevel@tonic-gate 			&tp->ta_sc,
2370Sstevel@tonic-gate 			&tp->ta_dc,
2380Sstevel@tonic-gate 			&tp->ta_fee) != EOF)
2390Sstevel@tonic-gate 			return(1);
2400Sstevel@tonic-gate 	} else {
2410Sstevel@tonic-gate 		if (fread(tp, sizeof(*tp), 1, fl[i]) == 1)
2420Sstevel@tonic-gate 			return(1);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 	fclose(fl[i]);
2450Sstevel@tonic-gate 	fl[i] = NULL;
2460Sstevel@tonic-gate 	return(0);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate char fmt[] = "%ld\t%.*s\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\t%lu\t%hu\t%hu\t%hu\n";
2500Sstevel@tonic-gate char fmtv[] = "%ld\t%.*s\t%e %e %e %e %e %e %e %lu %hu\t%hu\t%hu\n";
2510Sstevel@tonic-gate 
252*428Ssl108498 void
prtacct(struct tacct * tp)253*428Ssl108498 prtacct(struct tacct *tp)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	printf(verbose ? fmtv : fmt,
2560Sstevel@tonic-gate 	    tp->ta_uid,
2570Sstevel@tonic-gate 	    OUTPUT_NSZ,
2580Sstevel@tonic-gate 	    tp->ta_name,
2590Sstevel@tonic-gate 	    tp->ta_cpu[0], tp->ta_cpu[1],
2600Sstevel@tonic-gate 	    tp->ta_kcore[0], tp->ta_kcore[1],
2610Sstevel@tonic-gate 	    tp->ta_con[0], tp->ta_con[1],
2620Sstevel@tonic-gate 	    tp->ta_du,
2630Sstevel@tonic-gate 	    tp->ta_pc,
2640Sstevel@tonic-gate 	    tp->ta_sc,
2650Sstevel@tonic-gate 	    tp->ta_dc,
2660Sstevel@tonic-gate 	    tp->ta_fee);
2670Sstevel@tonic-gate }
268