Skip to content

Commit 318d138

Browse files
authored
[rocprofiler-systems] Update logging to use spdlog library (#2428)
## Motivation - Structured logging with proper log levels (TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL) - Better performance through compile-time formatting - Consistent formatting using fmt library - Runtime log level control via arguments and environment variables - Easier maintenance and debugging capabilities ## Technical Details - Added spdlog as a submodule and integrated it into CMake build system - Created new `rocprofiler-systems-logger` library wrapping spdlog functionality - Replaced custom logging macros (`ROCPROFSYS_VERBOSE`, `ROCPROFSYS_DEBUG`, `ROCPROFSYS_FATAL`, `ROCPROFSYS_REQUIRE`, `ROCPROFSYS_CI_THROW`, etc.) with spdlog equivalents (`LOG_DEBUG`, `LOG_WARNING`, `LOG_CRITICAL`, etc.) - Implemented log level control through command-line arguments and environment variables - Converted assertion macros to proper error handling with exceptions and std::abort()
1 parent 499127c commit 318d138

111 files changed

Lines changed: 2658 additions & 2603 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@
9898
[submodule "projects/rocprofiler-systems/external/filesystem"]
9999
path = projects/rocprofiler-systems/external/filesystem
100100
url = https://github.com/gulrak/filesystem.git
101+
[submodule "projects/rocprofiler-systems/external/spdlog"]
102+
path = projects/rocprofiler-systems/external/spdlog
103+
url = https://github.com/gabime/spdlog.git
104+
tag = v1.16.0

projects/rocprofiler-systems/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Full documentation for ROCm Systems Profiler is available at [https://rocm.docs.
99
### Added
1010

1111
- Documentation for `--trace-legacy` / `-L` CLI flag for direct tracing mode.
12+
- Added dependency to `spdlog` library.
13+
- Added environment variable `ROCPROFSYS_LOG_LEVEL` which control level of logging.
14+
- Available log levels: `critical`, `error`, `warning`, `info`(default), `debug`, `trace` and `off`.
1215

1316
### Changed
1417

@@ -18,6 +21,7 @@ Full documentation for ROCm Systems Profiler is available at [https://rocm.docs.
1821
- `--trace` / `-T` CLI flag enables tracing with cached mode by default.
1922
- `--trace-legacy` / `-L` CLI flag enables legacy direct mode for tracing.
2023
- Changed thread storage allocation from a hard-coded 4096-element array to a compile-time computed size derived from the ROCPROFSYS_MAX_THREADS configuration flag.
24+
- Changed logging module to use `spdlog` library.
2125

2226
### Resolved issues
2327

@@ -30,6 +34,7 @@ Full documentation for ROCm Systems Profiler is available at [https://rocm.docs.
3034
### Deprecated
3135

3236
- `ROCPROFSYS_USE_PERFETTO` environment variable (use `ROCPROFSYS_TRACE`).
37+
- `ROCPROFSYS_VERBOSE` and `ROCPROFSYS_DEBUG` environment variables (use `ROCPROFSYS_LOG_LEVEL`).
3338

3439
## ROCm Systems Profiler 1.3.0 for ROCm 7.2.0
3540

projects/rocprofiler-systems/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ rocprofiler_systems_add_option(ROCPROFSYS_BUILD_CODECOV "Build for code coverage
236236
rocprofiler_systems_add_option(ROCPROFSYS_INSTALL_PERFETTO_TOOLS
237237
"Install perfetto tools (i.e. traced, perfetto, etc.)" OFF
238238
)
239+
240+
rocprofiler_systems_add_option(ROCPROFSYS_BUILD_SPDLOG
241+
"Enable building spdlog library internally" ON
242+
)
243+
239244
rocprofiler_systems_add_option(ROCPROFSYS_BUILD_SQLITE3
240245
"Enable building sqlite3 library internally" ON
241246
)

projects/rocprofiler-systems/cmake/Packages.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ rocprofiler_systems_add_interface_library(rocprofiler-systems-sqlite3
6262
rocprofiler_systems_add_interface_library(rocprofiler-systems-json
6363
"Use nlohmann/json for json data handling"
6464
)
65+
rocprofiler_systems_add_interface_library(rocprofiler-systems-spdlog
66+
"Provides spdlog library"
67+
)
6568
rocprofiler_systems_add_interface_library(rocprofiler-systems-timemory
6669
"Provides timemory libraries"
6770
)
@@ -675,6 +678,14 @@ include(Perfetto)
675678

676679
include(SQLite3)
677680

681+
# ----------------------------------------------------------------------------------------#
682+
#
683+
# Spdlog
684+
#
685+
# ----------------------------------------------------------------------------------------#
686+
687+
include(Spdlog)
688+
678689
# ----------------------------------------------------------------------------------------#
679690
#
680691
# NlohmannJson
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
include_guard(GLOBAL)
2+
3+
if(ROCPROFSYS_BUILD_SPDLOG)
4+
message(STATUS "Building spdlog from source!")
5+
6+
include(FetchContent)
7+
8+
rocprofiler_systems_checkout_git_submodule(
9+
RELATIVE_PATH external/spdlog
10+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
11+
REPO_URL https://github.com/gabime/spdlog.git
12+
TEST_FILE CMakeLists.txt
13+
REPO_BRANCH "v1.16.0"
14+
)
15+
16+
FetchContent_Declare(spdlog SOURCE_DIR ${PROJECT_SOURCE_DIR}/external/spdlog)
17+
18+
set(SPDLOG_BUILD_SHARED OFF CACHE BOOL "" FORCE)
19+
set(SPDLOG_BUILD_EXAMPLE OFF CACHE BOOL "" FORCE)
20+
set(SPDLOG_BUILD_TESTS OFF CACHE BOOL "" FORCE)
21+
set(SPDLOG_INSTALL OFF CACHE BOOL "" FORCE)
22+
set(SPDLOG_FMT_EXTERNAL OFF CACHE BOOL "" FORCE)
23+
24+
# Spdlog workaround for building static library
25+
set(_ROCPROFSYS_BUILD_SHARED_LIBS_BACKUP ${BUILD_SHARED_LIBS})
26+
set(BUILD_SHARED_LIBS OFF)
27+
28+
FetchContent_MakeAvailable(spdlog)
29+
30+
set(BUILD_SHARED_LIBS ${_ROCPROFSYS_BUILD_SHARED_LIBS_BACKUP})
31+
unset(_ROCPROFSYS_BUILD_SHARED_LIBS_BACKUP)
32+
33+
target_link_libraries(rocprofiler-systems-spdlog INTERFACE spdlog::spdlog)
34+
else()
35+
message(STATUS "Using system spdlog library")
36+
find_package(spdlog REQUIRED)
37+
target_link_libraries(rocprofiler-systems-spdlog INTERFACE spdlog::spdlog)
38+
endif()

projects/rocprofiler-systems/docs/install/install.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Required third-party packages
100100

101101
* `libunwind <https://www.nongnu.org/libunwind/>`_ for call-stack sampling
102102
* `SQLite <https://github.com/sqlite/sqlite>`_ for database output
103+
* `spdlog <https://github.com/gabime/spdlog>`_ for logging
103104

104105
Any of the third-party packages required by Dyninst, along with Dyninst itself, can be built and installed
105106
during the ROCm Systems Profiler build. The following list indicates the package, the version,
@@ -112,6 +113,7 @@ while Dyninst requires TBB), and the CMake option to build the package alongside
112113
"Dyninst", "13.0", "ROCm Systems Profiler", "``ROCPROFSYS_BUILD_DYNINST`` (default: OFF)"
113114
"Libunwind", "", "ROCm Systems Profiler", "``ROCPROFSYS_BUILD_LIBUNWIND`` (default: ON)"
114115
"Nlohmann/JSON", "", "ROCm Systems Profiler", "``ROCPROFSYS_BUILD_NLOHMANN_JSON`` (default: ON)"
116+
"spdlog", "", "ROCm Systems Profiler", "``ROCPROFSYS_BUILD_SPDLOG`` (default: ON)"
115117
"SQLite", "", "ROCm Systems Profiler", "``ROCPROFSYS_BUILD_SQLITE`` (default: OFF)"
116118
"TBB", "2018.6", "Dyninst", "``ROCPROFSYS_BUILD_TBB`` (default: OFF)"
117119
"ElfUtils", "0.178", "Dyninst", "``ROCPROFSYS_BUILD_ELFUTILS`` (default: OFF)"
Submodule spdlog added at 486b555

projects/rocprofiler-systems/source/bin/rocprof-sys-avail/component_categories.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct component_categories
5151
};
5252
(void) _cleanup; // unused but set if sizeof...(Tp) == 0
5353

54-
TIMEMORY_FOLD_EXPRESSION(_v.emplace(TIMEMORY_JOIN(
54+
ROCPROFSYS_FOLD_EXPRESSION(_v.emplace(TIMEMORY_JOIN(
5555
"::", "component", _cleanup(rocprofsys::utility::demangle<Tp>(), "tim::"))));
5656
}
5757

@@ -68,7 +68,7 @@ struct component_categories<void>
6868
template <size_t... Idx>
6969
void operator()(std::set<std::string>& _v, std::index_sequence<Idx...>) const
7070
{
71-
TIMEMORY_FOLD_EXPRESSION(component_categories<comp::enumerator_t<Idx>>{}(_v));
71+
ROCPROFSYS_FOLD_EXPRESSION(component_categories<comp::enumerator_t<Idx>>{}(_v));
7272
}
7373

7474
void operator()(std::set<std::string>& _v) const

projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ parse_args(int argc, char** argv, std::vector<char*>& _env,
400400
std::min<int>(_cols - parser.get_help_width() - 8, 120));
401401

402402
parser.start_group("DEBUG OPTIONS", "");
403+
404+
parser.add_argument({ "--log-level" }, "Log level")
405+
.max_count(1)
406+
.dtype("string")
407+
.choices({ "trace", "debug", "info", "warn", "error", "critical", "off" })
408+
.action([&](parser_t& p) {
409+
update_env(_env, "ROCPROFSYS_LOG_LEVEL", p.get<std::string>("log-level"));
410+
});
411+
403412
parser.add_argument({ "--monochrome" }, "Disable colorized output")
404413
.max_count(1)
405414
.dtype("bool")
@@ -410,17 +419,26 @@ parse_args(int argc, char** argv, std::vector<char*>& _env,
410419
update_env(_env, "ROCPROFSYS_MONOCHROME", (_monochrome) ? "1" : "0");
411420
update_env(_env, "MONOCHROME", (_monochrome) ? "1" : "0");
412421
});
413-
parser.add_argument({ "--debug" }, "Debug output")
422+
parser.add_argument({ "--debug" }, "[DEPRECATED Use --log-level=debug] Debug output")
414423
.max_count(1)
415424
.action([&](parser_t& p) {
416425
update_env(_env, "ROCPROFSYS_DEBUG", p.get<bool>("debug"));
426+
update_env(_env, "ROCPROFSYS_LOG_LEVEL", "debug");
417427
});
418-
parser.add_argument({ "-v", "--verbose" }, "Verbose output")
428+
parser
429+
.add_argument({ "-v", "--verbose" },
430+
"[DEPRECATED Use --log-level=trace] Verbose output")
419431
.count(1)
420432
.action([&](parser_t& p) {
421433
auto _v = p.get<int>("verbose");
422434
verbose = _v;
423435
update_env(_env, "ROCPROFSYS_VERBOSE", _v);
436+
437+
constexpr std::array<const char*, 5> log_levels = { "off", "info", "debug",
438+
"debug", "trace" };
439+
440+
auto index = std::clamp(_v + 1, 0, static_cast<int>(log_levels.size() - 1));
441+
update_env(_env, "ROCPROFSYS_LOG_LEVEL", log_levels[index]);
424442
});
425443

426444
std::string _config_file = {};

projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,21 +294,47 @@ parse_args(int argc, char** argv, std::vector<char*>& _env)
294294
update_mode::REPLACE, ":", updated_envs,
295295
original_envs);
296296
});
297-
parser.add_argument({ "--debug" }, "Debug output")
297+
298+
parser.add_argument({ "--log-level" }, "Log level")
299+
.max_count(1)
300+
.dtype("string")
301+
.choices({ "trace", "debug", "info", "warn", "error", "critical", "off" })
302+
.action([&](parser_t& p) {
303+
rocprofsys::common::update_env(
304+
_env, "ROCPROFSYS_LOG_LEVEL", p.get<std::string>("log-level"),
305+
update_mode::REPLACE, ":", updated_envs, original_envs);
306+
});
307+
308+
parser.add_argument({ "--debug" }, "[DEPRECATED Use --log-level=debug] Debug output")
298309
.max_count(1)
299310
.action([&](parser_t& p) {
300311
rocprofsys::common::update_env(_env, "ROCPROFSYS_DEBUG", p.get<bool>("debug"),
301312
update_mode::REPLACE, ":", updated_envs,
302313
original_envs);
314+
315+
rocprofsys::common::update_env(_env, "ROCPROFSYS_LOG_LEVEL", "debug",
316+
update_mode::REPLACE, ":", updated_envs,
317+
original_envs);
303318
});
304-
parser.add_argument({ "-v", "--verbose" }, "Verbose output")
319+
parser
320+
.add_argument({ "-v", "--verbose" },
321+
"[DEPRECATED Use --log-level=trace] Verbose output")
305322
.count(1)
306323
.action([&](parser_t& p) {
307324
auto _v = p.get<int>("verbose");
308325
verbose = _v;
326+
309327
rocprofsys::common::update_env(_env, "ROCPROFSYS_VERBOSE", _v,
310328
update_mode::REPLACE, ":", updated_envs,
311329
original_envs);
330+
331+
constexpr std::array<const char*, 5> log_levels = { "off", "info", "debug",
332+
"debug", "trace" };
333+
334+
auto index = std::clamp(_v + 1, 0, static_cast<int>(log_levels.size() - 1));
335+
rocprofsys::common::update_env(_env, "ROCPROFSYS_LOG_LEVEL",
336+
log_levels[index], update_mode::REPLACE, ":",
337+
updated_envs, original_envs);
312338
});
313339

314340
parser.start_group("GENERAL OPTIONS",

0 commit comments

Comments
 (0)