add stopwatch class for profiling

This commit is contained in:
IntelOrca 2016-01-08 20:50:23 +00:00
parent 0012470504
commit 15bc414581
5 changed files with 235 additions and 0 deletions

View File

@ -35,6 +35,7 @@
<ClCompile Include="src\cmdline.c" />
<ClCompile Include="src\cmdline_sprite.c" />
<ClCompile Include="src\config.c" />
<ClCompile Include="src\core\Stopwatch.cpp" />
<ClCompile Include="src\cursors.c" />
<ClCompile Include="src\diagnostic.c" />
<ClCompile Include="src\drawing\drawing.c" />
@ -207,6 +208,8 @@
<ClInclude Include="src\core\IStream.hpp" />
<ClInclude Include="src\core\Math.hpp" />
<ClInclude Include="src\core\Memory.hpp" />
<ClInclude Include="src\core\stopwatch.h" />
<ClInclude Include="src\core\Stopwatch.hpp" />
<ClInclude Include="src\core\StringBuilder.hpp" />
<ClInclude Include="src\core\StringReader.hpp" />
<ClInclude Include="src\core\Util.hpp" />

View File

@ -561,6 +561,9 @@
<ClCompile Include="src\scenario_sources.c">
<Filter>Source</Filter>
</ClCompile>
<ClCompile Include="src\core\Stopwatch.cpp">
<Filter>Source\Core</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\management\award.h">
@ -842,5 +845,11 @@
<ClInclude Include="src\image_io.h">
<Filter>Source</Filter>
</ClInclude>
<ClInclude Include="src\core\stopwatch.h">
<Filter>Source\Core</Filter>
</ClInclude>
<ClInclude Include="src\core\Stopwatch.hpp">
<Filter>Source\Core</Filter>
</ClInclude>
</ItemGroup>
</Project>

164
src/core/Stopwatch.cpp Normal file
View File

@ -0,0 +1,164 @@
#ifdef _WIN32
#include <windows.h>
#else
#include "../platform/platform.h"
#endif
#include "Stopwatch.hpp"
uint64 Stopwatch::Frequency = 0;
Stopwatch::Stopwatch()
{
Reset();
}
uint64 Stopwatch::GetElapsedTicks()
{
uint64 result = _total;
if (_isRunning)
{
uint64 ticks = QueryCurrentTicks();
if (ticks != 0) {
result += QueryCurrentTicks() - _last;
}
}
return _total;
}
uint64 Stopwatch::GetElapsedMilliseconds()
{
if (Frequency == 0)
{
Frequency = QueryFrequency();
if (Frequency == 0)
{
return 0;
}
}
return (GetElapsedTicks() * 1000) / Frequency;
}
void Stopwatch::Reset()
{
_isRunning = false;
_total = 0;
_last = 0;
}
void Stopwatch::Start()
{
if (_isRunning) return;
uint64 ticks = QueryCurrentTicks();
if (ticks != 0)
{
_last = ticks;
_isRunning = true;
}
}
void Stopwatch::Restart()
{
Reset();
Start();
}
void Stopwatch::Stop()
{
uint64 ticks = QueryCurrentTicks();
if (ticks != 0) {
_total += QueryCurrentTicks() - _last;
}
_isRunning = false;
}
uint64 Stopwatch::QueryFrequency()
{
#ifdef _WIN32
LARGE_INTEGER frequency;
if (QueryPerformanceFrequency(&frequency))
{
return frequency.QuadPart;
}
else
{
log_error("QueryPerformanceFrequency failed.");
return 0;
}
#else
STUB();
return 0;
#endif
}
uint64 Stopwatch::QueryCurrentTicks()
{
#ifdef _WIN32
LARGE_INTEGER counter;
if (QueryPerformanceCounter(&counter))
{
return counter.QuadPart;
}
else
{
log_error("QueryPerformanceCounter failed.");
return 0;
}
#else
STUB();
return 0;
#endif
}
extern "C" {
#include "stopwatch.h"
void stopwatch_create(stopwatch *stopwatch)
{
stopwatch->context = new Stopwatch();
}
void stopwatch_dispose(stopwatch *stopwatch)
{
delete ((Stopwatch*)stopwatch->context);
}
uint64 stopwatch_GetElapsedTicks(stopwatch *stopwatch)
{
Stopwatch *ctx = (Stopwatch*)stopwatch->context;
return ctx->GetElapsedTicks();
}
uint64 stopwatch_GetElapsedMilliseconds(stopwatch *stopwatch)
{
Stopwatch *ctx = (Stopwatch*)stopwatch->context;
return ctx->GetElapsedMilliseconds();
}
void stopwatch_Reset(stopwatch *stopwatch)
{
Stopwatch *ctx = (Stopwatch*)stopwatch->context;
return ctx->Reset();
}
void stopwatch_Start(stopwatch *stopwatch)
{
Stopwatch *ctx = (Stopwatch*)stopwatch->context;
return ctx->Start();
}
void stopwatch_Restart(stopwatch *stopwatch)
{
Stopwatch *ctx = (Stopwatch*)stopwatch->context;
return ctx->Restart();
}
void stopwatch_Stop(stopwatch *stopwatch)
{
Stopwatch *ctx = (Stopwatch*)stopwatch->context;
return ctx->Stop();
}
}

35
src/core/Stopwatch.hpp Normal file
View File

@ -0,0 +1,35 @@
#pragma once
extern "C" {
#include "../common.h"
}
/**
* Class to accuately measure elapsed time with high precision.
*/
class Stopwatch
{
private:
/** Number of ticks in a second. */
static uint64 Frequency;
uint64 _total;
uint64 _last;
bool _isRunning;
static uint64 QueryFrequency();
static uint64 QueryCurrentTicks();
public:
bool IsRunning() { return _isRunning; }
Stopwatch();
uint64 GetElapsedTicks();
uint64 GetElapsedMilliseconds();
void Reset();
void Start();
void Restart();
void Stop();
};

24
src/core/stopwatch.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef _STOPWATCH_H_
#define _STOPWATCH_H_
#include "../common.h"
/////////////////////////////
// C wrapper for Stopwatch //
/////////////////////////////
typedef struct {
void *context;
} stopwatch;
void stopwatch_create(stopwatch *stopwatch);
void stopwatch_dispose(stopwatch *stopwatch);
uint64 stopwatch_GetElapsedTicks(stopwatch *stopwatch);
uint64 stopwatch_GetElapsedMilliseconds(stopwatch *stopwatch);
void stopwatch_Reset(stopwatch *stopwatch);
void stopwatch_Start(stopwatch *stopwatch);
void stopwatch_Restart(stopwatch *stopwatch);
void stopwatch_Stop(stopwatch *stopwatch);
#endif