2007-02-27 00:56:17 +03:00
|
|
|
/* $NetBSD: time.c,v 1.18 2007/02/26 21:56:17 matt Exp $ */
|
1994-12-08 12:30:36 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-12-08 12:30:36 +03:00
|
|
|
* Copyright (c) 1987, 1988, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-20 07:28:20 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-20 07:28:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-08 12:30:36 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
2007-02-27 00:56:17 +03:00
|
|
|
__RCSID("$NetBSD: time.c,v 1.18 2007/02/26 21:56:17 matt Exp $");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
1993-08-27 23:05:29 +04:00
|
|
|
#include <sys/wait.h>
|
1999-06-05 23:19:19 +04:00
|
|
|
#include <errno.h>
|
2002-07-16 19:41:57 +04:00
|
|
|
#include <err.h>
|
1999-06-05 23:19:19 +04:00
|
|
|
#include <locale.h>
|
1995-06-27 04:31:00 +04:00
|
|
|
#include <signal.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1993-08-28 02:30:10 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
1993-08-27 23:05:29 +04:00
|
|
|
|
2007-02-25 00:30:27 +03:00
|
|
|
#include "ext.h"
|
|
|
|
|
2002-07-16 19:41:57 +04:00
|
|
|
int main(int, char **);
|
|
|
|
static void usage(void);
|
|
|
|
static void prl(long, const char *);
|
|
|
|
static void prtv(const char *, const char *, const struct timeval *,
|
|
|
|
const char *);
|
1997-10-20 07:28:20 +04:00
|
|
|
|
1993-08-28 02:30:10 +04:00
|
|
|
int
|
2006-12-18 18:17:38 +03:00
|
|
|
main(int argc, char ** volatile argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-20 07:28:20 +04:00
|
|
|
int pid;
|
1993-08-27 23:05:29 +04:00
|
|
|
int ch, status;
|
2006-12-18 18:17:38 +03:00
|
|
|
int volatile portableflag;
|
|
|
|
int volatile lflag;
|
2007-02-25 00:30:27 +03:00
|
|
|
int volatile cshflag;
|
1999-06-05 23:19:19 +04:00
|
|
|
const char *decpt;
|
|
|
|
const struct lconv *lconv;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct timeval before, after;
|
|
|
|
struct rusage ru;
|
|
|
|
|
1999-06-05 23:19:19 +04:00
|
|
|
(void)setlocale(LC_ALL, "");
|
|
|
|
|
2007-02-25 00:30:27 +03:00
|
|
|
cshflag = lflag = portableflag = 0;
|
|
|
|
while ((ch = getopt(argc, argv, "clp")) != -1) {
|
1999-06-05 23:19:19 +04:00
|
|
|
switch (ch) {
|
2007-02-25 00:30:27 +03:00
|
|
|
case 'c':
|
|
|
|
cshflag = 1;
|
2007-02-27 00:56:17 +03:00
|
|
|
portableflag = 0;
|
|
|
|
lflag = 0;
|
2007-02-25 00:30:27 +03:00
|
|
|
break;
|
1993-08-27 23:05:29 +04:00
|
|
|
case 'p':
|
|
|
|
portableflag = 1;
|
2007-02-27 00:56:17 +03:00
|
|
|
cshflag = 0;
|
|
|
|
lflag = 0;
|
1993-08-27 23:05:29 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
2007-02-27 00:56:17 +03:00
|
|
|
portableflag = 0;
|
|
|
|
cshflag = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
1999-06-05 23:19:19 +04:00
|
|
|
usage();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1999-06-05 23:19:19 +04:00
|
|
|
}
|
|
|
|
argc -= optind;
|
1993-03-21 12:45:37 +03:00
|
|
|
argv += optind;
|
|
|
|
|
1999-06-05 23:19:19 +04:00
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
gettimeofday(&before, (struct timezone *)NULL);
|
|
|
|
switch(pid = vfork()) {
|
|
|
|
case -1: /* error */
|
2002-07-16 19:41:57 +04:00
|
|
|
err(EXIT_FAILURE, "Vfork failed");
|
1993-03-21 12:45:37 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
case 0: /* child */
|
1999-06-05 23:19:19 +04:00
|
|
|
/* LINTED will return only on failure */
|
1993-03-21 12:45:37 +03:00
|
|
|
execvp(*argv, argv);
|
2002-07-16 19:41:57 +04:00
|
|
|
err((errno == ENOENT) ? 127 : 126, "Can't exec `%s'", *argv);
|
1993-03-21 12:45:37 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1993-08-27 23:05:29 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/* parent */
|
|
|
|
(void)signal(SIGINT, SIG_IGN);
|
|
|
|
(void)signal(SIGQUIT, SIG_IGN);
|
2002-07-16 19:41:57 +04:00
|
|
|
if ((pid = wait4(pid, &status, 0, &ru)) == -1)
|
|
|
|
err(EXIT_FAILURE, "wait4 %d failed", pid);
|
|
|
|
(void)gettimeofday(&after, (struct timezone *)NULL);
|
1993-08-27 23:05:29 +04:00
|
|
|
if (!WIFEXITED(status))
|
2002-07-16 19:41:57 +04:00
|
|
|
warnx("Command terminated abnormally.");
|
1995-03-21 17:16:23 +03:00
|
|
|
timersub(&after, &before, &after);
|
1993-08-27 23:05:29 +04:00
|
|
|
|
1999-06-05 23:19:19 +04:00
|
|
|
if ((lconv = localeconv()) == NULL ||
|
|
|
|
(decpt = lconv->decimal_point) == NULL)
|
|
|
|
decpt = ".";
|
|
|
|
|
2007-02-25 00:30:27 +03:00
|
|
|
if (cshflag) {
|
|
|
|
static struct rusage null_ru;
|
|
|
|
before.tv_sec = 0;
|
|
|
|
before.tv_usec = 0;
|
|
|
|
prusage(stderr, &null_ru, &ru, &after, &before);
|
|
|
|
} else if (portableflag) {
|
2002-07-16 19:41:57 +04:00
|
|
|
prtv("real ", decpt, &after, "\n");
|
|
|
|
prtv("user ", decpt, &ru.ru_utime, "\n");
|
|
|
|
prtv("sys ", decpt, &ru.ru_stime, "\n");
|
1993-08-27 23:05:29 +04:00
|
|
|
} else {
|
2002-07-16 19:41:57 +04:00
|
|
|
prtv("", decpt, &after, " real ");
|
|
|
|
prtv("", decpt, &ru.ru_utime, " user ");
|
|
|
|
prtv("", decpt, &ru.ru_stime, " sys\n");
|
1993-08-27 23:05:29 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (lflag) {
|
1999-06-05 23:19:19 +04:00
|
|
|
int hz = (int)sysconf(_SC_CLK_TCK);
|
2002-07-16 19:41:57 +04:00
|
|
|
unsigned long long ticks;
|
|
|
|
#define SCALE(x) (long)(ticks ? x / ticks : 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
|
2005-10-27 05:43:48 +04:00
|
|
|
hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
|
2002-07-16 19:41:57 +04:00
|
|
|
prl(ru.ru_maxrss, "maximum resident set size");
|
|
|
|
prl(SCALE(ru.ru_ixrss), "average shared memory size");
|
|
|
|
prl(SCALE(ru.ru_idrss), "average unshared data size");
|
|
|
|
prl(SCALE(ru.ru_isrss), "average unshared stack size");
|
|
|
|
prl(ru.ru_minflt, "page reclaims");
|
|
|
|
prl(ru.ru_majflt, "page faults");
|
|
|
|
prl(ru.ru_nswap, "swaps");
|
|
|
|
prl(ru.ru_inblock, "block input operations");
|
|
|
|
prl(ru.ru_oublock, "block output operations");
|
|
|
|
prl(ru.ru_msgsnd, "messages sent");
|
|
|
|
prl(ru.ru_msgrcv, "messages received");
|
|
|
|
prl(ru.ru_nsignals, "signals received");
|
|
|
|
prl(ru.ru_nvcsw, "voluntary context switches");
|
|
|
|
prl(ru.ru_nivcsw, "involuntary context switches");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1993-08-27 23:05:29 +04:00
|
|
|
|
2002-07-16 19:41:57 +04:00
|
|
|
return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
|
1999-06-05 23:19:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
2005-10-27 05:43:48 +04:00
|
|
|
|
2007-02-25 00:30:27 +03:00
|
|
|
(void)fprintf(stderr, "usage: %s [-clp] utility [argument ...]\n",
|
2002-07-16 19:41:57 +04:00
|
|
|
getprogname());
|
1999-06-05 23:19:19 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-07-16 19:41:57 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
prl(long val, const char *expn)
|
|
|
|
{
|
2005-10-27 05:43:48 +04:00
|
|
|
|
|
|
|
(void)fprintf(stderr, "%10ld %s\n", val, expn);
|
2002-07-16 19:41:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prtv(const char *pre, const char *decpt, const struct timeval *tv,
|
|
|
|
const char *post)
|
|
|
|
{
|
2005-10-27 05:43:48 +04:00
|
|
|
|
|
|
|
(void)fprintf(stderr, "%s%9ld%s%02ld%s", pre, (long)tv->tv_sec, decpt,
|
|
|
|
(long)tv->tv_usec / 10000, post);
|
2002-07-16 19:41:57 +04:00
|
|
|
}
|