clock_getcpuclockid.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifdef __NR_clock_getres
  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. int r = INTERNAL_SYSCALL (clock_getres, err, 2, pidclock, NULL);
  43. if (!INTERNAL_SYSCALL_ERROR_P (r, err))
  44. {
  45. *clock_id = pidclock;
  46. return 0;
  47. }
  48. # if !(__ASSUME_POSIX_TIMERS > 0)
  49. if (INTERNAL_SYSCALL_ERRNO (r, err) == ENOSYS)
  50. {
  51. /* The kernel doesn't support these calls at all. */
  52. __libc_missing_posix_timers = 1;
  53. __libc_missing_posix_cpu_timers = 1;
  54. }
  55. else
  56. # endif
  57. if (INTERNAL_SYSCALL_ERRNO (r, err) == EINVAL)
  58. {
  59. # if !(__ASSUME_POSIX_CPU_TIMERS > 0)
  60. if (pidclock == MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
  61. || INTERNAL_SYSCALL_ERROR_P (INTERNAL_SYSCALL
  62. (clock_getres, err, 2,
  63. MAKE_PROCESS_CPUCLOCK
  64. (0, CPUCLOCK_SCHED), NULL),
  65. err))
  66. /* The kernel doesn't support these clocks at all. */
  67. __libc_missing_posix_cpu_timers = 1;
  68. else
  69. # endif
  70. /* The clock_getres system call checked the PID for us. */
  71. return ESRCH;
  72. }
  73. else
  74. return INTERNAL_SYSCALL_ERRNO (r, err);
  75. }
  76. #endif
  77. /* We don't allow any process ID but our own. */
  78. if (pid != 0 && pid != getpid ())
  79. return EPERM;
  80. #ifdef CLOCK_PROCESS_CPUTIME_ID
  81. if (HAS_CPUCLOCK)
  82. {
  83. /* Store the number. */
  84. *clock_id = CLOCK_PROCESS_CPUTIME_ID;
  85. return 0;
  86. }
  87. #endif
  88. /* We don't have a timer for that. */
  89. return ENOENT;
  90. }