3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #include "sm_globals.h"
34 #include "HandleSys.h"
36 //Note: Do not add this to Linux yet, i haven't done the HPET timing research (if even available)
37 //nonetheless we need accurate counting
38 #if defined PLATFORM_LINUX
39 #error "Not supported"
49 #if defined PLATFORM_WINDOWS
58 HandleType_t g_ProfilerType = 0;
60 class ProfilerHelpers :
62 public IHandleTypeDispatch
65 void OnSourceModAllInitialized()
67 g_ProfilerType = g_HandleSys.CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
69 void OnSourceModShutdown()
71 g_HandleSys.RemoveType(g_ProfilerType, g_pCoreIdent);
73 void OnHandleDestroy(HandleType_t type, void *object)
75 Profiler *prof = (Profiler *)object;
80 static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
82 Profiler *p = new Profiler();
84 #if defined PLATFORM_WINDOWS
86 QueryPerformanceFrequency(&qpf);
87 p->freq = 1.0 / (double)(qpf.QuadPart);
90 Handle_t hndl = g_HandleSys.CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
91 if (hndl == BAD_HANDLE)
99 static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
101 Handle_t hndl = params[1];
102 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
106 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
109 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
112 #if defined PLATFORM_WINDOWS
113 QueryPerformanceCounter(&prof->start);
116 prof->started = true;
117 prof->stopped = false;
122 static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
124 Handle_t hndl = params[1];
125 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
129 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
132 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
137 return pContext->ThrowNativeError("Profiler was never started");
140 #if defined PLATFORM_WINDOWS
141 QueryPerformanceCounter(&prof->end);
144 prof->started = false;
145 prof->stopped = true;
150 static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
152 Handle_t hndl = params[1];
153 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
157 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
160 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
165 return pContext->ThrowNativeError("Profiler was never stopped");
170 #if defined PLATFORM_WINDOWS
171 LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
172 double seconds = diff * prof->freq;
173 fTime = (float)seconds;
176 return sp_ftoc(fTime);
179 REGISTER_NATIVES(profilerNatives)
181 {"CreateProfiler", CreateProfiler},
182 {"GetProfilerTime", GetProfilerTime},
183 {"StartProfiling", StartProfiling},
184 {"StopProfiling", StopProfiling},