Skip to content

Commit 498da73

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. Signed-off-by: Nathan Hjelm <[email protected]> (cherry picked from commit a718743) Signed-off-by: Nathan Hjelm <[email protected]>
1 parent b7a591e commit 498da73

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
@@ -10,6 +11,8 @@
1011
* Copyright (c) 2004-2005 The Regents of the University of California.
1112
* All rights reserved.
1213
* Copyright (c) 2016 Broadcom Limited. All rights reserved.
14+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
15+
* reserved.
1316
* $COPYRIGHT$
1417
*
1518
* Additional copyrights may follow
@@ -111,6 +114,17 @@ typedef long opal_timer_t;
111114
#endif
112115
#endif
113116

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

116130
#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 (c) 2016 Broadcom Limited. All rights reserved.
@@ -158,8 +158,8 @@ int opal_timer_linux_open(void)
158158
{
159159
int ret = OPAL_SUCCESS;
160160

161-
if(mca_timer_base_monotonic) {
162-
#if OPAL_HAVE_CLOCK_GETTIME
161+
if (mca_timer_base_monotonic && !opal_sys_timer_is_monotonic ()) {
162+
#if OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC)
163163
struct timespec res;
164164
if( 0 == clock_getres(CLOCK_MONOTONIC, &res)) {
165165
opal_timer_linux_freq = 1.e9;
@@ -168,11 +168,9 @@ int opal_timer_linux_open(void)
168168
return ret;
169169
}
170170
#else
171-
#if (0 == OPAL_TIMER_MONOTONIC)
172171
/* Monotonic time requested but cannot be found. Complain! */
173-
opal_show_help("help-opal-timer-linux.txt", "monotonic not supported", 1);
174-
#endif /* (0 == OPAL_TIMER_MONOTONIC) */
175-
#endif
172+
opal_show_help("help-opal-timer-linux.txt", "monotonic not supported", true);
173+
#endif /* OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC) */
176174
}
177175
ret = opal_timer_linux_find_freq();
178176
opal_timer_base_get_cycles = opal_timer_base_get_cycles_sys_timer;
@@ -183,22 +181,20 @@ int opal_timer_linux_open(void)
183181
#if OPAL_HAVE_CLOCK_GETTIME
184182
opal_timer_t opal_timer_base_get_usec_clock_gettime(void)
185183
{
186-
struct timespec tp;
184+
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
187185

188-
if( 0 == clock_gettime(CLOCK_MONOTONIC, &tp) ) {
189-
return (tp.tv_sec * 1e6 + tp.tv_nsec/1000);
190-
}
191-
return 0;
186+
(void) clock_gettime (CLOCK_MONOTONIC, &tp);
187+
188+
return (tp.tv_sec * 1e6 + tp.tv_nsec/1000);
192189
}
193190

194191
opal_timer_t opal_timer_base_get_cycles_clock_gettime(void)
195192
{
196-
struct timespec tp;
193+
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
197194

198-
if( 0 == clock_gettime(CLOCK_MONOTONIC, &tp) ) {
199-
return (tp.tv_sec * 1e9 + tp.tv_nsec);
200-
}
201-
return 0;
195+
(void) clock_gettime(CLOCK_MONOTONIC, &tp);
196+
197+
return (tp.tv_sec * 1e9 + tp.tv_nsec);
202198
}
203199
#endif /* OPAL_HAVE_CLOCK_GETTIME */
204200

0 commit comments

Comments
 (0)