clock_getcpuclockid.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* clock_getcpuclockid -- Get a clockid_t for process CPU time. Linux version.
  2. Copyright (C) 2004, 2005 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 <time.h>
  17. #include <sysdep.h>
  18. #include <unistd.h>
  19. #include <bits/kernel-features.h>
  20. #include "kernel-posix-cpu-timers.h"
  21. #ifndef HAS_CPUCLOCK
  22. # define HAS_CPUCLOCK 1
  23. #endif
  24. int
  25. clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
  26. {
  27. #if defined(__NR_clock_getres) || defined(__NR_clock_getres_time64)
  28. /* The clockid_t value is a simple computation from the PID.
  29. But we do a clock_getres call to validate it. */
  30. const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED);
  31. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  32. extern int __libc_missing_posix_cpu_timers attribute_hidden;
  33. # if !(__ASSUME_POSIX_TIMERS > 0)
  34. extern int __libc_missing_posix_timers attribute_hidden;
  35. if (__libc_missing_posix_timers && !__libc_missing_posix_cpu_timers)
  36. __libc_missing_posix_cpu_timers = 1;
  37. # endif
  38. if (!__libc_missing_posix_cpu_timers)
  39. # endif
  40. {
  41. INTERNAL_SYSCALL_DECL (err);
  42. # if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_getres_time64)
  43. int r = INTERNAL_SYSCALL (clock_getres_time64, err, 2, pidclock, NULL);
  44. # else
  45. int r = INTERNAL_SYSCALL (clock_getres, err, 2, pidclock, NULL);
  46. # endif
  47. if (!INTERNAL_SYSCALL_ERROR_P (r, err))
  48. {
  49. *clock_id = pidclock;
  50. return 0;
  51. }
  52. # if !(__ASSUME_POSIX_TIMERS > 0)
  53. if (INTERNAL_SYSCALL_ERRNO (r, err) == ENOSYS)
  54. {
  55. /* The kernel doesn't support these calls at all. */
  56. __libc_missing_posix_timers = 1;
  57. __libc_missing_posix_cpu_timers = 1;
  58. }
  59. else
  60. # endif
  61. if (INTERNAL_SYSCALL_ERRNO (r, err) == EINVAL)
  62. {
  63. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  64. # if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_getres_time64)
  65. if (pidclock == MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
  66. || INTERNAL_SYSCALL_ERROR_P (INTERNAL_SYSCALL
  67. (clock_getres_time64, err, 2,
  68. MAKE_PROCESS_CPUCLOCK
  69. (0, CPUCLOCK_SCHED), NULL),
  70. err))
  71. # else
  72. if (pidclock == MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
  73. || INTERNAL_SYSCALL_ERROR_P (INTERNAL_SYSCALL
  74. (clock_getres, err, 2,
  75. MAKE_PROCESS_CPUCLOCK
  76. (0, CPUCLOCK_SCHED), NULL),
  77. err))
  78. # endif
  79. /* The kernel doesn't support these clocks at all. */
  80. __libc_missing_posix_cpu_timers = 1;
  81. else
  82. # endif
  83. /* The clock_getres system call checked the PID for us. */
  84. return ESRCH;
  85. }
  86. else
  87. return INTERNAL_SYSCALL_ERRNO (r, err);
  88. }
  89. #endif
  90. /* We don't allow any process ID but our own. */
  91. if (pid != 0 && pid != getpid ())
  92. return EPERM;
  93. #ifdef CLOCK_PROCESS_CPUTIME_ID
  94. if (HAS_CPUCLOCK)
  95. {
  96. /* Store the number. */
  97. *clock_id = CLOCK_PROCESS_CPUTIME_ID;
  98. return 0;
  99. }
  100. #endif
  101. /* We don't have a timer for that. */
  102. return ENOENT;
  103. }