diff --git a/CMakeLists.txt b/CMakeLists.txt index 561a5e807..15784fa1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,6 +358,7 @@ set(SOURCES db/internal_stats.cc db/log_reader.cc db/log_writer.cc + db/malloc_stats.cc db/managed_iterator.cc db/memtable.cc db/memtable_list.cc diff --git a/TARGETS b/TARGETS index 4f5ee1c01..7a5c8f0eb 100644 --- a/TARGETS +++ b/TARGETS @@ -83,6 +83,7 @@ cpp_library( "db/internal_stats.cc", "db/log_reader.cc", "db/log_writer.cc", + "db/malloc_stats.cc", "db/managed_iterator.cc", "db/memtable.cc", "db/memtable_list.cc", diff --git a/db/db_impl.cc b/db/db_impl.cc index 765e9d29c..e660c1c93 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -18,9 +18,6 @@ #ifdef OS_SOLARIS #include #endif -#ifdef ROCKSDB_JEMALLOC -#include "jemalloc/jemalloc.h" -#endif #include #include @@ -46,6 +43,7 @@ #include "db/job_context.h" #include "db/log_reader.h" #include "db/log_writer.h" +#include "db/malloc_stats.h" #include "db/managed_iterator.h" #include "db/memtable.h" #include "db/memtable_list.h" @@ -370,39 +368,6 @@ void DBImpl::PrintStatistics() { } } -#ifndef ROCKSDB_LITE -#ifdef ROCKSDB_JEMALLOC -typedef struct { - char* cur; - char* end; -} MallocStatus; - -static void GetJemallocStatus(void* mstat_arg, const char* status) { - MallocStatus* mstat = reinterpret_cast(mstat_arg); - size_t status_len = status ? strlen(status) : 0; - size_t buf_size = (size_t)(mstat->end - mstat->cur); - if (!status_len || status_len > buf_size) { - return; - } - - snprintf(mstat->cur, buf_size, "%s", status); - mstat->cur += status_len; -} -#endif // ROCKSDB_JEMALLOC - -static void DumpMallocStats(std::string* stats) { -#ifdef ROCKSDB_JEMALLOC - MallocStatus mstat; - const unsigned int kMallocStatusLen = 1000000; - std::unique_ptr buf{new char[kMallocStatusLen + 1]}; - mstat.cur = buf.get(); - mstat.end = buf.get() + kMallocStatusLen; - je_malloc_stats_print(GetJemallocStatus, &mstat, ""); - stats->append(buf.get()); -#endif // ROCKSDB_JEMALLOC -} -#endif // !ROCKSDB_LITE - void DBImpl::MaybeDumpStats() { mutex_.Lock(); unsigned int stats_dump_period_sec = diff --git a/db/malloc_stats.cc b/db/malloc_stats.cc new file mode 100644 index 000000000..dbd2ddb7d --- /dev/null +++ b/db/malloc_stats.cc @@ -0,0 +1,52 @@ +// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. +// +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. + +#include "db/malloc_stats.h" + +#ifndef ROCKSDB_LITE +#include +#include + +namespace rocksdb { + +#ifdef ROCKSDB_JEMALLOC +#include "jemalloc/jemalloc.h" + +typedef struct { + char* cur; + char* end; +} MallocStatus; + +static void GetJemallocStatus(void* mstat_arg, const char* status) { + MallocStatus* mstat = reinterpret_cast(mstat_arg); + size_t status_len = status ? strlen(status) : 0; + size_t buf_size = (size_t)(mstat->end - mstat->cur); + if (!status_len || status_len > buf_size) { + return; + } + + snprintf(mstat->cur, buf_size, "%s", status); + mstat->cur += status_len; +} +#endif // ROCKSDB_JEMALLOC + +void DumpMallocStats(std::string* stats) { +#ifdef ROCKSDB_JEMALLOC + MallocStatus mstat; + const unsigned int kMallocStatusLen = 1000000; + std::unique_ptr buf{new char[kMallocStatusLen + 1]}; + mstat.cur = buf.get(); + mstat.end = buf.get() + kMallocStatusLen; + je_malloc_stats_print(GetJemallocStatus, &mstat, ""); + stats->append(buf.get()); +#endif // ROCKSDB_JEMALLOC +} + +} +#endif // !ROCKSDB_LITE diff --git a/db/malloc_stats.h b/db/malloc_stats.h new file mode 100644 index 000000000..212d447e2 --- /dev/null +++ b/db/malloc_stats.h @@ -0,0 +1,22 @@ +// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. +// +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. + +#pragma once + +#ifndef ROCKSDB_LITE + +#include + +namespace rocksdb { + +void DumpMallocStats(std::string*); + +} + +#endif // !ROCKSDB_LITE diff --git a/src.mk b/src.mk index c5645080a..b4ee808f2 100644 --- a/src.mk +++ b/src.mk @@ -35,6 +35,7 @@ LIB_SOURCES = \ db/internal_stats.cc \ db/log_reader.cc \ db/log_writer.cc \ + db/malloc_stats.cc \ db/managed_iterator.cc \ db/memtable.cc \ db/memtable_list.cc \