xref: /titanic_52/usr/src/cmd/acct/acctprc2.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10	*/
27 /*
28  *	acctprc2 <ptmp1 >ptacct
29  *	reads std. input (in ptmp.h/ascii format)
30  *	hashes items with identical uid/name together, sums times
31  *	sorts in uid/name order, writes tacct.h records to output
32  */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include "acctdef.h"
37 #include <stdio.h>
38 #include <string.h>
39 #include <search.h>
40 
41 struct	ptmp	pb;
42 struct	tacct	tb;
43 
44 struct	utab	{
45 	uid_t	ut_uid;
46 	char	ut_name[NSZ];
47 	float	ut_cpu[2];	/* cpu time (mins) */
48 	float	ut_kcore[2];	/* kcore-mins */
49 	long	ut_pc;		/* # processes */
50 } * ub;
51 static	usize;
52 int	ucmp();
53 void **root = NULL;
54 
55 main(argc, argv)
56 char **argv;
57 {
58 
59 	while (scanf("%ld\t%s\t%lu\t%lu\t%u",
60 		&pb.pt_uid,
61 		pb.pt_name,
62 		&pb.pt_cpu[0], &pb.pt_cpu[1],
63 		&pb.pt_mem) != EOF)
64 			enter(&pb);
65 	output();
66 	exit(0);
67 }
68 
69 int node_compare(const void *node1, const void *node2)
70 {
71 	if (((const struct utab *)node1)->ut_uid > \
72 		((const struct utab *)node2)->ut_uid)
73 		return(1);
74 	else if (((const struct utab *)node1)->ut_uid < \
75 		((const struct utab *)node2)->ut_uid)
76 		return(-1);
77 	else	return(strcmp(((const struct utab *) node1)->ut_name,
78 			((const struct utab *) node2)->ut_name));
79 
80 }
81 
82 enter(p)
83 register struct ptmp *p;
84 {
85 	register unsigned i;
86 	double memk;
87 	struct utab **pt;
88 
89 	/* clear end of short users' names */
90 	for(i = strlen(p->pt_name) + 1; i < NSZ; p->pt_name[i++] = '\0') ;
91 
92 	if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
93 		fprintf(stderr, "acctprc2: malloc fail!\n");
94 		exit(2);
95 	}
96 
97 	ub->ut_uid = p->pt_uid;
98 	CPYN(ub->ut_name, p->pt_name);
99 	ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
100 	ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
101 	memk = KCORE(pb.pt_mem);
102 	ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
103 	ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
104 	ub->ut_pc = 1;
105 
106 	if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root,  \
107 		node_compare)) == NULL) {
108 		fprintf(stderr, "Not enough space available to build tree\n");
109 		exit(1);
110 	}
111 
112 	if (*pt != ub) {
113 		(*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
114 		(*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
115 		(*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
116 		(*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
117 		(*pt)->ut_pc++;
118 		free(ub);
119 	}
120 }
121 
122 void print_node(const void *node, VISIT order, int level) {
123         if (order == postorder || order == leaf) {
124                 tb.ta_uid = (*(struct utab **)node)->ut_uid;
125                 CPYN(tb.ta_name, (*(struct utab **)node)->ut_name);
126                 tb.ta_cpu[0] = ((*(struct utab **)node)->ut_cpu[0]);
127                 tb.ta_cpu[1] = ((*(struct utab **)node)->ut_cpu[1]);
128                 tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
129                 tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
130                 tb.ta_pc = (*(struct utab **)node)->ut_pc;
131                 fwrite(&tb, sizeof(tb), 1, stdout);
132         }
133 }
134 
135 output()
136 {
137                 twalk((struct utab *)root, print_node);
138 }
139