From abf01f657d068aa6b22ab962cbe01b88f3b5f7ea Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Fri, 12 Sep 2025 16:35:40 +0200 Subject: event logging --- libs/timer_lib/.gitattributes | 22 +++++ libs/timer_lib/.gitignore | 163 ++++++++++++++++++++++++++++++ libs/timer_lib/Makefile | 23 +++++ libs/timer_lib/README | 37 +++++++ libs/timer_lib/test.vcxproj | 169 +++++++++++++++++++++++++++++++ libs/timer_lib/timer.c | 174 ++++++++++++++++++++++++++++++++ libs/timer_lib/timer.h | 77 +++++++++++++++ libs/timer_lib/timer.sln | 36 +++++++ libs/timer_lib/timer.vcxproj | 225 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 926 insertions(+) create mode 100644 libs/timer_lib/.gitattributes create mode 100644 libs/timer_lib/.gitignore create mode 100644 libs/timer_lib/Makefile create mode 100644 libs/timer_lib/README create mode 100644 libs/timer_lib/test.vcxproj create mode 100644 libs/timer_lib/timer.c create mode 100644 libs/timer_lib/timer.h create mode 100644 libs/timer_lib/timer.sln create mode 100644 libs/timer_lib/timer.vcxproj (limited to 'libs') diff --git a/libs/timer_lib/.gitattributes b/libs/timer_lib/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/libs/timer_lib/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/libs/timer_lib/.gitignore b/libs/timer_lib/.gitignore new file mode 100644 index 0000000..5ebd21a --- /dev/null +++ b/libs/timer_lib/.gitignore @@ -0,0 +1,163 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store diff --git a/libs/timer_lib/Makefile b/libs/timer_lib/Makefile new file mode 100644 index 0000000..3edaf73 --- /dev/null +++ b/libs/timer_lib/Makefile @@ -0,0 +1,23 @@ +SRC = timer.c +OBJ = $(SRC:.c=.o) +OUT = libtimer.a +TST = timertest +TSR = test.c +TOB = $(TSR:.c=.o) + +CFLAGS = -O3 +LDFLAGS = + +default: $(OUT) $(TST) + +.c.o: + gcc $(CFLAGS) -c $< -o $@ + +$(OUT): $(OBJ) + ar rcs $(OUT) $(OBJ) + +$(TST): $(OUT) $(TOB) + gcc $(CFLAGS) $(LDFLAGS) -o $(TST) $(TOB) $(OUT) -lrt + +clean: + rm -f $(OBJ) $(OUT) $(TOB) $(TST) diff --git a/libs/timer_lib/README b/libs/timer_lib/README new file mode 100644 index 0000000..16f604b --- /dev/null +++ b/libs/timer_lib/README @@ -0,0 +1,37 @@ +timer_lib - v1.0 - Public Domain - 2011 Mattias Jansson + +This library provides a cross-platform interface to measure +elapsed time with (at least) millisecond accuracy. The latest +source code is always available at + +https://github.com/mjansson/timer_lib + +This library is put in the public domain; you can redistribute +it and/or modify it without any restrictions. + +For a complete cross-platform application framework, look at +the foundation library available at + +https://github.com/mjansson/foundation_lib + + +VERSION HISTORY + + 0.1 (2011-03-15) Initial release + 0.2 (2011-03-20) Removed timer type (always high precision) + Fixed timer_ticks_per_second declaration + Added test application + 0.3 (2011-03-22) Removed unused error checks in POSIX code + Made timeGetTime fallback optional in Windows code (define USE_FALLBACK to 1) + Fixed check of QPC weirdness (signed issue) + 0.4 (2011-03-23) timer_lib_initialize() returns non-zero if failed (no high precision timer available) + Changed POSIX path to use CLOCK_MONOTONIC + POSIX timer_system use CLOCK_REALTIME for actual timestamp + Addded Mach-O path for MacOS X + Changed POSIX path to use nanosecond frequency as returned by clock_gettime + 0.5 (2012-10-01) Merged (cleaned up) MacOSX build fixes from Nicolas Léveillé + 0.6 (2013-01-11) Simplified API, only using tick_t type as timestamp and removing custom timer struct + Removed old legacy fallback code for Windows platform + 0.7 (2013-01-11) Using platform_lib for platform and compiler abstraction + 0.8 (2013-07-30) Made library standalone with no deps (not using deprecated platform_lib) + 1.0 (2021-12-15) Updated and cleaned up for clang 10+ and Visual Studio 2022 diff --git a/libs/timer_lib/test.vcxproj b/libs/timer_lib/test.vcxproj new file mode 100644 index 0000000..468b70e --- /dev/null +++ b/libs/timer_lib/test.vcxproj @@ -0,0 +1,169 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {54E95B82-7759-4825-9478-91E76F1C1C89} + Win32Proj + test + 10.0 + + + + Application + true + Unicode + v143 + + + Application + true + Unicode + v143 + + + Application + false + true + Unicode + v143 + + + Application + false + true + Unicode + v143 + + + + + + + + + + + + + + + + + + + true + + + true + build\$(ProjectName)\$(Platform)\$(Configuration)\ + build\$(ProjectName)\$(Platform)\$(Configuration)\ + + + false + + + false + build\$(ProjectName)\$(Platform)\$(Configuration)\ + build\$(ProjectName)\$(Platform)\$(Configuration)\ + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDebug + ../platform_lib + + + Console + true + winmm.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDebug + ../platform_lib + + + Console + true + winmm.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreaded + ../platform_lib + + + Console + true + true + true + winmm.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreaded + ../platform_lib + + + Console + true + true + true + winmm.lib;%(AdditionalDependencies) + + + + + + + + {321bab92-2ede-4b3b-939d-bd0be0248c8f} + + + + + + \ No newline at end of file diff --git a/libs/timer_lib/timer.c b/libs/timer_lib/timer.c new file mode 100644 index 0000000..dd6434c --- /dev/null +++ b/libs/timer_lib/timer.c @@ -0,0 +1,174 @@ +/* timer.h - Cross-platform timer library - Public Domain - 2011 Mattias Jansson / Rampant Pixels + * + * This library provides a cross-platform interface to measure + * elapsed time with (at least) millisecond accuracy. + * + * This library is put in the public domain; you can redistribute + * it and/or modify it without any restrictions. + * + */ + +#include "timer.h" + +#define TIMER_PLATFORM_WINDOWS 0 +#define TIMER_PLATFORM_APPLE 0 +#define TIMER_PLATFORM_POSIX 0 + +#if defined( _WIN32 ) || defined( _WIN64 ) +# undef TIMER_PLATFORM_WINDOWS +# define TIMER_PLATFORM_WINDOWS 1 +# define WIN32_LEAN_AND_MEAN +# include +#elif defined( __APPLE__ ) +# undef TIMER_PLATFORM_APPLE +# define TIMER_PLATFORM_APPLE 1 +# include +# include +static mach_timebase_info_data_t timerlib_info; +static void absolutetime_to_nanoseconds (uint64_t mach_time, uint64_t* clock ) { *clock = mach_time * timerlib_info.numer / timerlib_info.denom; } +#else +# undef TIMER_PLATFORM_POSIX +# define TIMER_PLATFORM_POSIX 1 +# include +# include +# include +#endif + +static tick_t timerlib_freq = 0; +static double timerlib_oofreq = 0; + + +int timer_lib_initialize( void ) +{ +#if TIMER_PLATFORM_WINDOWS + tick_t unused; + if( !QueryPerformanceFrequency( (LARGE_INTEGER*)&timerlib_freq ) || + !QueryPerformanceCounter( (LARGE_INTEGER*)&unused ) ) + return -1; +#elif TIMER_PLATFORM_APPLE + if( mach_timebase_info( &timerlib_info ) ) + return -1; + timerlib_freq = 1000000000ULL; +#elif TIMER_PLATFORM_POSIX + struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 }; + if( clock_gettime( CLOCK_MONOTONIC, &ts ) ) + return -1; + timerlib_freq = 1000000000ULL; +#endif + + timerlib_oofreq = 1.0 / (double)timerlib_freq; + + return 0; +} + + +void timer_lib_shutdown( void ) +{ +} + + +tick_t timer_current( void ) +{ +#if TIMER_PLATFORM_WINDOWS + + tick_t curclock; + QueryPerformanceCounter( (LARGE_INTEGER*)&curclock ); + return curclock; + +#elif TIMER_PLATFORM_APPLE + + tick_t curclock = 0; + absolutetime_to_nanoseconds( mach_absolute_time(), &curclock ); + return curclock; + +#elif TIMER_PLATFORM_POSIX + + struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 }; + clock_gettime( CLOCK_MONOTONIC, &ts ); + return ( (uint64_t)ts.tv_sec * 1000000000ULL ) + ts.tv_nsec; + +#endif +} + + +tick_t timer_ticks_per_second( void ) +{ + return timerlib_freq; +} + + +deltatime_t timer_elapsed( const tick_t t ) +{ + return (deltatime_t)( (double)timer_elapsed_ticks( t ) * timerlib_oofreq ); +} + + +tick_t timer_elapsed_ticks( const tick_t t ) +{ + tick_t dt = 0; + +#if TIMER_PLATFORM_WINDOWS + + tick_t curclock = t; + QueryPerformanceCounter( (LARGE_INTEGER*)&curclock ); + dt = curclock - t; + +#elif TIMER_PLATFORM_APPLE + + tick_t curclock = t; + absolutetime_to_nanoseconds( mach_absolute_time(), &curclock ); + dt = curclock - t; + +#elif TIMER_PLATFORM_POSIX + + tick_t curclock; + struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 }; + clock_gettime( CLOCK_MONOTONIC, &ts ); + + curclock = ( (tick_t)ts.tv_sec * 1000000000ULL ) + ts.tv_nsec; + dt = curclock - t; + +#endif + + return dt; +} + + +deltatime_t timer_ticks_to_seconds( const tick_t dt ) +{ + return (deltatime_t)( (double)dt * timerlib_oofreq ); +} + + +#if TIMER_PLATFORM_WINDOWS +struct __timeb64 { + __time64_t time; + unsigned short millitm; + short timezone; + short dstflag; + }; +_CRTIMP errno_t __cdecl _ftime64_s(_Out_ struct __timeb64 * _Time); +#endif + +tick_t timer_system( void ) +{ +#if TIMER_PLATFORM_WINDOWS + + struct __timeb64 tb; + _ftime64_s( &tb ); + return ( (tick_t)tb.time * 1000ULL ) + (tick_t)tb.millitm; + +#elif TIMER_PLATFORM_APPLE + + tick_t curclock = 0; + absolutetime_to_nanoseconds( mach_absolute_time(), &curclock ); + return ( curclock / 1000000ULL ); + +#elif TIMER_PLATFORM_POSIX + + struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 }; + clock_gettime( CLOCK_REALTIME, &ts ); + return ( (uint64_t)ts.tv_sec * 1000ULL ) + ( ts.tv_nsec / 1000000ULL ); + +#endif +} diff --git a/libs/timer_lib/timer.h b/libs/timer_lib/timer.h new file mode 100644 index 0000000..c7d8bba --- /dev/null +++ b/libs/timer_lib/timer.h @@ -0,0 +1,77 @@ +/* timer.h - Cross-platform timer library - Public Domain - 2011 Mattias Jansson / Rampant Pixels + * + * This library provides a cross-platform interface to measure + * elapsed time with (at least) millisecond accuracy. + * + * This library is put in the public domain; you can redistribute + * it and/or modify it without any restrictions. + * + */ + +#pragma once + +/*! \file timer.h + Time measurements */ + +#if TIMER_COMPILE +#define TIMER_API +#else +#if __cplusplus +#define TIMER_API extern "C" +#else +#define TIMER_API extern +#endif +#endif + +#if defined( _WIN32 ) || defined( _WIN64 ) + +//! Tick type +typedef unsigned __int64 tick_t; + +#else + +#include +//! Tick type +typedef uint64_t tick_t; + +#endif + +//! Deltatime type (float or double) +//typedef float deltatime_t; +typedef double deltatime_t; + + +/*! Initialize timer library */ +TIMER_API int timer_lib_initialize( void ); + +/*! Shutdown timer library */ +TIMER_API void timer_lib_shutdown( void ); + +/*! Get current timestamp, in ticks of system-specific frequency (queryable with timer_ticks_per_second), measured from some system-specific base timestamp + and not in sync with other timestamps + \return Current timestamp */ +TIMER_API tick_t timer_current( void ); + +/*! Get elapsed time since given timestamp + \param t Timestamp + \return Number of seconds elapsed */ +TIMER_API deltatime_t timer_elapsed( const tick_t t ); + +/*! Get elapsed ticks since given timestamp + \param t Timestamp + \return Number of ticks elapsed */ +TIMER_API tick_t timer_elapsed_ticks( const tick_t t ); + +/*! Get timer frequency, as number of ticks per second + \return Ticks per second */ +TIMER_API tick_t timer_ticks_per_second( void ); + +/*! Get ticks as seconds (effectively calculating ticks/timer_ticks_per_second()) + \param dt Deltatime in ticks + \return Deltatime in seconds */ +TIMER_API deltatime_t timer_ticks_to_seconds( const tick_t dt ); + +/*! Get system time, in milliseconds since the epoch (UNIX time) + \return Current timestamp, in milliseconds */ +TIMER_API tick_t timer_system( void ); + diff --git a/libs/timer_lib/timer.sln b/libs/timer_lib/timer.sln new file mode 100644 index 0000000..0146411 --- /dev/null +++ b/libs/timer_lib/timer.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "timer", "timer.vcxproj", "{321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test.vcxproj", "{54E95B82-7759-4825-9478-91E76F1C1C89}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}.Debug|Win32.ActiveCfg = Debug|Win32 + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}.Debug|Win32.Build.0 = Debug|Win32 + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}.Debug|x64.ActiveCfg = Debug|x64 + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}.Debug|x64.Build.0 = Debug|x64 + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}.Release|Win32.ActiveCfg = Release|Win32 + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}.Release|Win32.Build.0 = Release|Win32 + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}.Release|x64.ActiveCfg = Release|x64 + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F}.Release|x64.Build.0 = Release|x64 + {54E95B82-7759-4825-9478-91E76F1C1C89}.Debug|Win32.ActiveCfg = Debug|Win32 + {54E95B82-7759-4825-9478-91E76F1C1C89}.Debug|Win32.Build.0 = Debug|Win32 + {54E95B82-7759-4825-9478-91E76F1C1C89}.Debug|x64.ActiveCfg = Debug|x64 + {54E95B82-7759-4825-9478-91E76F1C1C89}.Debug|x64.Build.0 = Debug|x64 + {54E95B82-7759-4825-9478-91E76F1C1C89}.Release|Win32.ActiveCfg = Release|Win32 + {54E95B82-7759-4825-9478-91E76F1C1C89}.Release|Win32.Build.0 = Release|Win32 + {54E95B82-7759-4825-9478-91E76F1C1C89}.Release|x64.ActiveCfg = Release|x64 + {54E95B82-7759-4825-9478-91E76F1C1C89}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/libs/timer_lib/timer.vcxproj b/libs/timer_lib/timer.vcxproj new file mode 100644 index 0000000..83e5ded --- /dev/null +++ b/libs/timer_lib/timer.vcxproj @@ -0,0 +1,225 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + + + + + + + {321BAB92-2EDE-4B3B-939D-BD0BE0248C8F} + Win32Proj + timer + 10.0 + + + + StaticLibrary + true + Unicode + v143 + + + StaticLibrary + true + Unicode + v143 + + + StaticLibrary + false + true + Unicode + v143 + + + StaticLibrary + false + true + Unicode + v143 + + + + + + + + + + + + + + + + + + + $(SolutionDir) + $(Platform)\$(Configuration)\ + $(ProjectName)32d + + + $(SolutionDir) + build\$(ProjectName)\$(Platform)\$(Configuration)\ + $(ProjectName)64d + + + $(SolutionDir) + $(Platform)\$(Configuration)\ + $(ProjectName)32 + + + $(SolutionDir) + build\$(ProjectName)\$(Platform)\$(Configuration)\ + $(ProjectName)64 + + + + + + Level3 + Disabled + TIMER_COMPILE;%(PreprocessorDefinitions) + ProgramDatabase + false + false + false + false + false + false + MultiThreadedDebug + false + Fast + false + false + false + false + + + + + Windows + true + + + + + + + Level3 + Disabled + TIMER_COMPILE;%(PreprocessorDefinitions) + ProgramDatabase + false + false + false + false + false + false + MultiThreadedDebug + false + Fast + false + false + false + false + + + + + Windows + true + + + + + Level3 + + + MaxSpeed + false + true + TIMER_COMPILE;%(PreprocessorDefinitions) + ProgramDatabase + false + false + Speed + false + true + true + false + MultiThreaded + false + Fast + false + false + false + false + + + + + Windows + true + true + true + + + + + Level3 + + + MaxSpeed + false + true + TIMER_COMPILE;%(PreprocessorDefinitions) + ProgramDatabase + false + false + Speed + false + true + true + false + MultiThreaded + false + Fast + false + false + false + false + + + + + Windows + true + true + true + + + + + + \ No newline at end of file -- cgit v1.2.3-70-g09d2