From 5351bbec71c50b59aed3baf3d120cdf907874d8f Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 7 Jul 2008 06:24:54 +0000 Subject: [PATCH] more api for core sets profiler+debugger now --HG-- branch : refac-jit extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/branches/refac-jit%402376 --- public/IPluginSys.h | 19 ++++++++---- public/sourcepawn/sp_vm_api.h | 43 ++++++++++++++++++++++++---- sourcepawn/jit/BaseRuntime.cpp | 18 ++++++++++++ sourcepawn/jit/BaseRuntime.h | 1 + sourcepawn/jit/engine2.cpp | 6 ++++ sourcepawn/jit/engine2.h | 1 + sourcepawn/jit/jit_shared.h | 3 ++ sourcepawn/jit/sp_vm_basecontext.cpp | 23 +++++++++++++++ sourcepawn/jit/sp_vm_basecontext.h | 4 +++ sourcepawn/jit/sp_vm_engine.cpp | 2 +- sourcepawn/jit/sp_vm_engine.h | 1 + sourcepawn/jit/x86/jit_x86.cpp | 10 +++++++ 12 files changed, 119 insertions(+), 12 deletions(-) diff --git a/public/IPluginSys.h b/public/IPluginSys.h index c6dd03138..6c0d9d46c 100644 --- a/public/IPluginSys.h +++ b/public/IPluginSys.h @@ -109,25 +109,25 @@ namespace SourceMod virtual PluginType GetType() =0; /** - * @brief Returns the current API context being used in the plugin. + * @brief Returns the IPluginRuntime::GetDefaultContext() value. * * @return Pointer to an IPluginContext, or NULL if not loaded. */ virtual SourcePawn::IPluginContext *GetBaseContext() =0; /** - * @brief Returns the context structure being used in the plugin. + * @brief Deprecated, returns NULL. * - * @return Pointer to an sp_context_t, or NULL if not loaded. + * @return NULL. */ virtual sp_context_t *GetContext() =0; /** - * @brief Returns the plugin file structure. + * @brief Deprecated, returns NULL. * - * @return Pointer to an sp_plugin_t, or NULL if not loaded. + * @return NULL. */ - virtual const sp_plugin_t *GetPluginStructure() =0; + virtual void *GetPluginStructure() =0; /** * @brief Returns information about the plugin by reference. @@ -189,6 +189,13 @@ namespace SourceMod * @return True if the property existed, false otherwise. */ virtual bool GetProperty(const char *prop, void **ptr, bool remove=false) =0; + + /** + * @brief Returns the runtime representing this plugin. + * + * @return IPluginRuntime pointer, or NULL if not loaded. + */ + virtual SourcePawn::IPluginRuntime *GetRuntime() =0; }; diff --git a/public/sourcepawn/sp_vm_api.h b/public/sourcepawn/sp_vm_api.h index ad56dfb59..a86575bec 100644 --- a/public/sourcepawn/sp_vm_api.h +++ b/public/sourcepawn/sp_vm_api.h @@ -234,7 +234,7 @@ namespace SourcePawn * @brief Executes the forward, resets the pushed parameter list, and * performs any copybacks. * - * Note: A function can only be executed given a context it was created in. + * Note: A function can only be executed given a runtime it was created in. * * @param ctx Context to execute the function in. * @param result Pointer to store return value in. @@ -248,7 +248,7 @@ namespace SourcePawn * NOTE: You will get an error if you attempt to use CallFunction() with * previously pushed parameters. * - * Note: A function can only be executed given a context it was created in. + * Note: A function can only be executed given a runtime it was created in. * * @param ctx Context to execute the function in. * @param params Array of cell parameters. @@ -466,6 +466,13 @@ namespace SourcePawn * @return Pause state (true = paused, false = not). */ virtual bool IsPaused() =0; + + /** + * @brief Returns the estimated memory usage of this plugin. + * + * @return Memory usage, in bytes. + */ + virtual size_t GetMemUsage() =0; }; /** @@ -765,10 +772,10 @@ namespace SourcePawn */ virtual IPluginFunction *GetFunctionById(funcid_t func_id) =0; -#if defined SOURCEMOD_BUILD /** * @brief Returns the identity token for this context. - * Note: This is a helper function for native calls and the Handle System. + * + * Note: This is a compatibility shim and is the same as GetKey(1). * * @return Identity token. */ @@ -791,7 +798,6 @@ namespace SourcePawn * @param addr Destination output pointer. */ virtual int LocalToStringNULL(cell_t local_addr, char **addr) =0; -#endif /** * @brief Deprecated; do not use. @@ -849,6 +855,25 @@ namespace SourcePawn * @return Parameter stack. */ virtual cell_t *GetLocalParams() =0; + + /** + * @brief Sets a local "key" that can be used for fast lookup. + * + * Only the "owner" of the context should be setting this. + * + * @param key Key number (values allowed: 1 through 4). + * @param value Pointer value. + */ + virtual void SetKey(int k, void *value) =0; + + /** + * @brief Retrieves a previously set "key." + * + * @param key Key number (values allowed: 1 through 4). + * @param value Pointer to store value. + * @return True on success, false on failure. + */ + virtual bool GetKey(int k, void **value) =0; }; @@ -1190,6 +1215,14 @@ namespace SourcePawn * @param profiler Profiler pointer. */ virtual void SetProfiler(IProfiler *profiler) =0; + + /** + * @brief Returns the string representation of an error message. + * + * @param err Error code. + * @return Error string, or NULL if not found. + */ + virtual const char *GetErrorString(int err) =0; }; }; diff --git a/sourcepawn/jit/BaseRuntime.cpp b/sourcepawn/jit/BaseRuntime.cpp index 86a8fd36a..ace6223ed 100644 --- a/sourcepawn/jit/BaseRuntime.cpp +++ b/sourcepawn/jit/BaseRuntime.cpp @@ -5,6 +5,7 @@ #include "sp_vm_engine.h" #include "x86/jit_x86.h" #include "sp_vm_basecontext.h" +#include "engine2.h" using namespace SourcePawn; @@ -28,6 +29,9 @@ BaseRuntime::BaseRuntime(sp_plugin_t *pl) : m_Debug(pl), m_pPlugin(pl) { m_PubFuncs = NULL; } + + m_pPlugin->dbreak = GlobalDebugBreak; + m_pPlugin->profiler = g_engine2.GetProfiler(); } BaseRuntime::~BaseRuntime() @@ -331,3 +335,17 @@ bool BaseRuntime::IsPaused() { return ((m_pPlugin->run_flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED); } + +size_t BaseRuntime::GetMemUsage() +{ + size_t mem = 0; + + mem += sizeof(this); + mem += sizeof(sp_plugin_t); + mem += sizeof(BaseContext); + mem += m_pPlugin->base_size; + mem += m_pPlugin->jit_codesize; + mem += m_pPlugin->jit_memsize; + + return mem; +} diff --git a/sourcepawn/jit/BaseRuntime.h b/sourcepawn/jit/BaseRuntime.h index 38527987a..fc90b4ebc 100644 --- a/sourcepawn/jit/BaseRuntime.h +++ b/sourcepawn/jit/BaseRuntime.h @@ -44,6 +44,7 @@ public: virtual int ApplyCompilationOptions(ICompilation *co); virtual void SetPauseState(bool paused); virtual bool IsPaused(); + virtual size_t GetMemUsage(); private: void ClearCompile(); void RefreshFunctionCache(); diff --git a/sourcepawn/jit/engine2.cpp b/sourcepawn/jit/engine2.cpp index 8626fece4..e9e0b941f 100644 --- a/sourcepawn/jit/engine2.cpp +++ b/sourcepawn/jit/engine2.cpp @@ -195,6 +195,7 @@ IPluginRuntime *SourcePawnEngine2::LoadPlugin(ICompilation *co, const char *file memset(plugin, 0, sizeof(sp_plugin_t)); + plugin->base_size = hdr.imagesize; if (!_ReadPlugin(&hdr, base, plugin, err)) { delete plugin; @@ -269,3 +270,8 @@ ICompilation *SourcePawnEngine2::StartCompilation() { return g_Jit1.StartCompilation(); } + +const char *SourcePawnEngine2::GetErrorString(int err) +{ + return g_engine1.GetErrorString(err); +} diff --git a/sourcepawn/jit/engine2.h b/sourcepawn/jit/engine2.h index 7a92e0aaf..09de29d56 100644 --- a/sourcepawn/jit/engine2.h +++ b/sourcepawn/jit/engine2.h @@ -22,6 +22,7 @@ namespace SourcePawn IDebugListener *SetDebugListener(IDebugListener *listener); void SetProfiler(IProfiler *profiler); ICompilation *StartCompilation(); + const char *GetErrorString(int err); public: IProfiler *GetProfiler(); private: diff --git a/sourcepawn/jit/jit_shared.h b/sourcepawn/jit/jit_shared.h index e4b92e39b..11f4f5732 100644 --- a/sourcepawn/jit/jit_shared.h +++ b/sourcepawn/jit/jit_shared.h @@ -61,6 +61,7 @@ namespace SourcePawn uint16_t flags; /**< Code flags */ sp_plugin_infotab_t info; /**< Base info table */ sp_plugin_debug_t debug; /**< Debug info table */ + size_t base_size; /**< Size of the entire plugin base */ void *codebase; /**< Base of generated code and memory */ SPVM_DEBUGBREAK dbreak; /**< Debug break function */ uint8_t *memory; /**< Data chunk */ @@ -73,6 +74,8 @@ namespace SourcePawn IProfiler *profiler; /**< Pointer to IProfiler */ uint32_t prof_flags; /**< Profiling flags */ uint32_t run_flags; /**< Runtime flags */ + size_t jit_codesize; /**< JIT compiled codesize */ + size_t jit_memsize; /**< JIT additional memory */ } sp_plugin_t; } diff --git a/sourcepawn/jit/sp_vm_basecontext.cpp b/sourcepawn/jit/sp_vm_basecontext.cpp index 7bd0b003b..eee58a7ff 100644 --- a/sourcepawn/jit/sp_vm_basecontext.cpp +++ b/sourcepawn/jit/sp_vm_basecontext.cpp @@ -769,3 +769,26 @@ cell_t *BaseContext::GetLocalParams() { return (cell_t *)(m_pPlugin->memory + m_ctx.frm + (2 * sizeof(cell_t))); } + +void BaseContext::SetKey(int k, void *value) +{ + if (k < 1 || k > 4) + { + return; + } + + m_keys[k - 1] = value; + m_keys_set[k - 1] = true; +} + +bool BaseContext::GetKey(int k, void **value) +{ + if (k < 1 || k > 4 || m_keys_set[k - 1] == false) + { + return false; + } + + *value = m_keys[k - 1]; + + return true; +} diff --git a/sourcepawn/jit/sp_vm_basecontext.h b/sourcepawn/jit/sp_vm_basecontext.h index d8e50e22f..d586e4109 100644 --- a/sourcepawn/jit/sp_vm_basecontext.h +++ b/sourcepawn/jit/sp_vm_basecontext.h @@ -90,6 +90,8 @@ public: //IPluginContext IPluginRuntime *GetRuntime(); int GetLastNativeError(); cell_t *GetLocalParams(); + void SetKey(int k, void *value); + bool GetKey(int k, void **value); public: bool IsInExec(); private: @@ -105,6 +107,8 @@ private: bool m_InExec; BaseRuntime *m_pRuntime; sp_context_t m_ctx; + void *m_keys[4]; + bool m_keys_set[4]; }; #endif //_INCLUDE_SOURCEPAWN_BASECONTEXT_H_ diff --git a/sourcepawn/jit/sp_vm_engine.cpp b/sourcepawn/jit/sp_vm_engine.cpp index 226b60990..9191d9fbc 100644 --- a/sourcepawn/jit/sp_vm_engine.cpp +++ b/sourcepawn/jit/sp_vm_engine.cpp @@ -94,7 +94,7 @@ static const char *g_ErrorMsgTable[] = "Call was aborted", }; -const char *GetSourcePawnErrorMessage(int error) +const char *SourcePawnEngine::GetErrorString(int error) { if (error < 1 || error > ERROR_MESSAGE_MAX) { diff --git a/sourcepawn/jit/sp_vm_engine.h b/sourcepawn/jit/sp_vm_engine.h index 137f1c1e3..08e5c79a6 100644 --- a/sourcepawn/jit/sp_vm_engine.h +++ b/sourcepawn/jit/sp_vm_engine.h @@ -86,6 +86,7 @@ public: //ISourcePawnEngine void SetReadWrite(void *ptr); void SetReadExecute(void *ptr); void FreePageMemory(void *ptr); + const char *GetErrorString(int err); public: //Debugger Stuff /** * @brief Pushes a context onto the top of the call tracer. diff --git a/sourcepawn/jit/x86/jit_x86.cpp b/sourcepawn/jit/x86/jit_x86.cpp index bb35c2dd0..9ac62866e 100644 --- a/sourcepawn/jit/x86/jit_x86.cpp +++ b/sourcepawn/jit/x86/jit_x86.cpp @@ -2673,6 +2673,8 @@ jit_rewind: } plugin->codebase = writer.outbase; + plugin->jit_codesize = codemem; + plugin->jit_memsize = 0; /* setup memory */ @@ -2691,6 +2693,7 @@ jit_rewind: if ((max = plugin->info.publics_num)) { plugin->publics = new sp_public_t[max]; + plugin->jit_memsize += sizeof(sp_public_t) * max; for (iter=0; iterpublics[iter].name = strbase + plugin->info.publics[iter].name; @@ -2705,6 +2708,7 @@ jit_rewind: { uint8_t *dat = plugin->memory; plugin->pubvars = new sp_pubvar_t[max]; + plugin->jit_memsize += sizeof(sp_pubvar_t) * max; for (iter=0; iterpubvars[iter].name = strbase + plugin->info.pubvars[iter].name; @@ -2716,6 +2720,7 @@ jit_rewind: if ((max = plugin->info.natives_num)) { plugin->natives = new sp_native_t[max]; + plugin->jit_memsize += sizeof(sp_native_t) * max; for (iter=0; iternatives[iter].name = strbase + plugin->info.natives[iter].name; @@ -2736,6 +2741,7 @@ jit_rewind: /* relocate files */ max = plugin->debug.files_num; plugin->files = new sp_debug_file_t[max]; + plugin->jit_memsize += sizeof(sp_debug_file_t) * max; for (iter=0; iterfiles[iter].addr = RelocLookup(jit, plugin->debug.files[iter].addr, false); @@ -2745,6 +2751,7 @@ jit_rewind: /* relocate lines */ max = plugin->debug.lines_num; plugin->lines = new sp_debug_line_t[max]; + plugin->jit_memsize += sizeof(sp_debug_line_t) * max; for (iter=0; iterlines[iter].addr = RelocLookup(jit, plugin->debug.lines[iter].addr, false); @@ -2758,6 +2765,7 @@ jit_rewind: max = plugin->debug.syms_num; plugin->symbols = new sp_debug_symbol_t[max]; + plugin->jit_memsize += sizeof(sp_debug_symbol_t) * max; for (iter=0; iterpCur = trk->pBase; trk->size = 1024 / sizeof(cell_t); + plugin->jit_memsize += trk->size; + /* clean up relocation+compilation memory */ co->Abort();