]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/db/malloc_stats.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / rocksdb / db / malloc_stats.cc
CommitLineData
11fdf7f2
TL
1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2// This source code is licensed under both the GPLv2 (found in the
3// COPYING file in the root directory) and Apache 2.0 License
4// (found in the LICENSE.Apache file in the root directory).
5//
6// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7// Use of this source code is governed by a BSD-style license that can be
8// found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10#include "db/malloc_stats.h"
11
12#ifndef ROCKSDB_LITE
13#include <memory>
14#include <string.h>
15
494da23a
TL
16#include "port/jemalloc_helper.h"
17
f67539c2 18namespace ROCKSDB_NAMESPACE {
11fdf7f2
TL
19
20#ifdef ROCKSDB_JEMALLOC
494da23a 21
11fdf7f2
TL
22typedef struct {
23 char* cur;
24 char* end;
25} MallocStatus;
26
27static void GetJemallocStatus(void* mstat_arg, const char* status) {
28 MallocStatus* mstat = reinterpret_cast<MallocStatus*>(mstat_arg);
29 size_t status_len = status ? strlen(status) : 0;
30 size_t buf_size = (size_t)(mstat->end - mstat->cur);
31 if (!status_len || status_len > buf_size) {
32 return;
33 }
34
35 snprintf(mstat->cur, buf_size, "%s", status);
36 mstat->cur += status_len;
37}
11fdf7f2 38void DumpMallocStats(std::string* stats) {
494da23a
TL
39 if (!HasJemalloc()) {
40 return;
41 }
11fdf7f2
TL
42 MallocStatus mstat;
43 const unsigned int kMallocStatusLen = 1000000;
44 std::unique_ptr<char[]> buf{new char[kMallocStatusLen + 1]};
45 mstat.cur = buf.get();
46 mstat.end = buf.get() + kMallocStatusLen;
47 malloc_stats_print(GetJemallocStatus, &mstat, "");
48 stats->append(buf.get());
49}
50#else
51void DumpMallocStats(std::string*) {}
52#endif // ROCKSDB_JEMALLOC
f67539c2 53} // namespace ROCKSDB_NAMESPACE
11fdf7f2 54#endif // !ROCKSDB_LITE