@@ -694,15 +694,35 @@ char *Jim_StrDupLen(const char *s, int l)
694694 * Time related functions
695695 * ---------------------------------------------------------------------------*/
696696
697- /* Returns current time in microseconds */
698- static jim_wide JimClock(void)
697+ /* Returns current time in microseconds
698+ * CLOCK_MONOTONIC (monotonic clock that is affected by time adjustments)
699+ * CLOCK_MONOTONIC_RAW (monotonic clock that is not affected by time adjustments)
700+ * CLOCK_REALTIME (wall time)
701+ */
702+ jim_wide Jim_GetTimeUsec(unsigned type)
699703{
704+ long long now;
700705 struct timeval tv;
701706
702- gettimeofday(&tv, NULL);
703- return (jim_wide) tv.tv_sec * 1000000 + tv.tv_usec;
707+ #if defined(HAVE_CLOCK_GETTIME)
708+ struct timespec ts;
709+
710+ if (clock_gettime(type, &ts) == 0) {
711+ now = ts.tv_sec * 1000000LL + ts.tv_nsec / 1000;
712+ }
713+ else
714+ #endif
715+ {
716+ gettimeofday(&tv, NULL);
717+
718+ now = tv.tv_sec * 1000000LL + tv.tv_usec;
719+ }
720+
721+ return now;
704722}
705723
724+
725+
706726/* -----------------------------------------------------------------------------
707727 * Hash Tables
708728 * ---------------------------------------------------------------------------*/
@@ -5614,7 +5634,7 @@ int Jim_Collect(Jim_Interp *interp)
56145634 }
56155635 Jim_FreeHashTable(&marks);
56165636 interp->lastCollectId = interp->referenceNextId;
5617- interp->lastCollectTime = JimClock( );
5637+ interp->lastCollectTime = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW );
56185638 return collected;
56195639}
56205640
@@ -5627,7 +5647,7 @@ void Jim_CollectIfNeeded(Jim_Interp *interp)
56275647 jim_wide elapsedTime;
56285648
56295649 elapsedId = interp->referenceNextId - interp->lastCollectId;
5630- elapsedTime = JimClock( ) - interp->lastCollectTime;
5650+ elapsedTime = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW ) - interp->lastCollectTime;
56315651
56325652
56335653 if (elapsedId > JIM_COLLECT_ID_PERIOD || elapsedTime > JIM_COLLECT_TIME_PERIOD) {
@@ -5658,7 +5678,7 @@ Jim_Interp *Jim_CreateInterp(void)
56585678
56595679 i->maxCallFrameDepth = JIM_MAX_CALLFRAME_DEPTH;
56605680 i->maxEvalDepth = JIM_MAX_EVAL_DEPTH;
5661- i->lastCollectTime = JimClock( );
5681+ i->lastCollectTime = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW );
56625682
56635683 /* Note that we can create objects only after the
56645684 * interpreter liveList and freeList pointers are
@@ -14588,7 +14608,7 @@ static int Jim_TimeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *arg
1458814608 if (count < 0)
1458914609 return JIM_OK;
1459014610 i = count;
14591- start = JimClock( );
14611+ start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW );
1459214612 while (i-- > 0) {
1459314613 int retval;
1459414614
@@ -14597,7 +14617,7 @@ static int Jim_TimeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *arg
1459714617 return retval;
1459814618 }
1459914619 }
14600- elapsed = JimClock( ) - start;
14620+ elapsed = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW ) - start;
1460114621 if (elapsed < count * 10) {
1460214622 Jim_SetResult(interp, Jim_NewDoubleObj(interp, elapsed * 1.0 / count));
1460314623 }
@@ -14633,23 +14653,23 @@ static int Jim_TimeRateCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const
1463314653 }
1463414654
1463514655 /* Run until we exceed the time limit */
14636- start = JimClock( );
14656+ start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW );
1463714657 count = 0;
1463814658 do {
1463914659 int retval = Jim_EvalObj(interp, argv[1]);
14640- delta = JimClock( ) - start;
14660+ delta = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW ) - start;
1464114661 if (retval != JIM_OK) {
1464214662 return retval;
1464314663 }
1464414664 count++;
1464514665 } while (delta < us);
1464614666
1464714667 /* Now try to account for the loop and eval overhead */
14648- start = JimClock( );
14668+ start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW );
1464914669 n = 0;
1465014670 do {
1465114671 int retval = Jim_EvalObj(interp, interp->nullScriptObj);
14652- overhead = JimClock( ) - start;
14672+ overhead = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW ) - start;
1465314673 if (retval != JIM_OK) {
1465414674 return retval;
1465514675 }
0 commit comments