17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
27*414388d7Ssl108498 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
287c478bd9Sstevel@tonic-gate * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate */
30*414388d7Ssl108498 #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * acctprc
347c478bd9Sstevel@tonic-gate * reads std. input (acct.h format),
357c478bd9Sstevel@tonic-gate * writes std. output (tacct format)
367c478bd9Sstevel@tonic-gate * sorted by uid
377c478bd9Sstevel@tonic-gate * adds login names
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/param.h>
437c478bd9Sstevel@tonic-gate #include "acctdef.h"
447c478bd9Sstevel@tonic-gate #include <sys/acct.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <search.h>
47*414388d7Ssl108498 #include <stdlib.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate struct acct ab;
507c478bd9Sstevel@tonic-gate struct ptmp pb;
517c478bd9Sstevel@tonic-gate struct tacct tb;
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate struct utab {
547c478bd9Sstevel@tonic-gate uid_t ut_uid;
557c478bd9Sstevel@tonic-gate char ut_name[NSZ];
567c478bd9Sstevel@tonic-gate float ut_cpu[2]; /* cpu time (mins) */
577c478bd9Sstevel@tonic-gate float ut_kcore[2]; /* kcore-mins */
587c478bd9Sstevel@tonic-gate long ut_pc; /* # processes */
597c478bd9Sstevel@tonic-gate } * ub;
60*414388d7Ssl108498 static int usize;
617c478bd9Sstevel@tonic-gate void **root = NULL;
627c478bd9Sstevel@tonic-gate
63*414388d7Ssl108498 void output(void);
64*414388d7Ssl108498 void enter(struct ptmp *);
65*414388d7Ssl108498
66*414388d7Ssl108498 int
main(int argc,char ** argv)67*414388d7Ssl108498 main(int argc, char **argv)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate long elaps[2];
707c478bd9Sstevel@tonic-gate ulong_t etime, stime;
717c478bd9Sstevel@tonic-gate unsigned long mem;
727c478bd9Sstevel@tonic-gate #ifdef uts
737c478bd9Sstevel@tonic-gate float expand();
747c478bd9Sstevel@tonic-gate #else
757c478bd9Sstevel@tonic-gate ulong_t expand();
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate while (fread(&ab, sizeof(ab), 1, stdin) == 1) {
797c478bd9Sstevel@tonic-gate if (!MYKIND(ab.ac_flag))
807c478bd9Sstevel@tonic-gate continue;
817c478bd9Sstevel@tonic-gate pb.pt_uid = ab.ac_uid;
827c478bd9Sstevel@tonic-gate CPYN(pb.pt_name, NULL);
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate * approximate cpu P/NP split as same as elapsed time
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate if ((etime = SECS(expand(ab.ac_etime))) == 0)
877c478bd9Sstevel@tonic-gate etime = 1;
887c478bd9Sstevel@tonic-gate stime = expand(ab.ac_stime) + expand(ab.ac_utime);
897c478bd9Sstevel@tonic-gate mem = expand(ab.ac_mem);
907c478bd9Sstevel@tonic-gate if(pnpsplit(ab.ac_btime, etime, elaps) == 0) {
917c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc: could not calculate prime/non-prime hours\n");
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate exit(1);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime;
967c478bd9Sstevel@tonic-gate pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0;
977c478bd9Sstevel@tonic-gate pb.pt_cpu[1] = stime - pb.pt_cpu[0];
987c478bd9Sstevel@tonic-gate if (stime)
997c478bd9Sstevel@tonic-gate pb.pt_mem = (mem + stime - 1) / stime;
1007c478bd9Sstevel@tonic-gate else
1017c478bd9Sstevel@tonic-gate pb.pt_mem = 0; /* unlikely */
1027c478bd9Sstevel@tonic-gate enter(&pb);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate output();
1057c478bd9Sstevel@tonic-gate exit(0);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
node_compare(const void * node1,const void * node2)1087c478bd9Sstevel@tonic-gate int node_compare(const void *node1, const void *node2)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate if (((const struct utab *)node1)->ut_uid > \
1117c478bd9Sstevel@tonic-gate ((const struct utab *)node2)->ut_uid)
1127c478bd9Sstevel@tonic-gate return(1);
1137c478bd9Sstevel@tonic-gate else if (((const struct utab *)node1)->ut_uid < \
1147c478bd9Sstevel@tonic-gate ((const struct utab *)node2)->ut_uid)
1157c478bd9Sstevel@tonic-gate return(-1);
1167c478bd9Sstevel@tonic-gate else return(0);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
119*414388d7Ssl108498 void
enter(struct ptmp * p)120*414388d7Ssl108498 enter(struct ptmp *p)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate double memk;
1237c478bd9Sstevel@tonic-gate struct utab **pt;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
1267c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc: malloc fail!\n");
1277c478bd9Sstevel@tonic-gate exit(2);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate ub->ut_uid = p->pt_uid;
1317c478bd9Sstevel@tonic-gate CPYN(ub->ut_name, p->pt_name);
1327c478bd9Sstevel@tonic-gate ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
1337c478bd9Sstevel@tonic-gate ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
1347c478bd9Sstevel@tonic-gate memk = KCORE(pb.pt_mem);
1357c478bd9Sstevel@tonic-gate ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
1367c478bd9Sstevel@tonic-gate ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
1377c478bd9Sstevel@tonic-gate ub->ut_pc = 1;
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root, \
1407c478bd9Sstevel@tonic-gate node_compare)) == NULL) {
1417c478bd9Sstevel@tonic-gate fprintf(stderr, "Not enough space available to build tree\n");
1427c478bd9Sstevel@tonic-gate exit(1);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate if (*pt != ub) {
1467c478bd9Sstevel@tonic-gate (*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
1477c478bd9Sstevel@tonic-gate (*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
1487c478bd9Sstevel@tonic-gate (*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
1497c478bd9Sstevel@tonic-gate (*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
1507c478bd9Sstevel@tonic-gate (*pt)->ut_pc++;
1517c478bd9Sstevel@tonic-gate free(ub);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
print_node(const void * node,VISIT order,int level)1557c478bd9Sstevel@tonic-gate void print_node(const void *node, VISIT order, int level) {
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate if (order == postorder || order == leaf) {
1587c478bd9Sstevel@tonic-gate tb.ta_uid = (*(struct utab **)node)->ut_uid;
1597c478bd9Sstevel@tonic-gate CPYN(tb.ta_name, (char *)uidtonam((*(struct utab **)node)->ut_uid));
1607c478bd9Sstevel@tonic-gate tb.ta_cpu[0] = (*(struct utab **)node)->ut_cpu[0];
1617c478bd9Sstevel@tonic-gate tb.ta_cpu[1] = (*(struct utab **)node)->ut_cpu[1];
1627c478bd9Sstevel@tonic-gate tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
1637c478bd9Sstevel@tonic-gate tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
1647c478bd9Sstevel@tonic-gate tb.ta_pc = (*(struct utab **)node)->ut_pc;
1657c478bd9Sstevel@tonic-gate fwrite(&tb, sizeof(tb), 1, stdout);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
169*414388d7Ssl108498 void
output(void)170*414388d7Ssl108498 output(void)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate twalk((struct utab *)root, print_node);
1737c478bd9Sstevel@tonic-gate }
174