1+ /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12/*
23 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
34 * University Research and Technology
45 * Corporation. All rights reserved.
5- * Copyright (c) 2004-2005 The University of Tennessee and The University
6+ * Copyright (c) 2004-2014 The University of Tennessee and The University
67 * of Tennessee Research Foundation. All rights
78 * reserved.
8- * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9+ * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
910 * University of Stuttgart. All rights reserved.
1011 * Copyright (c) 2004-2005 The Regents of the University of California.
1112 * All rights reserved.
1516 * reserved.
1617 * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
1718 * $COPYRIGHT$
18- *
19+ *
1920 * Additional copyrights may follow
20- *
21+ *
2122 * $HEADER$
2223 */
2324
2425#include "opal_config.h"
2526
26- #ifdef HAVE_STRING_H
2727#include <string.h>
28- #endif
2928
3029#include "opal/mca/timer/timer.h"
30+ #include "opal/mca/timer/base/base.h"
3131#include "opal/mca/timer/linux/timer_linux.h"
3232#include "opal/constants.h"
3333
34- opal_timer_t opal_timer_linux_freq ;
34+ static opal_timer_t opal_timer_base_get_cycles_sys_timer (void );
35+ static opal_timer_t opal_timer_base_get_usec_sys_timer (void );
36+
37+ #if OPAL_HAVE_CLOCK_GETTIME
38+ static opal_timer_t opal_timer_base_get_cycles_clock_gettime (void );
39+ static opal_timer_t opal_timer_base_get_usec_clock_gettime (void );
40+ opal_timer_t (* opal_timer_base_get_cycles )(void ) = opal_timer_base_get_cycles_clock_gettime ;
41+ opal_timer_t (* opal_timer_base_get_usec )(void ) = opal_timer_base_get_usec_clock_gettime ;
42+ #else
43+ opal_timer_t (* opal_timer_base_get_cycles )(void ) = opal_timer_base_get_cycles_sys_timer ;
44+ opal_timer_t (* opal_timer_base_get_usec )(void ) = opal_timer_base_get_usec_sys_timer ;
45+ #endif /* OPAL_HAVE_CLOCK_GETTIME */
46+
47+ opal_timer_t opal_timer_linux_freq = {0 };
3548
3649static int opal_timer_linux_open (void );
3750
@@ -68,6 +81,9 @@ find_info(FILE* fp, char *str, char *buf, size_t buflen)
6881 /* we found the line. Now eat everything up to,
6982 including, and one past the : */
7083 for (tmp = buf ; (* tmp != '\0' ) && (* tmp != ':' ) ; ++ tmp ) ;
84+ if (* tmp == '\0' ) {
85+ continue ;
86+ }
7187 for ( ++ tmp ; * tmp == ' ' ; ++ tmp );
7288 if ('\0' != * tmp ) {
7389 return tmp ;
@@ -78,8 +94,7 @@ find_info(FILE* fp, char *str, char *buf, size_t buflen)
7894 return NULL ;
7995}
8096
81- int
82- opal_timer_linux_open (void )
97+ static int opal_timer_linux_find_freq (void )
8398{
8499 FILE * fp ;
85100 char * loc ;
@@ -135,3 +150,78 @@ opal_timer_linux_open(void)
135150
136151 return OPAL_SUCCESS ;
137152}
153+
154+ int opal_timer_linux_open (void )
155+ {
156+ int ret = OPAL_SUCCESS ;
157+
158+ if (mca_timer_base_monotonic ) {
159+ #if OPAL_HAVE_CLOCK_GETTIME
160+ struct timespec res ;
161+ if ( 0 == clock_getres (CLOCK_MONOTONIC , & res )) {
162+ opal_timer_linux_freq = 1.e9 ;
163+ opal_timer_base_get_cycles = opal_timer_base_get_cycles_clock_gettime ;
164+ opal_timer_base_get_usec = opal_timer_base_get_usec_clock_gettime ;
165+ return ret ;
166+ }
167+ #else
168+ #if (0 == OPAL_TIMER_MONOTONIC )
169+ /* 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
173+ }
174+ ret = opal_timer_linux_find_freq ();
175+ opal_timer_base_get_cycles = opal_timer_base_get_cycles_sys_timer ;
176+ opal_timer_base_get_usec = opal_timer_base_get_usec_sys_timer ;
177+ return ret ;
178+ }
179+
180+ #if OPAL_HAVE_CLOCK_GETTIME
181+ opal_timer_t opal_timer_base_get_usec_clock_gettime (void )
182+ {
183+ struct timespec tp ;
184+
185+ if ( 0 == clock_gettime (CLOCK_MONOTONIC , & tp ) ) {
186+ return (tp .tv_sec * 1e6 + tp .tv_nsec /1000 );
187+ }
188+ return 0 ;
189+ }
190+
191+ opal_timer_t opal_timer_base_get_cycles_clock_gettime (void )
192+ {
193+ struct timespec tp ;
194+
195+ if ( 0 == clock_gettime (CLOCK_MONOTONIC , & tp ) ) {
196+ return (tp .tv_sec * 1e9 + tp .tv_nsec );
197+ }
198+ return 0 ;
199+ }
200+ #endif /* OPAL_HAVE_CLOCK_GETTIME */
201+
202+ opal_timer_t opal_timer_base_get_cycles_sys_timer (void )
203+ {
204+ #if OPAL_HAVE_SYS_TIMER_GET_CYCLES
205+ return opal_sys_timer_get_cycles ();
206+ #else
207+ return 0 ;
208+ #endif
209+ }
210+
211+
212+ opal_timer_t opal_timer_base_get_usec_sys_timer (void )
213+ {
214+ #if OPAL_HAVE_SYS_TIMER_GET_CYCLES
215+ /* freq is in Hz, so this gives usec */
216+ return opal_sys_timer_get_cycles () * 1000000 / opal_timer_linux_freq ;
217+ #else
218+ return 0 ;
219+ #endif
220+ }
221+
222+ opal_timer_t opal_timer_base_get_freq (void )
223+ {
224+ return opal_timer_linux_freq ;
225+ }
226+
227+
0 commit comments