xref: /onnv-gate/usr/src/cmd/acct/acctprc2.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  *	acctprc2 <ptmp1 >ptacct
320Sstevel@tonic-gate  *	reads std. input (in ptmp.h/ascii format)
330Sstevel@tonic-gate  *	hashes items with identical uid/name together, sums times
340Sstevel@tonic-gate  *	sorts in uid/name order, writes tacct.h records to output
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include "acctdef.h"
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate #include <search.h>
43*428Ssl108498 #include <stdlib.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate struct	ptmp	pb;
460Sstevel@tonic-gate struct	tacct	tb;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate struct	utab	{
490Sstevel@tonic-gate 	uid_t	ut_uid;
500Sstevel@tonic-gate 	char	ut_name[NSZ];
510Sstevel@tonic-gate 	float	ut_cpu[2];	/* cpu time (mins) */
520Sstevel@tonic-gate 	float	ut_kcore[2];	/* kcore-mins */
530Sstevel@tonic-gate 	long	ut_pc;		/* # processes */
540Sstevel@tonic-gate } * ub;
55*428Ssl108498 
56*428Ssl108498 static	int usize;
570Sstevel@tonic-gate void **root = NULL;
580Sstevel@tonic-gate 
59*428Ssl108498 void output(void);
60*428Ssl108498 void enter(struct ptmp *);
61*428Ssl108498 
62*428Ssl108498 int
main(int argc,char ** argv)63*428Ssl108498 main(int argc, char **argv)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	while (scanf("%ld\t%s\t%lu\t%lu\t%u",
670Sstevel@tonic-gate 		&pb.pt_uid,
680Sstevel@tonic-gate 		pb.pt_name,
690Sstevel@tonic-gate 		&pb.pt_cpu[0], &pb.pt_cpu[1],
700Sstevel@tonic-gate 		&pb.pt_mem) != EOF)
710Sstevel@tonic-gate 			enter(&pb);
720Sstevel@tonic-gate 	output();
730Sstevel@tonic-gate 	exit(0);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
node_compare(const void * node1,const void * node2)760Sstevel@tonic-gate int node_compare(const void *node1, const void *node2)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	if (((const struct utab *)node1)->ut_uid > \
790Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
800Sstevel@tonic-gate 		return(1);
810Sstevel@tonic-gate 	else if (((const struct utab *)node1)->ut_uid < \
820Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
830Sstevel@tonic-gate 		return(-1);
840Sstevel@tonic-gate 	else	return(strcmp(((const struct utab *) node1)->ut_name,
850Sstevel@tonic-gate 			((const struct utab *) node2)->ut_name));
860Sstevel@tonic-gate 
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
89*428Ssl108498 void
enter(struct ptmp * p)90*428Ssl108498 enter(struct ptmp *p)
910Sstevel@tonic-gate {
92*428Ssl108498 	unsigned int i;
930Sstevel@tonic-gate 	double memk;
940Sstevel@tonic-gate 	struct utab **pt;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/* clear end of short users' names */
970Sstevel@tonic-gate 	for(i = strlen(p->pt_name) + 1; i < NSZ; p->pt_name[i++] = '\0') ;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
1000Sstevel@tonic-gate 		fprintf(stderr, "acctprc2: malloc fail!\n");
1010Sstevel@tonic-gate 		exit(2);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	ub->ut_uid = p->pt_uid;
1050Sstevel@tonic-gate 	CPYN(ub->ut_name, p->pt_name);
1060Sstevel@tonic-gate 	ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
1070Sstevel@tonic-gate 	ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
1080Sstevel@tonic-gate 	memk = KCORE(pb.pt_mem);
1090Sstevel@tonic-gate 	ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
1100Sstevel@tonic-gate 	ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
1110Sstevel@tonic-gate 	ub->ut_pc = 1;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root,  \
1140Sstevel@tonic-gate 		node_compare)) == NULL) {
1150Sstevel@tonic-gate 		fprintf(stderr, "Not enough space available to build tree\n");
1160Sstevel@tonic-gate 		exit(1);
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (*pt != ub) {
1200Sstevel@tonic-gate 		(*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
1210Sstevel@tonic-gate 		(*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
1220Sstevel@tonic-gate 		(*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
1230Sstevel@tonic-gate 		(*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
1240Sstevel@tonic-gate 		(*pt)->ut_pc++;
1250Sstevel@tonic-gate 		free(ub);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
print_node(const void * node,VISIT order,int level)1290Sstevel@tonic-gate void print_node(const void *node, VISIT order, int level) {
1300Sstevel@tonic-gate         if (order == postorder || order == leaf) {
1310Sstevel@tonic-gate                 tb.ta_uid = (*(struct utab **)node)->ut_uid;
1320Sstevel@tonic-gate                 CPYN(tb.ta_name, (*(struct utab **)node)->ut_name);
1330Sstevel@tonic-gate                 tb.ta_cpu[0] = ((*(struct utab **)node)->ut_cpu[0]);
1340Sstevel@tonic-gate                 tb.ta_cpu[1] = ((*(struct utab **)node)->ut_cpu[1]);
1350Sstevel@tonic-gate                 tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
1360Sstevel@tonic-gate                 tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
1370Sstevel@tonic-gate                 tb.ta_pc = (*(struct utab **)node)->ut_pc;
1380Sstevel@tonic-gate                 fwrite(&tb, sizeof(tb), 1, stdout);
1390Sstevel@tonic-gate         }
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
142*428Ssl108498 void
output(void)143*428Ssl108498 output(void)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate                 twalk((struct utab *)root, print_node);
1460Sstevel@tonic-gate }
147