Remove *xattr syscalls.
[glibc.git] / sysdeps / unix / clock_gettime.c
blob5dc329e1c0409f413a311232da439af40140db7a
1 /* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 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.
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.
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. */
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>
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;
34 /* This function is defined in the thread library. */
35 extern int __pthread_clock_gettime (hp_timing_t freq, struct timespec *tp)
36 __attribute__ ((__weak__));
37 #endif
40 /* Get current value of CLOCK and store it in TP. */
41 int
42 clock_gettime (clockid_t clock_id, struct timespec *tp)
44 struct timeval tv;
45 int retval = -1;
47 switch (clock_id)
49 case CLOCK_REALTIME:
50 retval = gettimeofday (&tv, NULL);
51 if (retval == 0)
52 /* Convert into `timespec'. */
53 TIMEVAL_TO_TIMESPEC (&tv, tp);
54 break;
56 #if HP_TIMING_AVAIL
57 case CLOCK_PROCESS_CPUTIME_ID:
58 case CLOCK_THREAD_CPUTIME_ID:
60 hp_timing_t tsc;
62 if (__builtin_expect (freq == 0, 0))
64 /* This can only happen if we haven't initialized the `freq'
65 variable yet. Do this now. We don't have to protect this
66 code against multiple execution since all of them should
67 lead to the same result. */
68 freq = __get_clockfreq ();
69 if (__builtin_expect (freq == 0, 0))
70 /* Something went wrong. */
71 break;
74 if (clock_id == CLOCK_THREAD_CPUTIME_ID
75 && __pthread_clock_gettime != NULL)
77 retval = __pthread_clock_gettime (freq, tp);
78 break;
81 /* Get the current counter. */
82 HP_TIMING_NOW (tsc);
84 /* Compute the offset since the start time of the process. */
85 tsc -= GL(dl_cpuclock_offset);
87 /* Compute the seconds. */
88 tp->tv_sec = tsc / freq;
90 /* And the nanoseconds. This computation should be stable until
91 we get machines with about 16GHz frequency. */
92 tp->tv_nsec = ((tsc % freq) * UINT64_C (1000000000)) / freq;
94 retval = 0;
96 break;
97 #endif
99 default:
100 __set_errno (EINVAL);
101 break;
104 return retval;