core/logic/smn_profiler.cpp
author Peace-Maker <januk@freenet.de>
Sat May 10 05:42:33 2014 -0700 (2014-05-10)
changeset 4354 7011587e0021
parent 3639 93afd6413ce1
permissions -rw-r--r--
Remove LogMessageEx definition from logging.inc (bug 5897, r=kyles).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "common_logic.h"
    33 #include <IHandleSys.h>
    34 #if !defined PLATFORM_WINDOWS
    35 #include <stddef.h>
    36 #include <stdint.h>
    37 #include <sys/time.h>
    38 #endif
    39 
    40 struct Profiler
    41 {
    42 	Profiler()
    43 	{
    44 		started = false;
    45 		stopped = false;
    46 	}
    47 #if defined PLATFORM_WINDOWS
    48 	LARGE_INTEGER start;
    49 	LARGE_INTEGER end;
    50 	double freq;
    51 #else
    52 	struct timeval start;
    53 	struct timeval end;
    54 #endif
    55 	bool started;
    56 	bool stopped;
    57 };
    58 
    59 HandleType_t g_ProfilerType = 0;
    60 
    61 class ProfilerHelpers : 
    62 	public SMGlobalClass,
    63 	public IHandleTypeDispatch
    64 {
    65 public:
    66 	void OnSourceModAllInitialized()
    67 	{
    68 		g_ProfilerType = handlesys->CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    69 	}
    70 	void OnSourceModShutdown()
    71 	{
    72 		handlesys->RemoveType(g_ProfilerType, g_pCoreIdent);
    73 	}
    74 	void OnHandleDestroy(HandleType_t type, void *object)
    75 	{
    76 		Profiler *prof = (Profiler *)object;
    77 		delete prof;
    78 	}
    79 } s_ProfHelpers;
    80 
    81 static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
    82 {
    83 	Profiler *p = new Profiler();
    84 
    85 #if defined PLATFORM_WINDOWS
    86 	LARGE_INTEGER qpf;
    87 	QueryPerformanceFrequency(&qpf);
    88 	p->freq = 1.0 / (double)(qpf.QuadPart);
    89 #endif
    90 
    91 	Handle_t hndl = handlesys->CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
    92 	if (hndl == BAD_HANDLE)
    93 	{
    94 		delete p;
    95 	}
    96 
    97 	return hndl;
    98 }
    99 
   100 static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
   101 {
   102 	Handle_t hndl = params[1];
   103 	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   104 	HandleError err;
   105 	Profiler *prof;
   106 
   107 	if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   108 		!= HandleError_None)
   109 	{
   110 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   111 	}
   112 
   113 #if defined PLATFORM_WINDOWS
   114 	QueryPerformanceCounter(&prof->start);
   115 #else
   116 	gettimeofday(&prof->start, NULL);
   117 #endif
   118 
   119 	prof->started = true;
   120 	prof->stopped = false;
   121 
   122 	return 1;
   123 }
   124 
   125 static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
   126 {
   127 	Handle_t hndl = params[1];
   128 	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   129 	HandleError err;
   130 	Profiler *prof;
   131 
   132 	if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   133 		!= HandleError_None)
   134 	{
   135 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   136 	}
   137 
   138 	if (!prof->started)
   139 	{
   140 		return pContext->ThrowNativeError("Profiler was never started");
   141 	}
   142 
   143 #if defined PLATFORM_WINDOWS
   144 	QueryPerformanceCounter(&prof->end);
   145 #else
   146 	gettimeofday(&prof->end, NULL);
   147 #endif
   148 
   149 	prof->started = false;
   150 	prof->stopped = true;
   151 
   152 	return 1;
   153 }
   154 
   155 static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
   156 {
   157 	Handle_t hndl = params[1];
   158 	HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
   159 	HandleError err;
   160 	Profiler *prof;
   161 
   162 	if ((err = handlesys->ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
   163 		!= HandleError_None)
   164 	{
   165 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   166 	}
   167 
   168 	if (!prof->stopped)
   169 	{
   170 		return pContext->ThrowNativeError("Profiler was never stopped");
   171 	}
   172 
   173 	float fTime;
   174 
   175 #if defined PLATFORM_WINDOWS
   176 	LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
   177 	double seconds = diff * prof->freq;
   178 	fTime = (float)seconds;
   179 #else
   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;
   183 #endif
   184 
   185 	return sp_ftoc(fTime);
   186 }
   187 
   188 REGISTER_NATIVES(profilerNatives)
   189 {
   190 	{"CreateProfiler",			CreateProfiler},
   191 	{"GetProfilerTime",			GetProfilerTime},
   192 	{"StartProfiling",			StartProfiling},
   193 	{"StopProfiling",			StopProfiling},
   194 	{NULL,						NULL},
   195 };
   196