diff --git a/buildtest.bat b/buildtest.bat index 3ea2ae5..364e361 100644 --- a/buildtest.bat +++ b/buildtest.bat @@ -1 +1 @@ -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 \ No newline at end of file +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/test/test.cpp b/src/test/test.cpp index 1587e16..35b9e1d 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -5,7 +5,8 @@ #define _MT #define _DLL #include - #include + #include +//#include #endif #include "../debug.h" @@ -14,11 +15,31 @@ using namespace tmr; #ifdef _WIN64 +static inline int64_t getQPCTicks() { + LARGE_INTEGER ticks; + if (!QueryPerformanceCounter(&ticks)) + { + return 0; + } + return ticks.QuadPart; +} + +static inline int64_t getQPCFrequency() { + LARGE_INTEGER ticks; + if (!QueryPerformanceFrequency(&ticks)) + { + return 0; + } + return ticks.QuadPart; +} + + int main(int argc, char** argv){ - HMODULE dell; - DWORD fracaso; + //HMODULE dell; + //DWORD fracaso; //DllImport TSCTimer* timer; - int sleep = 1000; + uint64_t sleep = 1; + uint64_t checks = 0; /* * dell = LoadLibraryExW(TEXT("libtmrtsc.dll"), NULL, NULL); @@ -31,13 +52,42 @@ int main(int argc, char** argv){ timer = TSCTimer::getTimer(); if(timer == nullptr) return -2; - uint64_t t1 = timer->getTimeStamp(); - Sleep(sleep); - uint64_t t2 = timer->getTimeStamp(); - uint64_t fr = timer->getBaseFrequency(); - std::cout << "Siestecita de: " << sleep << " ms" << std::endl; - std::cout << "Windows Momento: \nt1: " << t1 << " t2: " << t2 << " fr: " << fr << std::endl; - std::cout << "restita: " << t2-t1 << std::endl; + uint64_t wfr = getQPCFrequency(); + int64_t fr = timer->getBaseFrequency(); + + + uint64_t wt1 = getQPCTicks(); + int64_t t1 = timer->getTimeStamp(); + int64_t t2 = timer->getTimeStamp(); + while ((t2 - t1) < (sleep * fr)) { + t2 = timer->getTimeStamp(); + checks++; + } + //uint64_t fr = timer->getBaseFrequency(); + uint64_t wt2 = getQPCTicks(); + std::cout << "Sleep: " << sleep << "s\n-----------------\nTest 1: TSCTimer within QPC\n" << std::endl; + std::cout << " QPC Momento: \nt1: " << wt1 << " t2: " << wt2 << " fr: " << wfr << std::endl; + std::cout << " restita: " << wt2-wt1 << std::endl; + std::cout << " TSCTimer Momento: \nt1: " << t1 << " t2: " << t2 << " fr: " << fr << std::endl; + std::cout << " restita: " << t2-t1 << std::endl; + std::cout << " checks: " << checks << "\n-----------------\nTest 2: QPC within TSCTimer\n"<< std::endl; + + checks = 0; + t1 = timer->getTimeStamp(); + wt1 = getQPCTicks(); + wt2 = getQPCTicks(); + while ((wt2 - wt1) < (sleep * wfr)) { + wt2 = getQPCTicks(); + checks++; + } + //uint64_t fr = timer->getBaseFrequency(); + t2 = timer->getTimeStamp(); + std::cout << "QPC Momento: \n t1: " << wt1 << " t2: " << wt2 << " fr: " << wfr << std::endl; + std::cout << "restita: " << wt2-wt1 << std::endl; + std::cout << "TSCTimer Momento: \n t1: " << t1 << " t2: " << t2 << " fr: " << fr << std::endl; + std::cout << " restita: " << t2-t1 << std::endl; + std::cout << " checks: " << checks << std::endl; + return 0; } diff --git a/src/tmrtsc.cpp b/src/tmrtsc.cpp index bac4398..e940356 100644 --- a/src/tmrtsc.cpp +++ b/src/tmrtsc.cpp @@ -47,7 +47,6 @@ namespace tmr { int64_t TSCTimer::intelRetrieveART(){ //TODO: Find a valid Intel CPU to debug ART route - //if (freq == nullptr) return - uint64_t raxde, rbxnum, rcxhz; asm volatile ( ".intel_syntax noprefix\t\n" \ diff --git a/src/tmrtsc.hpp b/src/tmrtsc.hpp index 5eb10b5..6389381 100644 --- a/src/tmrtsc.hpp +++ b/src/tmrtsc.hpp @@ -32,18 +32,18 @@ namespace tmr { #ifdef _WIN64 enum winReturnValues{ - CANT_OPEN_KEY = -1, - CANT_RETRIEVE_KEY_INFO = -2, + CANT_OPEN_KEY = -1, + CANT_RETRIEVE_KEY_INFO = -2, CANT_READ_DATA_FROM_VALUE = -3, - CANT_RETRIEVE_FREQUENCY = -4 + CANT_RETRIEVE_FREQUENCY = -4 }; #endif enum ReturnValues { - OPERATION_SUCCESSFUL = 0, - INVARIANT_TSC_NOT_SUPPORTED = -1, - UNSUPPORTED_CPU = -2, + OPERATION_SUCCESSFUL = 0, + INVARIANT_TSC_NOT_SUPPORTED = -1, + UNSUPPORTED_CPU = -2, CANT_READ_FREQUENCY_FROM_SYSTEM = -3, - ART_NOT_REPORTED = -4 + ART_NOT_REPORTED = -4 }; class LIB_EXPORT TSCTimer {