Skip to content

Commit

Permalink
Enable specifying stacktrace size at runtime. (#470)
Browse files Browse the repository at this point in the history
* Enable specifying trace size at runtime.

* Add extra optional parameter to StackTrace.
* Enable finding GTest installed in system
* Make build_config.h for unittests private.

* Address reviewer's comment.

* Make StackTrace size unsigned.
* Don't output build_config.h into binary directory.
  • Loading branch information
trivialfis authored and hcho3 committed Oct 26, 2018
1 parent 4fbca58 commit 32440d6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 44 deletions.
24 changes: 0 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,30 +203,6 @@ add_custom_target(dmlc_lint COMMAND ${CMAKE_COMMAND} -DMSVC=${MSVC} -DPYTHON_EXE

# Setup testing
if(GOOGLE_TEST)
if(MSVC)
if(MSVC_VERSION LESS 1900)
message(FATAL_ERROR "Need Visual Studio 2015 or newer to compile unit tests")
endif()
endif()

# Download and unpack googletest at configure time
message("${CMAKE_LOCAL}/gtest_cmake.in -> ${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt")
configure_file("${CMAKE_LOCAL}/gtest_cmake.in" "${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt")
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )
# Prevent GoogleTest from overriding our compiler/linker options
# when building with Visual Studio
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This adds
# the following targets: gtest, gtest_main, gmock
# and gmock_main
add_subdirectory("${CMAKE_BINARY_DIR}/googletest-src"
"${CMAKE_BINARY_DIR}/googletest-build")
include_directories("${gtest_SOURCE_DIR}/include"
"${gmock_SOURCE_DIR}/include")
include(CTest)
add_subdirectory(test/unittest)
endif()
12 changes: 6 additions & 6 deletions include/dmlc/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,14 @@ inline std::string Demangle(char const *msg_str) {
return string(msg_str);
}

inline std::string StackTrace() {
inline std::string StackTrace(
const size_t stack_size = DMLC_LOG_STACK_TRACE_SIZE) {
using std::string;
std::ostringstream stacktrace_os;
const int MAX_STACK_SIZE = DMLC_LOG_STACK_TRACE_SIZE;
void *stack[MAX_STACK_SIZE];
int nframes = backtrace(stack, MAX_STACK_SIZE);
std::vector<void*> stack(stack_size);
int nframes = backtrace(stack.data(), static_cast<int>(stack_size));
stacktrace_os << "Stack trace returned " << nframes << " entries:" << std::endl;
char **msgs = backtrace_symbols(stack, nframes);
char **msgs = backtrace_symbols(stack.data(), nframes);
if (msgs != nullptr) {
for (int frameno = 0; frameno < nframes; ++frameno) {
string msg = dmlc::Demangle(msgs[frameno]);
Expand All @@ -344,7 +344,7 @@ inline std::string demangle(char const* msg_str) {
return std::string();
}

inline std::string StackTrace() {
inline std::string StackTrace(const size_t stack_size = 0) {
return std::string("stack traces not available when "
"DMLC_LOG_STACK_TRACE is disabled at compile time.");
}
Expand Down
53 changes: 45 additions & 8 deletions test/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
# ---[ Google Test
if(MSVC)
if(MSVC_VERSION LESS 1900)
message(FATAL_ERROR "Need Visual Studio 2015 or newer to compile unit tests")
endif()
endif()

if (UNIX)
SET(CMAKE_EXE_LINKER_FLAGS "-pthread")
SET(CMAKE_EXE_LINKER_FLAGS "-pthread")
endif(UNIX)

enable_testing()
find_package(Threads REQUIRED)

file(GLOB_RECURSE UNIT_TEST_SOURCE "*.cc")
add_executable(${PROJECT_NAME}_unit_tests ${UNIT_TEST_SOURCE})
set_property(TARGET ${PROJECT_NAME}_unit_tests
PROPERTY RUNTIME_OUTPUT_DIRECTORY ${PRIVATE_RUNTIME_DIR})
PROPERTY RUNTIME_OUTPUT_DIRECTORY ${PRIVATE_RUNTIME_DIR})

message(STATUS "${CMAKE_CURRENT_SOURCE_DIR}/build_config.h.in -> ${CMAKE_CURRENT_SOURCE_DIR}/build_config.h")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/build_config.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/build_config.h")

target_link_libraries(${PROJECT_NAME}_unit_tests
gtest
dmlc
Threads::Threads
)
target_compile_definitions(${PROJECT_NAME}_unit_tests PRIVATE -DDMLC_UNIT_TESTS_USE_CMAKE)
target_include_directories(${PROJECT_NAME}_unit_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
add_test(AllTestsIn${PROJECT_NAME}UnitTests ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}_unit_tests)

find_package(GTest)
if (NOT GTEST_FOUND)
message(STATUS "GTest not found, downloading GTest.")
# Download and unpack googletest at configure time
message("${CMAKE_LOCAL}/gtest_cmake.in -> ${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt")
configure_file("${CMAKE_LOCAL}/gtest_cmake.in" "${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt")
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )
# Prevent GoogleTest from overriding our compiler/linker options
# when building with Visual Studio
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This adds
# the following targets: gtest, gtest_main, gmock
# and gmock_main
add_subdirectory("${CMAKE_BINARY_DIR}/googletest-src"
"${CMAKE_BINARY_DIR}/googletest-build")
include_directories("${gtest_SOURCE_DIR}/include"
"${gmock_SOURCE_DIR}/include")
target_link_libraries(${PROJECT_NAME}_unit_tests
gtest
dmlc
Threads::Threads)
else()
include_directories(${GTEST_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME}_unit_tests
${GTEST_LIBRARIES}
dmlc
Threads::Threads)
endif()

add_test(AllTestsIn${PROJECT_NAME}UnitTests ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}_unit_tests)
2 changes: 1 addition & 1 deletion test/unittest/unittest_inputsplit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ TEST(InputSplit, test_split_libsvm_distributed) {
#ifdef DMLC_UNIT_TESTS_USE_CMAKE
/* Don't run the following when CMake is not used */

#include <build_config.h>
#include "./build_config.h"

TEST(InputSplit, test_recordio) {
dmlc::TemporaryDirectory tempdir;
Expand Down
2 changes: 1 addition & 1 deletion test/unittest/unittest_lockfree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ TEST(Lockfree, BlockingConcurrentQueue) {

for(size_t x = 0; x < ITEM_COUNT; ++x) {
std::unique_lock<std::mutex> lk(data->cs_map_);
data->thread_map_.insert(x);
data->thread_map_.insert(static_cast<int>(x));
threads.create(TName("BlockingPullThread", x), true, BlockingPullThread<BlockingQueue>, x, data);
}
data->ready_->signal();
Expand Down
3 changes: 1 addition & 2 deletions test/unittest/unittest_logging.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright by Contributors
#define DMLC_LOG_FATAL_THROW 0

#include <dmlc/logging.h>
#include <gtest/gtest.h>

using namespace std;

TEST(Logging, basics) {
LOG(INFO) << "hello";
LOG(ERROR) << "error";
Expand Down
4 changes: 2 additions & 2 deletions test/unittest/unittest_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ TEST(CSVParser, test_int32_parse) {
char *out_data = const_cast<char *>(data.c_str());
parser->CallParseBlock(out_data, out_data + data.size(), rctr);
for (size_t i = 0; i < rctr->value.size(); i++) {
CHECK((i+20000000) == rctr->value[i]);
CHECK((i+20000000) == (size_t)rctr->value[i]);
}
}

Expand All @@ -134,7 +134,7 @@ TEST(CSVParser, test_int64_parse) {
char *out_data = const_cast<char *>(data.c_str());
parser->CallParseBlock(out_data, out_data + data.size(), rctr);
for (size_t i = 0; i < rctr->value.size(); i++) {
CHECK((i+2147483648) == rctr->value[i]);
CHECK((i+2147483648) == (size_t)rctr->value[i]);
}
}

Expand Down

0 comments on commit 32440d6

Please sign in to comment.