]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: add cputrace_print_to_stringstream() and make WITH_CPUTRACE configurable
authorJaya Prakash <jayaprakash@ibm.com>
Mon, 7 Jul 2025 14:49:16 +0000 (14:49 +0000)
committerJaya Prakash <jayaprakash@ibm.com>
Tue, 7 Oct 2025 14:13:48 +0000 (14:13 +0000)
- Add cputrace_print_to_stringstream() to support profiling data output into a stringstream.
- Update CMake to make WITH_CPUTRACE configurable in all build types.

Signed-off-by: Jaya Prakash <jayaprakash@ibm.com>
src/common/cputrace.cc
src/common/cputrace.h

index 7112dd2bae67bda4c303510abaed1bfc2ca7b501..2be8f9c3c74099e3be443d1cd09905f6d905cbe3 100644 (file)
@@ -377,6 +377,69 @@ void cputrace_dump(ceph::Formatter* f, const std::string& logger, const std::str
     pthread_mutex_unlock(&g_profiler.global_lock);
 }
 
+void cputrace_print_to_stringstream(std::stringstream& ss) {
+    pthread_mutex_lock(&g_profiler.global_lock);
+    ss << "cputrace:\n";
+    bool dumped = false;
+
+    for (int i = 0; i < CPUTRACE_MAX_ANCHORS; ++i) {
+        const auto& anchor = g_profiler.anchors[i];
+        if (!anchor.name) {
+            continue;
+        }
+
+        for (int j = 0; j < CPUTRACE_MAX_THREADS; ++j) {
+            if (active_contexts[i][j]) {
+                read_perf_event(active_contexts[i][j], &g_profiler.anchors[i], j);
+            }
+            aggregate_thread_results(&g_profiler.anchors[i], j);
+        }
+
+        ss << "  " << anchor.name << ":\n";
+        ss << "    call_count: " << anchor.call_count << "\n";
+
+        if (anchor.flags & HW_PROFILE_SWI) {
+            ss << "    context_switches: " << anchor.global_sum[CPUTRACE_RESULT_SWI];
+            if (anchor.call_count) {
+                ss << "\n    avg_context_switches: " << (double)anchor.global_sum[CPUTRACE_RESULT_SWI] / anchor.call_count;
+            }
+            ss << "\n";
+        }
+        if (anchor.flags & HW_PROFILE_CYC) {
+            ss << "    cpu_cycles: " << anchor.global_sum[CPUTRACE_RESULT_CYC];
+            if (anchor.call_count) {
+                ss << "\n    avg_cpu_cycles: " << (double)anchor.global_sum[CPUTRACE_RESULT_CYC] / anchor.call_count;
+            }
+            ss << "\n";
+        }
+        if (anchor.flags & HW_PROFILE_CMISS) {
+            ss << "    cache_misses: " << anchor.global_sum[CPUTRACE_RESULT_CMISS];
+            if (anchor.call_count) {
+                ss << "\n    avg_cache_misses: " << (double)anchor.global_sum[CPUTRACE_RESULT_CMISS] / anchor.call_count;
+            }
+            ss << "\n";
+        }
+        if (anchor.flags & HW_PROFILE_BMISS) {
+            ss << "    branch_misses: " << anchor.global_sum[CPUTRACE_RESULT_BMISS];
+            if (anchor.call_count) {
+                ss << "\n    avg_branch_misses: " << (double)anchor.global_sum[CPUTRACE_RESULT_BMISS] / anchor.call_count;
+            }
+            ss << "\n";
+        }
+        if (anchor.flags & HW_PROFILE_INS) {
+            ss << "    instructions: " << anchor.global_sum[CPUTRACE_RESULT_INS];
+            if (anchor.call_count) {
+                ss << "\n    avg_instructions: " << (double)anchor.global_sum[CPUTRACE_RESULT_INS] / anchor.call_count;
+            }
+            ss << "\n";
+        }
+        dumped = true;
+    }
+
+    ss << "status: " << (dumped ? "Profiling data dumped" : "No profiling data available") << "\n";
+    pthread_mutex_unlock(&g_profiler.global_lock);
+}
+
 __attribute__((constructor)) static void cputrace_init() {
     g_profiler.anchors = (cputrace_anchor*)calloc(CPUTRACE_MAX_ANCHORS, sizeof(cputrace_anchor));
     if (!g_profiler.anchors) {
index 84e069875ffddcb9f9d7a4e4e7105323d78beefb..39db7948dc5fc3dac6a83226a037bfd6650f5db2 100644 (file)
@@ -97,5 +97,6 @@ void cputrace_start(ceph::Formatter* f);
 void cputrace_stop(ceph::Formatter* f);
 void cputrace_reset(ceph::Formatter* f);
 void cputrace_dump(ceph::Formatter* f, const std::string& logger = "", const std::string& counter = "");
+void cputrace_print_to_stringstream(std::stringstream& ss);
 void cputrace_flush_thread_start();
 void cputrace_flush_thread_stop();
\ No newline at end of file