]>
Commit | Line | Data |
---|---|---|
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 | ||
18 | ||
11fdf7f2 TL |
19 | namespace rocksdb { |
20 | ||
21 | #ifdef ROCKSDB_JEMALLOC | |
494da23a | 22 | |
11fdf7f2 TL |
23 | #ifdef JEMALLOC_NO_RENAME |
24 | #define malloc_stats_print je_malloc_stats_print | |
25 | #endif | |
11fdf7f2 TL |
26 | |
27 | typedef struct { | |
28 | char* cur; | |
29 | char* end; | |
30 | } MallocStatus; | |
31 | ||
32 | static void GetJemallocStatus(void* mstat_arg, const char* status) { | |
33 | MallocStatus* mstat = reinterpret_cast<MallocStatus*>(mstat_arg); | |
34 | size_t status_len = status ? strlen(status) : 0; | |
35 | size_t buf_size = (size_t)(mstat->end - mstat->cur); | |
36 | if (!status_len || status_len > buf_size) { | |
37 | return; | |
38 | } | |
39 | ||
40 | snprintf(mstat->cur, buf_size, "%s", status); | |
41 | mstat->cur += status_len; | |
42 | } | |
11fdf7f2 | 43 | void DumpMallocStats(std::string* stats) { |
494da23a TL |
44 | if (!HasJemalloc()) { |
45 | return; | |
46 | } | |
11fdf7f2 TL |
47 | MallocStatus mstat; |
48 | const unsigned int kMallocStatusLen = 1000000; | |
49 | std::unique_ptr<char[]> buf{new char[kMallocStatusLen + 1]}; | |
50 | mstat.cur = buf.get(); | |
51 | mstat.end = buf.get() + kMallocStatusLen; | |
52 | malloc_stats_print(GetJemallocStatus, &mstat, ""); | |
53 | stats->append(buf.get()); | |
54 | } | |
55 | #else | |
56 | void DumpMallocStats(std::string*) {} | |
57 | #endif // ROCKSDB_JEMALLOC | |
494da23a | 58 | } // namespace rocksdb |
11fdf7f2 | 59 | #endif // !ROCKSDB_LITE |