Merge trunk version 207649 into gupc branch.
[official-gcc.git] / libgupc / smp / upc_tick.c
blob15064e111e17590172d929c34aafc4350ae01dc0
1 /* Copyright (C) 2012-2014 Free Software Foundation, Inc.
2 This file is part of the UPC runtime Library.
3 Written by Gary Funck <gary@intrepid.com>
4 and Nenad Vukicevic <nenad@intrepid.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "upc_config.h"
28 #include "upc_sysdep.h"
29 #include "upc_defs.h"
30 #include "upc_lib.h"
32 #if HAVE_CLOCK_GETTIME
33 #ifdef CLOCK_MONOTONIC_RAW
34 /* System clock id passed to clock_gettime. CLOCK_MONOTONIC_RAW
35 is preferred. It has been available in the Linux kernel
36 since version 2.6.28 */
37 #define SYS_RT_CLOCK_ID CLOCK_MONOTONIC_RAW
38 #else
39 #define SYS_RT_CLOCK_ID CLOCK_MONOTONIC
40 #endif
42 upc_tick_t
43 upc_ticks_now (void)
45 struct timespec ts;
46 upc_tick_t t;
47 if (clock_gettime (SYS_RT_CLOCK_ID, &ts) != 0)
49 perror ("clock_gettime");
50 abort ();
52 t = (upc_tick_t) ts.tv_sec * 1000000000LL + (upc_tick_t) ts.tv_nsec;
53 return t;
56 #else /* !HAVE_CLOCK_GETTIME */
58 upc_tick_t
59 upc_ticks_now (void)
61 struct timeval tv;
62 upc_tick_t t;
63 if (gettimeofday (&tv, NULL) != 0)
65 perror ("gettimeofday");
66 abort ();
68 t = (upc_tick_t) tv.tv_sec * 1000000000LL + (upc_tick_t) tv.tv_usec * 1000;
69 return t;
72 #endif
74 uint64_t
75 upc_ticks_to_ns (upc_tick_t ticks)
77 return ticks;