core/Profiler.h
author Scott Ehlert <ds@alliedmods.net>
Tue Nov 18 06:00:31 2008 -0600 (2008-11-18)
changeset 2406 30c2595dcbfb
parent 2289 44045ead61cd
permissions -rw-r--r--
Updated makefiles to link against binaries at l4d directory instead of l4d_demo
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_
    33 #define _INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_
    34 
    35 #include <sp_vm_api.h>
    36 #include <sm_platform.h>
    37 #include <sm_trie_tpl.h>
    38 #include <sh_vector.h>
    39 #include <sh_stack.h>
    40 #include <stdio.h>
    41 #include "sm_globals.h"
    42 #include "sm_srvcmds.h"
    43 
    44 using namespace SourcePawn;
    45 using namespace SourceHook;
    46 
    47 struct prof_point_t
    48 {
    49 #if defined PLATFORM_WINDOWS
    50 	LARGE_INTEGER value;
    51 #elif defined PLATFORM_POSIX
    52 	struct timeval value;
    53 #endif
    54 	bool is_set;
    55 };
    56 
    57 struct prof_atom_t
    58 {
    59 	int atom_type;			/* Type of object we're profiling */
    60 	int atom_serial;		/* Serial number, if appropriate */
    61 	sp_context_t *ctx;		/* Plugin context. */
    62 	const char *name;		/* Name of the function */
    63 	prof_point_t start;		/* Start time */
    64 	prof_point_t end;		/* End time */
    65 	double base_time;		/* Known time from children or pausing. */
    66 };
    67 
    68 struct prof_atom_report_t
    69 {
    70 	char atom_name[256];	/* Full name to shove to logs */
    71 	double total_time;		/* Total time spent executing, in s */
    72 	unsigned int num_calls;	/* Number of invocations */
    73 	double min_time;		/* Min time spent in one call, in s */
    74 	double max_time;		/* Max time spent in one call, in s */
    75 };
    76 
    77 class ProfileReport
    78 {
    79 public:
    80 	~ProfileReport();
    81 public:
    82 	void SaveAtom(const prof_atom_t &atom);
    83 	size_t GetNumReports();
    84 	prof_atom_report_t *GetReport(size_t i);
    85 	void Clear();
    86 private:
    87 	KTrie<prof_atom_report_t *> m_ReportLookup;
    88 	CVector<prof_atom_report_t *> m_Reports;
    89 };
    90 
    91 class ProfileEngine :
    92 	public SMGlobalClass,
    93 	public IRootConsoleCommand,
    94 	public IProfiler
    95 {
    96 public:
    97 	ProfileEngine();
    98 public:
    99 	bool IsEnabled();
   100 	bool GenerateReport(FILE *fp);
   101 	void Clear();
   102 public: //SMGlobalClass
   103 	void OnSourceModAllInitialized();
   104 	void OnSourceModShutdown();
   105 public: //IRootConsoleCommand
   106 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   107 public: //IProfiler
   108 	void OnNativeBegin(IPluginContext *pContext, sp_native_t *native);
   109 	void OnNativeEnd() ;
   110 	void OnFunctionBegin(IPluginContext *pContext, const char *name);
   111 	void OnFunctionEnd();
   112 	int OnCallbackBegin(IPluginContext *pContext, sp_public_t *pubfunc);
   113 	void OnCallbackEnd(int serial);
   114 private:
   115 	void PushProfileStack(IPluginContext *ctx, int type, const char *name);
   116 	void PopProfileStack(ProfileReport *reporter);
   117 	void PauseParent();
   118 	void ResumeParent(double addTime);
   119 	void WriteReport(FILE *fp, ProfileReport *report, const char *name);
   120 private:
   121 	CStack<prof_atom_t> m_AtomStack;
   122 	ProfileReport m_Callbacks;
   123 	ProfileReport m_Functions;
   124 	ProfileReport m_Natives;
   125 	int m_serial;
   126 	prof_point_t m_ProfStart;
   127 };
   128 
   129 extern ProfileEngine g_Profiler;
   130 
   131 #endif //_INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_