xref: /onnv-gate/usr/src/cmd/acct/acctprc.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 
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*428Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
30*428Ssl108498 #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *      acctprc
340Sstevel@tonic-gate  *      reads std. input (acct.h format),
350Sstevel@tonic-gate  *      writes std. output (tacct format)
360Sstevel@tonic-gate  *      sorted by uid
370Sstevel@tonic-gate  *      adds login names
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/param.h>
430Sstevel@tonic-gate #include "acctdef.h"
440Sstevel@tonic-gate #include <sys/acct.h>
450Sstevel@tonic-gate #include <string.h>
460Sstevel@tonic-gate #include <search.h>
47*428Ssl108498 #include <stdlib.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate struct  acct    ab;
500Sstevel@tonic-gate struct  ptmp    pb;
510Sstevel@tonic-gate struct  tacct   tb;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate struct  utab    {
540Sstevel@tonic-gate         uid_t   ut_uid;
550Sstevel@tonic-gate         char    ut_name[NSZ];
560Sstevel@tonic-gate         float   ut_cpu[2];      /* cpu time (mins) */
570Sstevel@tonic-gate         float   ut_kcore[2];    /* kcore-mins */
580Sstevel@tonic-gate         long    ut_pc;          /* # processes */
590Sstevel@tonic-gate } * ub;
60*428Ssl108498 static int usize;
610Sstevel@tonic-gate void **root = NULL;
620Sstevel@tonic-gate 
63*428Ssl108498 void output(void);
64*428Ssl108498 void enter(struct ptmp *);
65*428Ssl108498 
66*428Ssl108498 int
main(int argc,char ** argv)67*428Ssl108498 main(int argc, char **argv)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	long		elaps[2];
700Sstevel@tonic-gate 	ulong_t		etime, stime;
710Sstevel@tonic-gate 	unsigned long	mem;
720Sstevel@tonic-gate #ifdef uts
730Sstevel@tonic-gate 	float   expand();
740Sstevel@tonic-gate #else
750Sstevel@tonic-gate 	ulong_t expand();
760Sstevel@tonic-gate #endif
770Sstevel@tonic-gate 
780Sstevel@tonic-gate         while (fread(&ab, sizeof(ab), 1, stdin) == 1) {
790Sstevel@tonic-gate                 if (!MYKIND(ab.ac_flag))
800Sstevel@tonic-gate                         continue;
810Sstevel@tonic-gate                 pb.pt_uid = ab.ac_uid;
820Sstevel@tonic-gate                 CPYN(pb.pt_name, NULL);
830Sstevel@tonic-gate                 /*
840Sstevel@tonic-gate                  * approximate cpu P/NP split as same as elapsed time
850Sstevel@tonic-gate                  */
860Sstevel@tonic-gate                 if ((etime = SECS(expand(ab.ac_etime))) == 0)
870Sstevel@tonic-gate                         etime = 1;
880Sstevel@tonic-gate                 stime = expand(ab.ac_stime) + expand(ab.ac_utime);
890Sstevel@tonic-gate                 mem = expand(ab.ac_mem);
900Sstevel@tonic-gate                 if(pnpsplit(ab.ac_btime, etime, elaps) == 0) {
910Sstevel@tonic-gate 			fprintf(stderr, "acctprc: could not calculate prime/non-prime hours\n");
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 			exit(1);
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate                 pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime;
960Sstevel@tonic-gate                 pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0;
970Sstevel@tonic-gate                 pb.pt_cpu[1] = stime - pb.pt_cpu[0];
980Sstevel@tonic-gate                 if (stime)
990Sstevel@tonic-gate                         pb.pt_mem = (mem + stime - 1) / stime;
1000Sstevel@tonic-gate                 else
1010Sstevel@tonic-gate                         pb.pt_mem = 0;  /* unlikely */
1020Sstevel@tonic-gate                 enter(&pb);
1030Sstevel@tonic-gate         }
1040Sstevel@tonic-gate         output();
1050Sstevel@tonic-gate 	exit(0);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
node_compare(const void * node1,const void * node2)1080Sstevel@tonic-gate int node_compare(const void *node1, const void *node2)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	if (((const struct utab *)node1)->ut_uid > \
1110Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
1120Sstevel@tonic-gate 		return(1);
1130Sstevel@tonic-gate 	else if (((const struct utab *)node1)->ut_uid < \
1140Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
1150Sstevel@tonic-gate 		return(-1);
1160Sstevel@tonic-gate 	else	return(0);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
119*428Ssl108498 void
enter(struct ptmp * p)120*428Ssl108498 enter(struct ptmp *p)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate         double memk;
1230Sstevel@tonic-gate         struct utab **pt;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
1260Sstevel@tonic-gate 		fprintf(stderr, "acctprc: malloc fail!\n");
1270Sstevel@tonic-gate 		exit(2);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate         ub->ut_uid = p->pt_uid;
1310Sstevel@tonic-gate         CPYN(ub->ut_name, p->pt_name);
1320Sstevel@tonic-gate         ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
1330Sstevel@tonic-gate         ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
1340Sstevel@tonic-gate         memk = KCORE(pb.pt_mem);
1350Sstevel@tonic-gate         ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
1360Sstevel@tonic-gate         ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
1370Sstevel@tonic-gate         ub->ut_pc = 1;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate         if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root,  \
1400Sstevel@tonic-gate                 node_compare)) == NULL) {
1410Sstevel@tonic-gate                 fprintf(stderr, "Not enough space available to build tree\n");
1420Sstevel@tonic-gate                 exit(1);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (*pt != ub) {
1460Sstevel@tonic-gate         	(*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
1470Sstevel@tonic-gate         	(*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
1480Sstevel@tonic-gate         	(*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
1490Sstevel@tonic-gate         	(*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
1500Sstevel@tonic-gate 		(*pt)->ut_pc++;
1510Sstevel@tonic-gate 		free(ub);
1520Sstevel@tonic-gate         }
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
print_node(const void * node,VISIT order,int level)1550Sstevel@tonic-gate void print_node(const void *node, VISIT order, int level) {
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (order == postorder || order == leaf) {
1580Sstevel@tonic-gate 		tb.ta_uid = (*(struct utab **)node)->ut_uid;
1590Sstevel@tonic-gate 		CPYN(tb.ta_name, (char *)uidtonam((*(struct utab **)node)->ut_uid));
1600Sstevel@tonic-gate 		tb.ta_cpu[0] = (*(struct utab **)node)->ut_cpu[0];
1610Sstevel@tonic-gate 		tb.ta_cpu[1] = (*(struct utab **)node)->ut_cpu[1];
1620Sstevel@tonic-gate                 tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
1630Sstevel@tonic-gate                 tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
1640Sstevel@tonic-gate                 tb.ta_pc = (*(struct utab **)node)->ut_pc;
1650Sstevel@tonic-gate                 fwrite(&tb, sizeof(tb), 1, stdout);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
169*428Ssl108498 void
output(void)170*428Ssl108498 output(void)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate                 twalk((struct utab *)root, print_node);
1730Sstevel@tonic-gate }
174