Fixed compilation of the performance-test tool on linux systems

This commit is contained in:
flobernd 2017-09-15 00:42:05 +02:00
parent 8540326e33
commit 75729e8446
2 changed files with 60 additions and 30 deletions

View File

@ -73,11 +73,11 @@ target_compile_definitions("Zydis" PRIVATE "_CRT_SECURE_NO_WARNINGS" "ZYDIS_EXPO
generate_export_header("Zydis" BASE_NAME "ZYDIS" EXPORT_FILE_NAME "ZydisExportConfig.h") generate_export_header("Zydis" BASE_NAME "ZYDIS" EXPORT_FILE_NAME "ZydisExportConfig.h")
if (NOT ZYDIS_FEATURE_ENCODER AND NOT ZYDIS_FEATURE_DECODER) if (NOT ZYDIS_FEATURE_ENCODER AND NOT ZYDIS_FEATURE_DECODER)
message( message(
FATAL_ERROR FATAL_ERROR
"\nIt's dangerous to go alone! Take at least one of these:\n" "\nIt's dangerous to go alone! Take at least one of these:\n"
"[ ] ZYDIS_FEATURE_ENCODER [ ] ZYDIS_FEATURE_DECODER" "[ ] ZYDIS_FEATURE_ENCODER [ ] ZYDIS_FEATURE_DECODER"
) )
endif () endif ()
if (ZYDIS_FEATURE_EVEX) if (ZYDIS_FEATURE_EVEX)
@ -160,26 +160,29 @@ install(DIRECTORY "include" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# =============================================================================================== # # =============================================================================================== #
if (ZYDIS_BUILD_EXAMPLES) if (ZYDIS_BUILD_EXAMPLES)
if (ZYDIS_FEATURE_DECODER) if (ZYDIS_FEATURE_DECODER)
add_executable("FormatterHooks" add_executable("FormatterHooks"
"examples/FormatterHooks.c" "examples/FormatterHooks.c"
"examples/FormatHelper.h") "examples/FormatHelper.h")
target_link_libraries("FormatterHooks" "Zydis") target_link_libraries("FormatterHooks" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter") set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter")
target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS") target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS")
add_executable("ZydisFuzzIn" "examples/ZydisFuzzIn.c") add_executable("ZydisFuzzIn" "examples/ZydisFuzzIn.c")
target_link_libraries("ZydisFuzzIn" "Zydis") target_link_libraries("ZydisFuzzIn" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples") set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS") target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS")
if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Windows") add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c")
add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c") target_link_libraries("ZydisPerfTest" "Zydis")
target_link_libraries("ZydisPerfTest" "Zydis") set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples") target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS")
target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS") if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_compile_definitions("ZydisPerfTest" PRIVATE "_GNU_SOURCE")
find_package(Threads REQUIRED)
target_link_libraries("ZydisPerfTest" Threads::Threads)
endif () endif ()
endif () endif ()
endif () endif ()
# =============================================================================================== # # =============================================================================================== #
@ -187,15 +190,15 @@ endif ()
# =============================================================================================== # # =============================================================================================== #
if (ZYDIS_BUILD_TOOLS) if (ZYDIS_BUILD_TOOLS)
if (ZYDIS_FEATURE_DECODER) if (ZYDIS_FEATURE_DECODER)
add_executable("ZydisDisasm" "tools/ZydisDisasm.c") add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
target_link_libraries("ZydisDisasm" "Zydis") target_link_libraries("ZydisDisasm" "Zydis")
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools") set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS") target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")
add_executable("ZydisInfo" "tools/ZydisInfo.c") add_executable("ZydisInfo" "tools/ZydisInfo.c")
target_link_libraries("ZydisInfo" "Zydis") target_link_libraries("ZydisInfo" "Zydis")
set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools") set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS") target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS")
endif () endif ()
endif () endif ()

View File

@ -39,7 +39,7 @@
# include <mach/mach_time.h> # include <mach/mach_time.h>
#elif defined(ZYDIS_LINUX) #elif defined(ZYDIS_LINUX)
# include <sys/time.h> # include <sys/time.h>
# inlcude <pthread.h> # include <pthread.h>
#else #else
# error "Unsupported platform detected" # error "Unsupported platform detected"
#endif #endif
@ -61,7 +61,7 @@ void StartCounter()
LARGE_INTEGER li; LARGE_INTEGER li;
if (!QueryPerformanceFrequency(&li)) if (!QueryPerformanceFrequency(&li))
{ {
fputs("QueryPerformanceFrequency failed!\n", stderr); fputs("Error: QueryPerformanceFrequency failed!\n", stderr);
} }
CounterFreq = (double)li.QuadPart / 1000.0; CounterFreq = (double)li.QuadPart / 1000.0;
QueryPerformanceCounter(&li); QueryPerformanceCounter(&li);
@ -95,7 +95,21 @@ double GetCounter()
return (double)elapsed * timebaseInfo.numer / timebaseInfo.denom / 1000000; return (double)elapsed * timebaseInfo.numer / timebaseInfo.denom / 1000000;
} }
#elif defined(ZYDIS_LINUX) #elif defined(ZYDIS_LINUX)
// TODO: struct timeval t1;
void StartCounter()
{
gettimeofday(&t1, NULL);
}
double GetCounter()
{
struct timeval t2;
gettimeofday(&t2, NULL);
double t = (t2.tv_sec - t1.tv_sec) * 1000.0;
return t + (t2.tv_usec - t1.tv_usec) / 1000.0;
}
#endif #endif
/* ---------------------------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------------------------- */
@ -127,7 +141,7 @@ void adjustProcessAndThreadPriority()
pthread_t thread = pthread_self(); pthread_t thread = pthread_self();
cpu_set_t cpus; cpu_set_t cpus;
CPU_ZERO(&cpus); CPU_ZERO(&cpus);
CPU_SET(cpu, &cpus); CPU_SET(0, &cpus);
pthread_setaffinity_np(thread, sizeof(cpus), &cpus); pthread_setaffinity_np(thread, sizeof(cpus), &cpus);
#endif #endif
} }
@ -364,16 +378,29 @@ int main(int argc, char** argv)
fseek(file, 0L, SEEK_END); fseek(file, 0L, SEEK_END);
long length = ftell(file); long length = ftell(file);
void* buffer = malloc(length); void* buffer = malloc(length);
if (!buffer)
{
fprintf(stderr, "Failed to allocate %" PRIu64 " on the heap", (uint64_t)length);
goto NextFile2;
}
rewind(file); rewind(file);
fread(buffer, 1, length, file); if (fread(buffer, 1, length, file) != length)
{
fprintf(stderr,
"Could not read %" PRIu64 " bytes from file \"%s\"", (uint64_t)length, &buf[0]);
goto NextFile1;
}
printf("Testing %s ...\n", tests[i].encoding); printf("Testing %s ...\n", tests[i].encoding);
testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_MINIMAL, ZYDIS_FALSE); testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_MINIMAL, ZYDIS_FALSE);
testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_FULL , ZYDIS_FALSE); testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_FULL , ZYDIS_FALSE);
testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_FULL , ZYDIS_TRUE ); testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_FULL , ZYDIS_TRUE );
puts(""); puts("");
NextFile1:
free(buffer); free(buffer);
NextFile2:
fclose(file); fclose(file);
} }
} }