summaryrefslogtreecommitdiffstats
path: root/src/rocksdb/cache/cache_entry_roles.cc
blob: b27349554d32a4b04ee28f5d3afe7c5853d3a604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//  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",
    "BlobValue",
    "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-value",
    "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