From 94ce21c6c99bc514b669bd49026629f5400e15be Mon Sep 17 00:00:00 2001 From: "Artiom N." Date: Mon, 29 Jul 2024 02:03:49 +0300 Subject: [PATCH 1/3] Options prefix add. Executables build option add: #794 --- CMakeLists.txt | 28 +++++++++-- src/CMakeLists.txt | 116 +++++++++++++++++++++++---------------------- 2 files changed, 82 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1678f9f4..a326a261 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,29 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright (c) 2022, Intel Corporation - + cmake_minimum_required(VERSION 3.5) project(PCM) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) +# set(CMAKE_POSITION_INDEPENDENT_CODE ON) + + +option(PCM_NO_ASAN "Disable address sanitizer" OFF) +option(PCM_FUZZ "Enable fuzzing" OFF) +option(PCM_BUILD_EXECUTABLES "Build PCM utilities" ON) + +if(MSVC) + option(PCM_NO_STATIC_MSVC_RUNTIME_LIBRARY "Disable using static runtime under MSVC" OFF) +endif() + +foreach(opt NO_STATIC_MSVC_RUNTIME_LIBRARY;FUZZ;NO_ASAN) + if(${opt}) + message(DEPRECATION "Option \"${opt}\" is deprecated and will be removed soon. Please use \"PCM_${opt}\"") + set(PCM_${opt} ${opt}) + endif() +endforeach() include(GNUInstallDirs) @@ -19,6 +36,7 @@ foreach(file ${PCM_X_ARTIFACTS}) file(REMOVE ${file}) message(STATUS "Removing old artifact from current source directory : ${file}") endforeach() + if(PCM_X_ARTIFACTS) message(WARNING " Old pcm utilities (.x) were indicated in build folder.\n" @@ -82,12 +100,12 @@ if(UNIX) # APPLE, LINUX, FREE_BSD elseif() set (PCM_DYNAMIC "") endif() - if(NO_ASAN) + if(PCM_NO_ASAN) message(STATUS "AddressSanitizer is disabled") set(PCM_ASAN "") else() message(STATUS "AddressSanitizer is enabled") - message(STATUS "To disable AddressSanitizer, use -DNO_ASAN=1 option") + message(STATUS "To disable AddressSanitizer, use -DPCM_NO_ASAN=1 option") set(PCM_ASAN "-fsanitize=address") endif() set(PCM_HARDENING_FLAGS "-fPIE -fstack-protector -D_FORTIFY_SOURCE=2 -ftrapv ${PCM_ASAN} -fwrapv -fno-delete-null-pointer-checks -fno-strict-overflow -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer") @@ -117,12 +135,12 @@ if(UNIX) # APPLE, LINUX, FREE_BSD endif(UNIX) -if(FUZZ) +if(PCM_FUZZ) set(FUZZER_OPTIONS "-fsanitize=fuzzer,address -fprofile-instr-generate -fcoverage-mapping") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FUZZER_OPTIONS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FUZZER_OPTIONS}") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FUZZER_OPTIONS}") -endif(FUZZ) +endif(PCM_FUZZ) ####################### # Install diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 53c899c6..2b8a50cf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -68,7 +68,7 @@ if(UNIX) # LINUX, FREE_BSD, APPLE $<$:PCM_SILENT> ) - if(NO_ASAN) + if(PCM_NO_ASAN) set(PCM_DYNAMIC_ASAN "") set(PCM_STATIC_ASAN "") else() @@ -102,7 +102,7 @@ if(MSVC) file(GLOB WINDOWS_SOURCES winpmem/winpmem.cpp windows/stdafx.cpp freegetopt/getopt.cpp) add_library(PCM_STATIC STATIC ${COMMON_SOURCES} ${WINDOWS_SOURCES}) target_compile_definitions(PCM_STATIC PRIVATE UNICODE _UNICODE _CONSOLE) - if (NO_STATIC_MSVC_RUNTIME_LIBRARY) + if(PCM_NO_STATIC_MSVC_RUNTIME_LIBRARY) set(PCM_MSVC_RUNTIME_LIBRARY_OPTIONS "") else() set(PCM_MSVC_RUNTIME_LIBRARY_OPTIONS "/MT$<$:d>") @@ -175,66 +175,68 @@ endif(SIMDJSON_IS_APPLICABLE) # End of SIMDJSON dependency section ####################### -foreach(PROJECT_NAME ${PROJECT_NAMES}) - file(GLOB PROJECT_FILE ${PROJECT_NAME}.cpp) - set(LIBS PCM_STATIC) +if(PCM_BUILD_EXECUTABLES) + foreach(PROJECT_NAME ${PROJECT_NAMES}) + file(GLOB PROJECT_FILE ${PROJECT_NAME}.cpp) + set(LIBS PCM_STATIC) - add_executable(${PROJECT_NAME} ${PROJECT_FILE}) + add_executable(${PROJECT_NAME} ${PROJECT_FILE}) - if(MSVC) - target_compile_options(${PROJECT_NAME} PRIVATE "${PCM_MSVC_RUNTIME_LIBRARY_OPTIONS}") - endif(MSVC) + if(MSVC) + target_compile_options(${PROJECT_NAME} PRIVATE "${PCM_MSVC_RUNTIME_LIBRARY_OPTIONS}") + endif(MSVC) - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "${PCM_STATIC_ASAN}") - endif() - - # specific file for pcm-raw project - if(${PROJECT_NAME} STREQUAL pcm-raw) - set(LIBS ${LIBS} PCM_SIMDJSON) - endif(${PROJECT_NAME} STREQUAL pcm-raw) - - if(${PROJECT_NAME} STREQUAL pcm-sensor-server) - if(NO_SSL) - message(STATUS "SSL is disabled") - else() - message(STATUS "Compiling with SSL support, requires libssl-dev or openssl-devel or libopenssl-devel or libopenssl-dev package installed") - message(STATUS "To disable SSL support, use -DNO_SSL=1 option") - find_package(OpenSSL ${MINIMUM_OPENSSL_VERSION} QUIET) - if(OPENSSL_FOUND) - message(STATUS "OpenSSL version ${OPENSSL_VERSION} >= ${MINIMUM_OPENSSL_VERSION}, OpenSSL support enabled") - target_compile_options(${PROJECT_NAME} PRIVATE "-DUSE_SSL") - set(LIBS ${LIBS} OpenSSL::SSL OpenSSL::Crypto) - else() - message(STATUS "OpenSSL support has been disabled, the version is less than ${MINIMUM_OPENSSL_VERSION}") + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "${PCM_STATIC_ASAN}") endif() - endif() - file(READ pcm-sensor-server.service.in SENSOR_SERVICE_IN) - string(REPLACE "@@CMAKE_INSTALL_SBINDIR@@" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SBINDIR}" SENSOR_SERVICE "${SENSOR_SERVICE_IN}") - file(WRITE "${CMAKE_BINARY_DIR}/pcm-sensor-server.service" "${SENSOR_SERVICE}") - file(GLOB PROJECT_FILE ${PROJECT_NAME}.cpp pcm-accel-common.h pcm-accel-common.cpp) - target_include_directories(pcm-sensor-server PUBLIC ${CMAKE_SOURCE_DIR}) - if(LINUX_SYSTEMD) - install(FILES "${CMAKE_BINARY_DIR}/pcm-sensor-server.service" DESTINATION "${LINUX_SYSTEMD_UNITDIR}") - endif(LINUX_SYSTEMD) - endif(${PROJECT_NAME} STREQUAL pcm-sensor-server) - - if(LINUX OR FREE_BSD) - set(LIBS ${LIBS} Threads::Threads) - install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_SBINDIR}) - endif(LINUX OR FREE_BSD) - if(APPLE) - set(LIBS ${LIBS} Threads::Threads PcmMsr) - install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_SBINDIR}) - endif(APPLE) - - if(MSVC) - target_compile_definitions(${PROJECT_NAME} PRIVATE _UNICODE UNICODE _CONSOLE) # for all, except pcm-lib and pcm-service - endif(MSVC) - - target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBS}) -endforeach(PROJECT_NAME ${PROJECT_NAMES}) + # specific file for pcm-raw project + if(${PROJECT_NAME} STREQUAL pcm-raw) + set(LIBS ${LIBS} PCM_SIMDJSON) + endif(${PROJECT_NAME} STREQUAL pcm-raw) + + if(${PROJECT_NAME} STREQUAL pcm-sensor-server) + if(NO_SSL) + message(STATUS "SSL is disabled") + else() + message(STATUS "Compiling with SSL support, requires libssl-dev or openssl-devel or libopenssl-devel or libopenssl-dev package installed") + message(STATUS "To disable SSL support, use -DNO_SSL=1 option") + find_package(OpenSSL ${MINIMUM_OPENSSL_VERSION} QUIET) + if(OPENSSL_FOUND) + message(STATUS "OpenSSL version ${OPENSSL_VERSION} >= ${MINIMUM_OPENSSL_VERSION}, OpenSSL support enabled") + target_compile_options(${PROJECT_NAME} PRIVATE "-DUSE_SSL") + set(LIBS ${LIBS} OpenSSL::SSL OpenSSL::Crypto) + else() + message(STATUS "OpenSSL support has been disabled, the version is less than ${MINIMUM_OPENSSL_VERSION}") + endif() + endif() + file(READ pcm-sensor-server.service.in SENSOR_SERVICE_IN) + string(REPLACE "@@CMAKE_INSTALL_SBINDIR@@" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_SBINDIR}" SENSOR_SERVICE "${SENSOR_SERVICE_IN}") + file(WRITE "${CMAKE_BINARY_DIR}/pcm-sensor-server.service" "${SENSOR_SERVICE}") + file(GLOB PROJECT_FILE ${PROJECT_NAME}.cpp pcm-accel-common.h pcm-accel-common.cpp) + target_include_directories(pcm-sensor-server PUBLIC ${CMAKE_SOURCE_DIR}) + if(LINUX_SYSTEMD) + install(FILES "${CMAKE_BINARY_DIR}/pcm-sensor-server.service" DESTINATION "${LINUX_SYSTEMD_UNITDIR}") + endif(LINUX_SYSTEMD) + endif(${PROJECT_NAME} STREQUAL pcm-sensor-server) + + if(LINUX OR FREE_BSD) + set(LIBS ${LIBS} Threads::Threads) + install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_SBINDIR}) + endif(LINUX OR FREE_BSD) + + if(APPLE) + set(LIBS ${LIBS} Threads::Threads PcmMsr) + install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_SBINDIR}) + endif(APPLE) + + if(MSVC) + target_compile_definitions(${PROJECT_NAME} PRIVATE _UNICODE UNICODE _CONSOLE) # for all, except pcm-lib and pcm-service + endif(MSVC) + + target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBS}) + endforeach(PROJECT_NAME ${PROJECT_NAMES}) +endif(PCM_BUILD_EXECUTABLES) ####################### # Install From 01f2566c98fd6d7bc00dff6cf72572a42d35173c Mon Sep 17 00:00:00 2001 From: "Artiom N." Date: Tue, 30 Jul 2024 12:59:29 +0300 Subject: [PATCH 2/3] NO_STATIC_LIBASAN replaced with option. pcm.spec fixed: #794 --- CMakeLists.txt | 3 ++- pcm.spec | 2 +- src/CMakeLists.txt | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a326a261..37845378 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,12 +13,13 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) option(PCM_NO_ASAN "Disable address sanitizer" OFF) option(PCM_FUZZ "Enable fuzzing" OFF) option(PCM_BUILD_EXECUTABLES "Build PCM utilities" ON) +option(PCM_NO_STATIC_LIBASAN OFF) if(MSVC) option(PCM_NO_STATIC_MSVC_RUNTIME_LIBRARY "Disable using static runtime under MSVC" OFF) endif() -foreach(opt NO_STATIC_MSVC_RUNTIME_LIBRARY;FUZZ;NO_ASAN) +foreach(opt NO_STATIC_MSVC_RUNTIME_LIBRARY;FUZZ;NO_ASAN;NO_STATIC_LIBASAN) if(${opt}) message(DEPRECATION "Option \"${opt}\" is deprecated and will be removed soon. Please use \"PCM_${opt}\"") set(PCM_${opt} ${opt}) diff --git a/pcm.spec b/pcm.spec index 803d4e9b..c75d4f90 100644 --- a/pcm.spec +++ b/pcm.spec @@ -32,7 +32,7 @@ Intel(r) Performance Counter Monitor (Intel(r) PCM) is an application programmin %build mkdir build cd build -cmake -DNO_STATIC_LIBASAN=1 -DCMAKE_INSTALL_PREFIX=/usr/ -DCMAKE_BUILD_TYPE=RelWithDebInfo .. +cmake -DPCM_NO_STATIC_LIBASAN=ON -DCMAKE_INSTALL_PREFIX=/usr/ -DCMAKE_BUILD_TYPE=RelWithDebInfo .. make -j %install diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2b8a50cf..2e0b7fe2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,9 +30,9 @@ if (LINUX) endif() endif() -if(NOT DEFINED NO_ASAN) +if(NOT PCM_NO_ASAN) if(OS_ID STREQUAL "centos") - set(NO_STATIC_LIBASAN 1) + set(PCM_NO_STATIC_LIBASAN ON) message(STATUS "CentOS detected, using dynamic libasan") endif() endif() @@ -72,7 +72,7 @@ if(UNIX) # LINUX, FREE_BSD, APPLE set(PCM_DYNAMIC_ASAN "") set(PCM_STATIC_ASAN "") else() - if(NO_STATIC_LIBASAN) + if(PCM_NO_STATIC_LIBASAN) message(STATUS "Using dynamic libasan") set(PCM_DYNAMIC_ASAN "asan") set(PCM_STATIC_ASAN "") @@ -80,7 +80,7 @@ if(UNIX) # LINUX, FREE_BSD, APPLE set(PCM_DYNAMIC_ASAN "") set(PCM_STATIC_ASAN "-static-libasan") message(STATUS "Using static libasan") - message(STATUS "To use dynamic libasan, use -DNO_STATIC_LIBASAN=1 option (required for CentOS)") + message(STATUS "To use dynamic libasan, use -DPCM_NO_STATIC_LIBASAN=1 option (required for CentOS)") endif() endif() From e3f3fb72ea4ab732097a2cdd1c45d53f68abe527 Mon Sep 17 00:00:00 2001 From: "Artiom N." Date: Tue, 13 Aug 2024 23:52:19 +0300 Subject: [PATCH 3/3] PCM::checkStatus() implemented (#799) --- src/cpucounters.cpp | 67 +++++++++++++++++++++++++++++++-------------- src/cpucounters.h | 6 ++++ 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/cpucounters.cpp b/src/cpucounters.cpp index aa1b6fc9..68abab4b 100644 --- a/src/cpucounters.cpp +++ b/src/cpucounters.cpp @@ -74,6 +74,7 @@ #include #include #include +#include #ifdef __APPLE__ #include @@ -3834,30 +3835,54 @@ PCM::ErrorCode PCM::program(const PCM::ProgramMode mode_, const void * parameter return PCM::Success; } +void PCM::checkStatus(const PCM::ErrorCode status) +{ + switch (status) + { + case pcm::PCM::Success: + { + break; + } + case pcm::PCM::MSRAccessDenied: + throw std::system_error(pcm::PCM::MSRAccessDenied, std::generic_category(), + "Access to Intel(r) Performance Counter Monitor has denied (no MSR or PCI CFG space access)."); + case pcm::PCM::PMUBusy: + throw std::system_error(pcm::PCM::PMUBusy, std::generic_category(), + "Access to Intel(r) Performance Counter Monitor has denied (Performance Monitoring Unit" + " is occupied by other application). Try to stop the application that uses PMU," + " or reset PMU configuration from PCM application itself"); + default: + throw std::system_error(pcm::PCM::UnknownError, std::generic_category(), + "Access to Intel(r) Performance Counter Monitor has denied (Unknown error)."); + } +} + void PCM::checkError(const PCM::ErrorCode code) { - switch (code) + try { - case PCM::Success: - break; - case PCM::MSRAccessDenied: - std::cerr << "Access to Intel(r) Performance Counter Monitor has denied (no MSR or PCI CFG space access).\n"; - exit(EXIT_FAILURE); - case PCM::PMUBusy: - std::cerr << "Access to Intel(r) Performance Counter Monitor has denied (Performance Monitoring Unit is occupied by other application)\n"; - std::cerr << "Try to stop the application that uses PMU, or reset PMU configuration from PCM application itself\n"; - std::cerr << "You can try to reset PMU configuration now. Try to reset? (y/n)\n"; - char yn; - std::cin >> yn; - if ('y' == yn) - { - resetPMU(); - std::cerr << "PMU configuration has been reset. Try to rerun the program again.\n"; - } - exit(EXIT_FAILURE); - default: - std::cerr << "Access to Intel(r) Performance Counter Monitor has denied (Unknown error).\n"; - exit(EXIT_FAILURE); + checkStatus(code); + } + catch (const std::system_error &e) + { + switch (e.code().value()) + { + case PCM::PMUBusy: + std::cerr << e.what() << "\n" + << "You can try to reset PMU configuration now. Try to reset? (y/n)" << std::endl; + char yn; + std::cin >> yn; + if ('y' == yn) + { + resetPMU(); + std::cerr << "PMU configuration has been reset. Try to rerun the program again." << std::endl; + } + exit(EXIT_FAILURE); + case PCM::MSRAccessDenied: + default: + std::cerr << e.what() << std::endl; + exit(EXIT_FAILURE); + } } } diff --git a/src/cpucounters.h b/src/cpucounters.h index e38190ee..876b2e5f 100644 --- a/src/cpucounters.h +++ b/src/cpucounters.h @@ -1442,6 +1442,12 @@ class PCM_API PCM */ ErrorCode program(const ProgramMode mode_ = DEFAULT_EVENTS, const void * parameter_ = NULL, const bool silent = false, const int pid = -1); // program counters and start counting + /*! \brief checks the error without side effects. + \throw std::system_error generic_category exception with PCM error code. + \param code error code from the 'program' call + */ + void checkStatus(const ErrorCode status); + /*! \brief checks the error and suggests solution and/or exits the process \param code error code from the 'program' call */