/usr.bin/time/time.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 303 lines · 231 code · 26 blank · 46 comment · 32 complexity · ca6de1622f94d4a8581d2996e0207acd MD5 · raw file

  1. /*
  2. * Copyright (c) 1987, 1988, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef lint
  30. static const char copyright[] =
  31. "@(#) Copyright (c) 1987, 1988, 1993\n\
  32. The Regents of the University of California. All rights reserved.\n";
  33. #endif /* not lint */
  34. #ifndef lint
  35. #if 0
  36. static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93";
  37. #endif
  38. static const char rcsid[] =
  39. "$FreeBSD$";
  40. #endif /* not lint */
  41. #include <sys/types.h>
  42. #include <sys/resource.h>
  43. #include <sys/signal.h>
  44. #include <sys/sysctl.h>
  45. #include <sys/time.h>
  46. #include <sys/wait.h>
  47. #include <err.h>
  48. #include <errno.h>
  49. #include <locale.h>
  50. #include <signal.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <stdint.h>
  54. #include <string.h>
  55. #include <unistd.h>
  56. static int getstathz(void);
  57. static void humantime(FILE *, long, long);
  58. static void showtime(FILE *, struct timeval *, struct timeval *,
  59. struct rusage *);
  60. static void siginfo(int);
  61. static void usage(void);
  62. static char decimal_point;
  63. static struct timeval before_tv;
  64. static int hflag, pflag;
  65. int
  66. main(int argc, char **argv)
  67. {
  68. int aflag, ch, lflag, status;
  69. int exitonsig;
  70. pid_t pid;
  71. struct rlimit rl;
  72. struct rusage ru;
  73. struct timeval after;
  74. char *ofn = NULL;
  75. FILE *out = stderr;
  76. (void) setlocale(LC_NUMERIC, "");
  77. decimal_point = localeconv()->decimal_point[0];
  78. aflag = hflag = lflag = pflag = 0;
  79. while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
  80. switch((char)ch) {
  81. case 'a':
  82. aflag = 1;
  83. break;
  84. case 'h':
  85. hflag = 1;
  86. break;
  87. case 'l':
  88. lflag = 1;
  89. break;
  90. case 'o':
  91. ofn = optarg;
  92. break;
  93. case 'p':
  94. pflag = 1;
  95. break;
  96. case '?':
  97. default:
  98. usage();
  99. }
  100. if (!(argc -= optind))
  101. exit(0);
  102. argv += optind;
  103. if (ofn) {
  104. if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
  105. err(1, "%s", ofn);
  106. setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
  107. }
  108. (void)gettimeofday(&before_tv, NULL);
  109. switch(pid = fork()) {
  110. case -1: /* error */
  111. err(1, "time");
  112. /* NOTREACHED */
  113. case 0: /* child */
  114. if (ofn)
  115. fclose(out);
  116. execvp(*argv, argv);
  117. err(errno == ENOENT ? 127 : 126, "%s", *argv);
  118. /* NOTREACHED */
  119. }
  120. /* parent */
  121. (void)signal(SIGINT, SIG_IGN);
  122. (void)signal(SIGQUIT, SIG_IGN);
  123. (void)signal(SIGINFO, siginfo);
  124. while (wait4(pid, &status, 0, &ru) != pid);
  125. (void)gettimeofday(&after, NULL);
  126. if ( ! WIFEXITED(status))
  127. warnx("command terminated abnormally");
  128. exitonsig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
  129. showtime(out, &before_tv, &after, &ru);
  130. if (lflag) {
  131. int hz = getstathz();
  132. u_long ticks;
  133. ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
  134. hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
  135. /*
  136. * If our round-off on the tick calculation still puts us at 0,
  137. * then always assume at least one tick.
  138. */
  139. if (ticks == 0)
  140. ticks = 1;
  141. fprintf(out, "%10ld %s\n",
  142. ru.ru_maxrss, "maximum resident set size");
  143. fprintf(out, "%10ld %s\n",
  144. ru.ru_ixrss / ticks, "average shared memory size");
  145. fprintf(out, "%10ld %s\n",
  146. ru.ru_idrss / ticks, "average unshared data size");
  147. fprintf(out, "%10ld %s\n",
  148. ru.ru_isrss / ticks, "average unshared stack size");
  149. fprintf(out, "%10ld %s\n",
  150. ru.ru_minflt, "page reclaims");
  151. fprintf(out, "%10ld %s\n",
  152. ru.ru_majflt, "page faults");
  153. fprintf(out, "%10ld %s\n",
  154. ru.ru_nswap, "swaps");
  155. fprintf(out, "%10ld %s\n",
  156. ru.ru_inblock, "block input operations");
  157. fprintf(out, "%10ld %s\n",
  158. ru.ru_oublock, "block output operations");
  159. fprintf(out, "%10ld %s\n",
  160. ru.ru_msgsnd, "messages sent");
  161. fprintf(out, "%10ld %s\n",
  162. ru.ru_msgrcv, "messages received");
  163. fprintf(out, "%10ld %s\n",
  164. ru.ru_nsignals, "signals received");
  165. fprintf(out, "%10ld %s\n",
  166. ru.ru_nvcsw, "voluntary context switches");
  167. fprintf(out, "%10ld %s\n",
  168. ru.ru_nivcsw, "involuntary context switches");
  169. }
  170. /*
  171. * If the child has exited on a signal, exit on the same
  172. * signal, too, in order to reproduce the child's exit status.
  173. * However, avoid actually dumping core from the current process.
  174. */
  175. if (exitonsig) {
  176. if (signal(exitonsig, SIG_DFL) == SIG_ERR)
  177. warn("signal");
  178. else {
  179. rl.rlim_max = rl.rlim_cur = 0;
  180. if (setrlimit(RLIMIT_CORE, &rl) == -1)
  181. warn("setrlimit");
  182. kill(getpid(), exitonsig);
  183. }
  184. }
  185. exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
  186. }
  187. static void
  188. usage(void)
  189. {
  190. fprintf(stderr,
  191. "usage: time [-al] [-h | -p] [-o file] utility [argument ...]\n");
  192. exit(1);
  193. }
  194. /*
  195. * Return the frequency of the kernel's statistics clock.
  196. */
  197. static int
  198. getstathz(void)
  199. {
  200. int mib[2];
  201. size_t size;
  202. struct clockinfo clockrate;
  203. mib[0] = CTL_KERN;
  204. mib[1] = KERN_CLOCKRATE;
  205. size = sizeof clockrate;
  206. if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
  207. err(1, "sysctl kern.clockrate");
  208. return clockrate.stathz;
  209. }
  210. static void
  211. humantime(FILE *out, long sec, long usec)
  212. {
  213. long days, hrs, mins;
  214. days = sec / (60L * 60 * 24);
  215. sec %= (60L * 60 * 24);
  216. hrs = sec / (60L * 60);
  217. sec %= (60L * 60);
  218. mins = sec / 60;
  219. sec %= 60;
  220. fprintf(out, "\t");
  221. if (days)
  222. fprintf(out, "%ldd", days);
  223. if (hrs)
  224. fprintf(out, "%ldh", hrs);
  225. if (mins)
  226. fprintf(out, "%ldm", mins);
  227. fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
  228. }
  229. static void
  230. showtime(FILE *out, struct timeval *before, struct timeval *after,
  231. struct rusage *ru)
  232. {
  233. after->tv_sec -= before->tv_sec;
  234. after->tv_usec -= before->tv_usec;
  235. if (after->tv_usec < 0)
  236. after->tv_sec--, after->tv_usec += 1000000;
  237. if (pflag) {
  238. /* POSIX wants output that must look like
  239. "real %f\nuser %f\nsys %f\n" and requires
  240. at least two digits after the radix. */
  241. fprintf(out, "real %jd%c%02ld\n",
  242. (intmax_t)after->tv_sec, decimal_point,
  243. after->tv_usec/10000);
  244. fprintf(out, "user %jd%c%02ld\n",
  245. (intmax_t)ru->ru_utime.tv_sec, decimal_point,
  246. ru->ru_utime.tv_usec/10000);
  247. fprintf(out, "sys %jd%c%02ld\n",
  248. (intmax_t)ru->ru_stime.tv_sec, decimal_point,
  249. ru->ru_stime.tv_usec/10000);
  250. } else if (hflag) {
  251. humantime(out, after->tv_sec, after->tv_usec/10000);
  252. fprintf(out, " real\t");
  253. humantime(out, ru->ru_utime.tv_sec, ru->ru_utime.tv_usec/10000);
  254. fprintf(out, " user\t");
  255. humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000);
  256. fprintf(out, " sys\n");
  257. } else {
  258. fprintf(out, "%9jd%c%02ld real ",
  259. (intmax_t)after->tv_sec, decimal_point,
  260. after->tv_usec/10000);
  261. fprintf(out, "%9jd%c%02ld user ",
  262. (intmax_t)ru->ru_utime.tv_sec, decimal_point,
  263. ru->ru_utime.tv_usec/10000);
  264. fprintf(out, "%9jd%c%02ld sys\n",
  265. (intmax_t)ru->ru_stime.tv_sec, decimal_point,
  266. ru->ru_stime.tv_usec/10000);
  267. }
  268. }
  269. static void
  270. siginfo(int sig __unused)
  271. {
  272. struct timeval after;
  273. struct rusage ru;
  274. (void)gettimeofday(&after, NULL);
  275. getrusage(RUSAGE_CHILDREN, &ru);
  276. showtime(stdout, &before_tv, &after, &ru);
  277. }