Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
udi-speedb committed Mar 14, 2024
1 parent d6c5760 commit 2ceceb5
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 20 deletions.
6 changes: 4 additions & 2 deletions db/db_block_cache_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,10 @@ class DBBlockCacheTest : public DBTestBase {
for (const auto cfh : cf_handles) {
auto cfh_impl = static_cast<ColumnFamilyHandleImpl*>(cfh);
auto cache_owner_id = cfh_impl->cfd()->GetCacheOwnerId();
total_role_charges_all_cfs +=
actual_stats.charge_per_item_owner[cache_owner_id][role_idx];
for (auto level_cat_idx = 0U; level_cat_idx < pinning::kNumLevelCategories; ++level_cat_idx) {
total_role_charges_all_cfs +=
actual_stats.charge_per_item_owner[cache_owner_id][role_idx][level_cat_idx];
}
}
ASSERT_EQ(actual_stats.total_charges[role_idx],
total_role_charges_all_cfs);
Expand Down
22 changes: 14 additions & 8 deletions db/internal_stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,8 @@ InternalStats::CacheEntryRoleStats::GetEntryCallback() {
static_cast<size_t>(helper ? helper->role : CacheEntryRole::kMisc);
entry_counts[role_idx]++;
total_charges[role_idx] += charge;
charge_per_item_owner[item_owner_id][role_idx] += charge;
auto level_category_value = item_owner_id >> 14;
charge_per_item_owner[item_owner_id][role_idx][level_category_value] += charge;
};
}

Expand Down Expand Up @@ -800,7 +801,10 @@ std::string InternalStats::CacheEntryRoleStats::CacheOwnerStatsToString(
auto role_idx = static_cast<unsigned int>(role);
uint64_t role_total_charge = 0U;
if (cf_charges_per_role_pos != charge_per_item_owner.end()) {
role_total_charge = cf_charges_per_role_pos->second[role_idx];
const auto& per_level_charges = cf_charges_per_role_pos->second;
for (auto level_cat_idx = 0U; level_cat_idx < pinning::kNumLevelCategories; ++level_cat_idx) {
role_total_charge += per_level_charges[role_idx][level_cat_idx];
}
}

str << " " << kCacheEntryRoleToCamelString[role_idx] << "("
Expand Down Expand Up @@ -840,14 +844,16 @@ void InternalStats::CacheEntryRoleStats::CacheOwnerStatsToMap(
v[BlockCacheCfStatsMapKeys::CfName()] = cf_name;
v[BlockCacheCfStatsMapKeys::CacheId()] = cache_id;
const auto& cache_owner_charges = charge_per_item_owner.find(cache_owner_id);
for (size_t i = 0; i < kNumCacheEntryRoles; ++i) {
auto role = static_cast<CacheEntryRole>(i);
for (size_t role_idx = 0U; role_idx < kNumCacheEntryRoles; ++role_idx) {
auto role = static_cast<CacheEntryRole>(role_idx);
auto total_role_charge = 0U;
if (cache_owner_charges != charge_per_item_owner.end()) {
v[BlockCacheCfStatsMapKeys::UsedBytes(role)] =
std::to_string(charge_per_item_owner.at(cache_owner_id)[i]);
} else {
v[BlockCacheCfStatsMapKeys::UsedBytes(role)] = "0";
const auto& per_level_charges = cache_owner_charges->second;
for (auto level_cat_idx = 0U; level_cat_idx < pinning::kNumLevelCategories; ++level_cat_idx) {
total_role_charge += per_level_charges[role_idx][level_cat_idx];
}
}
v[BlockCacheCfStatsMapKeys::UsedBytes(role)] = std::to_string(total_role_charge);
}
}

Expand Down
3 changes: 2 additions & 1 deletion db/internal_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "db/version_set.h"
#include "rocksdb/system_clock.h"
#include "util/hash_containers.h"
#include "include/rocksdb/table_pinning_policy.h"

namespace ROCKSDB_NAMESPACE {

Expand Down Expand Up @@ -495,7 +496,7 @@ class InternalStats {
uint32_t hash_seed = 0;

std::unordered_map<Cache::ItemOwnerId,
std::array<size_t, kNumCacheEntryRoles>>
std::array<std::array<size_t, pinning::kNumLevelCategories>, kNumCacheEntryRoles>>
charge_per_item_owner;

void Clear() {
Expand Down
2 changes: 1 addition & 1 deletion include/rocksdb/table_pinning_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ constexpr uint32_t kNumHierarchyCategories =

std::string GetHierarchyCategoryName(HierarchyCategory category);

enum class LevelCategory { LEVEL_0, MIDDLE_LEVEL, LAST_LEVEL_WITH_DATA, UNKNOWN_LEVEL };
enum class LevelCategory { LEVEL_0 = 0, MIDDLE_LEVEL = 1, LAST_LEVEL_WITH_DATA = 2, UNKNOWN_LEVEL = 3 };

constexpr uint32_t kNumLevelCategories =
static_cast<uint32_t>(LevelCategory::UNKNOWN_LEVEL) + 1;
Expand Down
8 changes: 6 additions & 2 deletions table/block_based/block_based_table_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ Status BlockBasedTable::Open(
BlockCacheLookupContext lookup_context{TableReaderCaller::kPrefetch};
Rep* rep = new BlockBasedTable::Rep(
ioptions, env_options, table_options, internal_comparator, skip_filters,
file_size, level, immortal_table, user_defined_timestamps_persisted,
file_size, level, is_last_level_with_data, immortal_table, user_defined_timestamps_persisted,
cache_owner_id);
rep->file = std::move(file);
rep->footer = footer;
Expand Down Expand Up @@ -1373,10 +1373,14 @@ WithBlocklikeCheck<Status, TBlocklike> BlockBasedTable::PutDataBlockToCache(
if (block_cache && block_holder->own_bytes()) {
size_t charge = block_holder->ApproximateMemoryUsage();
BlockCacheTypedHandle<TBlocklike>* cache_handle = nullptr;
auto owner_id_with_level = rep_->cache_owner_id;
auto level_category_value = static_cast<int>(pinning::GetLevelCategory(rep_->level, rep_->last_level_with_data));
owner_id_with_level &= (level_category_value << 14);

s = block_cache.InsertFull(cache_key, block_holder.get(), charge,
&cache_handle, GetCachePriority<TBlocklike>(),
rep_->ioptions.lowest_used_cache_tier,
rep_->cache_owner_id);
owner_id_with_level);

if (s.ok()) {
assert(cache_handle != nullptr);
Expand Down
5 changes: 4 additions & 1 deletion table/block_based/block_based_table_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ struct BlockBasedTable::Rep {
Rep(const ImmutableOptions& _ioptions, const EnvOptions& _env_options,
const BlockBasedTableOptions& _table_opt,
const InternalKeyComparator& _internal_comparator, bool skip_filters,
uint64_t _file_size, int _level, const bool _immortal_table,
uint64_t _file_size, int _level, bool _last_level_with_data, const bool _immortal_table,
const bool _user_defined_timestamps_persisted = true,
Cache::ItemOwnerId _cache_owner_id = Cache::kUnknownItemOwnerId)
: ioptions(_ioptions),
Expand All @@ -587,6 +587,7 @@ struct BlockBasedTable::Rep {
global_seqno(kDisableGlobalSequenceNumber),
file_size(_file_size),
level(_level),
last_level_with_data(_last_level_with_data),
immortal_table(_immortal_table),
user_defined_timestamps_persisted(_user_defined_timestamps_persisted),
cache_owner_id(_cache_owner_id) {}
Expand Down Expand Up @@ -647,6 +648,8 @@ struct BlockBasedTable::Rep {
// move is involved
int level;

bool last_level_with_data;

// the timestamp range of table
// Points into memory owned by TableProperties. This would need to change if
// TableProperties become subject to cache eviction.
Expand Down
2 changes: 1 addition & 1 deletion table/block_based/mock_block_based_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MockBlockBasedTableTester {
constexpr bool immortal_table = false;
table_.reset(new MockBlockBasedTable(new BlockBasedTable::Rep(
ioptions_, env_options_, table_options_, icomp_, skip_filters,
12345 /*file_size*/, kMockLevel, immortal_table)));
12345 /*file_size*/, kMockLevel, false /* last_level_with_data */, immortal_table)));
}

FilterBitsBuilder* GetBuilder() const {
Expand Down
2 changes: 1 addition & 1 deletion table/block_based/partitioned_filter_block_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class PartitionedFilterBlockTest
constexpr bool immortal_table = false;
table_.reset(new MockedBlockBasedTable(
new BlockBasedTable::Rep(ioptions_, env_options_, table_options_,
icomp_, skip_filters, file_size, level,
icomp_, skip_filters, file_size, level, false /* last_level_with_data */,
immortal_table,
user_defined_timestamps_persisted_),
pib));
Expand Down
3 changes: 0 additions & 3 deletions table/block_based/recording_pinning_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ class RecordingPinningPolicy : public TablePinningPolicy {
size_t limit) const = 0;

private:
void AddCacheItemOwnerId(Cache::ItemOwnerId item_owner_id);
void RemoveCacheItemOwnerId(Cache::ItemOwnerId item_owner_id);

std::optional<size_t> GetOwnerIdTotalPinnedUsageNonLocking(
Cache::ItemOwnerId item_owner_id) const;

Expand Down

0 comments on commit 2ceceb5

Please sign in to comment.