2.9 KiB
x86 TSC-based tick timer written in C++ and extended ASM.
This timer queries RDTSCP/RDTSC manually on compatible systems and presents a quick and easy-to-use interface to obtain timestamps. On Windows, this is basically what QueryPerformanceCounter achieves behind the curtains (and with fallbacks where not applicable, like HPET), but with lower resolution.
While functional on some CPU families (Zen/+ and Haswell have been tested), this library is not ready for use. Linux support is TBD, alongside testing on different CPU families and implementing support for heterogeneous architectures and products (such as Tiger Lake+/Zen4+Zen4c systems).
Worth noting as well is the fact that calculating TSC resolution from user-space on an AMD CPU is not possible. Precise enough estimations can be calculated, but this artificial limitation adds a non-insignificant level of friction to this project.
A small console program to test this library and comparing it agains QueryPerformancecounter is provided, busywaiting for a second and comparing both.
But why?
During development of the mouse-on-keyboard script, I grew curious about this topic. Win32's Sleep() is a reliably unreliable time-skipping measure, subject to the wishes of thread time-slicing and scheduling. High resolution timers in the vein of QueryPerformanceCounter are what Win32 offers in this regard, but a spark of interest and curiosity had already grown about this topic. I created this library, this incomplete pet-project, as a means to learn and refresh knowledge about x86 ASM, registry traversal and modification, modern OSes, current x86 hardware features and shared libraries.
How to build
Build requirements
- Both test and Developed and tested with
clangusing llvm-mingw UCRT 20220906. AnyMinGW-backedclangcompiler should work, but your mileage may vary. - This lib has only been compiled and tested as a dynamic lib, but it should work statically linked as well.
How to compile
Add TMRDEBUG to print debug information. Currently, it only offers relevant information for ART calculation.
- Library
clang++ -shared src\tmrtsc.cpp -o libtmrtsc.dll -Wall -Wextra -Wpedantic -std=c++11 -L C:\pathtollvmmingw\x86_64-w64-mingw32\lib -Wl,-Bstatic -l c++ --verbose
- Test
clang++ src\test\test.cpp -o testd.exe -Wall -Wextra -Wpedantic -std=c++11 -L C:\pathtollvmmingw\x86_64-w64-mingw32\lib -L . -Wl,-Bstatic -l c++ -Wl,-Bdynamic -l tmrtsc --verbose
Bonus
This repo contains this project's prototipe in .\src\proto, which can be messed with build very similarly:
clang++ src\proto\proto.cpp -o proto.exe -Wall -Wextra -Wpedantic -std=c++11 -L C:\pathtollvmmingw\x86_64-w64-mingw32\lib -static-libstdc++ --verbose