3 * =============================================================================
5 * Copyright (C) 2004-2008 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 "common_logic.h"
33 #include <IHandleSys.h>
34 #if !defined PLATFORM_WINDOWS
47 #if defined PLATFORM_WINDOWS
59 HandleType_t g_ProfilerType = 0;
61 class ProfilerHelpers :
63 public IHandleTypeDispatch
66 void OnSourceModAllInitialized()
68 g_ProfilerType = handlesys->CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
70 void OnSourceModShutdown()
72 handlesys->RemoveType(g_ProfilerType, g_pCoreIdent);
74 void OnHandleDestroy(HandleType_t type, void *object)
76 Profiler *prof = (Profiler *)object;
81 static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
83 Profiler *p = new Profiler();
85 #if defined PLATFORM_WINDOWS
87 QueryPerformanceFrequency(&qpf);
88 p->freq = 1.0 / (double)(qpf.QuadPart);
91 Handle_t hndl = handlesys->CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
92 if (hndl == BAD_HANDLE)
100 static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
102 Handle_t hndl = params[1];
103 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
107 if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
110 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
113 #if defined PLATFORM_WINDOWS
114 QueryPerformanceCounter(&prof->start);
116 gettimeofday(&prof->start, NULL);
119 prof->started = true;
120 prof->stopped = false;
125 static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
127 Handle_t hndl = params[1];
128 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
132 if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
135 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
140 return pContext->ThrowNativeError("Profiler was never started");
143 #if defined PLATFORM_WINDOWS
144 QueryPerformanceCounter(&prof->end);
146 gettimeofday(&prof->end, NULL);
149 prof->started = false;
150 prof->stopped = true;
155 static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
157 Handle_t hndl = params[1];
158 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
162 if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
165 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
170 return pContext->ThrowNativeError("Profiler was never stopped");
175 #if defined PLATFORM_WINDOWS
176 LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
177 double seconds = diff * prof->freq;
178 fTime = (float)seconds;
180 int64_t start_us = int64_t(prof->start.tv_sec) * 1000000 + prof->start.tv_usec;
181 int64_t stop_us = int64_t(prof->end.tv_sec) * 1000000 + prof->end.tv_usec;
182 fTime = double(stop_us - start_us) / 1000000.0;
185 return sp_ftoc(fTime);
188 REGISTER_NATIVES(profilerNatives)
190 {"CreateProfiler", CreateProfiler},
191 {"GetProfilerTime", GetProfilerTime},
192 {"StartProfiling", StartProfiling},
193 {"StopProfiling", StopProfiling},