core/smn_profiler.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/core/smn_profiler.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,185 +0,0 @@
     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 -#include "sm_globals.h"
    1.36 -#include "HandleSys.h"
    1.37 -
    1.38 -//Note: Do not add this to Linux yet, i haven't done the HPET timing research (if even available)
    1.39 -//nonetheless we need accurate counting
    1.40 -#if defined PLATFORM_LINUX
    1.41 -#error "Not supported"
    1.42 -#endif
    1.43 -
    1.44 -struct Profiler
    1.45 -{
    1.46 -	Profiler()
    1.47 -	{
    1.48 -		started = false;
    1.49 -		stopped = false;
    1.50 -	}
    1.51 -#if defined PLATFORM_WINDOWS
    1.52 -	LARGE_INTEGER start;
    1.53 -	LARGE_INTEGER end;
    1.54 -	double freq;
    1.55 -#endif
    1.56 -	bool started;
    1.57 -	bool stopped;
    1.58 -};
    1.59 -
    1.60 -HandleType_t g_ProfilerType = 0;
    1.61 -
    1.62 -class ProfilerHelpers : 
    1.63 -	public SMGlobalClass,
    1.64 -	public IHandleTypeDispatch
    1.65 -{
    1.66 -public:
    1.67 -	void OnSourceModAllInitialized()
    1.68 -	{
    1.69 -		g_ProfilerType = g_HandleSys.CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    1.70 -	}
    1.71 -	void OnSourceModShutdown()
    1.72 -	{
    1.73 -		g_HandleSys.RemoveType(g_ProfilerType, g_pCoreIdent);
    1.74 -	}
    1.75 -	void OnHandleDestroy(HandleType_t type, void *object)
    1.76 -	{
    1.77 -		Profiler *prof = (Profiler *)object;
    1.78 -		delete prof;
    1.79 -	}
    1.80 -} s_ProfHelpers;
    1.81 -
    1.82 -static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
    1.83 -{
    1.84 -	Profiler *p = new Profiler();
    1.85 -
    1.86 -#if defined PLATFORM_WINDOWS
    1.87 -	LARGE_INTEGER qpf;
    1.88 -	QueryPerformanceFrequency(&qpf);
    1.89 -	p->freq = 1.0 / (double)(qpf.QuadPart);
    1.90 -#endif
    1.91 -
    1.92 -	Handle_t hndl = g_HandleSys.CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
    1.93 -	if (hndl == BAD_HANDLE)
    1.94 -	{
    1.95 -		delete p;
    1.96 -	}
    1.97 -
    1.98 -	return hndl;
    1.99 -}
   1.100 -
   1.101 -static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
   1.102 -{
   1.103 -	Handle_t hndl = params[1];
   1.104 -	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   1.105 -	HandleError err;
   1.106 -	Profiler *prof;
   1.107 -
   1.108 -	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   1.109 -		!= HandleError_None)
   1.110 -	{
   1.111 -		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   1.112 -	}
   1.113 -
   1.114 -#if defined PLATFORM_WINDOWS
   1.115 -	QueryPerformanceCounter(&prof->start);
   1.116 -#endif
   1.117 -
   1.118 -	prof->started = true;
   1.119 -	prof->stopped = false;
   1.120 -
   1.121 -	return 1;
   1.122 -}
   1.123 -
   1.124 -static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
   1.125 -{
   1.126 -	Handle_t hndl = params[1];
   1.127 -	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   1.128 -	HandleError err;
   1.129 -	Profiler *prof;
   1.130 -
   1.131 -	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   1.132 -		!= HandleError_None)
   1.133 -	{
   1.134 -		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   1.135 -	}
   1.136 -
   1.137 -	if (!prof->started)
   1.138 -	{
   1.139 -		return pContext->ThrowNativeError("Profiler was never started");
   1.140 -	}
   1.141 -
   1.142 -#if defined PLATFORM_WINDOWS
   1.143 -	QueryPerformanceCounter(&prof->end);
   1.144 -#endif
   1.145 -
   1.146 -	prof->started = false;
   1.147 -	prof->stopped = true;
   1.148 -
   1.149 -	return 1;
   1.150 -}
   1.151 -
   1.152 -static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
   1.153 -{
   1.154 -	Handle_t hndl = params[1];
   1.155 -	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   1.156 -	HandleError err;
   1.157 -	Profiler *prof;
   1.158 -
   1.159 -	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   1.160 -		!= HandleError_None)
   1.161 -	{
   1.162 -		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   1.163 -	}
   1.164 -
   1.165 -	if (!prof->stopped)
   1.166 -	{
   1.167 -		return pContext->ThrowNativeError("Profiler was never stopped");
   1.168 -	}
   1.169 -
   1.170 -	float fTime;
   1.171 -
   1.172 -#if defined PLATFORM_WINDOWS
   1.173 -	LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
   1.174 -	double seconds = diff * prof->freq;
   1.175 -	fTime = (float)seconds;
   1.176 -#endif
   1.177 -
   1.178 -	return sp_ftoc(fTime);
   1.179 -}
   1.180 -
   1.181 -REGISTER_NATIVES(profilerNatives)
   1.182 -{
   1.183 -	{"CreateProfiler",			CreateProfiler},
   1.184 -	{"GetProfilerTime",			GetProfilerTime},
   1.185 -	{"StartProfiling",			StartProfiling},
   1.186 -	{"StopProfiling",			StopProfiling},
   1.187 -	{NULL,						NULL},
   1.188 -};