xref: /illumos-gate/usr/src/cmd/acct/acctprc2.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  */
29*2a8bcb4eSToomas Soome 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *	acctprc2 <ptmp1 >ptacct
327c478bd9Sstevel@tonic-gate  *	reads std. input (in ptmp.h/ascii format)
337c478bd9Sstevel@tonic-gate  *	hashes items with identical uid/name together, sums times
347c478bd9Sstevel@tonic-gate  *	sorts in uid/name order, writes tacct.h records to output
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include "acctdef.h"
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <search.h>
43414388d7Ssl #include <stdlib.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate struct	ptmp	pb;
467c478bd9Sstevel@tonic-gate struct	tacct	tb;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate struct	utab	{
497c478bd9Sstevel@tonic-gate 	uid_t	ut_uid;
507c478bd9Sstevel@tonic-gate 	char	ut_name[NSZ];
517c478bd9Sstevel@tonic-gate 	float	ut_cpu[2];	/* cpu time (mins) */
527c478bd9Sstevel@tonic-gate 	float	ut_kcore[2];	/* kcore-mins */
537c478bd9Sstevel@tonic-gate 	long	ut_pc;		/* # processes */
547c478bd9Sstevel@tonic-gate } * ub;
55414388d7Ssl 
56414388d7Ssl static	int usize;
577c478bd9Sstevel@tonic-gate void **root = NULL;
587c478bd9Sstevel@tonic-gate 
59414388d7Ssl void output(void);
60414388d7Ssl void enter(struct ptmp *);
61414388d7Ssl 
62414388d7Ssl int
main(int argc,char ** argv)63414388d7Ssl main(int argc, char **argv)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	while (scanf("%ld\t%s\t%lu\t%lu\t%u",
677c478bd9Sstevel@tonic-gate 		&pb.pt_uid,
687c478bd9Sstevel@tonic-gate 		pb.pt_name,
697c478bd9Sstevel@tonic-gate 		&pb.pt_cpu[0], &pb.pt_cpu[1],
707c478bd9Sstevel@tonic-gate 		&pb.pt_mem) != EOF)
717c478bd9Sstevel@tonic-gate 			enter(&pb);
727c478bd9Sstevel@tonic-gate 	output();
737c478bd9Sstevel@tonic-gate 	exit(0);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
node_compare(const void * node1,const void * node2)767c478bd9Sstevel@tonic-gate int node_compare(const void *node1, const void *node2)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	if (((const struct utab *)node1)->ut_uid > \
797c478bd9Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
807c478bd9Sstevel@tonic-gate 		return(1);
817c478bd9Sstevel@tonic-gate 	else if (((const struct utab *)node1)->ut_uid < \
827c478bd9Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
837c478bd9Sstevel@tonic-gate 		return(-1);
847c478bd9Sstevel@tonic-gate 	else	return(strcmp(((const struct utab *) node1)->ut_name,
857c478bd9Sstevel@tonic-gate 			((const struct utab *) node2)->ut_name));
86*2a8bcb4eSToomas Soome 
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
89414388d7Ssl void
enter(struct ptmp * p)90414388d7Ssl enter(struct ptmp *p)
917c478bd9Sstevel@tonic-gate {
92414388d7Ssl 	unsigned int i;
937c478bd9Sstevel@tonic-gate 	double memk;
947c478bd9Sstevel@tonic-gate 	struct utab **pt;
95*2a8bcb4eSToomas Soome 
967c478bd9Sstevel@tonic-gate 	/* clear end of short users' names */
977c478bd9Sstevel@tonic-gate 	for(i = strlen(p->pt_name) + 1; i < NSZ; p->pt_name[i++] = '\0') ;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
1007c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctprc2: malloc fail!\n");
1017c478bd9Sstevel@tonic-gate 		exit(2);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	ub->ut_uid = p->pt_uid;
1057c478bd9Sstevel@tonic-gate 	CPYN(ub->ut_name, p->pt_name);
1067c478bd9Sstevel@tonic-gate 	ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
1077c478bd9Sstevel@tonic-gate 	ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
1087c478bd9Sstevel@tonic-gate 	memk = KCORE(pb.pt_mem);
1097c478bd9Sstevel@tonic-gate 	ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
1107c478bd9Sstevel@tonic-gate 	ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
1117c478bd9Sstevel@tonic-gate 	ub->ut_pc = 1;
112*2a8bcb4eSToomas Soome 
1137c478bd9Sstevel@tonic-gate 	if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root,  \
1147c478bd9Sstevel@tonic-gate 		node_compare)) == NULL) {
1157c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Not enough space available to build tree\n");
1167c478bd9Sstevel@tonic-gate 		exit(1);
1177c478bd9Sstevel@tonic-gate 	}
118*2a8bcb4eSToomas Soome 
1197c478bd9Sstevel@tonic-gate 	if (*pt != ub) {
1207c478bd9Sstevel@tonic-gate 		(*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
1217c478bd9Sstevel@tonic-gate 		(*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
1227c478bd9Sstevel@tonic-gate 		(*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
1237c478bd9Sstevel@tonic-gate 		(*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
1247c478bd9Sstevel@tonic-gate 		(*pt)->ut_pc++;
1257c478bd9Sstevel@tonic-gate 		free(ub);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
print_node(const void * node,VISIT order,int level)1297c478bd9Sstevel@tonic-gate void print_node(const void *node, VISIT order, int level) {
1307c478bd9Sstevel@tonic-gate         if (order == postorder || order == leaf) {
1317c478bd9Sstevel@tonic-gate                 tb.ta_uid = (*(struct utab **)node)->ut_uid;
1327c478bd9Sstevel@tonic-gate                 CPYN(tb.ta_name, (*(struct utab **)node)->ut_name);
1337c478bd9Sstevel@tonic-gate                 tb.ta_cpu[0] = ((*(struct utab **)node)->ut_cpu[0]);
1347c478bd9Sstevel@tonic-gate                 tb.ta_cpu[1] = ((*(struct utab **)node)->ut_cpu[1]);
1357c478bd9Sstevel@tonic-gate                 tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
1367c478bd9Sstevel@tonic-gate                 tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
1377c478bd9Sstevel@tonic-gate                 tb.ta_pc = (*(struct utab **)node)->ut_pc;
1387c478bd9Sstevel@tonic-gate                 fwrite(&tb, sizeof(tb), 1, stdout);
1397c478bd9Sstevel@tonic-gate         }
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
142414388d7Ssl void
output(void)143414388d7Ssl output(void)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate                 twalk((struct utab *)root, print_node);
1467c478bd9Sstevel@tonic-gate }
147