Skip to content

Commit 5a44018

Browse files
committed
opal/timer: add code to check if rtdtsc is core invariant
Newer x86 processors have a core invariant tsc. On these systems it is safe to use the rtdtsc instruction as a monotonic timer. This commit adds a new function to the opal timer code to check if the timer backend is monotonic. On x86 it checks the appropriate bit and on other architectures it parrots back the OPAL_TIMER_MONOTONIC value. (cherry picked from commit a718743) Signed-off-by: Nathan Hjelm <[email protected]>
1 parent 16c0ba1 commit 5a44018

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
@@ -51,7 +54,24 @@ opal_sys_timer_get_cycles(void)
5154

5255
#endif
5356

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

5676
#else
5777

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
@@ -110,6 +113,17 @@ typedef long opal_timer_t;
110113
#endif
111114
#endif
112115

116+
#ifndef OPAL_HAVE_SYS_TIMER_IS_MONOTONIC
117+
118+
#define OPAL_HAVE_SYS_TIMER_IS_MONOTONIC 1
119+
120+
static inline bool opal_sys_timer_is_monotonic (void)
121+
{
122+
return OPAL_TIMER_MONOTONIC;
123+
}
124+
125+
#endif
126+
113127
END_C_DECLS
114128

115129
#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$
@@ -155,8 +155,8 @@ int opal_timer_linux_open(void)
155155
{
156156
int ret = OPAL_SUCCESS;
157157

158-
if(mca_timer_base_monotonic) {
159-
#if OPAL_HAVE_CLOCK_GETTIME
158+
if (mca_timer_base_monotonic && !opal_sys_timer_is_monotonic ()) {
159+
#if OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC)
160160
struct timespec res;
161161
if( 0 == clock_getres(CLOCK_MONOTONIC, &res)) {
162162
opal_timer_linux_freq = 1.e9;
@@ -165,11 +165,9 @@ int opal_timer_linux_open(void)
165165
return ret;
166166
}
167167
#else
168-
#if (0 == OPAL_TIMER_MONOTONIC)
169168
/* Monotonic time requested but cannot be found. Complain! */
170-
opal_show_help("help-opal-timer-linux.txt", "monotonic not supported", 1);
171-
#endif /* (0 == OPAL_TIMER_MONOTONIC) */
172-
#endif
169+
opal_show_help("help-opal-timer-linux.txt", "monotonic not supported", true);
170+
#endif /* OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC) */
173171
}
174172
ret = opal_timer_linux_find_freq();
175173
opal_timer_base_get_cycles = opal_timer_base_get_cycles_sys_timer;
@@ -180,22 +178,20 @@ int opal_timer_linux_open(void)
180178
#if OPAL_HAVE_CLOCK_GETTIME
181179
opal_timer_t opal_timer_base_get_usec_clock_gettime(void)
182180
{
183-
struct timespec tp;
181+
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
184182

185-
if( 0 == clock_gettime(CLOCK_MONOTONIC, &tp) ) {
186-
return (tp.tv_sec * 1e6 + tp.tv_nsec/1000);
187-
}
188-
return 0;
183+
(void) clock_gettime (CLOCK_MONOTONIC, &tp);
184+
185+
return (tp.tv_sec * 1e6 + tp.tv_nsec/1000);
189186
}
190187

191188
opal_timer_t opal_timer_base_get_cycles_clock_gettime(void)
192189
{
193-
struct timespec tp;
190+
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
194191

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

0 commit comments

Comments
 (0)