Coverage Report

Created: 2025-10-26 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/db/malloc_stats.cc
Line
Count
Source
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
#include <cstring>
13
#include <memory>
14
15
#include "port/jemalloc_helper.h"
16
17
namespace ROCKSDB_NAMESPACE {
18
19
#ifdef ROCKSDB_JEMALLOC
20
21
struct MallocStatus {
22
  char* cur;
23
  char* end;
24
};
25
26
static void GetJemallocStatus(void* mstat_arg, const char* status) {
27
  MallocStatus* mstat = static_cast<MallocStatus*>(mstat_arg);
28
  size_t status_len = status ? strlen(status) : 0;
29
  size_t buf_size = (size_t)(mstat->end - mstat->cur);
30
  if (!status_len || status_len > buf_size) {
31
    return;
32
  }
33
34
  snprintf(mstat->cur, buf_size, "%s", status);
35
  mstat->cur += status_len;
36
}
37
void DumpMallocStats(std::string* stats) {
38
  if (!HasJemalloc()) {
39
    return;
40
  }
41
  MallocStatus mstat;
42
  const unsigned int kMallocStatusLen = 1000000;
43
  std::unique_ptr<char[]> buf{new char[kMallocStatusLen + 1]};
44
  mstat.cur = buf.get();
45
  mstat.end = buf.get() + kMallocStatusLen;
46
  malloc_stats_print(GetJemallocStatus, &mstat, "");
47
  stats->append(buf.get());
48
}
49
#else
50
0
void DumpMallocStats(std::string*) {}
51
#endif  // ROCKSDB_JEMALLOC
52
}  // namespace ROCKSDB_NAMESPACE