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