common: add admin socket control and CMake flag for cputrace

- Introduced a separate CMake flag to enable cputrace:
    cmake -DWITH_CPUTRACE=1 ...

- Added admin socket interface for runtime control:
    - `ceph tell osd.X cputrace start`   → Start profiling
    - `ceph tell osd.X cputrace stop`    → Stop profiling
    - `ceph tell osd.X cputrace dump`    → Dump collected metrics
    - `ceph tell osd.X cputrace reset`   → Reset profiling state

This enables developers to dynamically control profiling during runtime
without rebuilding or restarting the OSD, and ensures `cputrace` remains
disabled in production unless explicitly enabled at build time.

Signed-off-by: Jaya Prakash <jayaprakash@ibm.com>
This commit is contained in:
Jaya Prakash 2025-07-03 11:25:39 +00:00
parent efc8b48366
commit 905d2a0714
4 changed files with 40 additions and 0 deletions

View File

@ -431,6 +431,8 @@ endif(WITH_LZ4)
CMAKE_DEPENDENT_OPTION(WITH_CEPH_DEBUG_MUTEX "Use debug ceph::mutex with lockdep" ON
"CMAKE_BUILD_TYPE STREQUAL Debug" OFF)
option(WITH_CPUTRACE "Enable Cputrace support via the admin socket" OFF)
#if allocator is set on command line make sure it matches below strings
set(ALLOCATOR "" CACHE STRING
"specify memory allocator to use. currently tcmalloc, tcmalloc_minimal, \

View File

@ -199,6 +199,10 @@ if(WITH_CEPH_DEBUG_MUTEX)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-DCEPH_DEBUG_MUTEX>)
endif()
if(WITH_CPUTRACE)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-DWITH_CPUTRACE>)
endif()
include(CheckCCompilerFlag)
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
CHECK_C_COMPILER_FLAG(-fstack-protector-strong HAS_STACK_PROTECT)

View File

@ -112,7 +112,13 @@ set(common_srcs
mclock_common.cc
tcp_info.cc)
if(WITH_CPUTRACE)
list(APPEND common_srcs
cputrace.cc)
endif()
include_directories(${CMAKE_SOURCE_DIR}/src/dmclock/support/src)
if(WITH_SYSTEMD)
list(APPEND common_srcs
Journald.cc)

View File

@ -67,6 +67,10 @@
// for CINIT_FLAGS
#include "common/common_init.h"
#ifdef WITH_CPUTRACE
#include "common/cputrace.h"
#endif
#include <iostream>
#include <pthread.h>
@ -685,6 +689,24 @@ int CephContext::_do_command(
else if (command == "log reopen") {
_log->reopen_log_file();
}
#ifdef WITH_CPUTRACE
else if (command == "cputrace start") {
cputrace_start(f);
}
else if (command == "cputrace stop") {
cputrace_stop(f);
}
else if (command == "cputrace dump") {
std::string logger;
std::string counter;
cmd_getval(cmdmap, "logger", logger);
cmd_getval(cmdmap, "counter", counter);
cputrace_dump(f, logger, counter);
}
else if (command == "cputrace reset") {
cputrace_reset(f);
}
#endif
else {
ceph_abort_msg("registered under wrong command?");
}
@ -781,6 +803,12 @@ CephContext::CephContext(uint32_t module_type_,
_admin_socket->register_command("log dump", _admin_hook, "dump recent log entries to log file");
_admin_socket->register_command("log reopen", _admin_hook, "reopen log file");
#ifdef WITH_CPUTRACE
_admin_socket->register_command("cputrace start", _admin_hook, "start cpu profiling");
_admin_socket->register_command("cputrace stop", _admin_hook, "stop cpu profiling");
_admin_socket->register_command("cputrace reset", _admin_hook, "reset cpu profiling");
_admin_socket->register_command("cputrace dump name=logger,type=CephString,req=false name=counter,type=CephString,req=false", _admin_hook, "dump cpu profiling results");
#endif
_crypto_none = CryptoHandler::create(CEPH_CRYPTO_NONE);
_crypto_aes = CryptoHandler::create(CEPH_CRYPTO_AES);
_crypto_random.reset(new CryptoRandom());