]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/db/malloc_stats.cc
update ceph source to reef 18.1.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
11fdf7f2
TL
13#include <string.h>
14
1e59de90
TL
15#include <memory>
16
494da23a
TL
17#include "port/jemalloc_helper.h"
18
f67539c2 19namespace ROCKSDB_NAMESPACE {
11fdf7f2
TL
20
21#ifdef ROCKSDB_JEMALLOC
494da23a 22
1e59de90 23struct MallocStatus {
11fdf7f2
TL
24 char* cur;
25 char* end;
1e59de90 26};
11fdf7f2
TL
27
28static void GetJemallocStatus(void* mstat_arg, const char* status) {
29 MallocStatus* mstat = reinterpret_cast<MallocStatus*>(mstat_arg);
30 size_t status_len = status ? strlen(status) : 0;
31 size_t buf_size = (size_t)(mstat->end - mstat->cur);
32 if (!status_len || status_len > buf_size) {
33 return;
34 }
35
36 snprintf(mstat->cur, buf_size, "%s", status);
37 mstat->cur += status_len;
38}
11fdf7f2 39void DumpMallocStats(std::string* stats) {
494da23a
TL
40 if (!HasJemalloc()) {
41 return;
42 }
11fdf7f2
TL
43 MallocStatus mstat;
44 const unsigned int kMallocStatusLen = 1000000;
45 std::unique_ptr<char[]> buf{new char[kMallocStatusLen + 1]};
46 mstat.cur = buf.get();
47 mstat.end = buf.get() + kMallocStatusLen;
48 malloc_stats_print(GetJemallocStatus, &mstat, "");
49 stats->append(buf.get());
50}
51#else
52void DumpMallocStats(std::string*) {}
53#endif // ROCKSDB_JEMALLOC
f67539c2 54} // namespace ROCKSDB_NAMESPACE
11fdf7f2 55#endif // !ROCKSDB_LITE