From 2035c1e10ec2c0f5167d36e21ee0779c42324a55 Mon Sep 17 00:00:00 2001 From: Hane Date: Thu, 18 Jan 2024 02:06:15 +0100 Subject: [PATCH] repo cleanup, added actual README --- README.md | 58 ++++++++++++++++++++++++++++++++------------- buildall.bat | 5 ---- builddall.bat | 5 ---- builddlib.bat | 3 --- builddtest.bat | 4 ---- buildlib.bat | 2 -- buildproto.bat | 1 - buildtest.bat | 1 - src/debug.h | 2 +- src/proto/proto.cpp | 6 +++++ src/test/test.cpp | 4 ++-- src/tmrtsc.cpp | 10 ++++++-- src/tmrtsc.hpp | 6 ++--- 13 files changed, 61 insertions(+), 46 deletions(-) delete mode 100644 buildall.bat delete mode 100644 builddall.bat delete mode 100644 builddlib.bat delete mode 100644 builddtest.bat delete mode 100644 buildlib.bat delete mode 100644 buildproto.bat delete mode 100644 buildtest.bat diff --git a/README.md b/README.md index 929252b..1293552 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,44 @@ -# tmr +x86 TSC-based tick timer written in C++ and extended ASM. -no sé si desde dentro del dll/so, desde main, lo puedo forzar a X hilo. Es lo que faltaría para tener esta impl 100% funcional* -[20:59] -no puedo hacerlo bien del todo sin acceso a un procesador intel de 12a o 13a generación (editado) -[21:00] -si pudiese clampear la dll a hilo X desde ella misma cuando ntdll la carga, al menos estaría todo bien hardcodeadito al hilo 0 y funcionaría por el poder de ser un mugroso -[21:00] -va a funcionar igual en el 9X% de casos, porque todos los so intentan sincronizar el tsc de todos los hilos hasta en multisocket -[21:00] -pero eso está técnicamente mal -[21:01] -y bueno, necesito un procesador de intel más reciente para explorar el cálculo del ART, y 12/13 gen para explorar el tsc entre hilos de cpu de distinta uarch -[21:01] -no va a pasar, así que a joderse -[21:02] -intentaré clampear desde dll a un hilo, pondré el Modo Unix™️ y a otra cosa + 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.](https://github.com/jdmccalpin/low-overhead-timers/issues/1) 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 `clang` using [**llvm-mingw UCRT 20220906**](https://github.com/mstorsjo/llvm-mingw/releases/tag/20220906). Any `MinGW`-backed `clang` compiler 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 + ``` diff --git a/buildall.bat b/buildall.bat deleted file mode 100644 index cacdbc5..0000000 --- a/buildall.bat +++ /dev/null @@ -1,5 +0,0 @@ -REM MS yo me cago en tus putisimos muertos que cojones es este bug centenario estupido y miserable con los parametros? -REM este comentario es de cuando me lo encontre hace rato, pero me hace gracia, asi que se queda - -CALL buildlib.bat -CALL buildtest.bat diff --git a/builddall.bat b/builddall.bat deleted file mode 100644 index 122e1ea..0000000 --- a/builddall.bat +++ /dev/null @@ -1,5 +0,0 @@ -REM MS yo me cago en tus putisimos muertos que cojones es este bug centenario estupido y miserable con los parametros? -REM este comentario es de cuando me lo encontre hace rato, pero me hace gracia, asi que se queda - -CALL builddlib.bat -CALL builddtest.bat diff --git a/builddlib.bat b/builddlib.bat deleted file mode 100644 index e69d0df..0000000 --- a/builddlib.bat +++ /dev/null @@ -1,3 +0,0 @@ -clang++ -DDEBUG -D_DEBUG -shared src\tmrtsc.cpp -o build\libtmrtscd.dll -Wall -Wextra -Wpedantic -std=c++11 -g -gcodeview -Wl,-pdb= -L C:\capybara\libclang\x86_64-w64-mingw32\bin -Wl,-Bstatic -l c++ -l unwind --verbose - -REM -lmsvcrt -lucrtbase -lvcruntime140_app diff --git a/builddtest.bat b/builddtest.bat deleted file mode 100644 index e729880..0000000 --- a/builddtest.bat +++ /dev/null @@ -1,4 +0,0 @@ -clang++ -DDEBUG -D_DEBUG -dynamic src\test\test.cpp -o build\testd.exe -Wall -Wextra -Wpedantic -std=c++11 -g -gcodeview -Wl,-pdb= -L C:\capybara\libclang\x86_64-w64-mingw32\bin -L D:\Contenido\Capybara\Cositas\tmr\build -Wl,-Bstatic -l c++ -l unwind -Wl,-Bdynamic -l tmrtsc --verbose - -REM -fms-runtime-lib dll_dbgx -REM -lmsvcrt -lucrtbase -lvcruntime140_app diff --git a/buildlib.bat b/buildlib.bat deleted file mode 100644 index 8c6000b..0000000 --- a/buildlib.bat +++ /dev/null @@ -1,2 +0,0 @@ -clang++ -shared src\tmrtsc.cpp -o build\libtmrtsc.dll -Wall -Wextra -Wpedantic -std=c++11 -L C:\capybara\libclang\x86_64-w64-mingw32\bin -Wl,-Bstatic -l c++ -l unwind --verbose - diff --git a/buildproto.bat b/buildproto.bat deleted file mode 100644 index 116a7a3..0000000 --- a/buildproto.bat +++ /dev/null @@ -1 +0,0 @@ -clang++ -DDEBUG src\proto\proto.cpp -o build\ayo.exe -Wall -Wextra -Wpedantic -std=c++11 -g -gcodeview -Wl,-pdb= -L C:\capybara\libclang\x86_64-w64-mingw32\bin -static-libstdc++ -static --verbose diff --git a/buildtest.bat b/buildtest.bat deleted file mode 100644 index 364e361..0000000 --- a/buildtest.bat +++ /dev/null @@ -1 +0,0 @@ -clang++ -dynamic src\test\test.cpp -o build\test.exe -Wall -Wextra -Wpedantic -std=c++11 -L C:\capybara\libclang\x86_64-w64-mingw32\bin -L D:\Contenido\Capybara\Cositas\tmr\build -Wl,-Bstatic -l c++ -l unwind -Wl,-Bdynamic -l tmrtsc --verbose diff --git a/src/debug.h b/src/debug.h index f91232b..7187017 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,6 +1,6 @@ #pragma once -#if defined (QT_DEBUG) || defined (DEBUG) || defined (_DEBUG) +#ifdef TMRDEBUG #define log_debugcpp(str) do { \ std::cout << "[DEBUG]" << "(" << __FILE__ << ":" << __LINE__ << "): " << str << std::endl; \ diff --git a/src/proto/proto.cpp b/src/proto/proto.cpp index 53ba00f..1bfa729 100644 --- a/src/proto/proto.cpp +++ b/src/proto/proto.cpp @@ -1,3 +1,4 @@ + /* * THREADIN THE LINUS: https://linux.die.net/man/2/sched_setaffinity */ @@ -27,6 +28,11 @@ Intel(R) 64 and IA-32 Architectures Software Developer's Manual Volume 3 (3A, 3B //https://github.com/jdmccalpin/low-overhead-timers/issues/1 +/* + * no sé si desde dentro del dll/so, desde main, lo puedo forzar a X hilo. Es lo que faltaría para tener esta impl 100% funcional* [20:59] no puedo hacerlo bien del todo sin acceso a un procesador intel de 12a o 13a generación (editado) [21:00] si pudiese clampear la dll a hilo X desde ella misma cuando ntdll la carga, al menos estaría todo bien hardcodeadito al hilo 0 y funcionaría por el poder de la dureza [1:00] va a funcionar igual en el 9X% de casos, porque todos los so intentan sincronizar el tsc de todos los hilos hasta en multisocket [21:00] pero eso está técnicamente mal [21:01] y bueno, necesito un procesador de intel más reciente para explorar el cálculo del ART, y 12/13 gen para explorar el tsc entre hilos de cpu de distinta uarch [21:01] no va a pasar, así que a joderse [21:02] intentaré clampear desde dll a un hilo, pondré el Modo Unix™️ y a otra cosa + */ + + #ifdef _WIN64 //#include //#includqe diff --git a/src/test/test.cpp b/src/test/test.cpp index 8126175..942a032 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -2,8 +2,8 @@ #define UNICODE #define WIN32_LEAN_AND_MEAN #define DllImport __declspec( dllimport ) - #define _MT - #define _DLL +// #define _MT +// #define _DLL #include #include //#include diff --git a/src/tmrtsc.cpp b/src/tmrtsc.cpp index e940356..80c1698 100644 --- a/src/tmrtsc.cpp +++ b/src/tmrtsc.cpp @@ -37,11 +37,12 @@ namespace tmr { int byte = 0; for (int reg = 0; reg < 3; reg++, byte = 0){ char* charifiedReg = (char*)®s[reg]; - for(; byte < 4; byte++, siguienteLetra++ ){ + for(; byte < 4; byte++, siguienteLetra++) { palabritas[siguienteLetra] = charifiedReg[byte]; } } palabritas[siguienteLetra] = '\0'; + log_debugcpp(palabritas); return palabritas; } @@ -147,7 +148,7 @@ namespace tmr { #endif - bool TSCTimer::checkRDTSCP(){ //Generic + bool TSCTimer::checkRDTSCP() { //Generic uint64_t rdx; asm volatile (".intel_syntax noprefix\t\n" \ "mov eax, 0x80000001\t\n" \ @@ -179,6 +180,11 @@ namespace tmr { TSCTimer* TSCTimer::getTimer() { if (!checkInvariantTSC()) return nullptr; //TODO debug dis + #if defined TMRDEBUG && defined _WIN64 + //AttachConsole(ATTACH_PARENT_PROCESS ); + log_debugcpp("TSC Timer debug enabled."); + //FreeConsole(); + #endif const char vendorNames[2][13] = { {"AuthenticAMD"}, {"GenuineIntel"} diff --git a/src/tmrtsc.hpp b/src/tmrtsc.hpp index 6389381..9a22094 100644 --- a/src/tmrtsc.hpp +++ b/src/tmrtsc.hpp @@ -8,8 +8,8 @@ #define _WIN32_WINNT _WIN32_WINNT_WIN10 //0x0A00 #define MAX_KEY_LENGTH 255 #define MAX_VALUE_NAME 32767 - #define _MT - #define _DLL +// #define _MT +// #define _DLL #include #include #include @@ -18,12 +18,10 @@ #define LIB_EXPORT #endif - #include #include #include "debug.h" - #if _WIN64 extern "C" { #endif