From 8540326e333fdea2bd9b4eb0b3ce4b8f4fe61fe3 Mon Sep 17 00:00:00 2001 From: flobernd Date: Thu, 14 Sep 2017 23:41:25 +0200 Subject: [PATCH] Improved performance-test tool --- examples/ZydisPerfTest.c | 49 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/examples/ZydisPerfTest.c b/examples/ZydisPerfTest.c index 811ed72..f84df7a 100644 --- a/examples/ZydisPerfTest.c +++ b/examples/ZydisPerfTest.c @@ -39,6 +39,7 @@ # include #elif defined(ZYDIS_LINUX) # include +# inlcude #else # error "Unsupported platform detected" #endif @@ -47,6 +48,10 @@ /* Helper functions */ /* ============================================================================================== */ +/* ---------------------------------------------------------------------------------------------- */ +/* Time measurement */ +/* ---------------------------------------------------------------------------------------------- */ + #if defined(ZYDIS_WINDOWS) double CounterFreq = 0.0; uint64_t CounterStart = 0; @@ -93,6 +98,42 @@ double GetCounter() // TODO: #endif +/* ---------------------------------------------------------------------------------------------- */ +/* Process & Thread Priority */ +/* ---------------------------------------------------------------------------------------------- */ + +void adjustProcessAndThreadPriority() +{ +#ifdef ZYDIS_WINDOWS + SYSTEM_INFO info; + GetSystemInfo(&info); + if (info.dwNumberOfProcessors > 1) + { + if (!SetThreadAffinityMask(GetCurrentThread(), (DWORD_PTR)1)) + { + fputs("Warning: Could not set thread affinity mask.", stderr); + } + if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) + { + fputs("Warning: Could not set process priority class.", stderr); + } + if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)) + { + fputs("Warning: Could not set thread priority class.", stderr); + } + } +#endif +#ifdef ZYDIS_LINUX + pthread_t thread = pthread_self(); + cpu_set_t cpus; + CPU_ZERO(&cpus); + CPU_SET(cpu, &cpus); + pthread_setaffinity_np(thread, sizeof(cpus), &cpus); +#endif +} + +/* ---------------------------------------------------------------------------------------------- */ + /* ============================================================================================== */ /* Internal functions */ /* ============================================================================================== */ @@ -150,6 +191,10 @@ uint64_t processBuffer(const char* buffer, size_t length, ZydisDecodeGranularity void testPerformance(const char* buffer, size_t length, ZydisDecodeGranularity granularity, ZydisBool format) { + // Cache warmup + processBuffer(buffer, length, granularity, format); + + // Testing uint64_t count = 0; StartCounter(); for (uint8_t j = 0; j < 100; ++j) @@ -286,9 +331,11 @@ int main(int argc, char** argv) if (generate) { time_t t; - srand((unsigned) time(&t)); + srand((unsigned)time(&t)); } + adjustProcessAndThreadPriority(); + for (uint8_t i = 0; i < ZYDIS_ARRAY_SIZE(tests); ++i) { FILE* file;