summaryrefslogtreecommitdiff
path: root/usr.bin/time/time.c
diff options
context:
space:
mode:
authorKiyoshi Aman <kiyoshi.aman+adelie@gmail.com>2019-06-05 00:10:11 -0500
committerKiyoshi Aman <kiyoshi.aman+adelie@gmail.com>2019-06-05 00:10:11 -0500
commit0a732af73aa7a2378e6981d3a7443e50fd291d2c (patch)
tree7b06b42159bdf69ed9c2ccca55d0d10ec09d79e1 /usr.bin/time/time.c
parent781dfa351934e5c0f6927456458a9f1a19442107 (diff)
downloaduserland-0a732af73aa7a2378e6981d3a7443e50fd291d2c.tar.gz
userland-0a732af73aa7a2378e6981d3a7443e50fd291d2c.tar.bz2
userland-0a732af73aa7a2378e6981d3a7443e50fd291d2c.tar.xz
userland-0a732af73aa7a2378e6981d3a7443e50fd291d2c.zip
usr.bin/time: this was inappropriately pruned at some point
Diffstat (limited to 'usr.bin/time/time.c')
-rw-r--r--usr.bin/time/time.c150
1 files changed, 135 insertions, 15 deletions
diff --git a/usr.bin/time/time.c b/usr.bin/time/time.c
index 263db9a..1d43d9f 100644
--- a/usr.bin/time/time.c
+++ b/usr.bin/time/time.c
@@ -29,21 +29,9 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\
- The Regents of the University of California. All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93";
-#endif
-__RCSID("$NetBSD: time.c,v 1.23 2017/07/15 14:34:08 christos Exp $");
-#endif /* not lint */
-
#include <sys/types.h>
#include <sys/time.h>
+#include <bsd/sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <errno.h>
@@ -52,16 +40,22 @@ __RCSID("$NetBSD: time.c,v 1.23 2017/07/15 14:34:08 christos Exp $");
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <bsd/stdlib.h>
#include <unistd.h>
+#include <time.h>
-#include "ext.h"
+#include "strpct.h"
-__dead static void usage(void);
+static void usage(void);
static void prl(long, const char *);
static void prts(const char *, const char *, const struct timespec *,
const char *);
static void prtv(const char *, const char *, const struct timeval *,
const char *);
+void prusage1(FILE *, const char *fmt, struct rusage *, struct rusage *,
+ struct timespec *, struct timespec *);
+static void pdeltat(FILE *fp, struct timeval *t1, struct timeval *t0);
+static void pcsecs(FILE *fp, long l);
int
main(int argc, char ** volatile argv)
@@ -213,3 +207,129 @@ prtv(const char *pre, const char *decpt, const struct timeval *tv,
(void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)tv->tv_sec,
decpt, (long)tv->tv_usec / 10000, post);
}
+
+void
+prusage1(FILE *fp, const char *cp, struct rusage *r0, struct rusage *r1,
+ struct timespec *e, struct timespec *b)
+{
+ long i;
+ time_t t;
+ time_t ms;
+
+ ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_nsec - b->tv_nsec) / 10000000;
+ t = (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
+ (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
+ (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
+ (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
+
+ for (; *cp; cp++)
+ if (*cp != '%')
+ (void) fputc(*cp, fp);
+ else if (cp[1])
+ switch (*++cp) {
+ case 'D': /* (average) unshared data size */
+ (void)fprintf(fp, "%ld", t == 0 ? 0L :
+ (long)((r1->ru_idrss + r1->ru_isrss -
+ (r0->ru_idrss + r0->ru_isrss)) / t));
+ break;
+ case 'E': /* elapsed (wall-clock) time */
+ pcsecs(fp, (long) ms);
+ break;
+ case 'F': /* page faults */
+ (void)fprintf(fp, "%ld", r1->ru_majflt - r0->ru_majflt);
+ break;
+ case 'I': /* FS blocks in */
+ (void)fprintf(fp, "%ld", r1->ru_inblock - r0->ru_inblock);
+ break;
+ case 'K': /* (average) total data memory used */
+ (void)fprintf(fp, "%ld", t == 0 ? 0L :
+ (long)(((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) -
+ (r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t));
+ break;
+ case 'M': /* max. Resident Set Size */
+ (void)fprintf(fp, "%ld", r1->ru_maxrss / 2L);
+ break;
+ case 'O': /* FS blocks out */
+ (void)fprintf(fp, "%ld", r1->ru_oublock - r0->ru_oublock);
+ break;
+ case 'P': /* percent time spent running */
+ /* check if it did not run at all */
+ if (ms == 0) {
+ (void)fputs("0.0%", fp);
+ } else {
+ char pb[32];
+ (void)fputs(strpct(pb, sizeof(pb),
+ (uintmax_t)t, (uintmax_t)ms, 1), fp);
+ (void)fputc('%', fp);
+ }
+ break;
+ case 'R': /* page reclaims */
+ (void)fprintf(fp, "%ld", r1->ru_minflt - r0->ru_minflt);
+ break;
+ case 'S': /* system CPU time used */
+ pdeltat(fp, &r1->ru_stime, &r0->ru_stime);
+ break;
+ case 'U': /* user CPU time used */
+ pdeltat(fp, &r1->ru_utime, &r0->ru_utime);
+ break;
+ case 'W': /* number of swaps */
+ i = r1->ru_nswap - r0->ru_nswap;
+ (void)fprintf(fp, "%ld", i);
+ break;
+ case 'X': /* (average) shared text size */
+ (void)fprintf(fp, "%ld", t == 0 ? 0L :
+ (long)((r1->ru_ixrss - r0->ru_ixrss) / t));
+ break;
+ case 'c': /* num. involuntary context switches */
+ (void)fprintf(fp, "%ld", r1->ru_nivcsw - r0->ru_nivcsw);
+ break;
+ case 'k': /* number of signals received */
+ (void)fprintf(fp, "%ld", r1->ru_nsignals-r0->ru_nsignals);
+ break;
+ case 'r': /* socket messages received */
+ (void)fprintf(fp, "%ld", r1->ru_msgrcv - r0->ru_msgrcv);
+ break;
+ case 's': /* socket messages sent */
+ (void)fprintf(fp, "%ld", r1->ru_msgsnd - r0->ru_msgsnd);
+ break;
+ case 'w': /* num. voluntary context switches (waits) */
+ (void)fprintf(fp, "%ld", r1->ru_nvcsw - r0->ru_nvcsw);
+ break;
+ }
+ (void)fputc('\n', fp);
+}
+
+static void
+pdeltat(FILE *fp, struct timeval *t1, struct timeval *t0)
+{
+ struct timeval td;
+
+ timersub(t1, t0, &td);
+ (void)fprintf(fp, "%ld.%01ld", (long)td.tv_sec,
+ (long)(td.tv_usec / 100000));
+}
+
+#define P2DIG(fp, i) (void)fprintf(fp, "%ld%ld", (i) / 10, (i) % 10)
+
+static void
+pcsecs(FILE *fp, long l) /* PWP: print mm:ss.dd, l is in sec*100 */
+{
+ long i;
+
+ i = l / 360000;
+ if (i) {
+ (void)fprintf(fp, "%ld:", i);
+ i = (l % 360000) / 100;
+ P2DIG(fp, i / 60);
+ goto minsec;
+ }
+ i = l / 100;
+ (void)fprintf(fp, "%ld", i / 60);
+minsec:
+ i %= 60;
+ (void)fputc(':', fp);
+ P2DIG(fp, i);
+ (void)fputc('.', fp);
+ P2DIG(fp, (l % 100));
+}
+