]> sourceware.org Git - glibc.git/blob - sysdeps/unix/clock_gettime.c
7a3db297442b3c8244855d91c20e3bcbee57507c
[glibc.git] / sysdeps / unix / clock_gettime.c
1 /* Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19 #include <errno.h>
20 #include <stdint.h>
21 #include <time.h>
22 #include <sys/time.h>
23 #include <libc-internal.h>
24 #include <ldsodefs.h>
25
26
27 #if HP_TIMING_AVAIL
28 /* Clock frequency of the processor. We make it a 64-bit variable
29 because some jokers are already playing with processors with more
30 than 4GHz. */
31 static hp_timing_t freq;
32
33
34 /* This function is defined in the thread library. */
35 extern int __pthread_clock_gettime (clockid_t clock_id, hp_timing_t freq,
36 struct timespec *tp)
37 __attribute__ ((__weak__));
38 #endif
39
40
41 /* Get current value of CLOCK and store it in TP. */
42 int
43 clock_gettime (clockid_t clock_id, struct timespec *tp)
44 {
45 int retval = -1;
46
47 switch (clock_id)
48 {
49 #define HANDLE_REALTIME \
50 do { \
51 struct timeval tv; \
52 retval = gettimeofday (&tv, NULL); \
53 if (retval == 0) \
54 /* Convert into `timespec'. */ \
55 TIMEVAL_TO_TIMESPEC (&tv, tp); \
56 } while (0)
57
58 #ifdef SYSDEP_GETTIME
59 SYSDEP_GETTIME;
60 #endif
61
62 #ifndef HANDLED_REALTIME
63 case CLOCK_REALTIME:
64 HANDLE_REALTIME;
65 break;
66 #endif
67
68 default:
69 #if HP_TIMING_AVAIL
70 if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
71 != CLOCK_THREAD_CPUTIME_ID)
72 #endif
73 {
74 __set_errno (EINVAL);
75 break;
76 }
77
78 #if HP_TIMING_AVAIL
79 /* FALLTHROUGH. */
80 case CLOCK_PROCESS_CPUTIME_ID:
81 {
82 hp_timing_t tsc;
83
84 if (__builtin_expect (freq == 0, 0))
85 {
86 /* This can only happen if we haven't initialized the `freq'
87 variable yet. Do this now. We don't have to protect this
88 code against multiple execution since all of them should
89 lead to the same result. */
90 freq = __get_clockfreq ();
91 if (__builtin_expect (freq == 0, 0))
92 /* Something went wrong. */
93 break;
94 }
95
96 if (clock_id != CLOCK_PROCESS_CPUTIME_ID
97 && __pthread_clock_gettime != NULL)
98 {
99 retval = __pthread_clock_gettime (clock_id, freq, tp);
100 break;
101 }
102
103 /* Get the current counter. */
104 HP_TIMING_NOW (tsc);
105
106 /* Compute the offset since the start time of the process. */
107 tsc -= GL(dl_cpuclock_offset);
108
109 /* Compute the seconds. */
110 tp->tv_sec = tsc / freq;
111
112 /* And the nanoseconds. This computation should be stable until
113 we get machines with about 16GHz frequency. */
114 tp->tv_nsec = ((tsc % freq) * UINT64_C (1000000000)) / freq;
115
116 retval = 0;
117 }
118 break;
119 #endif
120 }
121
122 return retval;
123 }
This page took 0.05011 seconds and 4 git commands to generate.