xref: /illumos-gate/usr/src/cmd/acct/acctmerg.c (revision 2a8bcb4e)
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 
25414388d7Ssl /*
26414388d7Ssl  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27414388d7Ssl  * Use is subject to license terms.
28414388d7Ssl  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *	acctmerg [-a] [-i] [-p] [-t] [-u] [-v] [file...]
327c478bd9Sstevel@tonic-gate  *	-a	output in tacct.h/ascii (instead of tacct.h)
337c478bd9Sstevel@tonic-gate  *	-i	input is in tacct.h/ascii (instead of tacct.h)
347c478bd9Sstevel@tonic-gate  *	-p	print input files with no processing
357c478bd9Sstevel@tonic-gate  *	-t	output single record that totals all input
367c478bd9Sstevel@tonic-gate  *	-u	summarize by uid, rather than uid/name
377c478bd9Sstevel@tonic-gate  *	-v	output in verbose tacct.h/ascii
387c478bd9Sstevel@tonic-gate  *	reads std input and 0-NFILE files, all in tacct.h format,
397c478bd9Sstevel@tonic-gate  *	sorted by uid/name.
407c478bd9Sstevel@tonic-gate  *	merge/adds all records with same uid/name (or same uid if -u,
417c478bd9Sstevel@tonic-gate  *	or all records if -t], writes to std. output
427c478bd9Sstevel@tonic-gate  *	(still in tacct.h format)
437c478bd9Sstevel@tonic-gate  *	note that this can be used to summarize the std input
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include "acctdef.h"
50414388d7Ssl #include <stdlib.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate int	nfile;			/* index of last used in fl */
537c478bd9Sstevel@tonic-gate FILE	*fl[NFILE]	= {stdin};
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate struct	tacct tb[NFILE];	/* current record from each file */
567c478bd9Sstevel@tonic-gate struct	tacct	tt = {
577c478bd9Sstevel@tonic-gate 	0,
587c478bd9Sstevel@tonic-gate 	"TOTAL"
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate int	asciiout;
617c478bd9Sstevel@tonic-gate int	asciiinp;
627c478bd9Sstevel@tonic-gate int	printonly;
637c478bd9Sstevel@tonic-gate int	totalonly;
647c478bd9Sstevel@tonic-gate int	uidsum;
657c478bd9Sstevel@tonic-gate int	verbose;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate int 	exitcode = 0;
687c478bd9Sstevel@tonic-gate 
69414388d7Ssl void prtacct(struct tacct *);
70414388d7Ssl struct tacct *getleast(void);
71414388d7Ssl void output(struct tacct *);
72414388d7Ssl void tacctadd(struct tacct *, struct tacct *);
73414388d7Ssl void sumcurr(struct tacct *);
74414388d7Ssl 
75414388d7Ssl int
main(int argc,char ** argv)76414388d7Ssl main(int argc, char **argv)
777c478bd9Sstevel@tonic-gate {
78414388d7Ssl 	int i;
79414388d7Ssl 	struct tacct *tp;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	while (--argc > 0) {
827c478bd9Sstevel@tonic-gate 		if (**++argv == '-')
837c478bd9Sstevel@tonic-gate 			switch (*++*argv) {
847c478bd9Sstevel@tonic-gate 			case 'a':
857c478bd9Sstevel@tonic-gate 				asciiout++;
867c478bd9Sstevel@tonic-gate 				continue;
877c478bd9Sstevel@tonic-gate 			case 'i':
887c478bd9Sstevel@tonic-gate 				asciiinp++;
897c478bd9Sstevel@tonic-gate 				continue;
907c478bd9Sstevel@tonic-gate 			case 'p':
917c478bd9Sstevel@tonic-gate 				printonly++;
927c478bd9Sstevel@tonic-gate 				continue;
937c478bd9Sstevel@tonic-gate 			case 't':
947c478bd9Sstevel@tonic-gate 				totalonly++;
957c478bd9Sstevel@tonic-gate 				continue;
967c478bd9Sstevel@tonic-gate 			case 'u':
977c478bd9Sstevel@tonic-gate 				uidsum++;
987c478bd9Sstevel@tonic-gate 				continue;
997c478bd9Sstevel@tonic-gate 			case 'v':
1007c478bd9Sstevel@tonic-gate 				verbose++;
1017c478bd9Sstevel@tonic-gate 				asciiout++;
1027c478bd9Sstevel@tonic-gate 				continue;
1037c478bd9Sstevel@tonic-gate 			}
1047c478bd9Sstevel@tonic-gate 		else {
1057c478bd9Sstevel@tonic-gate 			if (++nfile >= NFILE) {
1067c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctmerg: >%d files\n", NFILE);
1077c478bd9Sstevel@tonic-gate 				exit(1);
1087c478bd9Sstevel@tonic-gate 			}
1097c478bd9Sstevel@tonic-gate 			if ((fl[nfile] = fopen(*argv, "r")) == NULL) {
1107c478bd9Sstevel@tonic-gate 				fprintf(stderr, "acctmerg: can't open %s\n", *argv);
1117c478bd9Sstevel@tonic-gate 				exitcode = 1;
1127c478bd9Sstevel@tonic-gate 				/*	exit(1); 	*/
1137c478bd9Sstevel@tonic-gate 			}
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (printonly) {
1187c478bd9Sstevel@tonic-gate 		for (i = 0; i <= nfile; i++)
1197c478bd9Sstevel@tonic-gate 			while (getnext(i))
1207c478bd9Sstevel@tonic-gate 				prtacct(&tb[i]);
1217c478bd9Sstevel@tonic-gate 		exit(exitcode);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	for (i = 0; i <= nfile; i++)
1257c478bd9Sstevel@tonic-gate 		if(getnext(i) == 0) {
1267c478bd9Sstevel@tonic-gate 			fprintf(stderr,"acctmerg: read error file %d.  File may be empty.\n", i);
1277c478bd9Sstevel@tonic-gate 			exitcode = 2;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	while ((tp = getleast()) != NULL)	/* get least uid of all files, */
1327c478bd9Sstevel@tonic-gate 		sumcurr(tp);			/* sum all entries for that uid, */
1337c478bd9Sstevel@tonic-gate 	if (totalonly)				/* and write the 'summed' record */
1347c478bd9Sstevel@tonic-gate 		output(&tt);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	exit(exitcode);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
140*2a8bcb4eSToomas Soome  *	getleast returns ptr to least (lowest uid)  element of current
1417c478bd9Sstevel@tonic-gate  *	avail, NULL if none left; always returns 1st of equals
1427c478bd9Sstevel@tonic-gate  */
143414388d7Ssl struct tacct *
getleast(void)144414388d7Ssl getleast(void)
1457c478bd9Sstevel@tonic-gate {
146414388d7Ssl 	struct tacct *tp, *least;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	least = NULL;
1497c478bd9Sstevel@tonic-gate 	for (tp = tb; tp <= &tb[nfile]; tp++) {
1507c478bd9Sstevel@tonic-gate 		if (tp->ta_name[0] == '\0')
1517c478bd9Sstevel@tonic-gate 			continue;
1527c478bd9Sstevel@tonic-gate 		if (least == NULL ||
1537c478bd9Sstevel@tonic-gate 			tp->ta_uid < least->ta_uid ||
1547c478bd9Sstevel@tonic-gate 			((tp->ta_uid == least->ta_uid) &&
1557c478bd9Sstevel@tonic-gate 			!uidsum &&
1567c478bd9Sstevel@tonic-gate 			(strncmp(tp->ta_name, least->ta_name, NSZ) < 0)))
1577c478bd9Sstevel@tonic-gate 			least = tp;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	return(least);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  *	sumcurr sums all entries with same uid/name (into tp->tacct record)
1647c478bd9Sstevel@tonic-gate  *	writes it out, gets new entry
1657c478bd9Sstevel@tonic-gate  */
166414388d7Ssl void
sumcurr(struct tacct * tp)167414388d7Ssl sumcurr(struct tacct *tp)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	struct tacct tc;
1707c478bd9Sstevel@tonic-gate 	char *memcpy();
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	memcpy(&tc, tp, sizeof(struct tacct));
1737c478bd9Sstevel@tonic-gate 	tacctadd(&tt, tp);	/* gets total of all uids */
1747c478bd9Sstevel@tonic-gate 	getnext(tp-&tb[0]);	/* get next one in same file */
1757c478bd9Sstevel@tonic-gate 	while (tp <= &tb[nfile])
1767c478bd9Sstevel@tonic-gate 		if (tp->ta_name[0] != '\0' &&
1777c478bd9Sstevel@tonic-gate 			tp->ta_uid == tc.ta_uid &&
1787c478bd9Sstevel@tonic-gate 			(uidsum || EQN(tp->ta_name, tc.ta_name))) {
1797c478bd9Sstevel@tonic-gate 			tacctadd(&tc, tp);
1807c478bd9Sstevel@tonic-gate 			tacctadd(&tt, tp);
1817c478bd9Sstevel@tonic-gate 			getnext(tp-&tb[0]);
1827c478bd9Sstevel@tonic-gate 		} else
1837c478bd9Sstevel@tonic-gate 			tp++;	/* look at next file */
1847c478bd9Sstevel@tonic-gate 	if (!totalonly)
1857c478bd9Sstevel@tonic-gate 		output(&tc);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
188414388d7Ssl void
tacctadd(struct tacct * t1,struct tacct * t2)189414388d7Ssl tacctadd(struct tacct *t1, struct tacct *t2)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	t1->ta_cpu[0] = t1->ta_cpu[0] + t2->ta_cpu[0];
1927c478bd9Sstevel@tonic-gate 	t1->ta_cpu[1] = t1->ta_cpu[1] + t2->ta_cpu[1];
1937c478bd9Sstevel@tonic-gate 	t1->ta_kcore[0] = t1->ta_kcore[0] + t2->ta_kcore[0];
1947c478bd9Sstevel@tonic-gate 	t1->ta_kcore[1] = t1->ta_kcore[1] + t2->ta_kcore[1];
1957c478bd9Sstevel@tonic-gate 	t1->ta_con[0] = t1->ta_con[0] + t2->ta_con[0];
1967c478bd9Sstevel@tonic-gate 	t1->ta_con[1] = t1->ta_con[1] + t2->ta_con[1];
1977c478bd9Sstevel@tonic-gate 	t1->ta_du = t1->ta_du + t2->ta_du;
1987c478bd9Sstevel@tonic-gate 	t1->ta_pc += t2->ta_pc;
1997c478bd9Sstevel@tonic-gate 	t1->ta_sc += t2->ta_sc;
2007c478bd9Sstevel@tonic-gate 	t1->ta_dc += t2->ta_dc;
2017c478bd9Sstevel@tonic-gate 	t1->ta_fee += t2->ta_fee;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
204414388d7Ssl void
output(struct tacct * tp)205414388d7Ssl output(struct tacct *tp)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	if (asciiout)
2087c478bd9Sstevel@tonic-gate 		prtacct(tp);
2097c478bd9Sstevel@tonic-gate 	else
2107c478bd9Sstevel@tonic-gate 		fwrite(tp, sizeof(*tp), 1, stdout);
2117c478bd9Sstevel@tonic-gate }
212414388d7Ssl 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  *	getnext reads next record from stream i, returns 1 if one existed
2157c478bd9Sstevel@tonic-gate  */
216414388d7Ssl int
getnext(int i)217414388d7Ssl getnext(int i)
2187c478bd9Sstevel@tonic-gate {
219414388d7Ssl 	struct tacct *tp;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	tp = &tb[i];
2227c478bd9Sstevel@tonic-gate 	tp->ta_name[0] = '\0';
2237c478bd9Sstevel@tonic-gate 	if (fl[i] == NULL)
2247c478bd9Sstevel@tonic-gate 		return(0);
2257c478bd9Sstevel@tonic-gate 	if (asciiinp) {
2267c478bd9Sstevel@tonic-gate 		if (fscanf(fl[i],
2277c478bd9Sstevel@tonic-gate 			"%ld\t%s\t%e %e %e %e %e %e %e %lu\t%hu\t%hu\t%hu",
2287c478bd9Sstevel@tonic-gate 			&tp->ta_uid,
2297c478bd9Sstevel@tonic-gate 			tp->ta_name,
2307c478bd9Sstevel@tonic-gate 			&tp->ta_cpu[0], &tp->ta_cpu[1],
2317c478bd9Sstevel@tonic-gate 			&tp->ta_kcore[0], &tp->ta_kcore[1],
2327c478bd9Sstevel@tonic-gate 			&tp->ta_con[0], &tp->ta_con[1],
2337c478bd9Sstevel@tonic-gate 			&tp->ta_du,
2347c478bd9Sstevel@tonic-gate 			&tp->ta_pc,
2357c478bd9Sstevel@tonic-gate 			&tp->ta_sc,
2367c478bd9Sstevel@tonic-gate 			&tp->ta_dc,
2377c478bd9Sstevel@tonic-gate 			&tp->ta_fee) != EOF)
2387c478bd9Sstevel@tonic-gate 			return(1);
2397c478bd9Sstevel@tonic-gate 	} else {
2407c478bd9Sstevel@tonic-gate 		if (fread(tp, sizeof(*tp), 1, fl[i]) == 1)
2417c478bd9Sstevel@tonic-gate 			return(1);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	fclose(fl[i]);
2447c478bd9Sstevel@tonic-gate 	fl[i] = NULL;
2457c478bd9Sstevel@tonic-gate 	return(0);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@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";
2497c478bd9Sstevel@tonic-gate char fmtv[] = "%ld\t%.*s\t%e %e %e %e %e %e %e %lu %hu\t%hu\t%hu\n";
2507c478bd9Sstevel@tonic-gate 
251414388d7Ssl void
prtacct(struct tacct * tp)252414388d7Ssl prtacct(struct tacct *tp)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	printf(verbose ? fmtv : fmt,
2557c478bd9Sstevel@tonic-gate 	    tp->ta_uid,
2567c478bd9Sstevel@tonic-gate 	    OUTPUT_NSZ,
2577c478bd9Sstevel@tonic-gate 	    tp->ta_name,
2587c478bd9Sstevel@tonic-gate 	    tp->ta_cpu[0], tp->ta_cpu[1],
2597c478bd9Sstevel@tonic-gate 	    tp->ta_kcore[0], tp->ta_kcore[1],
2607c478bd9Sstevel@tonic-gate 	    tp->ta_con[0], tp->ta_con[1],
2617c478bd9Sstevel@tonic-gate 	    tp->ta_du,
2627c478bd9Sstevel@tonic-gate 	    tp->ta_pc,
2637c478bd9Sstevel@tonic-gate 	    tp->ta_sc,
2647c478bd9Sstevel@tonic-gate 	    tp->ta_dc,
2657c478bd9Sstevel@tonic-gate 	    tp->ta_fee);
2667c478bd9Sstevel@tonic-gate }
267