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 /*
27414388d7Ssl * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
287c478bd9Sstevel@tonic-gate * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * acctprc
33*2a8bcb4eSToomas Soome * reads std. input (acct.h format),
347c478bd9Sstevel@tonic-gate * writes std. output (tacct format)
357c478bd9Sstevel@tonic-gate * sorted by uid
367c478bd9Sstevel@tonic-gate * adds login names
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include "acctdef.h"
437c478bd9Sstevel@tonic-gate #include <sys/acct.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <search.h>
46414388d7Ssl #include <stdlib.h>
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate struct acct ab;
497c478bd9Sstevel@tonic-gate struct ptmp pb;
507c478bd9Sstevel@tonic-gate struct tacct tb;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate struct utab {
537c478bd9Sstevel@tonic-gate uid_t ut_uid;
547c478bd9Sstevel@tonic-gate char ut_name[NSZ];
557c478bd9Sstevel@tonic-gate float ut_cpu[2]; /* cpu time (mins) */
567c478bd9Sstevel@tonic-gate float ut_kcore[2]; /* kcore-mins */
577c478bd9Sstevel@tonic-gate long ut_pc; /* # processes */
58*2a8bcb4eSToomas Soome } * ub;
59414388d7Ssl static int usize;
607c478bd9Sstevel@tonic-gate void **root = NULL;
617c478bd9Sstevel@tonic-gate
62414388d7Ssl void output(void);
63414388d7Ssl void enter(struct ptmp *);
64414388d7Ssl
65414388d7Ssl int
main(int argc,char ** argv)66414388d7Ssl main(int argc, char **argv)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate long elaps[2];
697c478bd9Sstevel@tonic-gate ulong_t etime, stime;
707c478bd9Sstevel@tonic-gate unsigned long mem;
717c478bd9Sstevel@tonic-gate #ifdef uts
727c478bd9Sstevel@tonic-gate float expand();
737c478bd9Sstevel@tonic-gate #else
747c478bd9Sstevel@tonic-gate ulong_t expand();
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate while (fread(&ab, sizeof(ab), 1, stdin) == 1) {
787c478bd9Sstevel@tonic-gate if (!MYKIND(ab.ac_flag))
797c478bd9Sstevel@tonic-gate continue;
807c478bd9Sstevel@tonic-gate pb.pt_uid = ab.ac_uid;
817c478bd9Sstevel@tonic-gate CPYN(pb.pt_name, NULL);
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * approximate cpu P/NP split as same as elapsed time
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate if ((etime = SECS(expand(ab.ac_etime))) == 0)
867c478bd9Sstevel@tonic-gate etime = 1;
877c478bd9Sstevel@tonic-gate stime = expand(ab.ac_stime) + expand(ab.ac_utime);
887c478bd9Sstevel@tonic-gate mem = expand(ab.ac_mem);
897c478bd9Sstevel@tonic-gate if(pnpsplit(ab.ac_btime, etime, elaps) == 0) {
907c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc: could not calculate prime/non-prime hours\n");
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate exit(1);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime;
957c478bd9Sstevel@tonic-gate pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0;
967c478bd9Sstevel@tonic-gate pb.pt_cpu[1] = stime - pb.pt_cpu[0];
977c478bd9Sstevel@tonic-gate if (stime)
987c478bd9Sstevel@tonic-gate pb.pt_mem = (mem + stime - 1) / stime;
997c478bd9Sstevel@tonic-gate else
1007c478bd9Sstevel@tonic-gate pb.pt_mem = 0; /* unlikely */
1017c478bd9Sstevel@tonic-gate enter(&pb);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate output();
1047c478bd9Sstevel@tonic-gate exit(0);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate
node_compare(const void * node1,const void * node2)1077c478bd9Sstevel@tonic-gate int node_compare(const void *node1, const void *node2)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate if (((const struct utab *)node1)->ut_uid > \
1107c478bd9Sstevel@tonic-gate ((const struct utab *)node2)->ut_uid)
111*2a8bcb4eSToomas Soome return(1);
1127c478bd9Sstevel@tonic-gate else if (((const struct utab *)node1)->ut_uid < \
1137c478bd9Sstevel@tonic-gate ((const struct utab *)node2)->ut_uid)
1147c478bd9Sstevel@tonic-gate return(-1);
1157c478bd9Sstevel@tonic-gate else return(0);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
118414388d7Ssl void
enter(struct ptmp * p)119414388d7Ssl enter(struct ptmp *p)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate double memk;
1227c478bd9Sstevel@tonic-gate struct utab **pt;
123*2a8bcb4eSToomas Soome
1247c478bd9Sstevel@tonic-gate if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
1257c478bd9Sstevel@tonic-gate fprintf(stderr, "acctprc: malloc fail!\n");
1267c478bd9Sstevel@tonic-gate exit(2);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate ub->ut_uid = p->pt_uid;
1307c478bd9Sstevel@tonic-gate CPYN(ub->ut_name, p->pt_name);
1317c478bd9Sstevel@tonic-gate ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
1327c478bd9Sstevel@tonic-gate ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
133*2a8bcb4eSToomas Soome memk = KCORE(pb.pt_mem);
1347c478bd9Sstevel@tonic-gate ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
1357c478bd9Sstevel@tonic-gate ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
1367c478bd9Sstevel@tonic-gate ub->ut_pc = 1;
137*2a8bcb4eSToomas Soome
1387c478bd9Sstevel@tonic-gate if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root, \
1397c478bd9Sstevel@tonic-gate node_compare)) == NULL) {
1407c478bd9Sstevel@tonic-gate fprintf(stderr, "Not enough space available to build tree\n");
1417c478bd9Sstevel@tonic-gate exit(1);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate if (*pt != ub) {
1457c478bd9Sstevel@tonic-gate (*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
1467c478bd9Sstevel@tonic-gate (*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
1477c478bd9Sstevel@tonic-gate (*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
1487c478bd9Sstevel@tonic-gate (*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
1497c478bd9Sstevel@tonic-gate (*pt)->ut_pc++;
1507c478bd9Sstevel@tonic-gate free(ub);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate
print_node(const void * node,VISIT order,int level)1547c478bd9Sstevel@tonic-gate void print_node(const void *node, VISIT order, int level) {
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (order == postorder || order == leaf) {
1577c478bd9Sstevel@tonic-gate tb.ta_uid = (*(struct utab **)node)->ut_uid;
1587c478bd9Sstevel@tonic-gate CPYN(tb.ta_name, (char *)uidtonam((*(struct utab **)node)->ut_uid));
1597c478bd9Sstevel@tonic-gate tb.ta_cpu[0] = (*(struct utab **)node)->ut_cpu[0];
1607c478bd9Sstevel@tonic-gate tb.ta_cpu[1] = (*(struct utab **)node)->ut_cpu[1];
1617c478bd9Sstevel@tonic-gate tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
1627c478bd9Sstevel@tonic-gate tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
1637c478bd9Sstevel@tonic-gate tb.ta_pc = (*(struct utab **)node)->ut_pc;
1647c478bd9Sstevel@tonic-gate fwrite(&tb, sizeof(tb), 1, stdout);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate }
167*2a8bcb4eSToomas Soome
168414388d7Ssl void
output(void)169414388d7Ssl output(void)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate twalk((struct utab *)root, print_node);
1727c478bd9Sstevel@tonic-gate }
173