Moved profiler and adt_trie to logic binary.
--HG-- rename : core/Profiler.cpp => core/logic/Profiler.cpp rename : core/Profiler.h => core/logic/Profiler.h rename : core/smn_adt_trie.cpp => core/logic/smn_adt_trie.cpp
This commit is contained in:
+3
-1
@@ -22,7 +22,9 @@ OBJECTS = \
|
||||
ThreadSupport.cpp \
|
||||
smn_float.cpp \
|
||||
TextParsers.cpp \
|
||||
smn_textparse.cpp
|
||||
smn_textparse.cpp \
|
||||
smn_adt_trie.cpp \
|
||||
Profiler.cpp
|
||||
|
||||
##############################################
|
||||
### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ###
|
||||
|
||||
@@ -0,0 +1,486 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* =============================================================================
|
||||
* SourceMod
|
||||
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||
* =============================================================================
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||
* Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||
* this exception to all derivative works. AlliedModders LLC defines further
|
||||
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||
* or <http://www.sourcemod.net/license.php>.
|
||||
*
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#include "Profiler.h"
|
||||
#include <ISourceMod.h>
|
||||
#if defined PLATFORM_POSIX
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
#include <IPluginSys.h>
|
||||
|
||||
ProfileEngine g_Profiler;
|
||||
IProfiler *sm_profiler = &g_Profiler;
|
||||
|
||||
#if defined PLATFORM_WINDOWS
|
||||
double WINDOWS_PERFORMANCE_FREQUENCY;
|
||||
#endif
|
||||
|
||||
class EmptyProfiler : public IProfiler
|
||||
{
|
||||
public:
|
||||
void OnNativeBegin(IPluginContext *pContext, sp_native_t *native)
|
||||
{
|
||||
}
|
||||
void OnNativeEnd()
|
||||
{
|
||||
}
|
||||
void OnFunctionBegin(IPluginContext *pContext, const char *name)
|
||||
{
|
||||
}
|
||||
void OnFunctionEnd()
|
||||
{
|
||||
}
|
||||
int OnCallbackBegin(IPluginContext *pContext, sp_public_t *pubfunc)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
void OnCallbackEnd(int serial)
|
||||
{
|
||||
}
|
||||
} s_EmptyProfiler;
|
||||
|
||||
inline void InitProfPoint(prof_point_t &pt)
|
||||
{
|
||||
#if defined PLATFORM_WINDOWS
|
||||
QueryPerformanceCounter(&pt.value);
|
||||
#elif defined PLATFORM_POSIX
|
||||
gettimeofday(&pt.value, NULL);
|
||||
#endif
|
||||
pt.is_set = true;
|
||||
}
|
||||
|
||||
ProfileEngine::ProfileEngine()
|
||||
{
|
||||
m_serial = 0;
|
||||
|
||||
#if defined PLATFORM_WINDOWS
|
||||
LARGE_INTEGER pf;
|
||||
|
||||
if (QueryPerformanceFrequency(&pf))
|
||||
{
|
||||
WINDOWS_PERFORMANCE_FREQUENCY = 1.0 / (double)(pf.QuadPart);
|
||||
}
|
||||
else
|
||||
{
|
||||
WINDOWS_PERFORMANCE_FREQUENCY = -1.0;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (IsEnabled())
|
||||
{
|
||||
InitProfPoint(m_ProfStart);
|
||||
}
|
||||
else
|
||||
{
|
||||
sm_profiler = &s_EmptyProfiler;
|
||||
}
|
||||
}
|
||||
|
||||
bool ProfileEngine::IsEnabled()
|
||||
{
|
||||
#if defined PLATFORM_WINDOWS
|
||||
return (WINDOWS_PERFORMANCE_FREQUENCY > 0.0);
|
||||
#elif defined PLATFORM_POSIX
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline double DiffProfPoints(const prof_point_t &start, const prof_point_t &end)
|
||||
{
|
||||
double seconds;
|
||||
|
||||
#if defined PLATFORM_WINDOWS
|
||||
LONGLONG diff;
|
||||
|
||||
diff = end.value.QuadPart - start.value.QuadPart;
|
||||
seconds = diff * WINDOWS_PERFORMANCE_FREQUENCY;
|
||||
#elif defined PLATFORM_POSIX
|
||||
seconds = (double)(end.value.tv_sec - start.value.tv_sec);
|
||||
|
||||
if (start.value.tv_usec > end.value.tv_usec)
|
||||
{
|
||||
seconds -= 1.0;
|
||||
seconds += (double)(1000000 - (start.value.tv_usec - end.value.tv_usec)) / 1000000.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
seconds += (double)(end.value.tv_usec - start.value.tv_usec) / 1000000.0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return seconds;
|
||||
}
|
||||
|
||||
inline double CalcAtomTime(const prof_atom_t &atom)
|
||||
{
|
||||
if (!atom.end.is_set)
|
||||
{
|
||||
return atom.base_time;
|
||||
}
|
||||
|
||||
return atom.base_time + DiffProfPoints(atom.start, atom.end);
|
||||
}
|
||||
|
||||
void ProfileEngine::OnNativeBegin(IPluginContext *pContext, sp_native_t *native)
|
||||
{
|
||||
PushProfileStack(pContext, SP_PROF_NATIVES, native->name);
|
||||
}
|
||||
|
||||
void ProfileEngine::OnNativeEnd()
|
||||
{
|
||||
assert(!m_AtomStack.empty());
|
||||
assert(m_AtomStack.front().atom_type == SP_PROF_NATIVES);
|
||||
|
||||
PopProfileStack(&m_Natives);
|
||||
}
|
||||
|
||||
void ProfileEngine::OnFunctionBegin(IPluginContext *pContext, const char *name)
|
||||
{
|
||||
PushProfileStack(pContext, SP_PROF_FUNCTIONS, name);
|
||||
}
|
||||
|
||||
void ProfileEngine::OnFunctionEnd()
|
||||
{
|
||||
assert(!m_AtomStack.empty());
|
||||
assert(m_AtomStack.front().atom_type == SP_PROF_FUNCTIONS);
|
||||
|
||||
PopProfileStack(&m_Functions);
|
||||
}
|
||||
|
||||
int ProfileEngine::OnCallbackBegin(IPluginContext *pContext, sp_public_t *pubfunc)
|
||||
{
|
||||
PushProfileStack(pContext, SP_PROF_CALLBACKS, pubfunc->name);
|
||||
|
||||
return m_serial;
|
||||
}
|
||||
|
||||
void ProfileEngine::OnCallbackEnd(int serial)
|
||||
{
|
||||
assert(!m_AtomStack.empty());
|
||||
|
||||
/**
|
||||
* Account for the situation where the JIT discards the
|
||||
* stack because there was an RTE of sorts.
|
||||
*/
|
||||
if (m_AtomStack.front().atom_type != SP_PROF_CALLBACKS
|
||||
&& m_AtomStack.front().atom_serial != serial)
|
||||
{
|
||||
prof_atom_t atom;
|
||||
double total_time;
|
||||
|
||||
/* There was an error, and we need to discard things. */
|
||||
total_time = 0.0;
|
||||
while (!m_AtomStack.empty()
|
||||
&& m_AtomStack.front().atom_type != SP_PROF_CALLBACKS
|
||||
&& m_AtomStack.front().atom_serial != serial)
|
||||
{
|
||||
total_time += CalcAtomTime(m_AtomStack.front());
|
||||
m_AtomStack.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Now we can end and discard ourselves, without saving the data.
|
||||
* Since this data is all erroneous anyway, we don't care if it's
|
||||
* not totally accurate.
|
||||
*/
|
||||
|
||||
assert(!m_AtomStack.empty());
|
||||
atom = m_AtomStack.front();
|
||||
m_AtomStack.pop();
|
||||
|
||||
/* Note: We don't need to resume ourselves because end is set by Pause(). */
|
||||
total_time += CalcAtomTime(atom);
|
||||
|
||||
ResumeParent(total_time);
|
||||
return;
|
||||
}
|
||||
|
||||
PopProfileStack(&m_Callbacks);
|
||||
}
|
||||
|
||||
void ProfileEngine::PushProfileStack(IPluginContext *ctx, int type, const char *name)
|
||||
{
|
||||
prof_atom_t atom;
|
||||
|
||||
PauseParent();
|
||||
|
||||
atom.atom_type = type;
|
||||
atom.base_time = 0.0;
|
||||
atom.ctx = ctx->GetContext();
|
||||
atom.name = name;
|
||||
atom.end.is_set = false;
|
||||
|
||||
if (type == SP_PROF_CALLBACKS)
|
||||
{
|
||||
atom.atom_serial = ++m_serial;
|
||||
}
|
||||
else
|
||||
{
|
||||
atom.atom_serial = 0;
|
||||
}
|
||||
|
||||
m_AtomStack.push(atom);
|
||||
|
||||
/* Note: We do this after because the stack could grow and skew results */
|
||||
InitProfPoint(m_AtomStack.front().start);
|
||||
}
|
||||
|
||||
void ProfileEngine::PopProfileStack(ProfileReport *reporter)
|
||||
{
|
||||
double total_time;
|
||||
|
||||
prof_atom_t &atom = m_AtomStack.front();
|
||||
|
||||
/* We're okay to cache our used time. */
|
||||
InitProfPoint(atom.end);
|
||||
total_time = CalcAtomTime(atom);
|
||||
|
||||
/* Now it's time to save this! This may do a lot of computations which
|
||||
* is why we've cached the time beforehand.
|
||||
*/
|
||||
reporter->SaveAtom(atom);
|
||||
m_AtomStack.pop();
|
||||
|
||||
/* Finally, tell our parent how much time we used. */
|
||||
ResumeParent(total_time);
|
||||
}
|
||||
|
||||
void ProfileEngine::PauseParent()
|
||||
{
|
||||
if (m_AtomStack.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
InitProfPoint(m_AtomStack.front().end);
|
||||
}
|
||||
|
||||
void ProfileEngine::ResumeParent(double addTime)
|
||||
{
|
||||
if (m_AtomStack.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
prof_atom_t &atom = m_AtomStack.front();
|
||||
|
||||
/* Move its "paused time" to its base (known) time,
|
||||
* then reset the start/end. Note that since CalcAtomTime()
|
||||
* reads the base time, we SHOULD NOT use += to add.
|
||||
*/
|
||||
atom.base_time = CalcAtomTime(atom);
|
||||
atom.base_time += addTime;
|
||||
InitProfPoint(atom.start);
|
||||
atom.end.is_set = false;
|
||||
}
|
||||
|
||||
void ProfileEngine::Clear()
|
||||
{
|
||||
m_Natives.Clear();
|
||||
m_Callbacks.Clear();
|
||||
m_Functions.Clear();
|
||||
InitProfPoint(m_ProfStart);
|
||||
}
|
||||
|
||||
void ProfileEngine::OnSourceModAllInitialized()
|
||||
{
|
||||
rootmenu->AddRootConsoleCommand2("profiler", "Profiler commands", this);
|
||||
}
|
||||
|
||||
void ProfileEngine::OnSourceModShutdown()
|
||||
{
|
||||
rootmenu->RemoveRootConsoleCommand("profiler", this);
|
||||
}
|
||||
|
||||
void ProfileEngine::OnRootConsoleCommand2(const char *cmdname, const ICommandArgs *command)
|
||||
{
|
||||
if (command->ArgC() >= 3)
|
||||
{
|
||||
if (strcmp(command->Arg(2), "flush") == 0)
|
||||
{
|
||||
FILE *fp;
|
||||
char path[256];
|
||||
|
||||
g_pSM->BuildPath(Path_SM, path, sizeof(path), "logs/profile_%d.xml", (int)time(NULL));
|
||||
|
||||
if ((fp = fopen(path, "wt")) == NULL)
|
||||
{
|
||||
rootmenu->ConsolePrint("Failed, could not open file for writing: %s", path);
|
||||
return;
|
||||
}
|
||||
|
||||
GenerateReport(fp);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
rootmenu->ConsolePrint("Profiler report generated as: %s\n", path);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
rootmenu->ConsolePrint("Profiler commands:");
|
||||
rootmenu->DrawGenericOption("flush", "Flushes statistics to disk and starts over");
|
||||
}
|
||||
|
||||
bool ProfileEngine::GenerateReport(FILE *fp)
|
||||
{
|
||||
time_t t;
|
||||
double total_time;
|
||||
prof_point_t end_time;
|
||||
|
||||
InitProfPoint(end_time);
|
||||
total_time = DiffProfPoints(m_ProfStart, end_time);
|
||||
|
||||
t = time(NULL);
|
||||
|
||||
fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
|
||||
fprintf(fp, "<profile time=\"%d\" uptime=\"%f\">\n", (int)t, total_time);
|
||||
WriteReport(fp, &m_Natives, "natives");
|
||||
WriteReport(fp, &m_Callbacks, "callbacks");
|
||||
WriteReport(fp, &m_Functions, "functions");
|
||||
fprintf(fp, "</profile>\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ProfileEngine::WriteReport(FILE *fp, ProfileReport *report, const char *name)
|
||||
{
|
||||
size_t i, num;
|
||||
prof_atom_report_t *ar;
|
||||
char new_name[512];
|
||||
|
||||
fprintf(fp, " <report name=\"%s\">\n", name);
|
||||
|
||||
num = report->GetNumReports();
|
||||
for (i = 0; i < num; i++)
|
||||
{
|
||||
ar = report->GetReport(i);
|
||||
|
||||
smcore.strncopy(new_name, ar->atom_name, sizeof(new_name));
|
||||
smcore.ReplaceAll(new_name, sizeof(new_name), "<", "<", true);
|
||||
smcore.ReplaceAll(new_name, sizeof(new_name), ">", ">", true);
|
||||
|
||||
fprintf(fp, " <item name=\"%s\" numcalls=\"%d\" mintime=\"%f\" maxtime=\"%f\" totaltime=\"%f\"/>\n",
|
||||
new_name,
|
||||
ar->num_calls,
|
||||
ar->min_time,
|
||||
ar->max_time,
|
||||
ar->total_time);
|
||||
}
|
||||
|
||||
fprintf(fp, " </report>\n");
|
||||
}
|
||||
|
||||
ProfileReport::~ProfileReport()
|
||||
{
|
||||
for (size_t i = 0; i < m_Reports.size(); i++)
|
||||
{
|
||||
delete m_Reports[i];
|
||||
}
|
||||
}
|
||||
|
||||
void ProfileReport::Clear()
|
||||
{
|
||||
m_ReportLookup.clear();
|
||||
for (size_t i = 0; i < m_Reports.size(); i++)
|
||||
{
|
||||
delete m_Reports[i];
|
||||
}
|
||||
m_Reports.clear();
|
||||
}
|
||||
|
||||
size_t ProfileReport::GetNumReports()
|
||||
{
|
||||
return m_Reports.size();
|
||||
}
|
||||
|
||||
prof_atom_report_t *ProfileReport::GetReport(size_t i)
|
||||
{
|
||||
return m_Reports[i];
|
||||
}
|
||||
|
||||
void ProfileReport::SaveAtom(const prof_atom_t &atom)
|
||||
{
|
||||
double atom_time;
|
||||
char full_name[256];
|
||||
prof_atom_report_t **pReport, *report;
|
||||
|
||||
if (atom.atom_type == SP_PROF_NATIVES)
|
||||
{
|
||||
smcore.strncopy(full_name, atom.name, sizeof(full_name));
|
||||
}
|
||||
else
|
||||
{
|
||||
IPlugin *pl;
|
||||
const char *file;
|
||||
|
||||
file = "unknown";
|
||||
if ((pl = pluginsys->FindPluginByContext(atom.ctx)) != NULL)
|
||||
{
|
||||
file = pl->GetFilename();
|
||||
}
|
||||
|
||||
smcore.Format(full_name, sizeof(full_name), "%s!%s", file, atom.name);
|
||||
}
|
||||
|
||||
atom_time = CalcAtomTime(atom);
|
||||
|
||||
if ((pReport = m_ReportLookup.retrieve(full_name)) == NULL)
|
||||
{
|
||||
report = new prof_atom_report_t;
|
||||
|
||||
smcore.strncopy(report->atom_name, full_name, sizeof(report->atom_name));
|
||||
report->max_time = atom_time;
|
||||
report->min_time = atom_time;
|
||||
report->num_calls = 1;
|
||||
report->total_time = atom_time;
|
||||
|
||||
m_ReportLookup.insert(full_name, report);
|
||||
m_Reports.push_back(report);
|
||||
}
|
||||
else
|
||||
{
|
||||
report = *pReport;
|
||||
|
||||
if (atom_time > report->max_time)
|
||||
{
|
||||
report->max_time = atom_time;
|
||||
}
|
||||
if (atom_time < report->min_time)
|
||||
{
|
||||
report->min_time = atom_time;
|
||||
}
|
||||
report->num_calls++;
|
||||
report->total_time += atom_time;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* =============================================================================
|
||||
* SourceMod
|
||||
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||
* =============================================================================
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||
* Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||
* this exception to all derivative works. AlliedModders LLC defines further
|
||||
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||
* or <http://www.sourcemod.net/license.php>.
|
||||
*
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#ifndef _INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_
|
||||
#define _INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_
|
||||
|
||||
#include <sp_vm_api.h>
|
||||
#include <sm_platform.h>
|
||||
#include <sm_trie_tpl.h>
|
||||
#include <sh_vector.h>
|
||||
#include <sh_stack.h>
|
||||
#include <stdio.h>
|
||||
#include "common_logic.h"
|
||||
#include <IRootConsoleMenu.h>
|
||||
|
||||
using namespace SourcePawn;
|
||||
using namespace SourceHook;
|
||||
|
||||
struct prof_point_t
|
||||
{
|
||||
#if defined PLATFORM_WINDOWS
|
||||
LARGE_INTEGER value;
|
||||
#elif defined PLATFORM_POSIX
|
||||
struct timeval value;
|
||||
#endif
|
||||
bool is_set;
|
||||
};
|
||||
|
||||
struct prof_atom_t
|
||||
{
|
||||
int atom_type; /* Type of object we're profiling */
|
||||
int atom_serial; /* Serial number, if appropriate */
|
||||
sp_context_t *ctx; /* Plugin context. */
|
||||
const char *name; /* Name of the function */
|
||||
prof_point_t start; /* Start time */
|
||||
prof_point_t end; /* End time */
|
||||
double base_time; /* Known time from children or pausing. */
|
||||
};
|
||||
|
||||
struct prof_atom_report_t
|
||||
{
|
||||
char atom_name[256]; /* Full name to shove to logs */
|
||||
double total_time; /* Total time spent executing, in s */
|
||||
unsigned int num_calls; /* Number of invocations */
|
||||
double min_time; /* Min time spent in one call, in s */
|
||||
double max_time; /* Max time spent in one call, in s */
|
||||
};
|
||||
|
||||
class ProfileReport
|
||||
{
|
||||
public:
|
||||
~ProfileReport();
|
||||
public:
|
||||
void SaveAtom(const prof_atom_t &atom);
|
||||
size_t GetNumReports();
|
||||
prof_atom_report_t *GetReport(size_t i);
|
||||
void Clear();
|
||||
private:
|
||||
KTrie<prof_atom_report_t *> m_ReportLookup;
|
||||
CVector<prof_atom_report_t *> m_Reports;
|
||||
};
|
||||
|
||||
class ProfileEngine :
|
||||
public SMGlobalClass,
|
||||
public IRootConsoleCommand,
|
||||
public IProfiler
|
||||
{
|
||||
public:
|
||||
ProfileEngine();
|
||||
public:
|
||||
bool IsEnabled();
|
||||
bool GenerateReport(FILE *fp);
|
||||
void Clear();
|
||||
public: //SMGlobalClass
|
||||
void OnSourceModAllInitialized();
|
||||
void OnSourceModShutdown();
|
||||
public: //IRootConsoleCommand
|
||||
void OnRootConsoleCommand2(const char *cmdname, const ICommandArgs *command);
|
||||
public: //IProfiler
|
||||
void OnNativeBegin(IPluginContext *pContext, sp_native_t *native);
|
||||
void OnNativeEnd() ;
|
||||
void OnFunctionBegin(IPluginContext *pContext, const char *name);
|
||||
void OnFunctionEnd();
|
||||
int OnCallbackBegin(IPluginContext *pContext, sp_public_t *pubfunc);
|
||||
void OnCallbackEnd(int serial);
|
||||
private:
|
||||
void PushProfileStack(IPluginContext *ctx, int type, const char *name);
|
||||
void PopProfileStack(ProfileReport *reporter);
|
||||
void PauseParent();
|
||||
void ResumeParent(double addTime);
|
||||
void WriteReport(FILE *fp, ProfileReport *report, const char *name);
|
||||
private:
|
||||
CStack<prof_atom_t> m_AtomStack;
|
||||
ProfileReport m_Callbacks;
|
||||
ProfileReport m_Functions;
|
||||
ProfileReport m_Natives;
|
||||
int m_serial;
|
||||
prof_point_t m_ProfStart;
|
||||
};
|
||||
|
||||
extern ProfileEngine g_Profiler;
|
||||
|
||||
#endif //_INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_
|
||||
@@ -45,6 +45,8 @@ ILibrarySys *libsys;
|
||||
ITextParsers *textparser = &g_TextParser;
|
||||
IVEngineServer *engine;
|
||||
IShareSys *sharesys;
|
||||
IRootConsole *rootmenu;
|
||||
IPluginManager *pluginsys;
|
||||
|
||||
static sm_logic_t logic =
|
||||
{
|
||||
@@ -65,6 +67,8 @@ static void logic_init(const sm_core_t* core, sm_logic_t* _logic)
|
||||
g_pCoreIdent = core->core_ident;
|
||||
g_pSM = core->sm;
|
||||
sharesys = core->sharesys;
|
||||
rootmenu = core->rootmenu;
|
||||
pluginsys = core->pluginsys;
|
||||
}
|
||||
|
||||
PLATFORM_EXTERN_C ITextParsers *get_textparsers()
|
||||
|
||||
@@ -44,6 +44,8 @@ extern ILibrarySys *libsys;
|
||||
extern ITextParsers *textparser;
|
||||
extern IVEngineServer *engine;
|
||||
extern IShareSys *sharesys;
|
||||
extern IRootConsole *rootmenu;
|
||||
extern IPluginManager *pluginsys;
|
||||
|
||||
#endif /* _INCLUDE_SOURCEMOD_COMMON_LOGIC_H_ */
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ using namespace SourceMod;
|
||||
* Add 1 to the RHS of this expression to bump the intercom file
|
||||
* This is to prevent mismatching core/logic binaries
|
||||
*/
|
||||
#define SM_LOGIC_MAGIC (0x0F47C0DE - 0)
|
||||
#define SM_LOGIC_MAGIC (0x0F47C0DE - 1)
|
||||
|
||||
#if defined SM_LOGIC
|
||||
class IVEngineServer
|
||||
@@ -60,6 +60,8 @@ namespace SourceMod
|
||||
class ILibrarySys;
|
||||
class ITextParsers;
|
||||
class IThreader;
|
||||
class IRootConsole;
|
||||
class IPluginManager;
|
||||
}
|
||||
|
||||
class IVEngineServer;
|
||||
@@ -74,6 +76,8 @@ struct sm_core_t
|
||||
ILibrarySys *libsys;
|
||||
IVEngineServer *engine;
|
||||
IShareSys *sharesys;
|
||||
IRootConsole *rootmenu;
|
||||
IPluginManager *pluginsys;
|
||||
/* Functions */
|
||||
void (*AddNatives)(sp_nativeinfo_t* nlist);
|
||||
ConVar * (*FindConVar)(const char*);
|
||||
@@ -82,12 +86,14 @@ struct sm_core_t
|
||||
void (*LogError)(const char*, ...);
|
||||
const char * (*GetCvarString)(ConVar*);
|
||||
size_t (*Format)(char*, size_t, const char*, ...);
|
||||
unsigned int (*ReplaceAll)(char*, size_t, const char *, const char *, bool);
|
||||
};
|
||||
|
||||
struct sm_logic_t
|
||||
{
|
||||
SMGlobalClass *head;
|
||||
IThreader *threader;
|
||||
IProfiler *profiler;
|
||||
};
|
||||
|
||||
typedef void (*LogicInitFunction)(const sm_core_t *core, sm_logic_t *logic);
|
||||
|
||||
@@ -0,0 +1,530 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* =============================================================================
|
||||
* SourceMod
|
||||
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||
* =============================================================================
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||
* Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||
* this exception to all derivative works. AlliedModders LLC defines further
|
||||
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||
* or <http://www.sourcemod.net/license.php>.
|
||||
*
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "common_logic.h"
|
||||
#include <sm_trie_tpl.h>
|
||||
|
||||
HandleType_t htCellTrie;
|
||||
|
||||
enum TrieNodeType
|
||||
{
|
||||
TrieNode_Cell,
|
||||
TrieNode_CellArray,
|
||||
TrieNode_String,
|
||||
};
|
||||
|
||||
struct SmartTrieNode
|
||||
{
|
||||
SmartTrieNode()
|
||||
{
|
||||
ptr = NULL;
|
||||
type = TrieNode_Cell;
|
||||
}
|
||||
SmartTrieNode(const SmartTrieNode &obj)
|
||||
{
|
||||
type = obj.type;
|
||||
ptr = obj.ptr;
|
||||
data = obj.data;
|
||||
data_len = obj.data_len;
|
||||
}
|
||||
SmartTrieNode & operator =(const SmartTrieNode &src)
|
||||
{
|
||||
type = src.type;
|
||||
ptr = src.ptr;
|
||||
data = src.data;
|
||||
data_len = src.data_len;
|
||||
return *this;
|
||||
}
|
||||
TrieNodeType type;
|
||||
cell_t *ptr;
|
||||
cell_t data;
|
||||
cell_t data_len;
|
||||
};
|
||||
|
||||
struct CellTrie
|
||||
{
|
||||
KTrie<SmartTrieNode> trie;
|
||||
cell_t mem_usage;
|
||||
};
|
||||
|
||||
class TrieHelpers :
|
||||
public SMGlobalClass,
|
||||
public IHandleTypeDispatch
|
||||
{
|
||||
public: //SMGlobalClass
|
||||
void OnSourceModAllInitialized()
|
||||
{
|
||||
htCellTrie = handlesys->CreateType("Trie", this, 0, NULL, NULL, g_pCoreIdent, NULL);
|
||||
}
|
||||
void OnSourceModShutdown()
|
||||
{
|
||||
handlesys->RemoveType(htCellTrie, g_pCoreIdent);
|
||||
}
|
||||
public: //IHandleTypeDispatch
|
||||
static void DestroySmartTrieNode(SmartTrieNode *pNode)
|
||||
{
|
||||
free(pNode->ptr);
|
||||
}
|
||||
void OnHandleDestroy(HandleType_t type, void *object)
|
||||
{
|
||||
CellTrie *pTrie = (CellTrie *)object;
|
||||
|
||||
pTrie->trie.run_destructor(DestroySmartTrieNode);
|
||||
|
||||
delete pTrie;
|
||||
}
|
||||
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
|
||||
{
|
||||
CellTrie *pArray = (CellTrie *)object;
|
||||
*pSize = sizeof(CellTrie) + pArray->mem_usage + pArray->trie.mem_usage();
|
||||
return true;
|
||||
}
|
||||
} s_CellTrieHelpers;
|
||||
|
||||
static cell_t CreateTrie(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
CellTrie *pTrie = new CellTrie;
|
||||
Handle_t hndl;
|
||||
|
||||
pTrie->mem_usage = 0;
|
||||
|
||||
if ((hndl = handlesys->CreateHandle(htCellTrie, pTrie, pContext->GetIdentity(), g_pCoreIdent, NULL))
|
||||
== BAD_HANDLE)
|
||||
{
|
||||
delete pTrie;
|
||||
return BAD_HANDLE;
|
||||
}
|
||||
|
||||
return hndl;
|
||||
}
|
||||
|
||||
static void UpdateNodeCells(CellTrie *pTrie, SmartTrieNode *pData, const cell_t *cells, cell_t num_cells)
|
||||
{
|
||||
if (num_cells == 1)
|
||||
{
|
||||
pData->data = *cells;
|
||||
pData->type = TrieNode_Cell;
|
||||
}
|
||||
else
|
||||
{
|
||||
pData->type = TrieNode_CellArray;
|
||||
if (pData->ptr == NULL)
|
||||
{
|
||||
pData->ptr = (cell_t *)malloc(num_cells * sizeof(cell_t));
|
||||
pData->data_len = num_cells;
|
||||
pTrie->mem_usage += (pData->data_len * sizeof(cell_t));
|
||||
}
|
||||
else if (pData->data_len < num_cells)
|
||||
{
|
||||
pData->ptr = (cell_t *)realloc(pData->ptr, num_cells * sizeof(cell_t));
|
||||
pTrie->mem_usage += (num_cells - pData->data_len) * sizeof(cell_t);
|
||||
pData->data_len = num_cells;
|
||||
}
|
||||
if (num_cells != 0)
|
||||
{
|
||||
memcpy(pData->ptr, cells, sizeof(cell_t) * num_cells);
|
||||
}
|
||||
pData->data = num_cells;
|
||||
}
|
||||
}
|
||||
|
||||
static void UpdateNodeString(CellTrie *pTrie, SmartTrieNode *pData, const char *str)
|
||||
{
|
||||
size_t len = strlen(str);
|
||||
cell_t num_cells = (len + sizeof(cell_t)) / sizeof(cell_t);
|
||||
|
||||
if (pData->ptr == NULL)
|
||||
{
|
||||
pData->ptr = (cell_t *)malloc(num_cells * sizeof(cell_t));
|
||||
pData->data_len = num_cells;
|
||||
pTrie->mem_usage += (pData->data_len * sizeof(cell_t));
|
||||
}
|
||||
else if (pData->data_len < num_cells)
|
||||
{
|
||||
pData->ptr = (cell_t *)realloc(pData->ptr, num_cells * sizeof(cell_t));
|
||||
pTrie->mem_usage += (num_cells - pData->data_len) * sizeof(cell_t);
|
||||
pData->data_len = num_cells;
|
||||
}
|
||||
|
||||
strcpy((char *)pData->ptr, str);
|
||||
pData->data = len;
|
||||
pData->type = TrieNode_String;
|
||||
}
|
||||
|
||||
static cell_t SetTrieValue(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
char *key;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
|
||||
SmartTrieNode *pNode;
|
||||
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
|
||||
{
|
||||
SmartTrieNode node;
|
||||
UpdateNodeCells(pTrie, &node, ¶ms[3], 1);
|
||||
return pTrie->trie.insert(key, node) ? 1 : 0;
|
||||
}
|
||||
|
||||
if (!params[4])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
UpdateNodeCells(pTrie, pNode, ¶ms[3], 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t SetTrieArray(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (params[4] < 0)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid array size: %d", params[4]);
|
||||
}
|
||||
|
||||
char *key;
|
||||
cell_t *array;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
pContext->LocalToPhysAddr(params[3], &array);
|
||||
|
||||
SmartTrieNode *pNode;
|
||||
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
|
||||
{
|
||||
SmartTrieNode node;
|
||||
UpdateNodeCells(pTrie, &node, array, params[4]);
|
||||
if (!pTrie->trie.insert(key, node))
|
||||
{
|
||||
free(node.ptr);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!params[4])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
UpdateNodeCells(pTrie, pNode, array, params[4]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t SetTrieString(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
char *key, *val;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
pContext->LocalToString(params[3], &val);
|
||||
|
||||
SmartTrieNode *pNode;
|
||||
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
|
||||
{
|
||||
SmartTrieNode node;
|
||||
UpdateNodeString(pTrie, &node, val);
|
||||
if (!pTrie->trie.insert(key, node))
|
||||
{
|
||||
free(node.ptr);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!params[4])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
UpdateNodeString(pTrie, pNode, val);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t RemoveFromTrie(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
char *key;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
|
||||
SmartTrieNode *pNode;
|
||||
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
free(pNode->ptr);
|
||||
pNode->ptr = NULL;
|
||||
|
||||
return pTrie->trie.remove(key) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t ClearTrie(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
pTrie->trie.run_destructor(TrieHelpers::DestroySmartTrieNode);
|
||||
pTrie->trie.clear();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t GetTrieValue(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
char *key;
|
||||
cell_t *pValue;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
pContext->LocalToPhysAddr(params[3], &pValue);
|
||||
|
||||
SmartTrieNode *pNode;
|
||||
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (pNode->type == TrieNode_Cell)
|
||||
{
|
||||
*pValue = pNode->data;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static cell_t GetTrieArray(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (params[4] < 0)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid array size: %d", params[4]);
|
||||
}
|
||||
|
||||
char *key;
|
||||
cell_t *pValue, *pSize;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
pContext->LocalToPhysAddr(params[3], &pValue);
|
||||
pContext->LocalToPhysAddr(params[5], &pSize);
|
||||
|
||||
SmartTrieNode *pNode;
|
||||
if ((pNode = pTrie->trie.retrieve(key)) == NULL
|
||||
|| pNode->type != TrieNode_CellArray)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (pNode->ptr == NULL)
|
||||
{
|
||||
*pSize = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (pNode->data > params[4])
|
||||
{
|
||||
*pSize = params[4];
|
||||
}
|
||||
else if (params[4] != 0)
|
||||
{
|
||||
*pSize = pNode->data;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
memcpy(pValue, pNode->ptr, sizeof(cell_t) * pSize[0]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t GetTrieString(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (params[4] < 0)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid buffer size: %d", params[4]);
|
||||
}
|
||||
|
||||
char *key;
|
||||
cell_t *pSize;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
pContext->LocalToPhysAddr(params[5], &pSize);
|
||||
|
||||
SmartTrieNode *pNode;
|
||||
if ((pNode = pTrie->trie.retrieve(key)) == NULL
|
||||
|| pNode->type != TrieNode_String)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (pNode->ptr == NULL)
|
||||
{
|
||||
*pSize = 0;
|
||||
pContext->StringToLocal(params[3], params[4], "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t written;
|
||||
pContext->StringToLocalUTF8(params[3], params[4], (char *)pNode->ptr, &written);
|
||||
|
||||
*pSize = (cell_t)written;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t GetTrieSize(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl;
|
||||
CellTrie *pTrie;
|
||||
HandleError err;
|
||||
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
|
||||
|
||||
hndl = params[1];
|
||||
|
||||
if ((err = handlesys->ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
|
||||
}
|
||||
|
||||
return pTrie->trie.size();
|
||||
}
|
||||
|
||||
REGISTER_NATIVES(trieNatives)
|
||||
{
|
||||
{"ClearTrie", ClearTrie},
|
||||
{"CreateTrie", CreateTrie},
|
||||
{"GetTrieArray", GetTrieArray},
|
||||
{"GetTrieString", GetTrieString},
|
||||
{"GetTrieValue", GetTrieValue},
|
||||
{"RemoveFromTrie", RemoveFromTrie},
|
||||
{"SetTrieArray", SetTrieArray},
|
||||
{"SetTrieString", SetTrieString},
|
||||
{"SetTrieValue", SetTrieValue},
|
||||
{"GetTrieSize", GetTrieSize},
|
||||
{NULL, NULL},
|
||||
};
|
||||
Reference in New Issue
Block a user