Skip to content

Commit

Permalink
sclang: make use of boost::chrono for timing-related methods & fix _E…
Browse files Browse the repository at this point in the history
…lapsedTime

Signed-off-by: Tim Blechmann <tim@klingt.org>
  • Loading branch information
timblechmann committed Dec 30, 2012
1 parent 87fd4a3 commit 89c4491
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 74 deletions.
1 change: 1 addition & 0 deletions include/lang/PyrSched.h
Expand Up @@ -35,6 +35,7 @@ SC_DLLEXPORT_C void schedStop();
SC_DLLEXPORT_C void schedClear();

double elapsedTime();
double elapsedRealTime();
int64 OSCTime();

int64 ElapsedTimeToOSC(double elapsed);
Expand Down
60 changes: 40 additions & 20 deletions lang/LangPrimSource/PyrSched.cpp
Expand Up @@ -32,13 +32,12 @@
#include <math.h>
#include <limits>

#ifndef SC_WIN32
#include <sys/time.h>
#endif

#include "SC_Win32Utils.h"
#include "SCBase.h"

#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono.hpp>

static const double dInfinity = std::numeric_limits<double>::infinity();

void runAwakeMessage(VMGlobals *g);
Expand Down Expand Up @@ -220,28 +219,41 @@ const double fSECONDS_FROM_1900_to_1970 = 2208988800.; /* 17 leap years */
#ifdef SC_DARWIN
static void syncOSCOffsetWithTimeOfDay();
void* resyncThread(void* arg);
#else // !SC_DARWIN
#endif // SC_DARWIN

#ifdef SC_WIN32
namespace chrono = boost::chrono; // we can later move to std::chrono in c++11

#else
# include <sys/time.h>
#endif
static boost::chrono::system_clock::time_point systemTimeOfInitialization;
static boost::chrono::high_resolution_clock::time_point hrTimeOfInitialization;

template <typename DurationType>
inline double DurToFloat(DurationType dur)
{
using namespace chrono;
seconds secs = duration_cast<seconds>(dur);
nanoseconds nanosecs = dur - secs;

return secs.count() + 1.0e-9 * nanosecs.count();
}

inline double GetTimeOfDay();
double GetTimeOfDay()
{
struct timeval tv;
gettimeofday(&tv, 0);
return (double)tv.tv_sec + 1.0e-6 * (double)tv.tv_usec;
using namespace chrono;

typename system_clock::time_point now = system_clock::now();
typename system_clock::duration since_epoch = now.time_since_epoch();

return DurToFloat(since_epoch);
}
#endif // SC_DARWIN

SC_DLLEXPORT_C void schedInit()
{
pthread_cond_init (&gSchedCond, NULL);
pthread_mutex_init (&gLangMutex, NULL);

systemTimeOfInitialization = chrono::system_clock::now();
hrTimeOfInitialization = chrono::high_resolution_clock::now();

#ifdef SC_DARWIN
syncOSCOffsetWithTimeOfDay();
pthread_create (&gResyncThread, NULL, resyncThread, (void*)0);
Expand All @@ -251,6 +263,7 @@ SC_DLLEXPORT_C void schedInit()
#else
gElapsedOSCoffset = (int64)kSECONDS_FROM_1900_to_1970 << 32;
#endif

}

SC_DLLEXPORT_C void schedCleanup()
Expand All @@ -269,27 +282,35 @@ double bootSeconds()
#endif
}

double elapsedTime();
double elapsedTime()
{
#ifdef SC_DARWIN
return 1e-9 * (double)(AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()) - gHostStartNanos);
#else
return GetTimeOfDay();
return DurToFloat(chrono::system_clock::now() - systemTimeOfInitialization);
#endif
}

double elapsedRealTime()
{
#ifdef SC_DARWIN
return 1e-9 * (double)(AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()) - gHostStartNanos);
#else
return DurToFloat(chrono::high_resolution_clock::now() - hrTimeOfInitialization);
#endif
}


int64 ElapsedTimeToOSC(double elapsed)
{
return (int64)(elapsed * kSecondsToOSC) + gElapsedOSCoffset;
}

double OSCToElapsedTime(int64 oscTime)
{
return (double)(oscTime - gElapsedOSCoffset) * kOSCtoSecs;
return (double)(oscTime - gElapsedOSCoffset) * kOSCtoSecs;
}

void ElapsedTimeToTimespec(double elapsed, struct timespec *spec);
void ElapsedTimeToTimespec(double elapsed, struct timespec *spec)
{
int64 oscTime = ElapsedTimeToOSC(elapsed);
Expand Down Expand Up @@ -1360,10 +1381,9 @@ int prSystemClock_SchedAbs(struct VMGlobals *g, int numArgsPushed)
return errNone;
}

int prElapsedTime(struct VMGlobals *g, int numArgsPushed);
int prElapsedTime(struct VMGlobals *g, int numArgsPushed)
{
SetFloat(g->sp, elapsedTime());
SetFloat(g->sp, elapsedRealTime());
return errNone;
}

Expand Down
75 changes: 37 additions & 38 deletions lang/LangPrimSource/PyrUnixPrim.cpp
Expand Up @@ -39,6 +39,9 @@ Primitives for Unix.
#include "sc_popen.h"
#include "SCBase.h"

#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono.hpp>

#ifdef SC_WIN32
#include "SC_Win32Utils.h"
#else
Expand Down Expand Up @@ -66,41 +69,41 @@ int prString_System(struct VMGlobals *g, int numArgsPushed)
int prString_Basename(struct VMGlobals *g, int numArgsPushed);
int prString_Basename(struct VMGlobals *g, int numArgsPushed)
{
PyrSlot *a = g->sp;
PyrSlot *a = g->sp;

char path[PATH_MAX];
int err = slotStrVal(a, path, PATH_MAX);
if (err) return err;
char path[PATH_MAX];
int err = slotStrVal(a, path, PATH_MAX);
if (err) return err;

char *basename0 = basename(path);
char *basename0 = basename(path);

int size = strlen(basename0);
PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
memcpy(strobj->s, basename0, size);
int size = strlen(basename0);
PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
memcpy(strobj->s, basename0, size);

SetObject(a, strobj);
SetObject(a, strobj);

return errNone;
return errNone;
}

int prString_Dirname(struct VMGlobals *g, int numArgsPushed);
int prString_Dirname(struct VMGlobals *g, int numArgsPushed)
{
PyrSlot *a = g->sp;
PyrSlot *a = g->sp;

char path[PATH_MAX];
int err = slotStrVal(a, path, PATH_MAX);
if (err) return err;
char path[PATH_MAX];
int err = slotStrVal(a, path, PATH_MAX);
if (err) return err;

char *dirname0 = dirname(path);
char *dirname0 = dirname(path);

int size = strlen(dirname0);
PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
memcpy(strobj->s, dirname0, size);
int size = strlen(dirname0);
PyrString *strobj = newPyrStringN(g->gc, size, 0, true);
memcpy(strobj->s, dirname0, size);

SetObject(a, strobj);
SetObject(a, strobj);

return errNone;
return errNone;
}

struct sc_process {
Expand Down Expand Up @@ -132,8 +135,8 @@ void* string_popen_thread_func(void *data)

free(process);

pthread_mutex_lock (&gLangMutex);
if(compiledOK) {
pthread_mutex_lock (&gLangMutex);
if(compiledOK) {
VMGlobals *g = gMainVMGlobals;
g->canCallOS = true;
++g->sp; SetObject(g->sp, class_string);
Expand All @@ -142,7 +145,7 @@ void* string_popen_thread_func(void *data)
runInterpreter(g, s_unixCmdAction, 3);
g->canCallOS = false;
}
pthread_mutex_unlock (&gLangMutex);
pthread_mutex_unlock (&gLangMutex);

return 0;
}
Expand Down Expand Up @@ -235,10 +238,6 @@ int prUnix_Errno(struct VMGlobals *g, int numArgsPushed)

#include <time.h>

#ifndef SC_WIN32
#include <sys/time.h>
#endif

double bootSeconds();

int prLocalTime(struct VMGlobals *g, int numArgsPushed);
Expand All @@ -247,10 +246,10 @@ int prLocalTime(struct VMGlobals *g, int numArgsPushed)
PyrSlot *a = g->sp;
PyrSlot *slots = slotRawObject(a)->slots;

struct timeval tv;
gettimeofday(&tv, 0);

struct tm* tm = localtime((const time_t*)&tv.tv_sec);
using namespace boost::chrono;
system_clock::time_point now = system_clock::now();
time_t now_time_t = system_clock::to_time_t(now);
struct tm* tm = localtime(&now_time_t);

SetInt(slots+0, tm->tm_year + 1900);
SetInt(slots+1, tm->tm_mon + 1); // 0 based month ??
Expand All @@ -259,7 +258,7 @@ int prLocalTime(struct VMGlobals *g, int numArgsPushed)
SetInt(slots+4, tm->tm_min);
SetInt(slots+5, tm->tm_sec);
SetInt(slots+6, tm->tm_wday);
SetFloat(slots+7, tv.tv_sec + 1e-6 * tv.tv_usec);
SetFloat(slots+7, duration_cast<nanoseconds>(now.time_since_epoch()).count() * 1.0e-9);
SetFloat(slots+8, bootSeconds());

return errNone;
Expand All @@ -271,10 +270,10 @@ int prGMTime(struct VMGlobals *g, int numArgsPushed)
PyrSlot *a = g->sp;
PyrSlot *slots = slotRawObject(a)->slots;

struct timeval tv;
gettimeofday(&tv, 0);

struct tm* tm = gmtime((const time_t*)&tv.tv_sec);
using namespace boost::chrono;
system_clock::time_point now = system_clock::now();
time_t now_time_t = system_clock::to_time_t(now);
struct tm* tm = localtime(&now_time_t);

SetInt(slots+0, tm->tm_year + 1900);
SetInt(slots+1, tm->tm_mon + 1);
Expand All @@ -283,7 +282,7 @@ int prGMTime(struct VMGlobals *g, int numArgsPushed)
SetInt(slots+4, tm->tm_min);
SetInt(slots+5, tm->tm_sec);
SetInt(slots+6, tm->tm_wday);
SetFloat(slots+7, tv.tv_sec + 1e-6 * tv.tv_usec);
SetFloat(slots+7, duration_cast<nanoseconds>(now.time_since_epoch()).count() * 1.0e-9);
SetFloat(slots+8, bootSeconds());

return errNone;
Expand Down Expand Up @@ -397,7 +396,7 @@ void initUnixPrimitives()

base = nextPrimitiveIndex();

s_unixCmdAction = getsym("doUnixCmdAction");
s_unixCmdAction = getsym("doUnixCmdAction");

definePrimitive(base, index++, "_String_System", prString_System, 1, 0);
definePrimitive(base, index++, "_String_Basename", prString_Basename, 1, 0);
Expand Down
6 changes: 3 additions & 3 deletions lang/LangSource/GC.cpp
Expand Up @@ -49,7 +49,7 @@ int checkScans = 0;
int checkPartialScans = 0;
int checkSlotsScanned = 0;

double elapsedTime();
double elapsedRealTime();

inline void PyrGC::beginPause()
{
Expand All @@ -59,12 +59,12 @@ inline void PyrGC::beginPause()
checkNumToScan = mNumToScan;
checkPartialScans = mNumPartialScans;
checkSlotsScanned = mSlotsScanned;
pauseBeginTime = elapsedTime();
pauseBeginTime = elapsedRealTime();
}

inline void PyrGC::endPause()
{
double pauseTime = elapsedTime() - pauseBeginTime;
double pauseTime = elapsedRealTime() - pauseBeginTime;
if (pauseTime > 0.001) numPausesGreaterThanOneMillisecond++;
if (pauseTime > maxPauseTime) {
maxPauseTime = pauseTime;
Expand Down
21 changes: 13 additions & 8 deletions lang/LangSource/PyrInterpreter3.cpp
Expand Up @@ -39,11 +39,8 @@
#include <string.h>
# include <signal.h>

#ifdef SC_WIN32
# include "SC_Win32Utils.h"
#else
# include <sys/time.h>
#endif
#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono.hpp>

#include <float.h>
#define kBigBigFloat DBL_MAX
Expand All @@ -60,9 +57,17 @@ double timeNow();

int32 timeseed()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec ^ tv.tv_usec;
using namespace boost::chrono;

high_resolution_clock::time_point now = high_resolution_clock::now();
high_resolution_clock::duration since_epoch = now.time_since_epoch();

seconds secs = duration_cast<seconds>(since_epoch);
nanoseconds nanosecs = since_epoch - secs;

boost::int_least64_t seed = secs.count() ^ nanosecs.count();

return (int32)seed;
}

VMGlobals gVMGlobals;
Expand Down
4 changes: 2 additions & 2 deletions lang/LangSource/PyrLexer.cpp
Expand Up @@ -1763,7 +1763,7 @@ void traverseFullDepTree2()
post("\tByte Code Size %d\n", totalByteCodes);
//elapsed = TickCount() - compileStartTime;
//elapsed = 0;
elapsed = elapsedTime() - compileStartTime;
elapsed = elapsedRealTime() - compileStartTime;
post("\tcompiled %d files in %.2f seconds\n",
gNumCompiledFiles, elapsed );
if(numOverwrites == 1){
Expand Down Expand Up @@ -2119,7 +2119,7 @@ SC_DLLEXPORT_C bool compileLibrary()

SC_LanguageConfig::readDefaultLibraryConfig();

compileStartTime = elapsedTime();
compileStartTime = elapsedRealTime();

totalByteCodes = 0;

Expand Down

0 comments on commit 89c4491

Please sign in to comment.