ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "sm_globals.h" ds@1952: #include "HandleSys.h" ds@1952: ds@1952: //Note: Do not add this to Linux yet, i haven't done the HPET timing research (if even available) ds@1952: //nonetheless we need accurate counting ds@1952: #if defined PLATFORM_LINUX ds@1952: #error "Not supported" ds@1952: #endif ds@1952: ds@1952: struct Profiler ds@1952: { ds@1952: Profiler() ds@1952: { ds@1952: started = false; ds@1952: stopped = false; ds@1952: } ds@1952: #if defined PLATFORM_WINDOWS ds@1952: LARGE_INTEGER start; ds@1952: LARGE_INTEGER end; ds@1952: double freq; ds@1952: #endif ds@1952: bool started; ds@1952: bool stopped; ds@1952: }; ds@1952: ds@1952: HandleType_t g_ProfilerType = 0; ds@1952: ds@1952: class ProfilerHelpers : ds@1952: public SMGlobalClass, ds@1952: public IHandleTypeDispatch ds@1952: { ds@1952: public: ds@1952: void OnSourceModAllInitialized() ds@1952: { ds@1952: g_ProfilerType = g_HandleSys.CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL); ds@1952: } ds@1952: void OnSourceModShutdown() ds@1952: { ds@1952: g_HandleSys.RemoveType(g_ProfilerType, g_pCoreIdent); ds@1952: } ds@1952: void OnHandleDestroy(HandleType_t type, void *object) ds@1952: { ds@1952: Profiler *prof = (Profiler *)object; ds@1952: delete prof; ds@1952: } ds@1952: } s_ProfHelpers; ds@1952: ds@1952: static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Profiler *p = new Profiler(); ds@1952: ds@1952: #if defined PLATFORM_WINDOWS ds@1952: LARGE_INTEGER qpf; ds@1952: QueryPerformanceFrequency(&qpf); ds@1952: p->freq = 1.0 / (double)(qpf.QuadPart); ds@1952: #endif ds@1952: ds@1952: Handle_t hndl = g_HandleSys.CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL); ds@1952: if (hndl == BAD_HANDLE) ds@1952: { ds@1952: delete p; ds@1952: } ds@1952: ds@1952: return hndl; ds@1952: } ds@1952: ds@1952: static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = params[1]; ds@1952: HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent); ds@1952: HandleError err; ds@1952: Profiler *prof; ds@1952: ds@1952: if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: #if defined PLATFORM_WINDOWS ds@1952: QueryPerformanceCounter(&prof->start); ds@1952: #endif ds@1952: ds@1952: prof->started = true; ds@1952: prof->stopped = false; ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = params[1]; ds@1952: HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent); ds@1952: HandleError err; ds@1952: Profiler *prof; ds@1952: ds@1952: if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: if (!prof->started) ds@1952: { ds@1952: return pContext->ThrowNativeError("Profiler was never started"); ds@1952: } ds@1952: ds@1952: #if defined PLATFORM_WINDOWS ds@1952: QueryPerformanceCounter(&prof->end); ds@1952: #endif ds@1952: ds@1952: prof->started = false; ds@1952: prof->stopped = true; ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = params[1]; ds@1952: HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent); ds@1952: HandleError err; ds@1952: Profiler *prof; ds@1952: ds@1952: if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: if (!prof->stopped) ds@1952: { ds@1952: return pContext->ThrowNativeError("Profiler was never stopped"); ds@1952: } ds@1952: ds@1952: float fTime; ds@1952: ds@1952: #if defined PLATFORM_WINDOWS ds@1952: LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart; ds@1952: double seconds = diff * prof->freq; ds@1952: fTime = (float)seconds; ds@1952: #endif ds@1952: ds@1952: return sp_ftoc(fTime); ds@1952: } ds@1952: ds@1952: REGISTER_NATIVES(profilerNatives) ds@1952: { ds@1952: {"CreateProfiler", CreateProfiler}, ds@1952: {"GetProfilerTime", GetProfilerTime}, ds@1952: {"StartProfiling", StartProfiling}, ds@1952: {"StopProfiling", StopProfiling}, ds@1952: {NULL, NULL}, ds@1952: };