clock_gettime.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* clock_gettime -- Get the current time from a POSIX clockid_t. Unix version.
  2. Copyright (C) 1999-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stdint.h>
  17. #include <time.h>
  18. #include <sys/time.h>
  19. #include <libc-internal.h>
  20. #include <ldsodefs.h>
  21. #if HP_TIMING_AVAIL
  22. /* Clock frequency of the processor. We make it a 64-bit variable
  23. because some jokers are already playing with processors with more
  24. than 4GHz. */
  25. static hp_timing_t freq;
  26. /* This function is defined in the thread library. */
  27. extern int __pthread_clock_gettime (clockid_t clock_id, hp_timing_t freq,
  28. struct timespec *tp)
  29. __attribute__ ((__weak__));
  30. static int
  31. hp_timing_gettime (clockid_t clock_id, struct timespec *tp)
  32. {
  33. hp_timing_t tsc;
  34. if (__glibc_unlikely (freq == 0))
  35. {
  36. /* This can only happen if we haven't initialized the `freq'
  37. variable yet. Do this now. We don't have to protect this
  38. code against multiple execution since all of them should
  39. lead to the same result. */
  40. freq = __get_clockfreq ();
  41. if (__glibc_unlikely (freq == 0))
  42. /* Something went wrong. */
  43. return -1;
  44. }
  45. if (clock_id != CLOCK_PROCESS_CPUTIME_ID
  46. && __pthread_clock_gettime != NULL)
  47. return __pthread_clock_gettime (clock_id, freq, tp);
  48. /* Get the current counter. */
  49. HP_TIMING_NOW (tsc);
  50. /* Compute the offset since the start time of the process. */
  51. tsc -= GL(dl_cpuclock_offset);
  52. /* Compute the seconds. */
  53. tp->tv_sec = tsc / freq;
  54. /* And the nanoseconds. This computation should be stable until
  55. we get machines with about 16GHz frequency. */
  56. tp->tv_nsec = ((tsc % freq) * UINT64_C (1000000000)) / freq;
  57. return 0;
  58. }
  59. #endif
  60. static inline int
  61. realtime_gettime (struct timespec *tp)
  62. {
  63. struct timeval tv;
  64. int retval = __gettimeofday (&tv, NULL);
  65. if (retval == 0)
  66. /* Convert into `timespec'. */
  67. TIMEVAL_TO_TIMESPEC (&tv, tp);
  68. return retval;
  69. }
  70. /* Get current value of CLOCK and store it in TP. */
  71. int
  72. __clock_gettime (clockid_t clock_id, struct timespec *tp)
  73. {
  74. int retval = -1;
  75. switch (clock_id)
  76. {
  77. #ifdef SYSDEP_GETTIME
  78. SYSDEP_GETTIME;
  79. #endif
  80. #ifndef HANDLED_REALTIME
  81. case CLOCK_REALTIME:
  82. {
  83. struct timeval tv;
  84. retval = __gettimeofday (&tv, NULL);
  85. if (retval == 0)
  86. TIMEVAL_TO_TIMESPEC (&tv, tp);
  87. }
  88. break;
  89. #endif
  90. default:
  91. #ifdef SYSDEP_GETTIME_CPU
  92. SYSDEP_GETTIME_CPU (clock_id, tp);
  93. #endif
  94. #if HP_TIMING_AVAIL
  95. if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
  96. == CLOCK_THREAD_CPUTIME_ID)
  97. retval = hp_timing_gettime (clock_id, tp);
  98. else
  99. #endif
  100. __set_errno (EINVAL);
  101. break;
  102. #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
  103. case CLOCK_PROCESS_CPUTIME_ID:
  104. retval = hp_timing_gettime (clock_id, tp);
  105. break;
  106. #endif
  107. }
  108. return retval;
  109. }
  110. weak_alias (__clock_gettime, clock_gettime)
  111. libc_hidden_def (__clock_gettime)