xref: /titanic_52/usr/src/cmd/acct/acctmerg.c (revision 414388d7cb2ee98771e2ac7c2338c460abd44304)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
25*414388d7Ssl108498 /*
26*414388d7Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27*414388d7Ssl108498  * Use is subject to license terms.
28*414388d7Ssl108498  */
29*414388d7Ssl108498 #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *	acctmerg [-a] [-i] [-p] [-t] [-u] [-v] [file...]
337c478bd9Sstevel@tonic-gate  *	-a	output in tacct.h/ascii (instead of tacct.h)
347c478bd9Sstevel@tonic-gate  *	-i	input is in tacct.h/ascii (instead of tacct.h)
357c478bd9Sstevel@tonic-gate  *	-p	print input files with no processing
367c478bd9Sstevel@tonic-gate  *	-t	output single record that totals all input
377c478bd9Sstevel@tonic-gate  *	-u	summarize by uid, rather than uid/name
387c478bd9Sstevel@tonic-gate  *	-v	output in verbose tacct.h/ascii
397c478bd9Sstevel@tonic-gate  *	reads std input and 0-NFILE files, all in tacct.h format,
407c478bd9Sstevel@tonic-gate  *	sorted by uid/name.
417c478bd9Sstevel@tonic-gate  *	merge/adds all records with same uid/name (or same uid if -u,
427c478bd9Sstevel@tonic-gate  *	or all records if -t], writes to std. output
437c478bd9Sstevel@tonic-gate  *	(still in tacct.h format)
447c478bd9Sstevel@tonic-gate  *	note that this can be used to summarize the std input
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <sys/param.h>
507c478bd9Sstevel@tonic-gate #include "acctdef.h"
51*414388d7Ssl108498 #include <stdlib.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int	nfile;			/* index of last used in fl */
547c478bd9Sstevel@tonic-gate FILE	*fl[NFILE]	= {stdin};
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate struct	tacct tb[NFILE];	/* current record from each file */
577c478bd9Sstevel@tonic-gate struct	tacct	tt = {
587c478bd9Sstevel@tonic-gate 	0,
597c478bd9Sstevel@tonic-gate 	"TOTAL"
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate int	asciiout;
627c478bd9Sstevel@tonic-gate int	asciiinp;
637c478bd9Sstevel@tonic-gate int	printonly;
647c478bd9Sstevel@tonic-gate int	totalonly;
657c478bd9Sstevel@tonic-gate int	uidsum;
667c478bd9Sstevel@tonic-gate int	verbose;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int 	exitcode = 0;
697c478bd9Sstevel@tonic-gate 
70*414388d7Ssl108498 void prtacct(struct tacct *);
71*414388d7Ssl108498 struct tacct *getleast(void);
72*414388d7Ssl108498 void output(struct tacct *);
73*414388d7Ssl108498 void tacctadd(struct tacct *, struct tacct *);
74*414388d7Ssl108498 void sumcurr(struct tacct *);
75*414388d7Ssl108498 
76*414388d7Ssl108498 int
77*414388d7Ssl108498 main(int argc, char **argv)
787c478bd9Sstevel@tonic-gate {
79*414388d7Ssl108498 	int i;
80*414388d7Ssl108498 	struct tacct *tp;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	while (--argc > 0) {
837c478bd9Sstevel@tonic-gate 		if (**++argv == '-')
847c478bd9Sstevel@tonic-gate 			switch (*++*argv) {
857c478bd9Sstevel@tonic-gate 			case 'a':
867c478bd9Sstevel@tonic-gate 				asciiout++;
877c478bd9Sstevel@tonic-gate 				continue;
887c478bd9Sstevel@tonic-gate 			case 'i':
897c478bd9Sstevel@tonic-gate 				asciiinp++;
907c478bd9Sstevel@tonic-gate 				continue;
917c478bd9Sstevel@tonic-gate 			case 'p':
927c478bd9Sstevel@tonic-gate 				printonly++;
937c478bd9Sstevel@tonic-gate 				continue;
947c478bd9Sstevel@tonic-gate 			case 't':
957c478bd9Sstevel@tonic-gate 				totalonly++;
967c478bd9Sstevel@tonic-gate 				continue;
977c478bd9Sstevel@tonic-gate 			case 'u':
987c478bd9Sstevel@tonic-gate 				uidsum++;
997c478bd9Sstevel@tonic-gate 				continue;
1007c478bd9Sstevel@tonic-gate 			case 'v':
1017c478bd9Sstevel@tonic-gate 				verbose++;
1027c478bd9Sstevel@tonic-gate 				asciiout++;
1037c478bd9Sstevel@tonic-gate 				continue;
1047c478bd9Sstevel@tonic-gate 			}
1057c478bd9Sstevel@tonic-gate 		else {
1067c478bd9Sstevel@tonic-gate 			if (++nfile >= NFILE) {
1077c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctmerg: >%d files\n", NFILE);
1087c478bd9Sstevel@tonic-gate 				exit(1);
1097c478bd9Sstevel@tonic-gate 			}
1107c478bd9Sstevel@tonic-gate 			if ((fl[nfile] = fopen(*argv, "r")) == NULL) {
1117c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctmerg: can't open %s\n", *argv);
1127c478bd9Sstevel@tonic-gate 				exitcode = 1;
1137c478bd9Sstevel@tonic-gate 				/*	exit(1); 	*/
1147c478bd9Sstevel@tonic-gate 			}
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (printonly) {
1197c478bd9Sstevel@tonic-gate 		for (i = 0; i <= nfile; i++)
1207c478bd9Sstevel@tonic-gate 			while (getnext(i))
1217c478bd9Sstevel@tonic-gate 				prtacct(&tb[i]);
1227c478bd9Sstevel@tonic-gate 		exit(exitcode);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	for (i = 0; i <= nfile; i++)
1267c478bd9Sstevel@tonic-gate 		if(getnext(i) == 0) {
1277c478bd9Sstevel@tonic-gate 			fprintf(stderr,"acctmerg: read error file %d.  File may be empty.\n", i);
1287c478bd9Sstevel@tonic-gate 			exitcode = 2;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	while ((tp = getleast()) != NULL)	/* get least uid of all files, */
1337c478bd9Sstevel@tonic-gate 		sumcurr(tp);			/* sum all entries for that uid, */
1347c478bd9Sstevel@tonic-gate 	if (totalonly)				/* and write the 'summed' record */
1357c478bd9Sstevel@tonic-gate 		output(&tt);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	exit(exitcode);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  *	getleast returns ptr to least (lowest uid)  element of current
1427c478bd9Sstevel@tonic-gate  *	avail, NULL if none left; always returns 1st of equals
1437c478bd9Sstevel@tonic-gate  */
144*414388d7Ssl108498 struct tacct *
145*414388d7Ssl108498 getleast(void)
1467c478bd9Sstevel@tonic-gate {
147*414388d7Ssl108498 	struct tacct *tp, *least;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	least = NULL;
1507c478bd9Sstevel@tonic-gate 	for (tp = tb; tp <= &tb[nfile]; tp++) {
1517c478bd9Sstevel@tonic-gate 		if (tp->ta_name[0] == '\0')
1527c478bd9Sstevel@tonic-gate 			continue;
1537c478bd9Sstevel@tonic-gate 		if (least == NULL ||
1547c478bd9Sstevel@tonic-gate 			tp->ta_uid < least->ta_uid ||
1557c478bd9Sstevel@tonic-gate 			((tp->ta_uid == least->ta_uid) &&
1567c478bd9Sstevel@tonic-gate 			!uidsum &&
1577c478bd9Sstevel@tonic-gate 			(strncmp(tp->ta_name, least->ta_name, NSZ) < 0)))
1587c478bd9Sstevel@tonic-gate 			least = tp;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	return(least);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  *	sumcurr sums all entries with same uid/name (into tp->tacct record)
1657c478bd9Sstevel@tonic-gate  *	writes it out, gets new entry
1667c478bd9Sstevel@tonic-gate  */
167*414388d7Ssl108498 void
168*414388d7Ssl108498 sumcurr(struct tacct *tp)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	struct tacct tc;
1717c478bd9Sstevel@tonic-gate 	char *memcpy();
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	memcpy(&tc, tp, sizeof(struct tacct));
1747c478bd9Sstevel@tonic-gate 	tacctadd(&tt, tp);	/* gets total of all uids */
1757c478bd9Sstevel@tonic-gate 	getnext(tp-&tb[0]);	/* get next one in same file */
1767c478bd9Sstevel@tonic-gate 	while (tp <= &tb[nfile])
1777c478bd9Sstevel@tonic-gate 		if (tp->ta_name[0] != '\0' &&
1787c478bd9Sstevel@tonic-gate 			tp->ta_uid == tc.ta_uid &&
1797c478bd9Sstevel@tonic-gate 			(uidsum || EQN(tp->ta_name, tc.ta_name))) {
1807c478bd9Sstevel@tonic-gate 			tacctadd(&tc, tp);
1817c478bd9Sstevel@tonic-gate 			tacctadd(&tt, tp);
1827c478bd9Sstevel@tonic-gate 			getnext(tp-&tb[0]);
1837c478bd9Sstevel@tonic-gate 		} else
1847c478bd9Sstevel@tonic-gate 			tp++;	/* look at next file */
1857c478bd9Sstevel@tonic-gate 	if (!totalonly)
1867c478bd9Sstevel@tonic-gate 		output(&tc);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
189*414388d7Ssl108498 void
190*414388d7Ssl108498 tacctadd(struct tacct *t1, struct tacct *t2)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	t1->ta_cpu[0] = t1->ta_cpu[0] + t2->ta_cpu[0];
1937c478bd9Sstevel@tonic-gate 	t1->ta_cpu[1] = t1->ta_cpu[1] + t2->ta_cpu[1];
1947c478bd9Sstevel@tonic-gate 	t1->ta_kcore[0] = t1->ta_kcore[0] + t2->ta_kcore[0];
1957c478bd9Sstevel@tonic-gate 	t1->ta_kcore[1] = t1->ta_kcore[1] + t2->ta_kcore[1];
1967c478bd9Sstevel@tonic-gate 	t1->ta_con[0] = t1->ta_con[0] + t2->ta_con[0];
1977c478bd9Sstevel@tonic-gate 	t1->ta_con[1] = t1->ta_con[1] + t2->ta_con[1];
1987c478bd9Sstevel@tonic-gate 	t1->ta_du = t1->ta_du + t2->ta_du;
1997c478bd9Sstevel@tonic-gate 	t1->ta_pc += t2->ta_pc;
2007c478bd9Sstevel@tonic-gate 	t1->ta_sc += t2->ta_sc;
2017c478bd9Sstevel@tonic-gate 	t1->ta_dc += t2->ta_dc;
2027c478bd9Sstevel@tonic-gate 	t1->ta_fee += t2->ta_fee;
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
205*414388d7Ssl108498 void
206*414388d7Ssl108498 output(struct tacct *tp)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	if (asciiout)
2097c478bd9Sstevel@tonic-gate 		prtacct(tp);
2107c478bd9Sstevel@tonic-gate 	else
2117c478bd9Sstevel@tonic-gate 		fwrite(tp, sizeof(*tp), 1, stdout);
2127c478bd9Sstevel@tonic-gate }
213*414388d7Ssl108498 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  *	getnext reads next record from stream i, returns 1 if one existed
2167c478bd9Sstevel@tonic-gate  */
217*414388d7Ssl108498 int
218*414388d7Ssl108498 getnext(int i)
2197c478bd9Sstevel@tonic-gate {
220*414388d7Ssl108498 	struct tacct *tp;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	tp = &tb[i];
2237c478bd9Sstevel@tonic-gate 	tp->ta_name[0] = '\0';
2247c478bd9Sstevel@tonic-gate 	if (fl[i] == NULL)
2257c478bd9Sstevel@tonic-gate 		return(0);
2267c478bd9Sstevel@tonic-gate 	if (asciiinp) {
2277c478bd9Sstevel@tonic-gate 		if (fscanf(fl[i],
2287c478bd9Sstevel@tonic-gate 			"%ld\t%s\t%e %e %e %e %e %e %e %lu\t%hu\t%hu\t%hu",
2297c478bd9Sstevel@tonic-gate 			&tp->ta_uid,
2307c478bd9Sstevel@tonic-gate 			tp->ta_name,
2317c478bd9Sstevel@tonic-gate 			&tp->ta_cpu[0], &tp->ta_cpu[1],
2327c478bd9Sstevel@tonic-gate 			&tp->ta_kcore[0], &tp->ta_kcore[1],
2337c478bd9Sstevel@tonic-gate 			&tp->ta_con[0], &tp->ta_con[1],
2347c478bd9Sstevel@tonic-gate 			&tp->ta_du,
2357c478bd9Sstevel@tonic-gate 			&tp->ta_pc,
2367c478bd9Sstevel@tonic-gate 			&tp->ta_sc,
2377c478bd9Sstevel@tonic-gate 			&tp->ta_dc,
2387c478bd9Sstevel@tonic-gate 			&tp->ta_fee) != EOF)
2397c478bd9Sstevel@tonic-gate 			return(1);
2407c478bd9Sstevel@tonic-gate 	} else {
2417c478bd9Sstevel@tonic-gate 		if (fread(tp, sizeof(*tp), 1, fl[i]) == 1)
2427c478bd9Sstevel@tonic-gate 			return(1);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	fclose(fl[i]);
2457c478bd9Sstevel@tonic-gate 	fl[i] = NULL;
2467c478bd9Sstevel@tonic-gate 	return(0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@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";
2507c478bd9Sstevel@tonic-gate char fmtv[] = "%ld\t%.*s\t%e %e %e %e %e %e %e %lu %hu\t%hu\t%hu\n";
2517c478bd9Sstevel@tonic-gate 
252*414388d7Ssl108498 void
253*414388d7Ssl108498 prtacct(struct tacct *tp)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	printf(verbose ? fmtv : fmt,
2567c478bd9Sstevel@tonic-gate 	    tp->ta_uid,
2577c478bd9Sstevel@tonic-gate 	    OUTPUT_NSZ,
2587c478bd9Sstevel@tonic-gate 	    tp->ta_name,
2597c478bd9Sstevel@tonic-gate 	    tp->ta_cpu[0], tp->ta_cpu[1],
2607c478bd9Sstevel@tonic-gate 	    tp->ta_kcore[0], tp->ta_kcore[1],
2617c478bd9Sstevel@tonic-gate 	    tp->ta_con[0], tp->ta_con[1],
2627c478bd9Sstevel@tonic-gate 	    tp->ta_du,
2637c478bd9Sstevel@tonic-gate 	    tp->ta_pc,
2647c478bd9Sstevel@tonic-gate 	    tp->ta_sc,
2657c478bd9Sstevel@tonic-gate 	    tp->ta_dc,
2667c478bd9Sstevel@tonic-gate 	    tp->ta_fee);
2677c478bd9Sstevel@tonic-gate }
268