Skip to content

Commit a718743

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]>
1 parent f9e8a55 commit a718743

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
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: 10 additions & 14 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.
@@ -162,7 +162,7 @@ int opal_timer_linux_open(void)
162162
{
163163
int ret = OPAL_SUCCESS;
164164

165-
if(mca_timer_base_monotonic) {
165+
if (mca_timer_base_monotonic && !opal_sys_timer_is_monotonic ()) {
166166
#if OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC)
167167
struct timespec res;
168168
if( 0 == clock_getres(CLOCK_MONOTONIC, &res)) {
@@ -172,10 +172,8 @@ int opal_timer_linux_open(void)
172172
return ret;
173173
}
174174
#else
175-
#if (0 == OPAL_TIMER_MONOTONIC)
176175
/* Monotonic time requested but cannot be found. Complain! */
177176
opal_show_help("help-opal-timer-linux.txt", "monotonic not supported", true);
178-
#endif /* (0 == OPAL_TIMER_MONOTONIC) */
179177
#endif /* OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC) */
180178
}
181179
ret = opal_timer_linux_find_freq();
@@ -187,22 +185,20 @@ int opal_timer_linux_open(void)
187185
#if OPAL_HAVE_CLOCK_GETTIME
188186
opal_timer_t opal_timer_base_get_usec_clock_gettime(void)
189187
{
190-
struct timespec tp;
188+
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
191189

192-
if( 0 == clock_gettime(CLOCK_MONOTONIC, &tp) ) {
193-
return (tp.tv_sec * 1e6 + tp.tv_nsec/1000);
194-
}
195-
return 0;
190+
(void) clock_gettime (CLOCK_MONOTONIC, &tp);
191+
192+
return (tp.tv_sec * 1e6 + tp.tv_nsec/1000);
196193
}
197194

198195
opal_timer_t opal_timer_base_get_cycles_clock_gettime(void)
199196
{
200-
struct timespec tp;
197+
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
201198

202-
if( 0 == clock_gettime(CLOCK_MONOTONIC, &tp) ) {
203-
return (tp.tv_sec * 1e9 + tp.tv_nsec);
204-
}
205-
return 0;
199+
(void) clock_gettime(CLOCK_MONOTONIC, &tp);
200+
201+
return (tp.tv_sec * 1e9 + tp.tv_nsec);
206202
}
207203
#endif /* OPAL_HAVE_CLOCK_GETTIME */
208204

0 commit comments

Comments
 (0)