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 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #include <sys/types.h> 33 #include <sys/times.h> 34 #include <sys/param.h> 35 #include <sys/wait.h> 36 #include <unistd.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <signal.h> 40 #include <strings.h> 41 #include <time.h> 42 #include <errno.h> 43 #include <pwd.h> 44 45 char fname[20]; 46 47 /* 48 * Quant[0] will get set to HZ/10 later. 49 */ 50 char quant[] = { 10, 10, 10, 6, 10, 6, 10, 10, 10 }; 51 52 void printt(char *, time_t); 53 void hmstime(char[]); 54 void diag(char *); 55 56 int 57 main(int argc, char **argv) 58 { 59 struct tms buffer, obuffer; 60 int status; 61 register pid_t p; 62 int c; 63 time_t before, after; 64 char stime[9], etime[9]; 65 char cmd[80]; 66 extern char *optarg; 67 extern int optind; 68 int pflg = 0, sflg = 0, oflg = 0; 69 char aopt[25]; 70 FILE *pipin; 71 char ttyid[12], line[150]; 72 char eol; 73 char fld[20][12]; 74 int iline = 0, i, nfld; 75 int ichar, iblok; 76 long chars = 0, bloks = 0; 77 78 /* initalize quant array using the sysconf() */ 79 quant[0] = ((int)sysconf(_SC_CLK_TCK))/10; 80 81 aopt[0] = '\0'; /* terminate the string #1245107 */ 82 /* check options; */ 83 while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF) 84 switch (c) { 85 case 's': sflg++; break; 86 case 'o': oflg++; break; 87 case 'p': pflg++; break; 88 89 case 'f': strcat(aopt, "-f "); break; 90 case 'h': strcat(aopt, "-h "); break; 91 case 'k': strcat(aopt, "-k "); break; 92 case 'm': strcat(aopt, "-m "); break; 93 case 'r': strcat(aopt, "-r "); break; 94 case 't': strcat(aopt, "-t "); break; 95 96 case '?': diag("Usage: timex [-s][-o][-p[-fhkmrt]] cmd"); 97 break; 98 } 99 if (optind >= argc) diag("Missing command"); 100 101 /* 102 * Check to see if accounting is installed and print a somewhat 103 * meaninful message if not. 104 */ 105 if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) { 106 oflg = 0; 107 pflg = 0; 108 fprintf(stderr, 109 "Information from -p and -o options not available\n"); 110 fprintf(stderr, 111 " because process accounting is not operational.\n"); 112 } 113 114 if (sflg) { 115 sprintf(fname, "/tmp/tmx%ld", getpid()); 116 sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname); 117 system(cmd); 118 } 119 if (pflg + oflg) hmstime(stime); 120 before = times(&obuffer); 121 if ((p = fork()) == (pid_t)-1) diag("Try again.\n"); 122 if (p == 0) { 123 setgid(getgid()); 124 execvp(*(argv+optind), (argv+optind)); 125 fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno)); 126 exit(1); 127 } 128 signal(SIGINT, SIG_IGN); 129 signal(SIGQUIT, SIG_IGN); 130 while (wait(&status) != p) 131 ; 132 if ((status&0377) != 0) 133 fprintf(stderr, "Command terminated abnormally.\n"); 134 signal(SIGINT, SIG_DFL); 135 signal(SIGQUIT, SIG_DFL); 136 after = times(&buffer); 137 if (pflg + oflg) hmstime(etime); 138 if (sflg) system(cmd); 139 140 fprintf(stderr, "\n"); 141 printt("real", (after-before)); 142 printt("user", buffer.tms_cutime - obuffer.tms_cutime); 143 printt("sys ", buffer.tms_cstime - obuffer.tms_cstime); 144 fprintf(stderr, "\n"); 145 146 if (oflg+pflg) { 147 if (isatty(0)) 148 sprintf(ttyid, "-l %s", ttyname(0)+5); 149 sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s", 150 stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt); 151 pipin = popen(cmd, "r"); 152 while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) { 153 if (pflg) 154 fprintf(stderr, "%s\n", line); 155 if (oflg) { 156 nfld = sscanf(line, 157 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 158 fld[0], fld[1], fld[2], fld[3], fld[4], 159 fld[5], fld[6], fld[7], fld[8], fld[9], 160 fld[10], fld[11], fld[12], fld[13], fld[14], 161 fld[15], fld[16], fld[17], fld[18], 162 fld[19]); 163 if (++iline == 3) 164 for (i = 0; i < nfld; i++) { 165 if (strcmp(fld[i], "CHARS") 166 == 0) 167 ichar = i+2; 168 if (strcmp(fld[i], "BLOCKS") 169 == 0) 170 iblok = i+2; 171 } 172 if (iline > 4) { 173 chars += atol(fld[ichar]); 174 bloks += atol(fld[iblok]); 175 } 176 } 177 } 178 pclose(pipin); 179 180 if (oflg) 181 if (iline > 4) 182 fprintf(stderr, 183 "\nCHARS TRNSFD = %ld\n" 184 "BLOCKS READ = %ld\n", chars, bloks); 185 else 186 fprintf(stderr, 187 "\nNo process records found!\n"); 188 } 189 190 if (sflg) { 191 sprintf(cmd, "/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname); 192 system(cmd); 193 unlink(fname); 194 } 195 exit(status>>8); 196 } 197 198 char *pad = "000 "; 199 char *sep = "\0\0.\0:\0:\0\0"; 200 char *nsep = "\0\0.\0 \0 \0\0"; 201 202 void 203 printt(char *s, time_t a) 204 { 205 char digit[9]; 206 int i; 207 char c; 208 int nonzero; 209 210 for (i = 0; i < 9; i++) { 211 digit[i] = a % quant[i]; 212 a /= quant[i]; 213 } 214 fprintf(stderr, s); 215 nonzero = 0; 216 while (--i > 0) { 217 c = digit[i] != 0 ? digit[i] + '0': 218 nonzero ? '0': 219 pad[i]; 220 if (c != '\0') putc(c, stderr); 221 nonzero |= digit[i]; 222 c = nonzero?sep[i]:nsep[i]; 223 if (c != '\0') putc(c, stderr); 224 } 225 fprintf(stderr, "%c", digit[0] * 100/HZ + '0'); 226 fprintf(stderr, "\n"); 227 } 228 229 /* 230 * hmstime() sets current time in hh:mm:ss string format in stime; 231 */ 232 233 void 234 hmstime(char stime[]) 235 { 236 char *ltime; 237 time_t tme; 238 239 tme = time((time_t *)0); 240 ltime = ctime(&tme); 241 strncpy(stime, ltime+11, 8); 242 stime[8] = '\0'; 243 } 244 245 void 246 diag(char *s) 247 { 248 fprintf(stderr, "%s\n", s); 249 unlink(fname); 250 exit(1); 251 } 252