time.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* time.c - time a simple command
  2. *
  3. * Copyright 2013 Rob Landley <rob@landley.net>
  4. *
  5. * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/time.html
  6. USE_TIME(NEWTOY(time, "<1^pv[-pv]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_MAYFORK))
  7. config TIME
  8. bool "time"
  9. default y
  10. help
  11. usage: time [-pv] COMMAND...
  12. Run command line and report real, user, and system time elapsed in seconds.
  13. (real = clock on the wall, user = cpu used by command's code,
  14. system = cpu used by OS on behalf of command.)
  15. -p POSIX format output
  16. -v Verbose
  17. */
  18. #define FOR_time
  19. #include "toys.h"
  20. void time_main(void)
  21. {
  22. struct timespec ts, ts2;
  23. struct rusage ru;
  24. long long sec[3];
  25. int stat, ii, idx, nano[3];
  26. pid_t pid;
  27. char *labels[] = {"\nreal"+!!FLAG(p), "user", "sys"}, **label = labels,
  28. *vlabels[] ={"Real", "User", "System"}, tab = toys.optflags ? ' ' : '\t';
  29. if (FLAG(v)) label = vlabels;
  30. clock_gettime(CLOCK_MONOTONIC, &ts);
  31. if (!(pid = XVFORK())) xexec(toys.optargs);
  32. wait4(pid, &stat, 0, &ru);
  33. clock_gettime(CLOCK_MONOTONIC, &ts2);
  34. sec[0] = nanodiff(&ts, &ts2);
  35. nano[0] = (sec[0] % 1000000000)/(toys.optflags ? 1000 : 1000000);
  36. sec[0] /= 1000000000;
  37. sec[1] = ru.ru_utime.tv_sec, nano[1] = ru.ru_utime.tv_usec;
  38. sec[2] = ru.ru_stime.tv_sec, nano[2] = ru.ru_stime.tv_usec;
  39. for (ii = idx = 0; ii<3; ii++)
  40. idx += sprintf(toybuf+idx, "%s%s%c%lld.%0*d\n", label[ii],
  41. FLAG(v) ? " time (s):" : "", tab, sec[ii],
  42. 6>>!toys.optflags, nano[ii]);
  43. if (FLAG(v)) idx += sprintf(toybuf+idx,
  44. "Max RSS (KiB): %ld\nMajor faults: %ld\n"
  45. "Minor faults: %ld\nFile system inputs: %ld\nFile system outputs: %ld\n"
  46. "Voluntary context switches: %ld\nInvoluntary context switches: %ld\n",
  47. ru.ru_maxrss, ru.ru_majflt, ru.ru_minflt, ru.ru_inblock,
  48. ru.ru_oublock, ru.ru_nvcsw, ru.ru_nivcsw);
  49. writeall(2, toybuf, idx);
  50. toys.exitval = WIFEXITED(stat) ? WEXITSTATUS(stat) : WTERMSIG(stat);
  51. }