From 1a04b8f8b7160a6c12ea9e94d98ca16500d1febd Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Mon, 28 Sep 2020 09:31:08 -0700 Subject: [PATCH] Add valgrind support to our cmake files (#5296) --- cmake/CMakeLists.txt | 8 ++++++++ onnxruntime/test/shared_lib/test_inference.cc | 18 ++++++++++-------- tools/ci_build/build.py | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index ccf63e6ae5..c49ec84abf 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -42,6 +42,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) # Enable CTest enable_testing() +include(Dart) if(NOT CMAKE_BUILD_TYPE) message(STATUS "Build type not set - using RelWithDebInfo") @@ -128,6 +129,9 @@ option(onnxruntime_ENABLE_TRAINING "Enable training functionality." OFF) option(onnxruntime_ENABLE_TRAINING_E2E_TESTS "Enable training end-to-end tests." OFF) option(onnxruntime_USE_HOROVOD "Build with HOROVOD support" OFF) option(onnxruntime_USE_NCCL "Build with NCCL support" ON) +#A special option just for debugging and sanitize check. Please do not enable in option in retail builds. +#The option has no effect on Windows. +option(onnxruntime_USE_VALGRIND "Build with valgrind hacks" OFF) # A special build option only used for gathering code coverage info option(onnxruntime_RUN_MODELTEST_IN_DEBUG_MODE "Run model tests even in debug mode" OFF) @@ -144,6 +148,10 @@ if(MSVC AND WIN32 AND onnxruntime_FUZZ_TEST AND onnxruntime_BUILD_SHARED_LIB AND set(onnxruntime_FUZZ_ENABLED ON) endif() +if(onnxruntime_USE_VALGRIND AND NOT WIN32) + add_definitions(-DRE2_ON_VALGRIND=1) +endif() + if (onnxruntime_ENABLE_NVTX_PROFILE) add_definitions(-DENABLE_NVTX_PROFILE=1) endif() diff --git a/onnxruntime/test/shared_lib/test_inference.cc b/onnxruntime/test/shared_lib/test_inference.cc index 55682978d5..891d13737f 100644 --- a/onnxruntime/test/shared_lib/test_inference.cc +++ b/onnxruntime/test/shared_lib/test_inference.cc @@ -755,7 +755,7 @@ TEST(CApiTest, end_profiling) { char* profile_file = session_1.EndProfiling(allocator.get()); ASSERT_TRUE(std::string(profile_file).find("profile_prefix") != std::string::npos); - + allocator->Free(profile_file); // Create session with profiling disabled Ort::SessionOptions session_options_2; #ifdef _WIN32 @@ -765,8 +765,8 @@ TEST(CApiTest, end_profiling) { #endif Ort::Session session_2(*ort_env, MODEL_WITH_CUSTOM_MODEL_METADATA, session_options_2); profile_file = session_2.EndProfiling(allocator.get()); - ASSERT_TRUE(std::string(profile_file) == std::string()); + allocator->Free(profile_file); } TEST(CApiTest, get_profiling_start_time) { @@ -780,14 +780,16 @@ TEST(CApiTest, get_profiling_start_time) { #endif uint64_t before_start_time = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now().time_since_epoch()).count(); // get current time + std::chrono::high_resolution_clock::now().time_since_epoch()) + .count(); // get current time Ort::Session session_1(*ort_env, MODEL_WITH_CUSTOM_MODEL_METADATA, session_options); uint64_t profiling_start_time = session_1.GetProfilingStartTimeNs(); uint64_t after_start_time = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now().time_since_epoch()).count(); - + std::chrono::high_resolution_clock::now().time_since_epoch()) + .count(); + // the profiler's start time needs to be between before_time and after_time - ASSERT_TRUE(before_start_time <= profiling_start_time && profiling_start_time <= after_start_time); + ASSERT_TRUE(before_start_time <= profiling_start_time && profiling_start_time <= after_start_time); } TEST(CApiTest, model_metadata) { @@ -899,8 +901,8 @@ TEST(CApiTest, TestSharedAllocatorUsingCreateAndRegisterAllocator) { OrtMemoryInfo* mem_info = nullptr; const auto& api = Ort::GetApi(); - std::unique_ptr rel_info(mem_info, api.ReleaseMemoryInfo); ASSERT_TRUE(api.CreateCpuMemoryInfo(OrtArenaAllocator, OrtMemTypeDefault, &mem_info) == nullptr); + std::unique_ptr rel_info(mem_info, api.ReleaseMemoryInfo); OrtArenaCfg arena_cfg{0, -1, -1, -1}; ASSERT_TRUE(api.CreateAndRegisterAllocator(env_ptr, mem_info, &arena_cfg) == nullptr); @@ -978,4 +980,4 @@ TEST(CApiTest, TestSharingOfInitializer) { expected_dims_y, expected_values_y, nullptr); -} \ No newline at end of file +} diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index 84f9fd1370..32ea0ee3ab 100755 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -1251,7 +1251,7 @@ def run_onnxruntime_tests(args, source_dir, ctest_path, build_dir, configs): source_dir, 'cmake\\codeconv.runsettings')] + executables, cwd=cwd2, dll_path=dll_path) else: - ctest_cmd = [ctest_path, "--build-config", config, "--verbose"] + ctest_cmd = [ctest_path, "--build-config", config, "--verbose", "--timeout", "3600"] run_subprocess(ctest_cmd, cwd=cwd, dll_path=dll_path) if args.enable_pybind: