clock_gettime.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <time.h>
  16. #include <mach.h>
  17. #include <assert.h>
  18. #include <shlib-compat.h>
  19. #include <mach/mig_errors.h>
  20. /* Get the current time of day, putting it into *TS.
  21. Returns 0 on success, -1 on errors. */
  22. int
  23. __clock_gettime (clockid_t clock_id, struct timespec *ts)
  24. {
  25. mach_msg_type_number_t count;
  26. error_t err;
  27. switch (clock_id) {
  28. case CLOCK_MONOTONIC:
  29. /* If HAVE_HOST_GET_UPTIME64 is not defined or not available,
  30. CLOCK_MONOTONIC will be equivalent to CLOCK_REALTIME. */
  31. #ifdef HAVE_HOST_GET_UPTIME64
  32. {
  33. time_value64_t tv;
  34. err = __host_get_uptime64 (__mach_host_self (), &tv);
  35. if (err != MIG_BAD_ID)
  36. {
  37. if (err)
  38. {
  39. __set_errno (err);
  40. return -1;
  41. }
  42. TIME_VALUE64_TO_TIMESPEC (&tv, ts);
  43. return 0;
  44. }
  45. }
  46. #endif
  47. /* FALLTHROUGH */
  48. case CLOCK_REALTIME:
  49. {
  50. #ifdef HAVE_HOST_GET_TIME64
  51. time_value64_t tv_64;
  52. err = __host_get_time64 (__mach_host_self (), &tv_64);
  53. /* If err is MIG_BAD_ID, it means an old gnumach which does not
  54. support __host_get_time64 is running against the new gnumach
  55. headers which has the signature of __host_get_time64. In that
  56. case, we fall back to __host_get_time. */
  57. if (err != MIG_BAD_ID)
  58. {
  59. if (err)
  60. {
  61. __set_errno (err);
  62. return -1;
  63. }
  64. TIME_VALUE64_TO_TIMESPEC (&tv_64, ts);
  65. return 0;
  66. }
  67. #endif
  68. /* __host_get_time can only fail if passed an invalid host_t.
  69. __mach_host_self could theoretically fail (producing an
  70. invalid host_t) due to resource exhaustion, but we assume
  71. this will never happen. */
  72. time_value_t tv;
  73. __host_get_time (__mach_host_self (), &tv);
  74. TIME_VALUE_TO_TIMESPEC (&tv, ts);
  75. return 0;
  76. }
  77. case CLOCK_PROCESS_CPUTIME_ID:
  78. {
  79. struct time_value t = { .seconds = 0, .microseconds = 0 };
  80. struct task_basic_info bi;
  81. struct task_thread_times_info tti;
  82. /* Dead threads CPU time. */
  83. count = TASK_BASIC_INFO_COUNT;
  84. err = __task_info (__mach_task_self (), TASK_BASIC_INFO,
  85. (task_info_t) &bi, &count);
  86. if (err)
  87. {
  88. __set_errno(err);
  89. return -1;
  90. }
  91. time_value_add (&t, &bi.user_time);
  92. time_value_add (&t, &bi.system_time);
  93. /* Live threads CPU time. */
  94. count = TASK_THREAD_TIMES_INFO_COUNT;
  95. err = __task_info (__mach_task_self (), TASK_THREAD_TIMES_INFO,
  96. (task_info_t) &tti, &count);
  97. if (err)
  98. {
  99. __set_errno(err);
  100. return -1;
  101. }
  102. time_value_add (&t, &tti.user_time);
  103. time_value_add (&t, &tti.system_time);
  104. TIME_VALUE_TO_TIMESPEC(&t, ts);
  105. return 0;
  106. }
  107. case CLOCK_THREAD_CPUTIME_ID:
  108. {
  109. struct thread_basic_info bi;
  110. mach_port_t self = __mach_thread_self ();
  111. count = THREAD_BASIC_INFO_COUNT;
  112. err = __thread_info (self, THREAD_BASIC_INFO,
  113. (thread_info_t) &bi, &count);
  114. __mach_port_deallocate (__mach_task_self (), self);
  115. if (err)
  116. {
  117. __set_errno(err);
  118. return -1;
  119. }
  120. time_value_add (&bi.user_time, &bi.system_time);
  121. TIME_VALUE_TO_TIMESPEC(&bi.user_time, ts);
  122. return 0;
  123. }
  124. }
  125. errno = EINVAL;
  126. return -1;
  127. }
  128. libc_hidden_def (__clock_gettime)
  129. versioned_symbol (libc, __clock_gettime, clock_gettime, GLIBC_2_17);
  130. /* clock_gettime moved to libc in version 2.17;
  131. old binaries may expect the symbol version it had in librt. */
  132. #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17)
  133. strong_alias (__clock_gettime, __clock_gettime_2);
  134. compat_symbol (libc, __clock_gettime_2, clock_gettime, GLIBC_2_2);
  135. #endif
  136. #if __TIMESIZE != 64
  137. int
  138. __clock_gettime64 (clockid_t clock_id, struct __timespec64 *ts64)
  139. {
  140. struct timespec ts;
  141. int ret;
  142. ret = __clock_gettime (clock_id, &ts);
  143. if (ret == 0)
  144. *ts64 = valid_timespec_to_timespec64 (ts);
  145. return ret;
  146. }
  147. libc_hidden_def (__clock_gettime64)
  148. #endif