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