From ed7b4b5fca0f36aa27367a31d29820981ecf4d24 Mon Sep 17 00:00:00 2001 From: janwas Date: Thu, 13 Jul 2006 20:09:20 +0000 Subject: [PATCH] # simple profiler/timer for JS scripts cheezy but low-overhead. call startXTimer(i) and stopXTimer(i), where i can be 0..9 for now. results are displayed via debug_out at end of program run. This was SVN commit r4095. --- source/scripting/ScriptGlue.cpp | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/source/scripting/ScriptGlue.cpp b/source/scripting/ScriptGlue.cpp index 6085604064..c3d67f5818 100644 --- a/source/scripting/ScriptGlue.cpp +++ b/source/scripting/ScriptGlue.cpp @@ -569,6 +569,54 @@ JSBool setSimRate(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* UNUSE } +static const uint MAX_XTIMERS = 10; +typedef TimerRdtsc XTimerImpl; // type must be kept in sync with timer.h TIMER_USE_RAW_TICKS +static XTimerImpl xtimer_impl; +static XTimerImpl::unit xstart_times[MAX_XTIMERS]; +static TimerClient xclients[MAX_XTIMERS]; +static char xdescription_buf[MAX_XTIMERS * 11]; + +static void initXTimers() +{ + char* pos = xdescription_buf; + for(uint i = 0; i < MAX_XTIMERS; i++) + { + const char* description = pos; + pos += sprintf(pos, "xtimer %d", i)+1; + timer_add_client(&xclients[i], description); + } +} + +JSBool startXTimer(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* UNUSED(rval)) +{ + ONCE(initXTimers()); + + REQUIRE_MIN_PARAMS(1, startXTimer); + REQUIRE_MIN_PARAMS(1, startXTimer); + uint slot = ToPrimitive(argv[0]); + debug_assert(slot < MAX_XTIMERS); + + debug_assert(xstart_times[slot] == 0); + xstart_times[slot] = xtimer_impl.get_timestamp(); + return( JS_TRUE ); +} + + +JSBool stopXTimer(JSContext* cx, JSObject*, uint argc, jsval* argv, jsval* UNUSED(rval)) +{ + REQUIRE_MIN_PARAMS(1, stopXTimer); + REQUIRE_MIN_PARAMS(1, stopXTimer); + uint slot = ToPrimitive(argv[0]); + debug_assert(slot < MAX_XTIMERS); + + debug_assert(xstart_times[slot] != 0); + XTimerImpl::unit dt = xtimer_impl.get_timestamp() - xstart_times[slot]; + xstart_times[slot] = 0; + timer_bill_client(&xclients[slot], dt); + return( JS_TRUE ); +} + + //----------------------------------------------------------------------------- // Game Setup //----------------------------------------------------------------------------- @@ -1256,6 +1304,9 @@ JSFunctionSpec ScriptFunctionTable[] = JS_FUNC(cancelTimer, cancelTimer, 0) JS_FUNC(setSimRate, setSimRate, 1) + JS_FUNC(startXTimer, startXTimer, 1) + JS_FUNC(stopXTimer, stopXTimer, 1) + // Game Setup JS_FUNC(startGame, startGame, 0) JS_FUNC(endGame, endGame, 0)