]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_exec.c
fix miscounting of timedemo frames on cold/warm runs in the same session
[xonotic/darkplaces.git] / prvm_exec.c
index eb8aa349718b224f67b5690f864706c2df9b0a9a..80287cab1ae37ea8054bf2f573df5409fcfc38de 100644 (file)
@@ -132,7 +132,7 @@ void PRVM_PrintStatement (dstatement_t *s)
                Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
 
        if (prvm_statementprofiling.integer)
-               Con_Printf("%7i ", prog->statement_profile[s - prog->statements]);
+               Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
 
        if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
        {
@@ -266,29 +266,48 @@ void PRVM_StackTrace (void)
 }
 
 
-/*
-============
-PRVM_Profile_f
-
-============
-*/
-void PRVM_Profile_f (void)
+void PRVM_CallProfile ()
 {
        mfunction_t *f, *best;
-       int i, num, max/*, howmany*/;
+       int i;
+       double max;
+       double sum;
+
+       Con_Printf( "%s Call Profile:\n", PRVM_NAME );
 
-       //howmany = 10;
-       //if (Cmd_Argc() == 2)
-       //      howmany = atoi(Cmd_Argv(1));
-       if(Cmd_Argc() != 2)
+       sum = 0;
+       do
        {
-               Con_Print("prvm_profile <program name>\n");
-               return;
-       }
+               max = 0;
+               best = NULL;
+               for (i=0 ; i<prog->progs->numfunctions ; i++)
+               {
+                       f = &prog->functions[i];
+                       if (max < f->totaltime)
+                       {
+                               max = f->totaltime;
+                               best = f;
+                       }
+               }
+               if (best)
+               {
+                       sum += best->totaltime;
+                       Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
+                       best->totaltime = 0;
+               }
+       } while (best);
 
-       PRVM_Begin;
-       if(!PRVM_SetProgFromString(Cmd_Argv(1)))
-               return;
+       Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
+       Con_Printf("       - used by QC code of this VM: %9.4f\n", sum);
+
+       prog->starttime = Sys_DoubleTime();
+}
+
+void PRVM_Profile (int maxfunctions, int mininstructions)
+{
+       mfunction_t *f, *best;
+       int i, num;
+       double max;
 
        Con_Printf( "%s Profile:\n[CallCount] [Statements] [BuiltinCost]\n", PRVM_NAME );
 
@@ -308,17 +327,68 @@ void PRVM_Profile_f (void)
                }
                if (best)
                {
-                       //if (num < howmany)
-                       if (best->first_statement < 0)
-                               Con_Printf("%10i ----- builtin ----- %s\n", best->callcount, PRVM_GetString(best->s_name));
-                       else
-                               Con_Printf("%10i%10i%10i %s\n", best->callcount, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
+                       if (num < maxfunctions && max >= mininstructions)
+                       {
+                               if (best->first_statement < 0)
+                                       Con_Printf("%9.0f ----- builtin ----- %s\n", best->callcount, PRVM_GetString(best->s_name));
+                               else
+                                       Con_Printf("%9.0f %9.0f %9.0f %s\n", best->callcount, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
+                       }
                        num++;
                        best->profile = 0;
                        best->builtinsprofile = 0;
                        best->callcount = 0;
                }
        } while (best);
+}
+
+/*
+============
+PRVM_CallProfile_f
+
+============
+*/
+void PRVM_CallProfile_f (void)
+{
+       if (Cmd_Argc() != 2)
+       {
+               Con_Print("prvm_callprofile <program name>\n");
+               return;
+       }
+
+       PRVM_Begin;
+       if(!PRVM_SetProgFromString(Cmd_Argv(1)))
+               return;
+
+       PRVM_CallProfile();
+
+       PRVM_End;
+}
+
+/*
+============
+PRVM_Profile_f
+
+============
+*/
+void PRVM_Profile_f (void)
+{
+       int howmany;
+
+       howmany = 1<<30;
+       if (Cmd_Argc() == 3)
+               howmany = atoi(Cmd_Argv(2));
+       else if (Cmd_Argc() != 2)
+       {
+               Con_Print("prvm_profile <program name>\n");
+               return;
+       }
+
+       PRVM_Begin;
+       if(!PRVM_SetProgFromString(Cmd_Argv(1)))
+               return;
+
+       PRVM_Profile(howmany, 1);
 
        PRVM_End;
 }
@@ -353,7 +423,8 @@ void PRVM_PrintState(void)
        PRVM_StackTrace ();
 }
 
-void PRVM_Crash()
+extern sizebuf_t vm_tempstringsbuf;
+void PRVM_Crash(void)
 {
        if (prog == NULL)
                return;
@@ -368,6 +439,9 @@ void PRVM_Crash()
        prog->depth = 0;
        prog->localstack_used = 0;
 
+       // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
+       vm_tempstringsbuf.cursize = 0;
+
        // reset the prog pointer
        prog = NULL;
 }
@@ -474,8 +548,7 @@ PRVM_ExecuteProgram
 extern cvar_t prvm_boundscheck;
 extern cvar_t prvm_traceqc;
 extern cvar_t prvm_statementprofiling;
-extern int             PRVM_ED_FindFieldOffset (const char *field);
-extern ddef_t* PRVM_ED_FindGlobal(const char *name);
+extern sizebuf_t vm_tempstringsbuf;
 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
 {
        dstatement_t    *st, *startst;
@@ -483,16 +556,23 @@ void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
        prvm_edict_t    *ed;
        prvm_eval_t     *ptr;
        int             jumpcount, cachedpr_trace, exitdepth;
+       int             restorevm_tempstringsbuf_cursize;
+       double  calltime;
+
+       calltime = Sys_DoubleTime();
 
        if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
        {
-               if (prog->self && PRVM_G_INT(prog->self->ofs))
-                       PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_G_INT(prog->self->ofs)));
+               if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
+                       PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
                PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
        }
 
        f = &prog->functions[fnum];
 
+       // after executing this function, delete all tempstrings it created
+       restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
+
        prog->trace = prvm_traceqc.integer;
 
        // we know we're done when pr_depth drops to this
@@ -575,4 +655,14 @@ chooseexecprogram:
                        }
                }
        }
+
+cleanup:
+       if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
+               Con_Printf("PRVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
+       // delete tempstrings created by this function
+       vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
+
+       prog->functions[fnum].totaltime += (Sys_DoubleTime() - calltime);
+
+       SV_FlushBroadcastMessages();
 }