1*38fd1498Szrj /* Copyright (C) 2005-2018 Free Software Foundation, Inc.
2*38fd1498Szrj    Contributed by Richard Henderson <rth@redhat.com>.
3*38fd1498Szrj 
4*38fd1498Szrj    This file is part of the GNU Offloading and Multi Processing Library
5*38fd1498Szrj    (libgomp).
6*38fd1498Szrj 
7*38fd1498Szrj    Libgomp is free software; you can redistribute it and/or modify it
8*38fd1498Szrj    under the terms of the GNU General Public License as published by
9*38fd1498Szrj    the Free Software Foundation; either version 3, or (at your option)
10*38fd1498Szrj    any later version.
11*38fd1498Szrj 
12*38fd1498Szrj    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14*38fd1498Szrj    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15*38fd1498Szrj    more details.
16*38fd1498Szrj 
17*38fd1498Szrj    Under Section 7 of GPL version 3, you are granted additional
18*38fd1498Szrj    permissions described in the GCC Runtime Library Exception, version
19*38fd1498Szrj    3.1, as published by the Free Software Foundation.
20*38fd1498Szrj 
21*38fd1498Szrj    You should have received a copy of the GNU General Public License and
22*38fd1498Szrj    a copy of the GCC Runtime Library Exception along with this program;
23*38fd1498Szrj    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24*38fd1498Szrj    <http://www.gnu.org/licenses/>.  */
25*38fd1498Szrj 
26*38fd1498Szrj /* This file contains system specific timer routines.  It is expected that
27*38fd1498Szrj    a system may well want to write special versions of each of these.
28*38fd1498Szrj 
29*38fd1498Szrj    The following implementation uses the most simple POSIX routines.
30*38fd1498Szrj    If present, POSIX 4 clocks should be used instead.  */
31*38fd1498Szrj 
32*38fd1498Szrj #include "libgomp.h"
33*38fd1498Szrj #include <unistd.h>
34*38fd1498Szrj #if TIME_WITH_SYS_TIME
35*38fd1498Szrj # include <sys/time.h>
36*38fd1498Szrj # include <time.h>
37*38fd1498Szrj #else
38*38fd1498Szrj # if HAVE_SYS_TIME_H
39*38fd1498Szrj #  include <sys/time.h>
40*38fd1498Szrj # else
41*38fd1498Szrj #  include <time.h>
42*38fd1498Szrj # endif
43*38fd1498Szrj #endif
44*38fd1498Szrj 
45*38fd1498Szrj 
46*38fd1498Szrj double
omp_get_wtime(void)47*38fd1498Szrj omp_get_wtime (void)
48*38fd1498Szrj {
49*38fd1498Szrj #ifdef HAVE_CLOCK_GETTIME
50*38fd1498Szrj   struct timespec ts;
51*38fd1498Szrj # ifdef CLOCK_MONOTONIC
52*38fd1498Szrj   if (clock_gettime (CLOCK_MONOTONIC, &ts) < 0)
53*38fd1498Szrj # endif
54*38fd1498Szrj     clock_gettime (CLOCK_REALTIME, &ts);
55*38fd1498Szrj   return ts.tv_sec + ts.tv_nsec / 1e9;
56*38fd1498Szrj #else
57*38fd1498Szrj   struct timeval tv;
58*38fd1498Szrj   gettimeofday (&tv, NULL);
59*38fd1498Szrj   return tv.tv_sec + tv.tv_usec / 1e6;
60*38fd1498Szrj #endif
61*38fd1498Szrj }
62*38fd1498Szrj 
63*38fd1498Szrj double
omp_get_wtick(void)64*38fd1498Szrj omp_get_wtick (void)
65*38fd1498Szrj {
66*38fd1498Szrj #ifdef HAVE_CLOCK_GETTIME
67*38fd1498Szrj   struct timespec ts;
68*38fd1498Szrj # ifdef CLOCK_MONOTONIC
69*38fd1498Szrj   if (clock_getres (CLOCK_MONOTONIC, &ts) < 0)
70*38fd1498Szrj # endif
71*38fd1498Szrj     clock_getres (CLOCK_REALTIME, &ts);
72*38fd1498Szrj   return ts.tv_sec + ts.tv_nsec / 1e9;
73*38fd1498Szrj #else
74*38fd1498Szrj   return 1.0 / sysconf(_SC_CLK_TCK);
75*38fd1498Szrj #endif
76*38fd1498Szrj }
77*38fd1498Szrj 
78*38fd1498Szrj ialias (omp_get_wtime)
79*38fd1498Szrj ialias (omp_get_wtick)
80