Move profile natives to logic, add linux support (bug 4927, r=ds).
authorDavid Anderson <dvander@alliedmods.net>
Sun May 22 02:04:34 2011 -0700 (2011-05-22)
changeset 32707d218f0a11fe
parent 3269 2139ba334ae0
child 3271 acc921bc57ef
Move profile natives to logic, add linux support (bug 4927, r=ds).
core/AMBuilder
core/logic/AMBuilder
core/logic/smn_profiler.cpp
core/smn_profiler.cpp
     1.1 --- a/core/AMBuilder	Thu May 12 11:33:50 2011 -0400
     1.2 +++ b/core/AMBuilder	Sun May 22 02:04:34 2011 -0700
     1.3 @@ -40,7 +40,6 @@
     1.4  		'concmd_cleaner.cpp',
     1.5  		'HalfLife2.cpp',
     1.6  		'NextMap.cpp',
     1.7 -		'smn_profiler.cpp',
     1.8  		'ConCmdManager.cpp',
     1.9  		'HandleSys.cpp',
    1.10  		'ConVarManager.cpp',
     2.1 --- a/core/logic/AMBuilder	Thu May 12 11:33:50 2011 -0400
     2.2 +++ b/core/logic/AMBuilder	Sun May 22 02:04:34 2011 -0700
     2.3 @@ -44,7 +44,8 @@
     2.4  	'smn_datapacks.cpp',
     2.5  	'smn_gameconfigs.cpp',
     2.6  	'GameConfigs.cpp',
     2.7 -	'sm_crc32.cpp'
     2.8 +	'sm_crc32.cpp',
     2.9 +	'smn_profiler.cpp'
    2.10  	]
    2.11  if AMBuild.target['platform'] == 'windows':
    2.12  	files.append('thread/WinThreads.cpp')
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/core/logic/smn_profiler.cpp	Sun May 22 02:04:34 2011 -0700
     3.3 @@ -0,0 +1,196 @@
     3.4 +/**
     3.5 + * vim: set ts=4 :
     3.6 + * =============================================================================
     3.7 + * SourceMod
     3.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     3.9 + * =============================================================================
    3.10 + *
    3.11 + * This program is free software; you can redistribute it and/or modify it under
    3.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    3.13 + * Free Software Foundation.
    3.14 + * 
    3.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    3.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    3.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    3.18 + * details.
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License along with
    3.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    3.22 + *
    3.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    3.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    3.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    3.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    3.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    3.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    3.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    3.30 + * or <http://www.sourcemod.net/license.php>.
    3.31 + *
    3.32 + * Version: $Id$
    3.33 + */
    3.34 +
    3.35 +#include "common_logic.h"
    3.36 +#include <IHandleSys.h>
    3.37 +#if !defined PLATFORM_WINDOWS
    3.38 +#include <stddef.h>
    3.39 +#include <stdint.h>
    3.40 +#include <sys/time.h>
    3.41 +#endif
    3.42 +
    3.43 +struct Profiler
    3.44 +{
    3.45 +	Profiler()
    3.46 +	{
    3.47 +		started = false;
    3.48 +		stopped = false;
    3.49 +	}
    3.50 +#if defined PLATFORM_WINDOWS
    3.51 +	LARGE_INTEGER start;
    3.52 +	LARGE_INTEGER end;
    3.53 +	double freq;
    3.54 +#else
    3.55 +	struct timeval start;
    3.56 +	struct timeval end;
    3.57 +#endif
    3.58 +	bool started;
    3.59 +	bool stopped;
    3.60 +};
    3.61 +
    3.62 +HandleType_t g_ProfilerType = 0;
    3.63 +
    3.64 +class ProfilerHelpers : 
    3.65 +	public SMGlobalClass,
    3.66 +	public IHandleTypeDispatch
    3.67 +{
    3.68 +public:
    3.69 +	void OnSourceModAllInitialized()
    3.70 +	{
    3.71 +		g_ProfilerType = handlesys->CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    3.72 +	}
    3.73 +	void OnSourceModShutdown()
    3.74 +	{
    3.75 +		handlesys->RemoveType(g_ProfilerType, g_pCoreIdent);
    3.76 +	}
    3.77 +	void OnHandleDestroy(HandleType_t type, void *object)
    3.78 +	{
    3.79 +		Profiler *prof = (Profiler *)object;
    3.80 +		delete prof;
    3.81 +	}
    3.82 +} s_ProfHelpers;
    3.83 +
    3.84 +static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
    3.85 +{
    3.86 +	Profiler *p = new Profiler();
    3.87 +
    3.88 +#if defined PLATFORM_WINDOWS
    3.89 +	LARGE_INTEGER qpf;
    3.90 +	QueryPerformanceFrequency(&qpf);
    3.91 +	p->freq = 1.0 / (double)(qpf.QuadPart);
    3.92 +#endif
    3.93 +
    3.94 +	Handle_t hndl = handlesys->CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
    3.95 +	if (hndl == BAD_HANDLE)
    3.96 +	{
    3.97 +		delete p;
    3.98 +	}
    3.99 +
   3.100 +	return hndl;
   3.101 +}
   3.102 +
   3.103 +static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
   3.104 +{
   3.105 +	Handle_t hndl = params[1];
   3.106 +	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   3.107 +	HandleError err;
   3.108 +	Profiler *prof;
   3.109 +
   3.110 +	if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   3.111 +		!= HandleError_None)
   3.112 +	{
   3.113 +		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   3.114 +	}
   3.115 +
   3.116 +#if defined PLATFORM_WINDOWS
   3.117 +	QueryPerformanceCounter(&prof->start);
   3.118 +#else
   3.119 +	gettimeofday(&prof->start, NULL);
   3.120 +#endif
   3.121 +
   3.122 +	prof->started = true;
   3.123 +	prof->stopped = false;
   3.124 +
   3.125 +	return 1;
   3.126 +}
   3.127 +
   3.128 +static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
   3.129 +{
   3.130 +	Handle_t hndl = params[1];
   3.131 +	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   3.132 +	HandleError err;
   3.133 +	Profiler *prof;
   3.134 +
   3.135 +	if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   3.136 +		!= HandleError_None)
   3.137 +	{
   3.138 +		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   3.139 +	}
   3.140 +
   3.141 +	if (!prof->started)
   3.142 +	{
   3.143 +		return pContext->ThrowNativeError("Profiler was never started");
   3.144 +	}
   3.145 +
   3.146 +#if defined PLATFORM_WINDOWS
   3.147 +	QueryPerformanceCounter(&prof->end);
   3.148 +#else
   3.149 +	gettimeofday(&prof->end, NULL);
   3.150 +#endif
   3.151 +
   3.152 +	prof->started = false;
   3.153 +	prof->stopped = true;
   3.154 +
   3.155 +	return 1;
   3.156 +}
   3.157 +
   3.158 +static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
   3.159 +{
   3.160 +	Handle_t hndl = params[1];
   3.161 +	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   3.162 +	HandleError err;
   3.163 +	Profiler *prof;
   3.164 +
   3.165 +	if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   3.166 +		!= HandleError_None)
   3.167 +	{
   3.168 +		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   3.169 +	}
   3.170 +
   3.171 +	if (!prof->stopped)
   3.172 +	{
   3.173 +		return pContext->ThrowNativeError("Profiler was never stopped");
   3.174 +	}
   3.175 +
   3.176 +	float fTime;
   3.177 +
   3.178 +#if defined PLATFORM_WINDOWS
   3.179 +	LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
   3.180 +	double seconds = diff * prof->freq;
   3.181 +	fTime = (float)seconds;
   3.182 +#else
   3.183 +	int64_t start_us = int64_t(prof->start.tv_sec) * 1000000 + prof->start.tv_usec;
   3.184 +	int64_t stop_us = int64_t(prof->end.tv_sec) * 1000000 + prof->end.tv_usec;
   3.185 +	fTime = double((stop_us - start_us) / 1000) / 1000.0;
   3.186 +#endif
   3.187 +
   3.188 +	return sp_ftoc(fTime);
   3.189 +}
   3.190 +
   3.191 +REGISTER_NATIVES(profilerNatives)
   3.192 +{
   3.193 +	{"CreateProfiler",			CreateProfiler},
   3.194 +	{"GetProfilerTime",			GetProfilerTime},
   3.195 +	{"StartProfiling",			StartProfiling},
   3.196 +	{"StopProfiling",			StopProfiling},
   3.197 +	{NULL,						NULL},
   3.198 +};
   3.199 +
     4.1 --- a/core/smn_profiler.cpp	Thu May 12 11:33:50 2011 -0400
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,185 +0,0 @@
     4.4 -/**
     4.5 - * vim: set ts=4 :
     4.6 - * =============================================================================
     4.7 - * SourceMod
     4.8 - * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     4.9 - * =============================================================================
    4.10 - *
    4.11 - * This program is free software; you can redistribute it and/or modify it under
    4.12 - * the terms of the GNU General Public License, version 3.0, as published by the
    4.13 - * Free Software Foundation.
    4.14 - * 
    4.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
    4.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    4.17 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    4.18 - * details.
    4.19 - *
    4.20 - * You should have received a copy of the GNU General Public License along with
    4.21 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    4.22 - *
    4.23 - * As a special exception, AlliedModders LLC gives you permission to link the
    4.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    4.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    4.26 - * by the Valve Corporation.  You must obey the GNU General Public License in
    4.27 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    4.28 - * this exception to all derivative works.  AlliedModders LLC defines further
    4.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    4.30 - * or <http://www.sourcemod.net/license.php>.
    4.31 - *
    4.32 - * Version: $Id$
    4.33 - */
    4.34 -
    4.35 -#include "sm_globals.h"
    4.36 -#include "HandleSys.h"
    4.37 -
    4.38 -//Note: Do not add this to Linux yet, i haven't done the HPET timing research (if even available)
    4.39 -//nonetheless we need accurate counting
    4.40 -#if !defined PLATFORM_LINUX && !defined PLATFORM_APPLE
    4.41 -
    4.42 -struct Profiler
    4.43 -{
    4.44 -	Profiler()
    4.45 -	{
    4.46 -		started = false;
    4.47 -		stopped = false;
    4.48 -	}
    4.49 -#if defined PLATFORM_WINDOWS
    4.50 -	LARGE_INTEGER start;
    4.51 -	LARGE_INTEGER end;
    4.52 -	double freq;
    4.53 -#endif
    4.54 -	bool started;
    4.55 -	bool stopped;
    4.56 -};
    4.57 -
    4.58 -HandleType_t g_ProfilerType = 0;
    4.59 -
    4.60 -class ProfilerHelpers : 
    4.61 -	public SMGlobalClass,
    4.62 -	public IHandleTypeDispatch
    4.63 -{
    4.64 -public:
    4.65 -	void OnSourceModAllInitialized()
    4.66 -	{
    4.67 -		g_ProfilerType = g_HandleSys.CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    4.68 -	}
    4.69 -	void OnSourceModShutdown()
    4.70 -	{
    4.71 -		g_HandleSys.RemoveType(g_ProfilerType, g_pCoreIdent);
    4.72 -	}
    4.73 -	void OnHandleDestroy(HandleType_t type, void *object)
    4.74 -	{
    4.75 -		Profiler *prof = (Profiler *)object;
    4.76 -		delete prof;
    4.77 -	}
    4.78 -} s_ProfHelpers;
    4.79 -
    4.80 -static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
    4.81 -{
    4.82 -	Profiler *p = new Profiler();
    4.83 -
    4.84 -#if defined PLATFORM_WINDOWS
    4.85 -	LARGE_INTEGER qpf;
    4.86 -	QueryPerformanceFrequency(&qpf);
    4.87 -	p->freq = 1.0 / (double)(qpf.QuadPart);
    4.88 -#endif
    4.89 -
    4.90 -	Handle_t hndl = g_HandleSys.CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
    4.91 -	if (hndl == BAD_HANDLE)
    4.92 -	{
    4.93 -		delete p;
    4.94 -	}
    4.95 -
    4.96 -	return hndl;
    4.97 -}
    4.98 -
    4.99 -static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
   4.100 -{
   4.101 -	Handle_t hndl = params[1];
   4.102 -	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   4.103 -	HandleError err;
   4.104 -	Profiler *prof;
   4.105 -
   4.106 -	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   4.107 -		!= HandleError_None)
   4.108 -	{
   4.109 -		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   4.110 -	}
   4.111 -
   4.112 -#if defined PLATFORM_WINDOWS
   4.113 -	QueryPerformanceCounter(&prof->start);
   4.114 -#endif
   4.115 -
   4.116 -	prof->started = true;
   4.117 -	prof->stopped = false;
   4.118 -
   4.119 -	return 1;
   4.120 -}
   4.121 -
   4.122 -static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
   4.123 -{
   4.124 -	Handle_t hndl = params[1];
   4.125 -	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   4.126 -	HandleError err;
   4.127 -	Profiler *prof;
   4.128 -
   4.129 -	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   4.130 -		!= HandleError_None)
   4.131 -	{
   4.132 -		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   4.133 -	}
   4.134 -
   4.135 -	if (!prof->started)
   4.136 -	{
   4.137 -		return pContext->ThrowNativeError("Profiler was never started");
   4.138 -	}
   4.139 -
   4.140 -#if defined PLATFORM_WINDOWS
   4.141 -	QueryPerformanceCounter(&prof->end);
   4.142 -#endif
   4.143 -
   4.144 -	prof->started = false;
   4.145 -	prof->stopped = true;
   4.146 -
   4.147 -	return 1;
   4.148 -}
   4.149 -
   4.150 -static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
   4.151 -{
   4.152 -	Handle_t hndl = params[1];
   4.153 -	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   4.154 -	HandleError err;
   4.155 -	Profiler *prof;
   4.156 -
   4.157 -	if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   4.158 -		!= HandleError_None)
   4.159 -	{
   4.160 -		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   4.161 -	}
   4.162 -
   4.163 -	if (!prof->stopped)
   4.164 -	{
   4.165 -		return pContext->ThrowNativeError("Profiler was never stopped");
   4.166 -	}
   4.167 -
   4.168 -	float fTime;
   4.169 -
   4.170 -#if defined PLATFORM_WINDOWS
   4.171 -	LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
   4.172 -	double seconds = diff * prof->freq;
   4.173 -	fTime = (float)seconds;
   4.174 -#endif
   4.175 -
   4.176 -	return sp_ftoc(fTime);
   4.177 -}
   4.178 -
   4.179 -REGISTER_NATIVES(profilerNatives)
   4.180 -{
   4.181 -	{"CreateProfiler",			CreateProfiler},
   4.182 -	{"GetProfilerTime",			GetProfilerTime},
   4.183 -	{"StartProfiling",			StartProfiling},
   4.184 -	{"StopProfiling",			StopProfiling},
   4.185 -	{NULL,						NULL},
   4.186 -};
   4.187 -#endif
   4.188 -