getruntime.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Return time used so far, in microseconds.
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. This file is part of the libiberty library.
  4. Libiberty is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. Libiberty is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with libiberty; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "config.h"
  17. #include "ansidecl.h"
  18. #include "libiberty.h"
  19. /* On some systems (such as WindISS), you must include <sys/types.h>
  20. to get the definition of "time_t" before you include <time.h>. */
  21. #include <sys/types.h>
  22. /* There are several ways to get elapsed execution time; unfortunately no
  23. single way is available for all host systems, nor are there reliable
  24. ways to find out which way is correct for a given host. */
  25. #ifdef TIME_WITH_SYS_TIME
  26. # include <sys/time.h>
  27. # include <time.h>
  28. #else
  29. # if HAVE_SYS_TIME_H
  30. # include <sys/time.h>
  31. # else
  32. # ifdef HAVE_TIME_H
  33. # include <time.h>
  34. # endif
  35. # endif
  36. #endif
  37. #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
  38. #include <sys/resource.h>
  39. #endif
  40. #ifdef HAVE_TIMES
  41. #ifdef HAVE_SYS_PARAM_H
  42. #include <sys/param.h>
  43. #endif
  44. #include <sys/times.h>
  45. #endif
  46. #ifdef HAVE_UNISTD_H
  47. #include <unistd.h>
  48. #endif
  49. /* This is a fallback; if wrong, it will likely make obviously wrong
  50. results. */
  51. #ifndef CLOCKS_PER_SEC
  52. #define CLOCKS_PER_SEC 1
  53. #endif
  54. #ifndef RUSAGE_SELF
  55. #define RUSAGE_SELF 0
  56. #endif
  57. #ifdef _SC_CLK_TCK
  58. #define GNU_HZ sysconf(_SC_CLK_TCK)
  59. #else
  60. #ifdef HZ
  61. #define GNU_HZ HZ
  62. #else
  63. #ifdef CLOCKS_PER_SEC
  64. #define GNU_HZ CLOCKS_PER_SEC
  65. #endif
  66. #endif
  67. #endif
  68. /*
  69. @deftypefn Replacement long get_run_time (void)
  70. Returns the time used so far, in microseconds. If possible, this is
  71. the time used by this process, else it is the elapsed time since the
  72. process started.
  73. @end deftypefn
  74. */
  75. long
  76. get_run_time (void)
  77. {
  78. #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
  79. struct rusage rusage;
  80. getrusage (RUSAGE_SELF, &rusage);
  81. return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  82. + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  83. #else /* ! HAVE_GETRUSAGE */
  84. #ifdef HAVE_TIMES
  85. struct tms tms;
  86. times (&tms);
  87. return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
  88. #else /* ! HAVE_TIMES */
  89. /* Fall back on clock and hope it's correctly implemented. */
  90. const long clocks_per_sec = CLOCKS_PER_SEC;
  91. if (clocks_per_sec <= 1000000)
  92. return clock () * (1000000 / clocks_per_sec);
  93. else
  94. return clock () / clocks_per_sec;
  95. #endif /* HAVE_TIMES */
  96. #endif /* HAVE_GETRUSAGE */
  97. }