Skip to content

Commit

Permalink
Feature #1983: Made CmiWallTimer monotonic in the common case
Browse files Browse the repository at this point in the history
Reimplemented one of CmiWallTimer's commonly used code branches to use
the highest resolution steady clock from C++11 instead of gettimeofday.
The new implementation also stores the epoch in an integral time_point,
and uses the starting time of the program as the epoch to reduce the
internal floating-point error.

Change-Id: I4ad82339bda69b9a17ba408ed824d1dea36cdf74
  • Loading branch information
VenkatSNarayanan authored and rbuch committed Feb 13, 2019
1 parent f5c7833 commit 73d9771
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
26 changes: 7 additions & 19 deletions src/conv-core/convcore.C
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hrctimer.h"
#ifndef __STDC_FORMAT_MACROS
# define __STDC_FORMAT_MACROS
#endif
Expand Down Expand Up @@ -1035,7 +1035,6 @@ double CmiInitTime(void)

void CmiTimerInit(char **argv)
{
struct timeval tv;
struct rusage ru;
CpvInitialize(double, inittime_virtual);

Expand All @@ -1049,8 +1048,7 @@ void CmiTimerInit(char **argv)
#endif
if(CmiMyRank() == 0) /* initialize only once */
{
gettimeofday(&tv,0);
inittime_wallclock = (tv.tv_sec * 1.0) + (tv.tv_usec*0.000001);
inittime_wallclock = inithrc();
#ifndef RUSAGE_WHO
CpvAccess(inittime_virtual) = inittime_wallclock;
#else
Expand Down Expand Up @@ -1088,18 +1086,9 @@ static double lastT = -1.0;

double CmiWallTimer(void)
{
struct timeval tv;
double currenttime;

gettimeofday(&tv,0);
currenttime = (tv.tv_sec * 1.0) + (tv.tv_usec * 0.000001);
#if CMK_ERROR_CHECKING
if (lastT > 0.0 && currenttime < lastT) {
currenttime = lastT;
}
lastT = currenttime;
#endif
return _absoluteTime?currenttime:currenttime - inittime_wallclock;
currenttime = gethrctime();
return _absoluteTime?currenttime+inittime_wallclock:currenttime;
}

double CmiTimer(void)
Expand Down Expand Up @@ -1295,10 +1284,9 @@ static inline uint64_t PPC64_TimeBase(void)
return result;
}

uint64_t __micro_timer (void) {
struct timeval tv;
gettimeofday( &tv, 0 );
return tv.tv_sec * 1000000ULL + tv.tv_usec;
uint64_t __micro_timer (void)
{
return gethrctime_micro();
}

void CmiTimerInit(char **argv)
Expand Down
27 changes: 27 additions & 0 deletions src/conv-core/hrctimer.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <chrono>
#include <type_traits>
#include "hrctimer.h"
#include <ratio>

using clock_type=typename std::conditional<
std::chrono::high_resolution_clock::is_steady,
std::chrono::high_resolution_clock,
std::chrono::steady_clock>::type;
static std::chrono::time_point<clock_type> epoch;

double inithrc() { //defines our HRC epoch
epoch = clock_type::now();
return std::chrono::duration<double>(epoch.time_since_epoch()).count();
}
double gethrctime() {
auto timepoint = clock_type::now(); //gets HRC timepoint
auto time_since_epoch = timepoint-epoch; //gets the elapsed time since the start of HRC clock
double seconds = std::chrono::duration<double>(time_since_epoch).count(); //converts that time into seconds(double)
return seconds;
}
uint64_t gethrctime_micro() {
auto timepoint = clock_type::now(); //gets HRC timepoint
auto time_since_epoch = timepoint-epoch; //gets the elapsed time since the start of HRC clock
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(time_since_epoch).count(); //converts that time into microseconds(integer)
return microseconds;
}
13 changes: 13 additions & 0 deletions src/conv-core/hrctimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef HRCTIMER_H
#define HRCTIMER_H
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
double inithrc();
double gethrctime();
uint64_t gethrctime_micro();
#ifdef __cplusplus
}
#endif
#endif
4 changes: 3 additions & 1 deletion src/scripts/Make.depends
Original file line number Diff line number Diff line change
Expand Up @@ -2444,7 +2444,7 @@ convcore.o: convcore.C converse.h conv-header.h conv-config.h \
conv-cpm.h conv-cpath.h conv-qd.h conv-random.h conv-lists.h \
conv-trace.h persistent.h conv-rdma.h cmirdmautils.h debug-conv.h \
sockRoutines.h conv-ccs.h ccs-server.h ckhashtable.h pup.h \
memory-isomalloc.h quiescence.h cmibacktrace.c
memory-isomalloc.h quiescence.h cmibacktrace.c hrctimer.h

converseProjections.o: converseProjections.C converse.h conv-header.h \
conv-config.h conv-autoconfig.h conv-common.h conv-mach-common.h \
Expand Down Expand Up @@ -2627,6 +2627,8 @@ global-nop.o: global-nop.c converse.h conv-header.h conv-config.h \

hilbert.o: hilbert.C hilbert.h

hrctimer.o: hrctimer.C hrctimer.h

init.o: init.C ckcheckpoint.h pup.h converse.h conv-header.h \
conv-config.h conv-autoconfig.h conv-common.h conv-mach-common.h \
conv-mach.h conv-mach-opt.h lrts-common.h cmiqueue.h pup_c.h lrtslock.h \
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ CVLIBS=$(L)/libconv-core.a \
$(L)/libccs-client.a $(L)/libconv-partition.a $(L)/libhwloc_embedded.a \
$(L)/libconv-ldb.a

LIBCONV_CORE=convcore.o conv-conds.o conv-taskQ.o queueing.o msgmgr.o \
LIBCONV_CORE=convcore.o hrctimer.o conv-conds.o conv-taskQ.o queueing.o msgmgr.o \
cpm.o cpthreads.o futures.o cldb.o random.o \
debug-conv.o debug-conv++.o conv-ccs.o ccs-builtins.o middle-ccs.o \
traceCore.o traceCoreCommon.o \
Expand Down

0 comments on commit 73d9771

Please sign in to comment.