xref: /titanic_53/usr/src/cmd/acct/acctprc2.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 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10	*/
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  *	acctprc2 <ptmp1 >ptacct
29*7c478bd9Sstevel@tonic-gate  *	reads std. input (in ptmp.h/ascii format)
30*7c478bd9Sstevel@tonic-gate  *	hashes items with identical uid/name together, sums times
31*7c478bd9Sstevel@tonic-gate  *	sorts in uid/name order, writes tacct.h records to output
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include "acctdef.h"
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <search.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate struct	ptmp	pb;
42*7c478bd9Sstevel@tonic-gate struct	tacct	tb;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate struct	utab	{
45*7c478bd9Sstevel@tonic-gate 	uid_t	ut_uid;
46*7c478bd9Sstevel@tonic-gate 	char	ut_name[NSZ];
47*7c478bd9Sstevel@tonic-gate 	float	ut_cpu[2];	/* cpu time (mins) */
48*7c478bd9Sstevel@tonic-gate 	float	ut_kcore[2];	/* kcore-mins */
49*7c478bd9Sstevel@tonic-gate 	long	ut_pc;		/* # processes */
50*7c478bd9Sstevel@tonic-gate } * ub;
51*7c478bd9Sstevel@tonic-gate static	usize;
52*7c478bd9Sstevel@tonic-gate int	ucmp();
53*7c478bd9Sstevel@tonic-gate void **root = NULL;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate main(argc, argv)
56*7c478bd9Sstevel@tonic-gate char **argv;
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	while (scanf("%ld\t%s\t%lu\t%lu\t%u",
60*7c478bd9Sstevel@tonic-gate 		&pb.pt_uid,
61*7c478bd9Sstevel@tonic-gate 		pb.pt_name,
62*7c478bd9Sstevel@tonic-gate 		&pb.pt_cpu[0], &pb.pt_cpu[1],
63*7c478bd9Sstevel@tonic-gate 		&pb.pt_mem) != EOF)
64*7c478bd9Sstevel@tonic-gate 			enter(&pb);
65*7c478bd9Sstevel@tonic-gate 	output();
66*7c478bd9Sstevel@tonic-gate 	exit(0);
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate int node_compare(const void *node1, const void *node2)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	if (((const struct utab *)node1)->ut_uid > \
72*7c478bd9Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
73*7c478bd9Sstevel@tonic-gate 		return(1);
74*7c478bd9Sstevel@tonic-gate 	else if (((const struct utab *)node1)->ut_uid < \
75*7c478bd9Sstevel@tonic-gate 		((const struct utab *)node2)->ut_uid)
76*7c478bd9Sstevel@tonic-gate 		return(-1);
77*7c478bd9Sstevel@tonic-gate 	else	return(strcmp(((const struct utab *) node1)->ut_name,
78*7c478bd9Sstevel@tonic-gate 			((const struct utab *) node2)->ut_name));
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate enter(p)
83*7c478bd9Sstevel@tonic-gate register struct ptmp *p;
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	register unsigned i;
86*7c478bd9Sstevel@tonic-gate 	double memk;
87*7c478bd9Sstevel@tonic-gate 	struct utab **pt;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	/* clear end of short users' names */
90*7c478bd9Sstevel@tonic-gate 	for(i = strlen(p->pt_name) + 1; i < NSZ; p->pt_name[i++] = '\0') ;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
93*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "acctprc2: malloc fail!\n");
94*7c478bd9Sstevel@tonic-gate 		exit(2);
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	ub->ut_uid = p->pt_uid;
98*7c478bd9Sstevel@tonic-gate 	CPYN(ub->ut_name, p->pt_name);
99*7c478bd9Sstevel@tonic-gate 	ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
100*7c478bd9Sstevel@tonic-gate 	ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
101*7c478bd9Sstevel@tonic-gate 	memk = KCORE(pb.pt_mem);
102*7c478bd9Sstevel@tonic-gate 	ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
103*7c478bd9Sstevel@tonic-gate 	ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
104*7c478bd9Sstevel@tonic-gate 	ub->ut_pc = 1;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root,  \
107*7c478bd9Sstevel@tonic-gate 		node_compare)) == NULL) {
108*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Not enough space available to build tree\n");
109*7c478bd9Sstevel@tonic-gate 		exit(1);
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	if (*pt != ub) {
113*7c478bd9Sstevel@tonic-gate 		(*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
114*7c478bd9Sstevel@tonic-gate 		(*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
115*7c478bd9Sstevel@tonic-gate 		(*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
116*7c478bd9Sstevel@tonic-gate 		(*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
117*7c478bd9Sstevel@tonic-gate 		(*pt)->ut_pc++;
118*7c478bd9Sstevel@tonic-gate 		free(ub);
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate void print_node(const void *node, VISIT order, int level) {
123*7c478bd9Sstevel@tonic-gate         if (order == postorder || order == leaf) {
124*7c478bd9Sstevel@tonic-gate                 tb.ta_uid = (*(struct utab **)node)->ut_uid;
125*7c478bd9Sstevel@tonic-gate                 CPYN(tb.ta_name, (*(struct utab **)node)->ut_name);
126*7c478bd9Sstevel@tonic-gate                 tb.ta_cpu[0] = ((*(struct utab **)node)->ut_cpu[0]);
127*7c478bd9Sstevel@tonic-gate                 tb.ta_cpu[1] = ((*(struct utab **)node)->ut_cpu[1]);
128*7c478bd9Sstevel@tonic-gate                 tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
129*7c478bd9Sstevel@tonic-gate                 tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
130*7c478bd9Sstevel@tonic-gate                 tb.ta_pc = (*(struct utab **)node)->ut_pc;
131*7c478bd9Sstevel@tonic-gate                 fwrite(&tb, sizeof(tb), 1, stdout);
132*7c478bd9Sstevel@tonic-gate         }
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate output()
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate                 twalk((struct utab *)root, print_node);
138*7c478bd9Sstevel@tonic-gate }
139