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