mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-27 11:43:49 +00:00
0b6bc101ba
Summary: To help service owners to manage their memory budget effectively, we have been working towards counting all major memory users inside RocksDB towards a single global memory limit (see e.g. https://github.com/facebook/rocksdb/wiki/Write-Buffer-Manager#cost-memory-used-in-memtable-to-block-cache). The global limit is specified by the capacity of the block-based table's block cache, and is technically implemented by inserting dummy entries ("reservations") into the block cache. The goal of this task is to support charging the memory usage of the new blob cache against this global memory limit when the backing cache of the blob cache and the block cache are different. This PR is a part of https://github.com/facebook/rocksdb/issues/10156 Pull Request resolved: https://github.com/facebook/rocksdb/pull/10321 Reviewed By: ltamasi Differential Revision: D37913590 Pulled By: gangliao fbshipit-source-id: eaacf23907f82dc7d18964a3f24d7039a2937a72
133 lines
3.6 KiB
C++
133 lines
3.6 KiB
C++
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
#include "cache/cache_entry_roles.h"
|
|
|
|
#include <mutex>
|
|
|
|
#include "port/lang.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
std::array<std::string, kNumCacheEntryRoles> kCacheEntryRoleToCamelString{{
|
|
"DataBlock",
|
|
"FilterBlock",
|
|
"FilterMetaBlock",
|
|
"DeprecatedFilterBlock",
|
|
"IndexBlock",
|
|
"OtherBlock",
|
|
"WriteBuffer",
|
|
"CompressionDictionaryBuildingBuffer",
|
|
"FilterConstruction",
|
|
"BlockBasedTableReader",
|
|
"FileMetadata",
|
|
"BlobCache",
|
|
"Misc",
|
|
}};
|
|
|
|
std::array<std::string, kNumCacheEntryRoles> kCacheEntryRoleToHyphenString{{
|
|
"data-block",
|
|
"filter-block",
|
|
"filter-meta-block",
|
|
"deprecated-filter-block",
|
|
"index-block",
|
|
"other-block",
|
|
"write-buffer",
|
|
"compression-dictionary-building-buffer",
|
|
"filter-construction",
|
|
"block-based-table-reader",
|
|
"file-metadata",
|
|
"blob-cache",
|
|
"misc",
|
|
}};
|
|
|
|
const std::string& GetCacheEntryRoleName(CacheEntryRole role) {
|
|
return kCacheEntryRoleToHyphenString[static_cast<size_t>(role)];
|
|
}
|
|
|
|
const std::string& BlockCacheEntryStatsMapKeys::CacheId() {
|
|
static const std::string kCacheId = "id";
|
|
return kCacheId;
|
|
}
|
|
|
|
const std::string& BlockCacheEntryStatsMapKeys::CacheCapacityBytes() {
|
|
static const std::string kCacheCapacityBytes = "capacity";
|
|
return kCacheCapacityBytes;
|
|
}
|
|
|
|
const std::string&
|
|
BlockCacheEntryStatsMapKeys::LastCollectionDurationSeconds() {
|
|
static const std::string kLastCollectionDurationSeconds =
|
|
"secs_for_last_collection";
|
|
return kLastCollectionDurationSeconds;
|
|
}
|
|
|
|
const std::string& BlockCacheEntryStatsMapKeys::LastCollectionAgeSeconds() {
|
|
static const std::string kLastCollectionAgeSeconds =
|
|
"secs_since_last_collection";
|
|
return kLastCollectionAgeSeconds;
|
|
}
|
|
|
|
namespace {
|
|
|
|
std::string GetPrefixedCacheEntryRoleName(const std::string& prefix,
|
|
CacheEntryRole role) {
|
|
const std::string& role_name = GetCacheEntryRoleName(role);
|
|
std::string prefixed_role_name;
|
|
prefixed_role_name.reserve(prefix.size() + role_name.size());
|
|
prefixed_role_name.append(prefix);
|
|
prefixed_role_name.append(role_name);
|
|
return prefixed_role_name;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::string BlockCacheEntryStatsMapKeys::EntryCount(CacheEntryRole role) {
|
|
const static std::string kPrefix = "count.";
|
|
return GetPrefixedCacheEntryRoleName(kPrefix, role);
|
|
}
|
|
|
|
std::string BlockCacheEntryStatsMapKeys::UsedBytes(CacheEntryRole role) {
|
|
const static std::string kPrefix = "bytes.";
|
|
return GetPrefixedCacheEntryRoleName(kPrefix, role);
|
|
}
|
|
|
|
std::string BlockCacheEntryStatsMapKeys::UsedPercent(CacheEntryRole role) {
|
|
const static std::string kPrefix = "percent.";
|
|
return GetPrefixedCacheEntryRoleName(kPrefix, role);
|
|
}
|
|
|
|
namespace {
|
|
|
|
struct Registry {
|
|
std::mutex mutex;
|
|
UnorderedMap<Cache::DeleterFn, CacheEntryRole> role_map;
|
|
void Register(Cache::DeleterFn fn, CacheEntryRole role) {
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
role_map[fn] = role;
|
|
}
|
|
UnorderedMap<Cache::DeleterFn, CacheEntryRole> Copy() {
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
return role_map;
|
|
}
|
|
};
|
|
|
|
Registry& GetRegistry() {
|
|
STATIC_AVOID_DESTRUCTION(Registry, registry);
|
|
return registry;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void RegisterCacheDeleterRole(Cache::DeleterFn fn, CacheEntryRole role) {
|
|
GetRegistry().Register(fn, role);
|
|
}
|
|
|
|
UnorderedMap<Cache::DeleterFn, CacheEntryRole> CopyCacheDeleterRoleMap() {
|
|
return GetRegistry().Copy();
|
|
}
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|