From e17ddb29bc24c36a42005b981b4f86281deb761c Mon Sep 17 00:00:00 2001 From: havoc Date: Thu, 20 Feb 2003 09:15:46 +0000 Subject: [PATCH] now counts (very approximate) cost of builtin functions called by progs, profile command reports this in addition to the instructions count, also profile now reports a whole list, not just the top 10 git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2768 d7cf8633-e32d-0410-b094-e92efae38249 --- host_cmd.c | 6 +++--- pr_cmds.c | 46 +++++++++++++++++++++++++++++++--------------- pr_comp.h | 32 ++++++++++++++++++++++++-------- pr_edict.c | 40 ++++++++++++++++++++++------------------ pr_exec.c | 24 +++++++++++++----------- pr_execprogram.h | 1 + progs.h | 8 ++++---- 7 files changed, 98 insertions(+), 59 deletions(-) diff --git a/host_cmd.c b/host_cmd.c index 25d861c2..3179cee2 100644 --- a/host_cmd.c +++ b/host_cmd.c @@ -24,7 +24,7 @@ int current_skill; char sv_spawnmap[MAX_QPATH]; char sv_loadgame[MAX_OSPATH]; -dfunction_t *ED_FindFunction (char *name); +mfunction_t *ED_FindFunction (char *name); /* ================== @@ -840,7 +840,7 @@ void Host_Color_f(void) { int top, bottom; int playercolor; - dfunction_t *f; + mfunction_t *f; func_t SV_ChangeTeam; if (Cmd_Argc() == 1) @@ -995,7 +995,7 @@ void Host_Spawn_f (void) client_t *client; edict_t *ent; func_t RestoreGame; - dfunction_t *f; + mfunction_t *f; if (cmd_source == src_command) { diff --git a/pr_cmds.c b/pr_cmds.c index 6ee5c362..1c4665ae 100644 --- a/pr_cmds.c +++ b/pr_cmds.c @@ -371,7 +371,7 @@ void PF_centerprint (void) } client = &svs.clients[entnum-1]; - + MSG_WriteChar (&client->message,svc_centerprint); MSG_WriteString (&client->message, s ); } @@ -515,7 +515,7 @@ random() void PF_random (void) { float num; - + num = (rand ()&0x7fff) / ((float)0x7fff); G_FLOAT(OFS_RETURN) = num; @@ -667,6 +667,8 @@ void PF_traceline (void) int nomonsters; edict_t *ent; + pr_xfunction->builtinsprofile += 30; + v1 = G_VECTOR(OFS_PARM0); v2 = G_VECTOR(OFS_PARM1); nomonsters = G_FLOAT(OFS_PARM2); @@ -709,6 +711,8 @@ void PF_tracebox (void) int nomonsters; edict_t *ent; + pr_xfunction->builtinsprofile += 30; + v1 = G_VECTOR(OFS_PARM0); m1 = G_VECTOR(OFS_PARM1); m2 = G_VECTOR(OFS_PARM2); @@ -725,7 +729,7 @@ void PF_tracebox (void) pr_global_struct->trace_inopen = trace.inopen; VectorCopy (trace.endpos, pr_global_struct->trace_endpos); VectorCopy (trace.plane.normal, pr_global_struct->trace_plane_normal); - pr_global_struct->trace_plane_dist = trace.plane.dist; + pr_global_struct->trace_plane_dist = trace.plane.dist; if (trace.ent) pr_global_struct->trace_ent = EDICT_TO_PROG(trace.ent); else @@ -739,6 +743,8 @@ void PF_TraceToss (void) edict_t *ent; edict_t *ignore; + pr_xfunction->builtinsprofile += 600; + ent = G_EDICT(OFS_PARM0); ignore = G_EDICT(OFS_PARM1); @@ -751,7 +757,7 @@ void PF_TraceToss (void) pr_global_struct->trace_inopen = trace.inopen; VectorCopy (trace.endpos, pr_global_struct->trace_endpos); VectorCopy (trace.plane.normal, pr_global_struct->trace_plane_normal); - pr_global_struct->trace_plane_dist = trace.plane.dist; + pr_global_struct->trace_plane_dist = trace.plane.dist; if (trace.ent) pr_global_struct->trace_ent = EDICT_TO_PROG(trace.ent); else @@ -799,6 +805,7 @@ int PF_newcheckclient (int check) for ( ; ; i++) { + pr_xfunction->builtinsprofile++; if (i == svs.maxclients+1) i = 1; @@ -902,11 +909,11 @@ void PF_stuffcmd (void) int entnum; char *str; client_t *old; - + entnum = G_EDICTNUM(OFS_PARM0); if (entnum < 1 || entnum > svs.maxclients) Host_Error ("Parm 0 not a client"); - str = G_STRING(OFS_PARM1); + str = G_STRING(OFS_PARM1); old = host_client; host_client = &svs.clients[entnum-1]; @@ -927,7 +934,7 @@ void PF_localcmd (void) { char *str; - str = G_STRING(OFS_PARM0); + str = G_STRING(OFS_PARM0); Cbuf_AddText (str); } @@ -941,7 +948,7 @@ float cvar (string) void PF_cvar (void) { char *str; - + str = G_STRING(OFS_PARM0); G_FLOAT(OFS_RETURN) = Cvar_VariableValue (str); @@ -957,10 +964,10 @@ float cvar (string) void PF_cvar_set (void) { char *var, *val; - + var = G_STRING(OFS_PARM0); val = G_STRING(OFS_PARM1); - + Cvar_Set (var, val); } @@ -991,6 +998,7 @@ void PF_findradius (void) ent = NEXT_EDICT(sv.edicts); for (i=1 ; ibuiltinsprofile++; if (ent->free) continue; if (ent->v->solid == SOLID_NOT) @@ -1074,6 +1082,7 @@ void PF_etos (void) void PF_Spawn (void) { edict_t *ed; + pr_xfunction->builtinsprofile += 20; ed = ED_Alloc(); RETURN_EDICT(ed); } @@ -1081,6 +1090,7 @@ void PF_Spawn (void) void PF_Remove (void) { edict_t *ed; + pr_xfunction->builtinsprofile += 20; ed = G_EDICT(OFS_PARM0); if (ed == sv.edicts) @@ -1110,6 +1120,7 @@ void PF_Find (void) for (e++ ; e < sv.num_edicts ; e++) { + pr_xfunction->builtinsprofile++; ed = EDICT_NUM(e); if (ed->free) continue; @@ -1140,6 +1151,7 @@ void PF_FindFloat (void) for (e++ ; e < sv.num_edicts ; e++) { + pr_xfunction->builtinsprofile++; ed = EDICT_NUM(e); if (ed->free) continue; @@ -1175,6 +1187,7 @@ void PF_findchain (void) ent = NEXT_EDICT(sv.edicts); for (i = 1;i < sv.num_edicts;i++, ent = NEXT_EDICT(ent)) { + pr_xfunction->builtinsprofile++; if (ent->free) continue; t = E_STRING(ent,f); @@ -1207,6 +1220,7 @@ void PF_findchainfloat (void) ent = NEXT_EDICT(sv.edicts); for (i = 1;i < sv.num_edicts;i++, ent = NEXT_EDICT(ent)) { + pr_xfunction->builtinsprofile++; if (ent->free) continue; if (E_FLOAT(ent,f) != s) @@ -1316,7 +1330,7 @@ void PF_walkmove (void) edict_t *ent; float yaw, dist; vec3_t move; - dfunction_t *oldf; + mfunction_t *oldf; int oldself; ent = PROG_TO_EDICT(pr_global_struct->self); @@ -1468,6 +1482,7 @@ void PF_nextent (void) i = G_EDICTNUM(OFS_PARM0); while (1) { + pr_xfunction->builtinsprofile++; i++; if (i == sv.num_edicts) { @@ -1526,6 +1541,7 @@ void PF_aim (void) check = NEXT_EDICT(sv.edicts); for (i=1 ; ibuiltinsprofile++; if (check->v->takedamage != DAMAGE_AIM) continue; if (check == ent) @@ -2027,14 +2043,14 @@ void PF_copyentity (void) /* ================= -PF_setcolor +PF_setcolors sets the color of a client and broadcasts the update to all connected clients -setcolor(clientent, value) +setcolors(clientent, value) ================= */ -void PF_setcolor (void) +void PF_setcolors (void) { client_t *client; int entnum, i; @@ -2751,7 +2767,7 @@ aa // #200 aa // #300 aa // #400 PF_copyentity, // #400 LordHavoc: builtin range (4xx) -PF_setcolor, // #401 +PF_setcolors, // #401 PF_findchain, // #402 PF_findchainfloat, // #403 PF_effect, // #404 diff --git a/pr_comp.h b/pr_comp.h index 87999997..6b35e5c1 100644 --- a/pr_comp.h +++ b/pr_comp.h @@ -143,41 +143,57 @@ typedef struct int first_statement; // negative numbers are builtins int parm_start; int locals; // total ints of parms + locals - + int profile; // runtime - + int s_name; int s_file; // source file defined in - + int numparms; qbyte parm_size[MAX_PARMS]; } dfunction_t; +typedef struct +{ + int first_statement; // negative numbers are builtins + int parm_start; + int locals; // total ints of parms + locals + + int profile; // runtime + int builtinsprofile; // cost of builtin functions called by this function + + int s_name; + int s_file; // source file defined in + + int numparms; + qbyte parm_size[MAX_PARMS]; +} mfunction_t; + #define PROG_VERSION 6 typedef struct { int version; int crc; // check of header file - + int ofs_statements; int numstatements; // statement 0 is an error int ofs_globaldefs; int numglobaldefs; - + int ofs_fielddefs; int numfielddefs; - + int ofs_functions; int numfunctions; // function 0 is an empty - + int ofs_strings; int numstrings; // first string is a null string int ofs_globals; int numglobals; - + int entityfields; } dprograms_t; diff --git a/pr_edict.c b/pr_edict.c index ac1f7835..374a9648 100644 --- a/pr_edict.c +++ b/pr_edict.c @@ -22,7 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" dprograms_t *progs; -dfunction_t *pr_functions; +mfunction_t *pr_functions; char *pr_strings; ddef_t *pr_fielddefs; ddef_t *pr_globaldefs; @@ -82,7 +82,7 @@ typedef struct { static gefv_cache gefvCache[GEFV_CACHESIZE] = {{NULL, ""}, {NULL, ""}}; ddef_t *ED_FindField (const char *name); -dfunction_t *ED_FindFunction (const char *name); +mfunction_t *ED_FindFunction (const char *name); // LordHavoc: in an effort to eliminate time wasted on GetEdictFieldValue... these are defined as externs in progs.h int eval_gravity; @@ -120,8 +120,8 @@ int eval_pmodel; int eval_punchvector; int eval_viewzoom; -dfunction_t *SV_PlayerPhysicsQC; -dfunction_t *EndFrameQC; +mfunction_t *SV_PlayerPhysicsQC; +mfunction_t *EndFrameQC; int FindFieldOffset(const char *field) { @@ -337,9 +337,9 @@ ddef_t *ED_FindGlobal (const char *name) ED_FindFunction ============ */ -dfunction_t *ED_FindFunction (const char *name) +mfunction_t *ED_FindFunction (const char *name) { - dfunction_t *func; + mfunction_t *func; int i; for (i=0 ; inumfunctions ; i++) @@ -364,7 +364,7 @@ char *PR_ValueString (etype_t type, eval_t *val) { static char line[1024]; // LordHavoc: enlarged a bit (was 256) ddef_t *def; - dfunction_t *f; + mfunction_t *f; int n; type &= ~DEF_SAVEGLOBAL; @@ -425,7 +425,7 @@ char *PR_UglyValueString (etype_t type, eval_t *val) int i; char *s; ddef_t *def; - dfunction_t *f; + mfunction_t *f; type &= ~DEF_SAVEGLOBAL; @@ -868,7 +868,7 @@ qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s) ddef_t *def; char *v, *w; void *d; - dfunction_t *func; + mfunction_t *func; d = (void *)((int *)base + key->ofs); @@ -1042,7 +1042,7 @@ void ED_LoadFromFile (const char *data) { edict_t *ent; int inhibit; - dfunction_t *func; + mfunction_t *func; ent = NULL; inhibit = 0; @@ -1172,6 +1172,7 @@ void PR_LoadProgs (void) dstatement_t *st; ddef_t *infielddefs; void *temp; + dfunction_t *dfunctions; // flush the non-C variable lookup cache for (i=0 ; icrc != PROGHEADER_CRC) Host_Error ("progs.dat system vars have been modified, progdefs.h is out of date"); - pr_functions = (dfunction_t *)((qbyte *)progs + progs->ofs_functions); + //pr_functions = (dfunction_t *)((qbyte *)progs + progs->ofs_functions); + dfunctions = (dfunction_t *)((qbyte *)progs + progs->ofs_functions); pr_strings = (char *)progs + progs->ofs_strings; pr_globaldefs = (ddef_t *)((qbyte *)progs + progs->ofs_globaldefs); @@ -1227,14 +1229,16 @@ void PR_LoadProgs (void) pr_statements[i].c = LittleShort(pr_statements[i].c); } - for (i=0 ; inumfunctions; i++) + pr_functions = Mem_Alloc(progs_mempool, sizeof(mfunction_t) * progs->numfunctions); + for (i = 0;i < progs->numfunctions;i++) { - pr_functions[i].first_statement = LittleLong (pr_functions[i].first_statement); - pr_functions[i].parm_start = LittleLong (pr_functions[i].parm_start); - pr_functions[i].s_name = LittleLong (pr_functions[i].s_name); - pr_functions[i].s_file = LittleLong (pr_functions[i].s_file); - pr_functions[i].numparms = LittleLong (pr_functions[i].numparms); - pr_functions[i].locals = LittleLong (pr_functions[i].locals); + pr_functions[i].first_statement = LittleLong (dfunctions[i].first_statement); + pr_functions[i].parm_start = LittleLong (dfunctions[i].parm_start); + pr_functions[i].s_name = LittleLong (dfunctions[i].s_name); + pr_functions[i].s_file = LittleLong (dfunctions[i].s_file); + pr_functions[i].numparms = LittleLong (dfunctions[i].numparms); + pr_functions[i].locals = LittleLong (dfunctions[i].locals); + memcpy(pr_functions[i].parm_size, dfunctions[i].parm_size, sizeof(dfunctions[i].parm_size)); } for (i=0 ; inumglobaldefs ; i++) diff --git a/pr_exec.c b/pr_exec.c index 640140f2..e0421bd9 100644 --- a/pr_exec.c +++ b/pr_exec.c @@ -24,7 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. typedef struct { int s; - dfunction_t *f; + mfunction_t *f; } prstack_t; #define MAX_STACK_DEPTH 256 @@ -37,7 +37,7 @@ int localstack_used; int pr_trace; -dfunction_t *pr_xfunction; +mfunction_t *pr_xfunction; int pr_xstatement; @@ -185,7 +185,7 @@ PR_StackTrace */ void PR_StackTrace (void) { - dfunction_t *f; + mfunction_t *f; int i; pr_stack[pr_depth].s = pr_xstatement; @@ -210,11 +210,12 @@ PR_Profile_f */ void PR_Profile_f (void) { - dfunction_t *f, *best; - int max; - int num; - int i; + mfunction_t *f, *best; + int i, num, max/*, howmany*/; + //howmany = 10; + //if (Cmd_Argc() == 2) + // howmany = atoi(Cmd_Argv(1)); num = 0; do { @@ -231,10 +232,11 @@ void PR_Profile_f (void) } if (best) { - if (num < 10) - Con_Printf ("%7i %s\n", best->profile, PR_GetString(best->s_name)); + //if (num < howmany) + Con_Printf ("%7i %7i %s\n", best->profile, best->builtinsprofile, PR_GetString(best->s_name)); num++; best->profile = 0; + best->builtinsprofile = 0; } } while (best); } @@ -282,7 +284,7 @@ PR_EnterFunction Returns the new program statement counter ==================== */ -int PR_EnterFunction (dfunction_t *f) +int PR_EnterFunction (mfunction_t *f) { int i, j, c, o; @@ -372,7 +374,7 @@ extern cvar_t pr_traceqc; void PR_ExecuteProgram (func_t fnum, const char *errormessage) { dstatement_t *st; - dfunction_t *f, *newf; + mfunction_t *f, *newf; edict_t *ed; eval_t *ptr; int profile, startprofile, cachedpr_trace, exitdepth; diff --git a/pr_execprogram.h b/pr_execprogram.h index f421e22c..aef70e84 100644 --- a/pr_execprogram.h +++ b/pr_execprogram.h @@ -255,6 +255,7 @@ // negative statements are built in functions if ((-newf->first_statement) >= pr_numbuiltins) Host_Error ("Bad builtin call number"); + pr_xfunction->builtinsprofile++; pr_builtins[-newf->first_statement] (); } else diff --git a/progs.h b/progs.h index a8a5f3b7..1438dce2 100644 --- a/progs.h +++ b/progs.h @@ -108,13 +108,13 @@ extern int eval_viewzoom; #define GETEDICTFIELDVALUE(ed, fieldoffset) (fieldoffset ? (eval_t *)((qbyte *)ed->v + fieldoffset) : NULL) -extern dfunction_t *SV_PlayerPhysicsQC; -extern dfunction_t *EndFrameQC; +extern mfunction_t *SV_PlayerPhysicsQC; +extern mfunction_t *EndFrameQC; //============================================================================ extern dprograms_t *progs; -extern dfunction_t *pr_functions; +extern mfunction_t *pr_functions; extern char *pr_strings; extern ddef_t *pr_globaldefs; extern ddef_t *pr_fielddefs; @@ -186,7 +186,7 @@ extern int pr_numbuiltins; extern int pr_argc; extern int pr_trace; -extern dfunction_t *pr_xfunction; +extern mfunction_t *pr_xfunction; extern int pr_xstatement; extern unsigned short pr_crc; -- 2.39.2