1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network/Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
17 // Use the newer gperftools header locations if available.
18 // If not, fall back to the old (gperftools < 2.0) locations.
20 #include <gperftools/heap-profiler.h>
21 #include <gperftools/malloc_extension.h>
23 #include "heap_profiler.h"
24 #include "common/environment.h"
25 #include "common/LogClient.h"
26 #include "global/global_context.h"
27 #include "common/debug.h"
29 #define dout_context g_ceph_context
31 bool ceph_using_tcmalloc()
36 void ceph_heap_profiler_init()
38 // Two other interesting environment variables to set are:
39 // HEAP_PROFILE_ALLOCATION_INTERVAL, HEAP_PROFILE_INUSE_INTERVAL
40 if (get_env_bool("CEPH_HEAP_PROFILER_INIT")) {
41 ceph_heap_profiler_start();
45 void ceph_heap_profiler_stats(char *buf, int length)
47 MallocExtension::instance()->GetStats(buf, length);
50 void ceph_heap_release_free_memory()
52 MallocExtension::instance()->ReleaseFreeMemory();
55 bool ceph_heap_get_numeric_property(
56 const char *property, size_t *value)
58 return MallocExtension::instance()->GetNumericProperty(
63 bool ceph_heap_set_numeric_property(
64 const char *property, size_t value)
66 return MallocExtension::instance()->SetNumericProperty(
71 bool ceph_heap_profiler_running()
73 #ifdef HAVE_LIBTCMALLOC
74 return IsHeapProfilerRunning();
80 static void get_profile_name(char *profile_name, int profile_name_len)
83 snprintf(path, sizeof(path), "%s", g_conf->log_file.c_str());
84 char *last_slash = rindex(path, '/');
86 if (last_slash == NULL) {
87 snprintf(profile_name, profile_name_len, "./%s.profile",
88 g_conf->name.to_cstr());
92 snprintf(profile_name, profile_name_len, "%s/%s.profile",
93 path, g_conf->name.to_cstr());
97 void ceph_heap_profiler_start()
99 #ifdef HAVE_LIBTCMALLOC
100 char profile_name[PATH_MAX];
101 get_profile_name(profile_name, sizeof(profile_name));
102 generic_dout(0) << "turning on heap profiler with prefix "
103 << profile_name << dendl;
104 HeapProfilerStart(profile_name);
108 void ceph_heap_profiler_stop()
110 #ifdef HAVE_LIBTCMALLOC
115 void ceph_heap_profiler_dump(const char *reason)
117 #ifdef HAVE_LIBTCMALLOC
118 HeapProfilerDump(reason);
122 #define HEAP_PROFILER_STATS_SIZE 2048
124 void ceph_heap_profiler_handle_command(const std::vector<std::string>& cmd,
127 #ifdef HAVE_LIBTCMALLOC
128 if (cmd.size() == 1 && cmd[0] == "dump") {
129 if (!ceph_heap_profiler_running()) {
130 out << "heap profiler not running; can't dump";
133 char heap_stats[HEAP_PROFILER_STATS_SIZE];
134 ceph_heap_profiler_stats(heap_stats, sizeof(heap_stats));
135 out << g_conf->name << " dumping heap profile now.\n"
137 ceph_heap_profiler_dump("admin request");
138 } else if (cmd.size() == 1 && cmd[0] == "start_profiler") {
139 ceph_heap_profiler_start();
140 out << g_conf->name << " started profiler";
141 } else if (cmd.size() == 1 && cmd[0] == "stop_profiler") {
142 ceph_heap_profiler_stop();
143 out << g_conf->name << " stopped profiler";
144 } else if (cmd.size() == 1 && cmd[0] == "release") {
145 ceph_heap_release_free_memory();
146 out << g_conf->name << " releasing free RAM back to system.";
149 if (cmd.size() == 1 && cmd[0] == "stats") {
150 char heap_stats[HEAP_PROFILER_STATS_SIZE];
151 ceph_heap_profiler_stats(heap_stats, sizeof(heap_stats));
152 out << g_conf->name << " tcmalloc heap stats:"
155 out << "unknown command " << cmd;