]>
Commit | Line | Data |
---|---|---|
2f4f3bd4 | 1 | /* clock_getres -- Get the resolution of a POSIX clockid_t. |
04277e02 | 2 | Copyright (C) 1999-2019 Free Software Foundation, Inc. |
13fa3676 UD |
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 | |
41bdb6e2 AJ |
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. | |
13fa3676 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
13fa3676 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
13fa3676 UD |
18 | |
19 | #include <errno.h> | |
3b5c1b57 | 20 | #include <stdint.h> |
8be918b7 | 21 | #include <time.h> |
13fa3676 | 22 | #include <unistd.h> |
3b5c1b57 UD |
23 | #include <sys/param.h> |
24 | #include <libc-internal.h> | |
13fa3676 UD |
25 | |
26 | ||
2f4f3bd4 RM |
27 | #if HP_TIMING_AVAIL |
28 | static long int nsec; /* Clock frequency of the processor. */ | |
29 | ||
3501542f | 30 | static int |
2f4f3bd4 RM |
31 | hp_timing_getres (struct timespec *res) |
32 | { | |
a1ffb40e | 33 | if (__glibc_unlikely (nsec == 0)) |
2f4f3bd4 RM |
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 (); | |
a1ffb40e | 42 | if (__glibc_unlikely (freq == 0)) |
2f4f3bd4 RM |
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 | } | |
13fa3676 UD |
56 | #endif |
57 | ||
2f4f3bd4 RM |
58 | static inline int |
59 | realtime_getres (struct timespec *res) | |
60 | { | |
dba2bdbe | 61 | long int clk_tck = __sysconf (_SC_CLK_TCK); |
2f4f3bd4 | 62 | |
a1ffb40e | 63 | if (__glibc_likely (clk_tck != -1)) |
2f4f3bd4 RM |
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 | ||
3b5c1b57 | 76 | |
13fa3676 UD |
77 | /* Get resolution of clock. */ |
78 | int | |
89fb6835 | 79 | __clock_getres (clockid_t clock_id, struct timespec *res) |
13fa3676 | 80 | { |
4a199526 | 81 | int retval = -1; |
13fa3676 UD |
82 | |
83 | switch (clock_id) | |
84 | { | |
ad0e8eb0 | 85 | case CLOCK_REALTIME: |
2f4f3bd4 | 86 | retval = realtime_getres (res); |
13fa3676 UD |
87 | break; |
88 | ||
4165d44d UD |
89 | default: |
90 | #if HP_TIMING_AVAIL | |
91 | if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1)) | |
2f4f3bd4 RM |
92 | == CLOCK_THREAD_CPUTIME_ID) |
93 | retval = hp_timing_getres (res); | |
94 | else | |
4165d44d | 95 | #endif |
2f4f3bd4 RM |
96 | __set_errno (EINVAL); |
97 | break; | |
4165d44d | 98 | |
b06f4c00 | 99 | #if HP_TIMING_AVAIL |
3b5c1b57 | 100 | case CLOCK_PROCESS_CPUTIME_ID: |
2f4f3bd4 RM |
101 | case CLOCK_THREAD_CPUTIME_ID: |
102 | retval = hp_timing_getres (res); | |
3b5c1b57 UD |
103 | break; |
104 | #endif | |
13fa3676 UD |
105 | } |
106 | ||
107 | return retval; | |
108 | } | |
89fb6835 | 109 | weak_alias (__clock_getres, clock_getres) |