1 | /* clock_getres -- Get the resolution of a POSIX clockid_t. |
2 | Copyright (C) 1999-2019 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <stdint.h> |
21 | #include <time.h> |
22 | #include <unistd.h> |
23 | #include <sys/param.h> |
24 | #include <libc-internal.h> |
25 | |
26 | |
27 | #if HP_TIMING_AVAIL |
28 | static long int nsec; /* Clock frequency of the processor. */ |
29 | |
30 | static int |
31 | hp_timing_getres (struct timespec *res) |
32 | { |
33 | if (__glibc_unlikely (nsec == 0)) |
34 | { |
35 | hp_timing_t freq; |
36 | |
37 | /* This can only happen if we haven't initialized the `nsec' |
38 | variable yet. Do this now. We don't have to protect this |
39 | code against multiple execution since all of them should |
40 | lead to the same result. */ |
41 | freq = __get_clockfreq (); |
42 | if (__glibc_unlikely (freq == 0)) |
43 | /* Something went wrong. */ |
44 | return -1; |
45 | |
46 | nsec = MAX (UINT64_C (1000000000) / freq, 1); |
47 | } |
48 | |
49 | /* Fill in the values. |
50 | The seconds are always zero (unless we have a 1Hz machine). */ |
51 | res->tv_sec = 0; |
52 | res->tv_nsec = nsec; |
53 | |
54 | return 0; |
55 | } |
56 | #endif |
57 | |
58 | static inline int |
59 | realtime_getres (struct timespec *res) |
60 | { |
61 | long int clk_tck = __sysconf (_SC_CLK_TCK); |
62 | |
63 | if (__glibc_likely (clk_tck != -1)) |
64 | { |
65 | /* This implementation assumes that the realtime clock has a |
66 | resolution higher than 1 second. This is the case for any |
67 | reasonable implementation. */ |
68 | res->tv_sec = 0; |
69 | res->tv_nsec = 1000000000 / clk_tck; |
70 | return 0; |
71 | } |
72 | |
73 | return -1; |
74 | } |
75 | |
76 | |
77 | /* Get resolution of clock. */ |
78 | int |
79 | __clock_getres (clockid_t clock_id, struct timespec *res) |
80 | { |
81 | int retval = -1; |
82 | |
83 | switch (clock_id) |
84 | { |
85 | #ifdef SYSDEP_GETRES |
86 | SYSDEP_GETRES; |
87 | #endif |
88 | |
89 | #ifndef HANDLED_REALTIME |
90 | case CLOCK_REALTIME: |
91 | retval = realtime_getres (res); |
92 | break; |
93 | #endif /* handled REALTIME */ |
94 | |
95 | default: |
96 | #ifdef SYSDEP_GETRES_CPU |
97 | SYSDEP_GETRES_CPU; |
98 | #endif |
99 | #if HP_TIMING_AVAIL |
100 | if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1)) |
101 | == CLOCK_THREAD_CPUTIME_ID) |
102 | retval = hp_timing_getres (res); |
103 | else |
104 | #endif |
105 | __set_errno (EINVAL); |
106 | break; |
107 | |
108 | #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME |
109 | case CLOCK_PROCESS_CPUTIME_ID: |
110 | case CLOCK_THREAD_CPUTIME_ID: |
111 | retval = hp_timing_getres (res); |
112 | break; |
113 | #endif |
114 | } |
115 | |
116 | return retval; |
117 | } |
118 | weak_alias (__clock_getres, clock_getres) |
119 | |