clock.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* ANSI-compatible clock function.
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. This file is part of the libiberty library. This library is free
  4. software; you can redistribute it and/or modify it under the
  5. terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This library 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU CC; see the file COPYING. If not, write to
  14. the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  15. As a special exception, if you link this library with files
  16. compiled with a GNU compiler to produce an executable, this does not cause
  17. the resulting executable to be covered by the GNU General Public License.
  18. This exception does not however invalidate any other reasons why
  19. the executable file might be covered by the GNU General Public License. */
  20. /*
  21. @deftypefn Supplemental long clock (void)
  22. Returns an approximation of the CPU time used by the process as a
  23. @code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
  24. number of seconds used.
  25. @end deftypefn
  26. */
  27. #include "config.h"
  28. #ifdef HAVE_GETRUSAGE
  29. #include <sys/time.h>
  30. #include <sys/resource.h>
  31. #endif
  32. #ifdef HAVE_TIMES
  33. #ifdef HAVE_SYS_PARAM_H
  34. #include <sys/param.h>
  35. #endif
  36. #include <sys/times.h>
  37. #endif
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #ifdef _SC_CLK_TCK
  42. #define GNU_HZ sysconf(_SC_CLK_TCK)
  43. #else
  44. #ifdef HZ
  45. #define GNU_HZ HZ
  46. #else
  47. #ifdef CLOCKS_PER_SEC
  48. #define GNU_HZ CLOCKS_PER_SEC
  49. #endif
  50. #endif
  51. #endif
  52. /* FIXME: should be able to declare as clock_t. */
  53. long
  54. clock (void)
  55. {
  56. #ifdef HAVE_GETRUSAGE
  57. struct rusage rusage;
  58. getrusage (0, &rusage);
  59. return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  60. + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  61. #else
  62. #ifdef HAVE_TIMES
  63. struct tms tms;
  64. times (&tms);
  65. return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
  66. #else
  67. #ifdef VMS
  68. struct
  69. {
  70. int proc_user_time;
  71. int proc_system_time;
  72. int child_user_time;
  73. int child_system_time;
  74. } vms_times;
  75. times (&vms_times);
  76. return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
  77. #else
  78. /* A fallback, if nothing else available. */
  79. return 0;
  80. #endif /* VMS */
  81. #endif /* HAVE_TIMES */
  82. #endif /* HAVE_GETRUSAGE */
  83. }