core/smn_profiler.cpp
changeset 914 cd241af51653
child 1231 17badaf66ce4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_profiler.cpp	Thu Jun 07 07:29:55 2007 +0000
     1.3 @@ -0,0 +1,168 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.8 + * ===============================================================
     1.9 + *
    1.10 + * This file is not open source and may not be copied without explicit
    1.11 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    1.12 + * in whole or significant part.
    1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.14 + *
    1.15 + * Version: $Id$
    1.16 + */
    1.17 +
    1.18 +#include "sm_globals.h"
    1.19 +#include "HandleSys.h"
    1.20 +
    1.21 +//Note: Do not add this to Linux yet, i haven't done the HPET timing research (if even available)
    1.22 +//nonetheless we need accurate counting
    1.23 +#if defined PLATFORM_LINUX
    1.24 +#error "Not supported"
    1.25 +#endif
    1.26 +
    1.27 +struct Profiler
    1.28 +{
    1.29 +	Profiler()
    1.30 +	{
    1.31 +		started = false;
    1.32 +		stopped = false;
    1.33 +	}
    1.34 +#if defined PLATFORM_WINDOWS
    1.35 +	LARGE_INTEGER start;
    1.36 +	LARGE_INTEGER end;
    1.37 +	double freq;
    1.38 +#endif
    1.39 +	bool started;
    1.40 +	bool stopped;
    1.41 +};
    1.42 +
    1.43 +HandleType_t g_ProfilerType = 0;
    1.44 +
    1.45 +class ProfilerHelpers : 
    1.46 +	public SMGlobalClass,
    1.47 +	public IHandleTypeDispatch
    1.48 +{
    1.49 +public:
    1.50 +	void OnSourceModAllInitialized()
    1.51 +	{
    1.52 +		g_ProfilerType = g_HandleSys.CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    1.53 +	}
    1.54 +	void OnSourceModShutdown()
    1.55 +	{
    1.56 +		g_HandleSys.RemoveType(g_ProfilerType, g_pCoreIdent);
    1.57 +	}
    1.58 +	void OnHandleDestroy(HandleType_t type, void *object)
    1.59 +	{
    1.60 +		Profiler *prof = (Profiler *)object;
    1.61 +		delete prof;
    1.62 +	}
    1.63 +} s_ProfHelpers;
    1.64 +
    1.65 +static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
    1.66 +{
    1.67 +	Profiler *p = new Profiler();
    1.68 +
    1.69 +#if defined PLATFORM_WINDOWS
    1.70 +	LARGE_INTEGER qpf;
    1.71 +	QueryPerformanceFrequency(&qpf);
    1.72 +	p->freq = 1.0 / (double)(qpf.QuadPart);
    1.73 +#endif
    1.74 +
    1.75 +	Handle_t hndl = g_HandleSys.CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
    1.76 +	if (hndl == BAD_HANDLE)
    1.77 +	{
    1.78 +		delete p;
    1.79 +	}
    1.80 +
    1.81 +	return hndl;
    1.82 +}
    1.83 +
    1.84 +static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
    1.85 +{
    1.86 +	Handle_t hndl = params[1];
    1.87 +	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
    1.88 +	HandleError err;
    1.89 +	Profiler *prof;
    1.90 +
    1.91 +	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
    1.92 +		!= HandleError_None)
    1.93 +	{
    1.94 +		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
    1.95 +	}
    1.96 +
    1.97 +#if defined PLATFORM_WINDOWS
    1.98 +	QueryPerformanceCounter(&prof->start);
    1.99 +#endif
   1.100 +
   1.101 +	prof->started = true;
   1.102 +	prof->stopped = false;
   1.103 +
   1.104 +	return 1;
   1.105 +}
   1.106 +
   1.107 +static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
   1.108 +{
   1.109 +	Handle_t hndl = params[1];
   1.110 +	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   1.111 +	HandleError err;
   1.112 +	Profiler *prof;
   1.113 +
   1.114 +	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   1.115 +		!= HandleError_None)
   1.116 +	{
   1.117 +		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   1.118 +	}
   1.119 +
   1.120 +	if (!prof->started)
   1.121 +	{
   1.122 +		return pContext->ThrowNativeError("Profiler was never started");
   1.123 +	}
   1.124 +
   1.125 +#if defined PLATFORM_WINDOWS
   1.126 +	QueryPerformanceCounter(&prof->end);
   1.127 +#endif
   1.128 +
   1.129 +	prof->started = false;
   1.130 +	prof->stopped = true;
   1.131 +
   1.132 +	return 1;
   1.133 +}
   1.134 +
   1.135 +static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
   1.136 +{
   1.137 +	Handle_t hndl = params[1];
   1.138 +	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   1.139 +	HandleError err;
   1.140 +	Profiler *prof;
   1.141 +
   1.142 +	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   1.143 +		!= HandleError_None)
   1.144 +	{
   1.145 +		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   1.146 +	}
   1.147 +
   1.148 +	if (!prof->stopped)
   1.149 +	{
   1.150 +		return pContext->ThrowNativeError("Profiler was never stopped");
   1.151 +	}
   1.152 +
   1.153 +	float fTime;
   1.154 +
   1.155 +#if defined PLATFORM_WINDOWS
   1.156 +	LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
   1.157 +	double seconds = diff * prof->freq;
   1.158 +	fTime = (float)seconds;
   1.159 +#endif
   1.160 +
   1.161 +	return sp_ftoc(fTime);
   1.162 +}
   1.163 +
   1.164 +REGISTER_NATIVES(profilerNatives)
   1.165 +{
   1.166 +	{"CreateProfiler",			CreateProfiler},
   1.167 +	{"GetProfilerTime",			GetProfilerTime},
   1.168 +	{"StartProfiling",			StartProfiling},
   1.169 +	{"StopProfiling",			StopProfiling},
   1.170 +	{NULL,						NULL},
   1.171 +};