]> git.proxmox.com Git - ceph.git/blob - ceph/src/perfglue/heap_profiler.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / perfglue / heap_profiler.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2011 New Dream Network/Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15 #include "acconfig.h"
16
17 // Use the newer gperftools header locations if available.
18 // If not, fall back to the old (gperftools < 2.0) locations.
19
20 #include <gperftools/heap-profiler.h>
21 #include <gperftools/malloc_extension.h>
22
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"
28
29 #define dout_context g_ceph_context
30
31 bool ceph_using_tcmalloc()
32 {
33 return true;
34 }
35
36 void ceph_heap_profiler_init()
37 {
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();
42 }
43 }
44
45 void ceph_heap_profiler_stats(char *buf, int length)
46 {
47 MallocExtension::instance()->GetStats(buf, length);
48 }
49
50 void ceph_heap_release_free_memory()
51 {
52 MallocExtension::instance()->ReleaseFreeMemory();
53 }
54
55 bool ceph_heap_get_numeric_property(
56 const char *property, size_t *value)
57 {
58 return MallocExtension::instance()->GetNumericProperty(
59 property,
60 value);
61 }
62
63 bool ceph_heap_set_numeric_property(
64 const char *property, size_t value)
65 {
66 return MallocExtension::instance()->SetNumericProperty(
67 property,
68 value);
69 }
70
71 bool ceph_heap_profiler_running()
72 {
73 #ifdef HAVE_LIBTCMALLOC
74 return IsHeapProfilerRunning();
75 #else
76 return false;
77 #endif
78 }
79
80 static void get_profile_name(char *profile_name, int profile_name_len)
81 {
82 char path[PATH_MAX];
83 snprintf(path, sizeof(path), "%s", g_conf->log_file.c_str());
84 char *last_slash = rindex(path, '/');
85
86 if (last_slash == NULL) {
87 snprintf(profile_name, profile_name_len, "./%s.profile",
88 g_conf->name.to_cstr());
89 }
90 else {
91 last_slash[1] = '\0';
92 snprintf(profile_name, profile_name_len, "%s/%s.profile",
93 path, g_conf->name.to_cstr());
94 }
95 }
96
97 void ceph_heap_profiler_start()
98 {
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);
105 #endif
106 }
107
108 void ceph_heap_profiler_stop()
109 {
110 #ifdef HAVE_LIBTCMALLOC
111 HeapProfilerStop();
112 #endif
113 }
114
115 void ceph_heap_profiler_dump(const char *reason)
116 {
117 #ifdef HAVE_LIBTCMALLOC
118 HeapProfilerDump(reason);
119 #endif
120 }
121
122 #define HEAP_PROFILER_STATS_SIZE 2048
123
124 void ceph_heap_profiler_handle_command(const std::vector<std::string>& cmd,
125 ostream& out)
126 {
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";
131 return;
132 }
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"
136 << heap_stats;
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.";
147 } else
148 #endif
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:"
153 << heap_stats;
154 } else {
155 out << "unknown command " << cmd;
156 }
157 }