summaryrefslogtreecommitdiffstats
path: root/src/perfglue
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:54:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:54:28 +0000
commite6918187568dbd01842d8d1d2c808ce16a894239 (patch)
tree64f88b554b444a49f656b6c656111a145cbbaa28 /src/perfglue
parentInitial commit. (diff)
downloadceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz
ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/perfglue')
-rw-r--r--src/perfglue/CMakeLists.txt22
-rw-r--r--src/perfglue/cpu_profiler.cc41
-rw-r--r--src/perfglue/cpu_profiler.h25
-rw-r--r--src/perfglue/disabled_heap_profiler.cc47
-rw-r--r--src/perfglue/disabled_stubs.cc25
-rw-r--r--src/perfglue/heap_profiler.cc188
-rw-r--r--src/perfglue/heap_profiler.h57
7 files changed, 405 insertions, 0 deletions
diff --git a/src/perfglue/CMakeLists.txt b/src/perfglue/CMakeLists.txt
new file mode 100644
index 000000000..9ad73d576
--- /dev/null
+++ b/src/perfglue/CMakeLists.txt
@@ -0,0 +1,22 @@
+if(ALLOCATOR STREQUAL "tcmalloc")
+ add_library(heap_profiler STATIC
+ heap_profiler.cc)
+ target_link_libraries(heap_profiler
+ gperftools::tcmalloc)
+else()
+ add_library(heap_profiler STATIC
+ disabled_heap_profiler.cc)
+endif()
+
+option(WITH_PROFILER "build extra profiler binaries" OFF)
+
+if(WITH_PROFILER)
+ find_package(gperftools 2.6.2 REQUIRED profiler)
+ add_library(cpu_profiler STATIC
+ cpu_profiler.cc)
+ target_link_libraries(cpu_profiler
+ gperftools::profiler)
+else()
+ add_library(cpu_profiler STATIC
+ disabled_stubs.cc)
+endif()
diff --git a/src/perfglue/cpu_profiler.cc b/src/perfglue/cpu_profiler.cc
new file mode 100644
index 000000000..81f2ccf8c
--- /dev/null
+++ b/src/perfglue/cpu_profiler.cc
@@ -0,0 +1,41 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2011 New Dream Network
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ *
+ */
+
+#include "acconfig.h"
+
+#include <gperftools/profiler.h>
+
+#include "common/LogClient.h"
+#include "perfglue/cpu_profiler.h"
+
+void cpu_profiler_handle_command(const std::vector<std::string> &cmd,
+ std::ostream& out)
+{
+ if (cmd.size() == 1 && cmd[0] == "status") {
+ ProfilerState st;
+ ProfilerGetCurrentState(&st);
+ out << "cpu_profiler " << (st.enabled ? "enabled":"not enabled")
+ << " start_time " << st.start_time
+ << " profile_name " << st.profile_name
+ << " samples " << st.samples_gathered;
+ }
+ else if (cmd.size() == 1 && cmd[0] == "flush") {
+ ProfilerFlush();
+ out << "cpu_profiler: flushed";
+ }
+ else {
+ out << "cpu_profiler: unrecognized command " << cmd
+ << "; expected one of status, flush.";
+ }
+}
diff --git a/src/perfglue/cpu_profiler.h b/src/perfglue/cpu_profiler.h
new file mode 100644
index 000000000..a2a8ce4ca
--- /dev/null
+++ b/src/perfglue/cpu_profiler.h
@@ -0,0 +1,25 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2011 New Dream Network
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ *
+ */
+#ifndef CEPH_PERFGLUE_CPU_PROFILER
+
+/*
+ * Ceph glue for the Google Perftools CPU profiler
+ */
+#include <string>
+#include <vector>
+
+void cpu_profiler_handle_command(const std::vector<std::string> &cmd,
+ std::ostream& out);
+
+#endif
diff --git a/src/perfglue/disabled_heap_profiler.cc b/src/perfglue/disabled_heap_profiler.cc
new file mode 100644
index 000000000..f17430bf1
--- /dev/null
+++ b/src/perfglue/disabled_heap_profiler.cc
@@ -0,0 +1,47 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2011 New Dream Network/Sage Weil <sage@newdream.net>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ *
+ */
+#include "heap_profiler.h"
+
+bool ceph_using_tcmalloc() { return false; }
+
+void ceph_heap_profiler_init() { return; }
+
+void ceph_heap_profiler_stats(char *buf, int length) { return; }
+
+void ceph_heap_release_free_memory() { return; }
+
+double ceph_heap_get_release_rate() { return 0; }
+
+void ceph_heap_set_release_rate(double value) { return; }
+
+bool ceph_heap_profiler_running() { return false; }
+
+void ceph_heap_profiler_start() { return; }
+
+void ceph_heap_profiler_stop() { return; }
+
+void ceph_heap_profiler_dump(const char *reason) { return; }
+
+bool ceph_heap_get_numeric_property(const char *property, size_t *value)
+{
+ return false;
+}
+
+bool ceph_heap_set_numeric_property(const char *property, size_t value)
+{
+ return false;
+}
+
+void ceph_heap_profiler_handle_command(const std::vector<std::string>& cmd,
+ std::ostream& out) { return; }
diff --git a/src/perfglue/disabled_stubs.cc b/src/perfglue/disabled_stubs.cc
new file mode 100644
index 000000000..40474f644
--- /dev/null
+++ b/src/perfglue/disabled_stubs.cc
@@ -0,0 +1,25 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2011 New Dream Network
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ *
+ */
+
+#include "common/LogClient.h"
+#include "perfglue/cpu_profiler.h"
+
+#include <vector>
+#include <string>
+
+void cpu_profiler_handle_command(const std::vector<std::string> &cmd,
+ std::ostream& out)
+{
+ out << "cpu_profiler support not linked in";
+}
diff --git a/src/perfglue/heap_profiler.cc b/src/perfglue/heap_profiler.cc
new file mode 100644
index 000000000..cbfc90086
--- /dev/null
+++ b/src/perfglue/heap_profiler.cc
@@ -0,0 +1,188 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2011 New Dream Network/Sage Weil <sage@newdream.net>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ *
+ */
+
+#include "acconfig.h"
+
+// Use the newer gperftools header locations if available.
+// If not, fall back to the old (gperftools < 2.0) locations.
+
+#include <gperftools/heap-profiler.h>
+#include <gperftools/malloc_extension.h>
+
+#include "heap_profiler.h"
+#include "common/environment.h"
+#include "common/LogClient.h"
+#include "global/global_context.h"
+#include "common/debug.h"
+
+#define dout_context g_ceph_context
+
+bool ceph_using_tcmalloc()
+{
+ return true;
+}
+
+void ceph_heap_profiler_init()
+{
+ // Two other interesting environment variables to set are:
+ // HEAP_PROFILE_ALLOCATION_INTERVAL, HEAP_PROFILE_INUSE_INTERVAL
+ if (get_env_bool("CEPH_HEAP_PROFILER_INIT")) {
+ ceph_heap_profiler_start();
+ }
+}
+
+void ceph_heap_profiler_stats(char *buf, int length)
+{
+ MallocExtension::instance()->GetStats(buf, length);
+}
+
+void ceph_heap_release_free_memory()
+{
+ MallocExtension::instance()->ReleaseFreeMemory();
+}
+
+double ceph_heap_get_release_rate()
+{
+ return MallocExtension::instance()->GetMemoryReleaseRate();
+}
+
+void ceph_heap_set_release_rate(double val)
+{
+ MallocExtension::instance()->SetMemoryReleaseRate(val);
+}
+
+bool ceph_heap_get_numeric_property(
+ const char *property, size_t *value)
+{
+ return MallocExtension::instance()->GetNumericProperty(
+ property,
+ value);
+}
+
+bool ceph_heap_set_numeric_property(
+ const char *property, size_t value)
+{
+ return MallocExtension::instance()->SetNumericProperty(
+ property,
+ value);
+}
+
+bool ceph_heap_profiler_running()
+{
+#ifdef HAVE_LIBTCMALLOC
+ return IsHeapProfilerRunning();
+#else
+ return false;
+#endif
+}
+
+static void get_profile_name(char *profile_name, int profile_name_len)
+{
+#if __GNUC__ && __GNUC__ >= 8
+#pragma GCC diagnostic push
+ // Don't care, it doesn't matter, and we can't do anything about it.
+#pragma GCC diagnostic ignored "-Wformat-truncation"
+#endif
+
+ char path[PATH_MAX];
+ snprintf(path, sizeof(path), "%s", g_conf()->log_file.c_str());
+ char *last_slash = rindex(path, '/');
+
+ if (last_slash == NULL) {
+ snprintf(profile_name, profile_name_len, "./%s.profile",
+ g_conf()->name.to_cstr());
+ }
+ else {
+ last_slash[1] = '\0';
+ snprintf(profile_name, profile_name_len, "%s/%s.profile",
+ path, g_conf()->name.to_cstr());
+ }
+#if __GNUC__ && __GNUC__ >= 8
+#pragma GCC diagnostic pop
+#endif
+}
+
+void ceph_heap_profiler_start()
+{
+#ifdef HAVE_LIBTCMALLOC
+ char profile_name[PATH_MAX];
+ get_profile_name(profile_name, sizeof(profile_name));
+ generic_dout(0) << "turning on heap profiler with prefix "
+ << profile_name << dendl;
+ HeapProfilerStart(profile_name);
+#endif
+}
+
+void ceph_heap_profiler_stop()
+{
+#ifdef HAVE_LIBTCMALLOC
+ HeapProfilerStop();
+#endif
+}
+
+void ceph_heap_profiler_dump(const char *reason)
+{
+#ifdef HAVE_LIBTCMALLOC
+ HeapProfilerDump(reason);
+#endif
+}
+
+#define HEAP_PROFILER_STATS_SIZE 2048
+
+void ceph_heap_profiler_handle_command(const std::vector<std::string>& cmd,
+ std::ostream& out)
+{
+#ifdef HAVE_LIBTCMALLOC
+ if (cmd.size() == 1 && cmd[0] == "dump") {
+ if (!ceph_heap_profiler_running()) {
+ out << "heap profiler not running; can't dump";
+ return;
+ }
+ char heap_stats[HEAP_PROFILER_STATS_SIZE];
+ ceph_heap_profiler_stats(heap_stats, sizeof(heap_stats));
+ out << g_conf()->name << " dumping heap profile now.\n"
+ << heap_stats;
+ ceph_heap_profiler_dump("admin request");
+ } else if (cmd.size() == 1 && cmd[0] == "start_profiler") {
+ ceph_heap_profiler_start();
+ out << g_conf()->name << " started profiler";
+ } else if (cmd.size() == 1 && cmd[0] == "stop_profiler") {
+ ceph_heap_profiler_stop();
+ out << g_conf()->name << " stopped profiler";
+ } else if (cmd.size() == 1 && cmd[0] == "release") {
+ ceph_heap_release_free_memory();
+ out << g_conf()->name << " releasing free RAM back to system.";
+ } else if (cmd.size() == 1 && cmd[0] == "get_release_rate") {
+ out << g_conf()->name << " release rate: "
+ << std::setprecision(4) << ceph_heap_get_release_rate() << "\n";
+ } else if (cmd.size() == 2 && cmd[0] == "set_release_rate") {
+ try {
+ double val = std::stod(cmd[1]);
+ ceph_heap_set_release_rate(val);
+ out << g_conf()->name << " release rate changed to: "
+ << std::setprecision(4) << ceph_heap_get_release_rate() << "\n";
+ } catch (...) {
+ out << g_conf()->name << " *** need an numerical value. ";
+ }
+ } else
+#endif
+ if (cmd.size() == 1 && cmd[0] == "stats") {
+ char heap_stats[HEAP_PROFILER_STATS_SIZE];
+ ceph_heap_profiler_stats(heap_stats, sizeof(heap_stats));
+ out << g_conf()->name << " tcmalloc heap stats:"
+ << heap_stats;
+ } else {
+ out << "unknown command " << cmd;
+ }
+}
diff --git a/src/perfglue/heap_profiler.h b/src/perfglue/heap_profiler.h
new file mode 100644
index 000000000..f53682e1d
--- /dev/null
+++ b/src/perfglue/heap_profiler.h
@@ -0,0 +1,57 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2011 New Dream Network/Sage Weil <sage@newdream.net>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ */
+#ifndef HEAP_PROFILER_H_
+#define HEAP_PROFILER_H_
+
+#include <string>
+#include <vector>
+#include "common/config.h"
+
+class LogClient;
+
+/*
+ * Ceph glue for the Google perftools heap profiler, included
+ * as part of tcmalloc. This replaces ugly function pointers
+ * and #ifdef hacks!
+ */
+bool ceph_using_tcmalloc();
+
+/*
+ * Configure the heap profiler
+ */
+void ceph_heap_profiler_init();
+
+void ceph_heap_profiler_stats(char *buf, int length);
+
+void ceph_heap_release_free_memory();
+
+double ceph_heap_get_release_rate();
+
+void ceph_heap_get_release_rate(double value);
+
+bool ceph_heap_profiler_running();
+
+void ceph_heap_profiler_start();
+
+void ceph_heap_profiler_stop();
+
+void ceph_heap_profiler_dump(const char *reason);
+
+bool ceph_heap_get_numeric_property(const char *property, size_t *value);
+
+bool ceph_heap_set_numeric_property(const char *property, size_t value);
+
+void ceph_heap_profiler_handle_command(const std::vector<std::string> &cmd,
+ std::ostream& out);
+
+#endif /* HEAP_PROFILER_H_ */