getruntime.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Return time used so far, in microseconds.
  2. Copyright (C) 1994, 1999, 2002 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. #ifdef _SC_CLK_TCK
  55. #define GNU_HZ sysconf(_SC_CLK_TCK)
  56. #else
  57. #ifdef HZ
  58. #define GNU_HZ HZ
  59. #else
  60. #ifdef CLOCKS_PER_SEC
  61. #define GNU_HZ CLOCKS_PER_SEC
  62. #endif
  63. #endif
  64. #endif
  65. /*
  66. @deftypefn Replacement long get_run_time (void)
  67. Returns the time used so far, in microseconds. If possible, this is
  68. the time used by this process, else it is the elapsed time since the
  69. process started.
  70. @end deftypefn
  71. */
  72. long
  73. get_run_time (void)
  74. {
  75. #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
  76. struct rusage rusage;
  77. getrusage (0, &rusage);
  78. return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  79. + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  80. #else /* ! HAVE_GETRUSAGE */
  81. #ifdef HAVE_TIMES
  82. struct tms tms;
  83. times (&tms);
  84. return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
  85. #else /* ! HAVE_TIMES */
  86. /* Fall back on clock and hope it's correctly implemented. */
  87. const long clocks_per_sec = CLOCKS_PER_SEC;
  88. if (clocks_per_sec <= 1000000)
  89. return clock () * (1000000 / clocks_per_sec);
  90. else
  91. return clock () / clocks_per_sec;
  92. #endif /* HAVE_TIMES */
  93. #endif /* HAVE_GETRUSAGE */
  94. }