Skip to content

Commit 3fcfaad

Browse files
authored
Add CMake flag to log C API invocations, to aid debugging (#5925)
* Add CMake flag to log C API invocations, to aid debugging * Remove unnecessary parentheses
1 parent 3b88bc9 commit 3fcfaad

File tree

6 files changed

+18
-1
lines changed

6 files changed

+18
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ option(USE_DEBUG_OUTPUT "Dump internal training results like gradients and predi
3434
Should only be used for debugging." OFF)
3535
option(FORCE_COLORED_OUTPUT "Force colored output from compilers, useful when ninja is used instead of make." OFF)
3636
option(ENABLE_ALL_WARNINGS "Enable all compiler warnings. Only effective for GCC/Clang" OFF)
37+
option(LOG_CAPI_INVOCATION "Log all C API invocations for debugging" OFF)
3738
option(GOOGLE_TEST "Build google tests" OFF)
3839
option(USE_DMLC_GTEST "Use google tests bundled with dmlc-core submodule" OFF)
3940
option(USE_NVTX "Build with cuda profiling annotations. Developers only." OFF)

jvm-packages/create_jni.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
"USE_CUDA": "OFF",
2323
"USE_NCCL": "OFF",
24-
"JVM_BINDINGS": "ON"
24+
"JVM_BINDINGS": "ON",
25+
"LOG_CAPI_INVOCATION": "OFF"
2526
}
2627

2728

@@ -70,6 +71,7 @@ def normpath(path):
7071

7172
if __name__ == "__main__":
7273
parser = argparse.ArgumentParser()
74+
parser.add_argument('--log-capi-invocation', type=str, choices=['ON', 'OFF'], default='OFF')
7375
parser.add_argument('--use-cuda', type=str, choices=['ON', 'OFF'], default='OFF')
7476
cli_args = parser.parse_args()
7577

@@ -93,6 +95,9 @@ def normpath(path):
9395
else:
9496
maybe_parallel_build = ""
9597

98+
if cli_args.log_capi_invocation == 'ON':
99+
CONFIG['LOG_CAPI_INVOCATION'] = 'ON'
100+
96101
if cli_args.use_cuda == 'ON':
97102
CONFIG['USE_CUDA'] = 'ON'
98103
CONFIG['USE_NCCL'] = 'ON'

jvm-packages/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<scala.version>2.12.8</scala.version>
3939
<scala.binary.version>2.12</scala.binary.version>
4040
<hadoop.version>2.7.3</hadoop.version>
41+
<log.capi.invocation>OFF</log.capi.invocation>
4142
<use.cuda>OFF</use.cuda>
4243
</properties>
4344
<repositories>

jvm-packages/xgboost4j/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
<executable>python</executable>
8585
<arguments>
8686
<argument>create_jni.py</argument>
87+
<argument>--log-capi-invocation</argument>
88+
<argument>${log.capi.invocation}</argument>
8789
<argument>--use-cuda</argument>
8890
<argument>${use.cuda}</argument>
8991
</arguments>

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ if (ENABLE_ALL_WARNINGS)
7676
target_compile_options(objxgboost PUBLIC
7777
$<IF:$<COMPILE_LANGUAGE:CUDA>,-Xcompiler=-Wall -Xcompiler=-Wextra,-Wall -Wextra>)
7878
endif (ENABLE_ALL_WARNINGS)
79+
if (LOG_CAPI_INVOCATION)
80+
target_compile_definitions(objxgboost PUBLIC -DLOG_CAPI_INVOCATION=1)
81+
endif (LOG_CAPI_INVOCATION)
7982

8083
set_target_properties(objxgboost PROPERTIES
8184
POSITION_INDEPENDENT_CODE ON

src/c_api/c_api_error.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
#include <dmlc/logging.h>
1111

1212
/*! \brief macro to guard beginning and end section of all functions */
13+
#ifdef LOG_CAPI_INVOCATION
14+
#define API_BEGIN() \
15+
LOG(CONSOLE) << "[XGBoost C API invocation] " << __PRETTY_FUNCTION__; try {
16+
#else // LOG_CAPI_INVOCATION
1317
#define API_BEGIN() try {
18+
#endif // LOG_CAPI_INVOCATION
1419
/*! \brief every function starts with API_BEGIN();
1520
and finishes with API_END() or API_END_HANDLE_ERROR */
1621
#define API_END() } catch(dmlc::Error &_except_) { return XGBAPIHandleException(_except_); } return 0; // NOLINT(*)

0 commit comments

Comments
 (0)