1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/Profiler.h Sun Mar 02 06:40:59 2008 +0000
1.3 @@ -0,0 +1,131 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#ifndef _INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_
1.36 +#define _INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_
1.37 +
1.38 +#include <sp_vm_api.h>
1.39 +#include <sm_platform.h>
1.40 +#include <sm_trie_tpl.h>
1.41 +#include <sh_vector.h>
1.42 +#include <sh_stack.h>
1.43 +#include <stdio.h>
1.44 +#include "sm_globals.h"
1.45 +#include "sm_srvcmds.h"
1.46 +
1.47 +using namespace SourcePawn;
1.48 +using namespace SourceHook;
1.49 +
1.50 +struct prof_point_t
1.51 +{
1.52 +#if defined PLATFORM_WINDOWS
1.53 + LARGE_INTEGER value;
1.54 +#elif defined PLATFORM_POSIX
1.55 + struct timeval value;
1.56 +#endif
1.57 + bool is_set;
1.58 +};
1.59 +
1.60 +struct prof_atom_t
1.61 +{
1.62 + int atom_type; /* Type of object we're profiling */
1.63 + int atom_serial; /* Serial number, if appropriate */
1.64 + sp_context_t *ctx; /* Plugin context. */
1.65 + const char *name; /* Name of the function */
1.66 + prof_point_t start; /* Start time */
1.67 + prof_point_t end; /* End time */
1.68 + double base_time; /* Known time from children or pausing. */
1.69 +};
1.70 +
1.71 +struct prof_atom_report_t
1.72 +{
1.73 + char atom_name[256]; /* Full name to shove to logs */
1.74 + double total_time; /* Total time spent executing, in s */
1.75 + unsigned int num_calls; /* Number of invocations */
1.76 + double min_time; /* Min time spent in one call, in s */
1.77 + double max_time; /* Max time spent in one call, in s */
1.78 +};
1.79 +
1.80 +class ProfileReport
1.81 +{
1.82 +public:
1.83 + ~ProfileReport();
1.84 +public:
1.85 + void SaveAtom(const prof_atom_t &atom);
1.86 + size_t GetNumReports();
1.87 + prof_atom_report_t *GetReport(size_t i);
1.88 + void Clear();
1.89 +private:
1.90 + KTrie<prof_atom_report_t *> m_ReportLookup;
1.91 + CVector<prof_atom_report_t *> m_Reports;
1.92 +};
1.93 +
1.94 +class ProfileEngine :
1.95 + public SMGlobalClass,
1.96 + public IRootConsoleCommand,
1.97 + public IProfiler
1.98 +{
1.99 +public:
1.100 + ProfileEngine();
1.101 +public:
1.102 + bool IsEnabled();
1.103 + bool GenerateReport(FILE *fp);
1.104 + void Clear();
1.105 +public: //SMGlobalClass
1.106 + void OnSourceModAllInitialized();
1.107 + void OnSourceModShutdown();
1.108 +public: //IRootConsoleCommand
1.109 + void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
1.110 +public: //IProfiler
1.111 + void OnNativeBegin(IPluginContext *pContext, sp_native_t *native);
1.112 + void OnNativeEnd() ;
1.113 + void OnFunctionBegin(IPluginContext *pContext, const char *name);
1.114 + void OnFunctionEnd();
1.115 + int OnCallbackBegin(IPluginContext *pContext, sp_public_t *pubfunc);
1.116 + void OnCallbackEnd(int serial);
1.117 +private:
1.118 + void PushProfileStack(IPluginContext *ctx, int type, const char *name);
1.119 + void PopProfileStack(ProfileReport *reporter);
1.120 + void PauseParent();
1.121 + void ResumeParent(double addTime);
1.122 + void WriteReport(FILE *fp, ProfileReport *report, const char *name);
1.123 +private:
1.124 + CStack<prof_atom_t> m_AtomStack;
1.125 + ProfileReport m_Callbacks;
1.126 + ProfileReport m_Functions;
1.127 + ProfileReport m_Natives;
1.128 + int m_serial;
1.129 + prof_point_t m_ProfStart;
1.130 +};
1.131 +
1.132 +extern ProfileEngine g_Profiler;
1.133 +
1.134 +#endif //_INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_