- added some profiling code to the VM.

This commit is contained in:
Christoph Oelckers 2016-12-31 17:59:48 +01:00
parent 3d61d2c1f4
commit 28d79cc2b0
3 changed files with 33 additions and 1 deletions

View file

@ -39,6 +39,10 @@
#include "r_state.h" #include "r_state.h"
#include "textures/textures.h" #include "textures/textures.h"
#include "math/cmath.h" #include "math/cmath.h"
#include "stats.h"
extern cycle_t VMCycles[10];
extern int VMCalls[10];
// This must be a separate function because the VC compiler would otherwise allocate memory on the stack for every separate instance of the exception object that may get thrown. // This must be a separate function because the VC compiler would otherwise allocate memory on the stack for every separate instance of the exception object that may get thrown.
void ThrowAbortException(EVMAbortException reason, const char *moreinfo, ...); void ThrowAbortException(EVMAbortException reason, const char *moreinfo, ...);

View file

@ -609,7 +609,9 @@ begin:
{ {
try try
{ {
VMCycles[0].Unclock();
numret = static_cast<VMNativeFunction *>(call)->NativeCall(reg.param + f->NumParam - B, call->DefaultArgs, B, returns, C); numret = static_cast<VMNativeFunction *>(call)->NativeCall(reg.param + f->NumParam - B, call->DefaultArgs, B, returns, C);
VMCycles[0].Clock();
} }
catch (CVMAbortException &err) catch (CVMAbortException &err)
{ {
@ -621,6 +623,7 @@ begin:
} }
else else
{ {
VMCalls[0]++;
VMScriptFunction *script = static_cast<VMScriptFunction *>(call); VMScriptFunction *script = static_cast<VMScriptFunction *>(call);
VMFrame *newf = stack->AllocFrame(script); VMFrame *newf = stack->AllocFrame(script);
VMFillParams(reg.param + f->NumParam - B, newf, B); VMFillParams(reg.param + f->NumParam - B, newf, B);
@ -663,7 +666,10 @@ begin:
{ {
try try
{ {
return static_cast<VMNativeFunction *>(call)->NativeCall(reg.param + f->NumParam - B, call->DefaultArgs, B, ret, numret); VMCycles[0].Unclock();
auto r = static_cast<VMNativeFunction *>(call)->NativeCall(reg.param + f->NumParam - B, call->DefaultArgs, B, ret, numret);
VMCycles[0].Clock();
return r;
} }
catch (CVMAbortException &err) catch (CVMAbortException &err)
{ {
@ -675,6 +681,7 @@ begin:
} }
else else
{ // FIXME: Not a true tail call { // FIXME: Not a true tail call
VMCalls[0]++;
VMScriptFunction *script = static_cast<VMScriptFunction *>(call); VMScriptFunction *script = static_cast<VMScriptFunction *>(call);
VMFrame *newf = stack->AllocFrame(script); VMFrame *newf = stack->AllocFrame(script);
VMFillParams(reg.param + f->NumParam - B, newf, B); VMFillParams(reg.param + f->NumParam - B, newf, B);

View file

@ -35,6 +35,10 @@
#include <new> #include <new>
#include "dobject.h" #include "dobject.h"
#include "v_text.h" #include "v_text.h"
#include "stats.h"
cycle_t VMCycles[10];
int VMCalls[10];
IMPLEMENT_CLASS(VMException, false, false) IMPLEMENT_CLASS(VMException, false, false)
IMPLEMENT_CLASS(VMFunction, true, true) IMPLEMENT_CLASS(VMFunction, true, true)
@ -463,11 +467,14 @@ int VMFrameStack::Call(VMFunction *func, VMValue *params, int numparams, VMRetur
} }
else else
{ {
VMCycles[0].Clock();
VMCalls[0]++;
AllocFrame(static_cast<VMScriptFunction *>(func)); AllocFrame(static_cast<VMScriptFunction *>(func));
allocated = true; allocated = true;
VMFillParams(params, TopFrame(), numparams); VMFillParams(params, TopFrame(), numparams);
int numret = VMExec(this, static_cast<VMScriptFunction *>(func)->Code, results, numresults); int numret = VMExec(this, static_cast<VMScriptFunction *>(func)->Code, results, numresults);
PopFrame(); PopFrame();
VMCycles[0].Unclock();
return numret; return numret;
} }
} }
@ -573,3 +580,17 @@ void ThrowVMException(VMException *x)
{ {
throw x; throw x;
} }
ADD_STAT(VM)
{
double added = 0;
int addedc = 0;
for (auto d : VMCycles) added += d.TimeMS();
for (auto d : VMCalls) addedc += d;
memmove(&VMCycles[1], &VMCycles[0], 9 * sizeof(cycle_t));
memmove(&VMCalls[1], &VMCalls[0], 9 * sizeof(int));
VMCycles[0].Reset();
VMCalls[0] = 0;
return FStringf("VM time in last 10 tics: %f ms, %d calls", added, addedc);
}