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