core/logic/Profiler.h
author Scott Ehlert <ds@alliedmods.net>
Sun Feb 09 22:50:20 2014 -0600 (2014-02-09)
changeset 4241 46fdb8cd9883
parent 4068 5a6151072b18
permissions -rw-r--r--
Use == instead of is operator for checking string equality in AMBuild scripts (bug 6043, r=dvander).
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     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 <sh_vector.h>
    38 #include <sh_stack.h>
    39 #include <sm_namehashset.h>
    40 #include <stdio.h>
    41 #include "common_logic.h"
    42 #include <IRootConsoleMenu.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 	static inline bool matches(const char *name, const prof_atom_report_t *report)
    77 	{
    78 		return strcmp(report->atom_name, name) == 0;
    79 	}
    80 };
    81 
    82 class ProfileReport
    83 {
    84 public:
    85 	~ProfileReport();
    86 public:
    87 	void SaveAtom(const prof_atom_t &atom);
    88 	size_t GetNumReports();
    89 	prof_atom_report_t *GetReport(size_t i);
    90 	void Clear();
    91 private:
    92 	NameHashSet<prof_atom_report_t *> m_ReportLookup;
    93 	CVector<prof_atom_report_t *> m_Reports;
    94 };
    95 
    96 class ProfileEngine :
    97 	public SMGlobalClass,
    98 	public IRootConsoleCommand,
    99 	public IProfiler
   100 {
   101 public:
   102 	ProfileEngine();
   103 public:
   104 	bool IsEnabled();
   105 	bool GenerateReport(FILE *fp);
   106 	void Clear();
   107 public: //SMGlobalClass
   108 	void OnSourceModAllInitialized();
   109 	void OnSourceModShutdown();
   110 public: //IRootConsoleCommand
   111 	void OnRootConsoleCommand2(const char *cmdname, const ICommandArgs *command);
   112 public: //IProfiler
   113 	void OnNativeBegin(IPluginContext *pContext, sp_native_t *native);
   114 	void OnNativeEnd() ;
   115 	void OnFunctionBegin(IPluginContext *pContext, const char *name);
   116 	void OnFunctionEnd();
   117 	int OnCallbackBegin(IPluginContext *pContext, sp_public_t *pubfunc);
   118 	void OnCallbackEnd(int serial);
   119 private:
   120 	void PushProfileStack(IPluginContext *ctx, int type, const char *name);
   121 	void PopProfileStack(ProfileReport *reporter);
   122 	void PauseParent();
   123 	void ResumeParent(double addTime);
   124 	void WriteReport(FILE *fp, ProfileReport *report, const char *name);
   125 private:
   126 	CStack<prof_atom_t> m_AtomStack;
   127 	ProfileReport m_Callbacks;
   128 	ProfileReport m_Functions;
   129 	ProfileReport m_Natives;
   130 	int m_serial;
   131 	prof_point_t m_ProfStart;
   132 };
   133 
   134 extern IProfiler *sm_profiler;
   135 
   136 #endif //_INCLUDE_SOURCEMOD_PLUGIN_PROFILER_H_