Skip to content

Commit b393c3b

Browse files
authored
Merge pull request #2611 from hjelmn/v2.0.x_timer
opal/timer: add code to check if rtdtsc is core invariant
2 parents a5ab6eb + 7d3b07d commit b393c3b

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

opal/include/opal/sys/amd64/timer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -9,6 +10,8 @@
910
* University of Stuttgart. All rights reserved.
1011
* Copyright (c) 2004-2005 The Regents of the University of California.
1112
* All rights reserved.
13+
* Copyright (c) 2016 Los Alamos National Security, LLC. ALl rights
14+
* reserved.
1215
* $COPYRIGHT$
1316
*
1417
* Additional copyrights may follow
@@ -56,7 +59,24 @@ opal_sys_timer_get_cycles(void)
5659
return ((opal_timer_t)l) | (((opal_timer_t)h) << 32);
5760
}
5861

62+
static inline bool opal_sys_timer_is_monotonic (void)
63+
{
64+
int32_t cpuid1, cpuid2, tmp;
65+
const int32_t level = 0x80000007;
66+
/* cpuid clobbers ebx but it must be restored for -fPIC so save
67+
* then restore ebx */
68+
__asm__ volatile ("xchgl %%ebx, %2\n"
69+
"cpuid\n"
70+
"xchgl %%ebx, %2\n":
71+
"=a" (cpuid1), "=d" (cpuid2), "=r" (tmp) :
72+
"a" (level) :
73+
"ecx");
74+
/* bit 8 of edx contains the invariant tsc flag */
75+
return !!(cpuid2 & (1 << 8));
76+
}
77+
5978
#define OPAL_HAVE_SYS_TIMER_GET_CYCLES 1
79+
#define OPAL_HAVE_SYS_TIMER_IS_MONOTONIC 1
6080

6181
#else
6282

opal/include/opal/sys/timer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -9,6 +10,8 @@
910
* University of Stuttgart. All rights reserved.
1011
* Copyright (c) 2004-2005 The Regents of the University of California.
1112
* All rights reserved.
13+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
14+
* reserved.
1215
* $COPYRIGHT$
1316
*
1417
* Additional copyrights may follow
@@ -108,6 +111,17 @@ typedef long opal_timer_t;
108111
#endif
109112
#endif
110113

114+
#ifndef OPAL_HAVE_SYS_TIMER_IS_MONOTONIC
115+
116+
#define OPAL_HAVE_SYS_TIMER_IS_MONOTONIC 1
117+
118+
static inline bool opal_sys_timer_is_monotonic (void)
119+
{
120+
return OPAL_TIMER_MONOTONIC;
121+
}
122+
123+
#endif
124+
111125
END_C_DECLS
112126

113127
#endif /* OPAL_SYS_TIMER_H */

opal/mca/timer/linux/timer_linux_component.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* All rights reserved.
1313
* Copyright (c) 2015 Research Organization for Information Science
1414
* and Technology (RIST). All rights reserved.
15-
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights
15+
* Copyright (c) 2015-2016 Los Alamos National Security, LLC. All rights
1616
* reserved.
1717
* Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
1818
* $COPYRIGHT$
@@ -153,8 +153,8 @@ int opal_timer_linux_open(void)
153153
{
154154
int ret = OPAL_SUCCESS;
155155

156-
if(mca_timer_base_monotonic) {
157-
#if OPAL_HAVE_CLOCK_GETTIME
156+
if (mca_timer_base_monotonic && !opal_sys_timer_is_monotonic ()) {
157+
#if OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC)
158158
struct timespec res;
159159
if( 0 == clock_getres(CLOCK_MONOTONIC, &res)) {
160160
opal_timer_linux_freq = 1.e9;
@@ -163,11 +163,9 @@ int opal_timer_linux_open(void)
163163
return ret;
164164
}
165165
#else
166-
#if (0 == OPAL_TIMER_MONOTONIC)
167166
/* Monotonic time requested but cannot be found. Complain! */
168-
opal_show_help("help-opal-timer-linux.txt", "monotonic not supported", 1);
169-
#endif /* (0 == OPAL_TIMER_MONOTONIC) */
170-
#endif
167+
opal_show_help("help-opal-timer-linux.txt", "monotonic not supported", true);
168+
#endif /* OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC) */
171169
}
172170
ret = opal_timer_linux_find_freq();
173171
opal_timer_base_get_cycles = opal_timer_base_get_cycles_sys_timer;
@@ -178,22 +176,20 @@ int opal_timer_linux_open(void)
178176
#if OPAL_HAVE_CLOCK_GETTIME
179177
opal_timer_t opal_timer_base_get_usec_clock_gettime(void)
180178
{
181-
struct timespec tp;
179+
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
182180

183-
if( 0 == clock_gettime(CLOCK_MONOTONIC, &tp) ) {
184-
return (tp.tv_sec * 1e6 + tp.tv_nsec/1000);
185-
}
186-
return 0;
181+
(void) clock_gettime (CLOCK_MONOTONIC, &tp);
182+
183+
return (tp.tv_sec * 1e6 + tp.tv_nsec/1000);
187184
}
188185

189186
opal_timer_t opal_timer_base_get_cycles_clock_gettime(void)
190187
{
191-
struct timespec tp;
188+
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
192189

193-
if( 0 == clock_gettime(CLOCK_MONOTONIC, &tp) ) {
194-
return (tp.tv_sec * 1e9 + tp.tv_nsec);
195-
}
196-
return 0;
190+
(void) clock_gettime(CLOCK_MONOTONIC, &tp);
191+
192+
return (tp.tv_sec * 1e9 + tp.tv_nsec);
197193
}
198194
#endif /* OPAL_HAVE_CLOCK_GETTIME */
199195

0 commit comments

Comments
 (0)