summaryrefslogtreecommitdiff
path: root/usr/src
diff options
context:
space:
mode:
authoras158974 <none@none>2007-04-18 03:07:55 -0700
committeras158974 <none@none>2007-04-18 03:07:55 -0700
commit231ecbdf6cfa117492d49356673032a7cdbd4030 (patch)
tree13199ae9c8980e45ce4e0a66dd3c20b6ee69fe8c /usr/src
parentc11ca7f77720937404169a911432724967678718 (diff)
downloadillumos-joyent-231ecbdf6cfa117492d49356673032a7cdbd4030.tar.gz
6451790 timex gives garbage when the real time overflows
6524847 printt() limitation in timex
Diffstat (limited to 'usr/src')
-rw-r--r--usr/src/cmd/sa/timex.c122
1 files changed, 75 insertions, 47 deletions
diff --git a/usr/src/cmd/sa/timex.c b/usr/src/cmd/sa/timex.c
index 027aca7bee..fe149ef808 100644
--- a/usr/src/cmd/sa/timex.c
+++ b/usr/src/cmd/sa/timex.c
@@ -18,20 +18,19 @@
*
* CDDL HEADER END
*/
-
/*
- * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
-/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
-/* All Rights Reserved */
-
+/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
+/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/times.h>
+#include <sys/time.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -43,14 +42,15 @@
#include <errno.h>
#include <pwd.h>
-char fname[20];
+#define NSEC_TO_TICK(nsec) ((nsec) / nsec_per_tick)
+#define NSEC_TO_TICK_ROUNDUP(nsec) NSEC_TO_TICK((nsec) + \
+ nsec_per_tick/2)
-/*
- * Quant[0] will get set to HZ/10 later.
- */
-int quant[] = { 10, 10, 10, 6, 10, 6, 10, 10, 10, 10, 10 };
+char fname[20];
+static int hz;
+static int nsec_per_tick;
-void printt(char *, time_t);
+void printt(char *, hrtime_t);
void hmstime(char[]);
void diag(char *);
@@ -61,7 +61,7 @@ main(int argc, char **argv)
int status;
register pid_t p;
int c;
- time_t before, after;
+ hrtime_t before, after, timediff;
char stime[9], etime[9];
char cmd[80];
int pflg = 0, sflg = 0, oflg = 0;
@@ -74,10 +74,11 @@ main(int argc, char **argv)
int ichar, iblok;
long chars = 0, bloks = 0;
- /* initalize quant array using the sysconf() */
- quant[0] = ((int)sysconf(_SC_CLK_TCK))/10;
+ aopt[0] = '\0';
+
+ hz = sysconf(_SC_CLK_TCK);
+ nsec_per_tick = NANOSEC / hz;
- aopt[0] = '\0'; /* terminate the string #1245107 */
/* check options; */
while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF)
switch (c) {
@@ -116,13 +117,18 @@ main(int argc, char **argv)
system(cmd);
}
if (pflg + oflg) hmstime(stime);
- before = times(&obuffer);
- if ((p = fork()) == (pid_t)-1) diag("Try again.\n");
+ before = gethrtime();
+ (void) times(&obuffer);
+ if ((p = fork()) == (pid_t)-1) {
+ perror("Fork Failed");
+ (void) unlink(fname);
+ exit(EXIT_FAILURE);
+ }
if (p == 0) {
setgid(getgid());
execvp(*(argv+optind), (argv+optind));
fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno));
- exit(1);
+ exit(EXIT_FAILURE);
}
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
@@ -132,12 +138,14 @@ main(int argc, char **argv)
fprintf(stderr, "Command terminated abnormally.\n");
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
- after = times(&buffer);
+ (void) times(&buffer);
+ after = gethrtime();
+ timediff = after - before;
if (pflg + oflg) hmstime(etime);
if (sflg) system(cmd);
fprintf(stderr, "\n");
- printt("real", (after-before));
+ printt("real", NSEC_TO_TICK_ROUNDUP(timediff));
printt("user", buffer.tms_cutime - obuffer.tms_cutime);
printt("sys ", buffer.tms_cstime - obuffer.tms_cstime);
fprintf(stderr, "\n");
@@ -191,38 +199,58 @@ main(int argc, char **argv)
system(cmd);
unlink(fname);
}
- exit(status>>8);
+ exit(WEXITSTATUS(status));
}
-char *pad = "000 ";
-char *sep = "\0\0.\0:\0:\0\0";
-char *nsep = "\0\0.\0 \0 \0\0";
-
void
-printt(char *s, time_t a)
+printt(char *label, hrtime_t ticks)
{
- int digit[11];
- int i;
- char c;
- int nonzero;
-
- for (i = 0; i < 11; i++) {
- digit[i] = a % quant[i];
- a /= quant[i];
+ long tk; /* number of leftover ticks */
+ long ss; /* number of seconds */
+ long mm; /* number of minutes */
+ long hh; /* number of hours */
+ longlong_t total = ticks;
+
+ tk = total % hz; /* ticks % hz */
+ total /= hz;
+ ss = total % 60; /* ticks / hz % 60 */
+ total /= 60;
+ mm = total % 60; /* ticks / hz / 60 % 60 */
+ hh = total / 60; /* ticks / hz / 60 / 60 */
+
+ (void) fprintf(stderr, "%s ", label);
+
+ /* Display either padding or the elapsed hours */
+ if (hh == 0L) {
+ (void) fprintf(stderr, "%6c", ' ');
+ } else {
+ (void) fprintf(stderr, "%5ld:", hh);
}
- fprintf(stderr, s);
- nonzero = 0;
- while (--i > 0) {
- c = digit[i] != 0 ? digit[i] + '0':
- nonzero ? '0':
- pad[i];
- if (c != '\0') putc(c, stderr);
- nonzero |= digit[i];
- c = nonzero?sep[i]:nsep[i];
- if (c != '\0') putc(c, stderr);
+
+ /*
+ * Display either nothing or the elapsed minutes, zero
+ * padding (if hours > 0) or space padding (if not).
+ */
+ if (mm == 0L && hh == 0L) {
+ (void) fprintf(stderr, "%3c", ' ');
+ } else if (mm != 0L && hh == 0L) {
+ (void) fprintf(stderr, "%2ld:", mm);
+ } else {
+ (void) fprintf(stderr, "%02ld:", mm);
}
- fprintf(stderr, "%c", digit[0] * 100/HZ + '0');
- fprintf(stderr, "\n");
+
+ /*
+ * Display the elapsed seconds; seconds are always
+ * zero padded.
+ */
+ if (hh == 0L && mm == 0L) {
+ (void) fprintf(stderr, "%2ld.", ss);
+ } else {
+ (void) fprintf(stderr, "%02ld.", ss);
+ }
+
+ /* Display hundredths of a second. */
+ (void) fprintf(stderr, "%02ld\n", tk * 100/hz);
}
/*
@@ -246,5 +274,5 @@ diag(char *s)
{
fprintf(stderr, "%s\n", s);
unlink(fname);
- exit(1);
+ exit(EXIT_FAILURE);
}