xref: /titanic_54/usr/src/cmd/acct/acctprc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3	*/
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  *      acctprc
34*7c478bd9Sstevel@tonic-gate  *      reads std. input (acct.h format),
35*7c478bd9Sstevel@tonic-gate  *      writes std. output (tacct format)
36*7c478bd9Sstevel@tonic-gate  *      sorted by uid
37*7c478bd9Sstevel@tonic-gate  *      adds login names
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
43*7c478bd9Sstevel@tonic-gate #include "acctdef.h"
44*7c478bd9Sstevel@tonic-gate #include <sys/acct.h>
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate #include <search.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate struct  acct    ab;
49*7c478bd9Sstevel@tonic-gate struct  ptmp    pb;
50*7c478bd9Sstevel@tonic-gate struct  tacct   tb;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate struct  utab    {
53*7c478bd9Sstevel@tonic-gate         uid_t   ut_uid;
54*7c478bd9Sstevel@tonic-gate         char    ut_name[NSZ];
55*7c478bd9Sstevel@tonic-gate         float   ut_cpu[2];      /* cpu time (mins) */
56*7c478bd9Sstevel@tonic-gate         float   ut_kcore[2];    /* kcore-mins */
57*7c478bd9Sstevel@tonic-gate         long    ut_pc;          /* # processes */
58*7c478bd9Sstevel@tonic-gate } * ub;
59*7c478bd9Sstevel@tonic-gate static  usize;
60*7c478bd9Sstevel@tonic-gate char	*strncpy();
61*7c478bd9Sstevel@tonic-gate void **root = NULL;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate main()
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	long		elaps[2];
66*7c478bd9Sstevel@tonic-gate 	ulong_t		etime, stime;
67*7c478bd9Sstevel@tonic-gate 	unsigned long	mem;
68*7c478bd9Sstevel@tonic-gate #ifdef uts
69*7c478bd9Sstevel@tonic-gate 	float   expand();
70*7c478bd9Sstevel@tonic-gate #else
71*7c478bd9Sstevel@tonic-gate 	ulong_t expand();
72*7c478bd9Sstevel@tonic-gate #endif
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate         while (fread(&ab, sizeof(ab), 1, stdin) == 1) {
75*7c478bd9Sstevel@tonic-gate                 if (!MYKIND(ab.ac_flag))
76*7c478bd9Sstevel@tonic-gate                         continue;
77*7c478bd9Sstevel@tonic-gate                 pb.pt_uid = ab.ac_uid;
78*7c478bd9Sstevel@tonic-gate                 CPYN(pb.pt_name, NULL);
79*7c478bd9Sstevel@tonic-gate                 /*
80*7c478bd9Sstevel@tonic-gate                  * approximate cpu P/NP split as same as elapsed time
81*7c478bd9Sstevel@tonic-gate                  */
82*7c478bd9Sstevel@tonic-gate                 if ((etime = SECS(expand(ab.ac_etime))) == 0)
83*7c478bd9Sstevel@tonic-gate                         etime = 1;
84*7c478bd9Sstevel@tonic-gate                 stime = expand(ab.ac_stime) + expand(ab.ac_utime);
85*7c478bd9Sstevel@tonic-gate                 mem = expand(ab.ac_mem);
86*7c478bd9Sstevel@tonic-gate                 if(pnpsplit(ab.ac_btime, etime, elaps) == 0) {
87*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "acctprc: could not calculate prime/non-prime hours\n");
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 			exit(1);
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate                 pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime;
92*7c478bd9Sstevel@tonic-gate                 pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0;
93*7c478bd9Sstevel@tonic-gate                 pb.pt_cpu[1] = stime - pb.pt_cpu[0];
94*7c478bd9Sstevel@tonic-gate                 if (stime)
95*7c478bd9Sstevel@tonic-gate                         pb.pt_mem = (mem + stime - 1) / stime;
96*7c478bd9Sstevel@tonic-gate                 else
97*7c478bd9Sstevel@tonic-gate                         pb.pt_mem = 0;  /* unlikely */
98*7c478bd9Sstevel@tonic-gate                 enter(&pb);
99*7c478bd9Sstevel@tonic-gate         }
100*7c478bd9Sstevel@tonic-gate         output();
101*7c478bd9Sstevel@tonic-gate 	exit(0);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate int node_compare(const void *node1, const void *node2)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	if (((const struct utab *)node1)->ut_uid > \
107*7c478bd9Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
108*7c478bd9Sstevel@tonic-gate 		return(1);
109*7c478bd9Sstevel@tonic-gate 	else if (((const struct utab *)node1)->ut_uid < \
110*7c478bd9Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
111*7c478bd9Sstevel@tonic-gate 		return(-1);
112*7c478bd9Sstevel@tonic-gate 	else	return(0);
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate enter(p)
116*7c478bd9Sstevel@tonic-gate register struct ptmp *p;
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate         double memk;
119*7c478bd9Sstevel@tonic-gate         struct utab **pt;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
122*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctprc: malloc fail!\n");
123*7c478bd9Sstevel@tonic-gate 		exit(2);
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate         ub->ut_uid = p->pt_uid;
127*7c478bd9Sstevel@tonic-gate         CPYN(ub->ut_name, p->pt_name);
128*7c478bd9Sstevel@tonic-gate         ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
129*7c478bd9Sstevel@tonic-gate         ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
130*7c478bd9Sstevel@tonic-gate         memk = KCORE(pb.pt_mem);
131*7c478bd9Sstevel@tonic-gate         ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
132*7c478bd9Sstevel@tonic-gate         ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
133*7c478bd9Sstevel@tonic-gate         ub->ut_pc = 1;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate         if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root,  \
136*7c478bd9Sstevel@tonic-gate                 node_compare)) == NULL) {
137*7c478bd9Sstevel@tonic-gate                 fprintf(stderr, "Not enough space available to build tree\n");
138*7c478bd9Sstevel@tonic-gate                 exit(1);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	if (*pt != ub) {
142*7c478bd9Sstevel@tonic-gate         	(*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
143*7c478bd9Sstevel@tonic-gate         	(*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
144*7c478bd9Sstevel@tonic-gate         	(*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
145*7c478bd9Sstevel@tonic-gate         	(*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
146*7c478bd9Sstevel@tonic-gate 		(*pt)->ut_pc++;
147*7c478bd9Sstevel@tonic-gate 		free(ub);
148*7c478bd9Sstevel@tonic-gate         }
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate void print_node(const void *node, VISIT order, int level) {
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	if (order == postorder || order == leaf) {
154*7c478bd9Sstevel@tonic-gate 		tb.ta_uid = (*(struct utab **)node)->ut_uid;
155*7c478bd9Sstevel@tonic-gate 		CPYN(tb.ta_name, (char *)uidtonam((*(struct utab **)node)->ut_uid));
156*7c478bd9Sstevel@tonic-gate 		tb.ta_cpu[0] = (*(struct utab **)node)->ut_cpu[0];
157*7c478bd9Sstevel@tonic-gate 		tb.ta_cpu[1] = (*(struct utab **)node)->ut_cpu[1];
158*7c478bd9Sstevel@tonic-gate                 tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
159*7c478bd9Sstevel@tonic-gate                 tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
160*7c478bd9Sstevel@tonic-gate                 tb.ta_pc = (*(struct utab **)node)->ut_pc;
161*7c478bd9Sstevel@tonic-gate                 fwrite(&tb, sizeof(tb), 1, stdout);
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate output()
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate                 twalk((struct utab *)root, print_node);
168*7c478bd9Sstevel@tonic-gate }
169