3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "sm_globals.h"
33 #include "HandleSys.h"
35 //Note: Do not add this to Linux yet, i haven't done the HPET timing research (if even available)
36 //nonetheless we need accurate counting
37 #if defined PLATFORM_LINUX
38 #error "Not supported"
48 #if defined PLATFORM_WINDOWS
57 HandleType_t g_ProfilerType = 0;
59 class ProfilerHelpers :
61 public IHandleTypeDispatch
64 void OnSourceModAllInitialized()
66 g_ProfilerType = g_HandleSys.CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
68 void OnSourceModShutdown()
70 g_HandleSys.RemoveType(g_ProfilerType, g_pCoreIdent);
72 void OnHandleDestroy(HandleType_t type, void *object)
74 Profiler *prof = (Profiler *)object;
79 static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
81 Profiler *p = new Profiler();
83 #if defined PLATFORM_WINDOWS
85 QueryPerformanceFrequency(&qpf);
86 p->freq = 1.0 / (double)(qpf.QuadPart);
89 Handle_t hndl = g_HandleSys.CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
90 if (hndl == BAD_HANDLE)
98 static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
100 Handle_t hndl = params[1];
101 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
105 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
108 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
111 #if defined PLATFORM_WINDOWS
112 QueryPerformanceCounter(&prof->start);
115 prof->started = true;
116 prof->stopped = false;
121 static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
123 Handle_t hndl = params[1];
124 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
128 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
131 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
136 return pContext->ThrowNativeError("Profiler was never started");
139 #if defined PLATFORM_WINDOWS
140 QueryPerformanceCounter(&prof->end);
143 prof->started = false;
144 prof->stopped = true;
149 static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
151 Handle_t hndl = params[1];
152 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
156 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
159 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
164 return pContext->ThrowNativeError("Profiler was never stopped");
169 #if defined PLATFORM_WINDOWS
170 LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
171 double seconds = diff * prof->freq;
172 fTime = (float)seconds;
175 return sp_ftoc(fTime);
178 REGISTER_NATIVES(profilerNatives)
180 {"CreateProfiler", CreateProfiler},
181 {"GetProfilerTime", GetProfilerTime},
182 {"StartProfiling", StartProfiling},
183 {"StopProfiling", StopProfiling},