clock_getcpuclockid.c 3.1 KB

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