Skip to content

Commit

Permalink
feat: make page size configurable (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason authored Dec 22, 2023
1 parent f94d5e7 commit 8d5c6fc
Show file tree
Hide file tree
Showing 31 changed files with 323 additions and 211 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ jobs:

- name: Test test
run: ctest --test-dir build --output-on-failure

- name: Setup tmate session
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
2 changes: 1 addition & 1 deletion benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# add_subdirectory(ycsb)
add_subdirectory(micro-benchmarks)
2 changes: 2 additions & 0 deletions benchmarks/micro-benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(CastBench CastBench.cpp)
target_link_libraries(CastBench gbench leanstore)
28 changes: 28 additions & 0 deletions benchmarks/micro-benchmarks/CastBench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "storage/buffer-manager/BufferFrame.hpp"
#include "utils/Misc.hpp"

#include <benchmark/benchmark.h>

using namespace leanstore;
using namespace leanstore::utils;
using namespace leanstore::storage;

static void BenchU8ToPage(benchmark::State& state) {
AlignedBuffer<512> alignedBuffer(FLAGS_page_size * 4);
auto buf = alignedBuffer.Get();
auto i = 1;
for (auto _ : state) {
reinterpret_cast<Page*>(&buf[i * FLAGS_page_size])->mGSN = 1;
}
}
static void BenchPageDirectly(benchmark::State& state) {
Page pages[4];
auto i = 1;
for (auto _ : state) {
pages[i].mGSN = 1;
}
}

BENCHMARK(BenchU8ToPage);
BENCHMARK(BenchPageDirectly);
BENCHMARK_MAIN();
4 changes: 2 additions & 2 deletions benchmarks/ycsb/deterministic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main(int argc, char** argv) {
cout << calculateMTPS(begin, end, n) << " M tps" << endl;
// -------------------------------------------------------------------------------------
const u64 written_pages = db.getBufferManager().consumedPages();
const u64 mib = written_pages * PAGE_SIZE / 1024 / 1024;
const u64 mib = written_pages * FLAGS_page_size / 1024 / 1024;
cout << "Inserted volume: (pages, MiB) = (" << written_pages << ", " << mib
<< ")" << endl;
cout << "------------------------------------------------------------------"
Expand All @@ -133,7 +133,7 @@ int main(int argc, char** argv) {
}
// -------------------------------------------------------------------------------------
const u64 DISTANCE =
8 * (PAGE_SIZE / (sizeof(YCSBKey) + sizeof(YCSBPayload)));
8 * (FLAGS_page_size / (sizeof(YCSBKey) + sizeof(YCSBPayload)));
cout << setprecision(4);
// -------------------------------------------------------------------------------------
cout << "~Transactions" << endl;
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/ycsb/ycsb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ int main(int argc, char** argv) {
cout << calculateMTPS(begin, end, n) << " M tps" << endl;
// -------------------------------------------------------------------------------------
const u64 written_pages = db.getBufferManager().consumedPages();
const u64 mib = written_pages * PAGE_SIZE / 1024 / 1024;
const u64 mib = written_pages * FLAGS_page_size / 1024 / 1024;
cout << "Inserted volume: (pages, MiB) = (" << written_pages << ", " << mib
<< ")" << endl;
cout << "------------------------------------------------------------------"
Expand Down
1 change: 1 addition & 0 deletions source/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gflags/gflags.h>

// Buffer management
DEFINE_uint32(page_size, 4096, "The page size (bytes)"); // 4 KiB
DEFINE_uint64(buffer_pool_size, 1073741824,
"The buffer pool size (bytes)"); // 1 GiB
DEFINE_string(data_dir, "~/.leanstore",
Expand Down
1 change: 1 addition & 0 deletions source/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>

// Buffer management
DECLARE_uint32(page_size);
DECLARE_uint64(buffer_pool_size);
DECLARE_string(data_dir);
DECLARE_uint64(db_file_capacity);
Expand Down
39 changes: 18 additions & 21 deletions source/concurrency-recovery/HistoryTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "storage/btree/core/BTreeSharedIterator.hpp"
#include "utils/Misc.hpp"

#include <alloca.h>
#include <atomic>
#include <condition_variable>
#include <functional>
Expand Down Expand Up @@ -152,10 +151,10 @@ void HistoryTree::purgeVersions(WORKERID workerId, TXID from_tx_id,
TXID to_tx_id, RemoveVersionCallback cb,
[[maybe_unused]] const u64 limit) {
auto keySize = sizeof(to_tx_id);
auto keyBuffer = utils::ArrayOnStack<u8>(PAGE_SIZE);
auto keyBuffer = utils::ArrayOnStack<u8>(FLAGS_page_size);
utils::fold(keyBuffer, from_tx_id);
Slice key(keyBuffer, keySize);
auto payload = utils::ArrayOnStack<u8>(PAGE_SIZE);
auto payload = utils::ArrayOnStack<u8>(FLAGS_page_size);
u16 payload_length;
volatile u64 removed_versions = 0;
BTreeLL* volatile btree = remove_btrees[workerId];
Expand All @@ -168,7 +167,7 @@ void HistoryTree::purgeVersions(WORKERID workerId, TXID from_tx_id,
iterator.exitLeafCallback(
[&](GuardedBufferFrame<BTreeNode>& guardedLeaf) {
if (guardedLeaf->freeSpaceAfterCompaction() >=
BTreeNodeHeader::sUnderFullSize) {
BTreeNode::UnderFullSize()) {
iterator.cleanUpCallback([&, to_find = guardedLeaf.mBf] {
JUMPMU_TRY() {
btree->tryMerge(*to_find);
Expand Down Expand Up @@ -228,12 +227,11 @@ void HistoryTree::purgeVersions(WORKERID workerId, TXID from_tx_id,

if (guardedLeaf->mLowerFence.length == 0) {
// Allocate in the stack, freed when the calling function exits.
u8* last_key = (u8*)alloca(
guardedLeaf->getFullKeyLen(guardedLeaf->mNumSeps - 1) *
sizeof(u8));
guardedLeaf->copyFullKey(guardedLeaf->mNumSeps - 1, last_key);
auto lastKey = utils::ArrayOnStack<u8>(
guardedLeaf->getFullKeyLen(guardedLeaf->mNumSeps - 1));
guardedLeaf->copyFullKey(guardedLeaf->mNumSeps - 1, lastKey);
TXID last_key_tx_id;
utils::unfold(last_key, last_key_tx_id);
utils::unfold(lastKey, last_key_tx_id);
if (last_key_tx_id > to_tx_id) {
should_try = false;
}
Expand All @@ -250,7 +248,7 @@ void HistoryTree::purgeVersions(WORKERID workerId, TXID from_tx_id,
iterator.exitLeafCallback(
[&](GuardedBufferFrame<BTreeNode>& guardedLeaf) {
if (guardedLeaf->freeSpaceAfterCompaction() >=
BTreeNodeHeader::sUnderFullSize) {
BTreeNode::UnderFullSize()) {
iterator.cleanUpCallback([&, to_find = guardedLeaf.mBf] {
JUMPMU_TRY() {
btree->tryMerge(*to_find);
Expand All @@ -270,19 +268,18 @@ void HistoryTree::purgeVersions(WORKERID workerId, TXID from_tx_id,
return;
}
// Allocate in the stack, freed when the calling function exits.
auto first_key =
(u8*)alloca(guardedLeaf->getFullKeyLen(0) * sizeof(u8));
guardedLeaf->copyFullKey(0, first_key);
auto firstKey =
utils::ArrayOnStack<u8>(guardedLeaf->getFullKeyLen(0));
guardedLeaf->copyFullKey(0, firstKey);
TXID first_key_tx_id;
utils::unfold(first_key, first_key_tx_id);
utils::unfold(firstKey, first_key_tx_id);

// Allocate in the stack, freed when the calling function exits.
auto last_key = (u8*)alloca(
guardedLeaf->getFullKeyLen(guardedLeaf->mNumSeps - 1) *
sizeof(u8));
guardedLeaf->copyFullKey(guardedLeaf->mNumSeps - 1, last_key);
auto lastKey = utils::ArrayOnStack<u8>(
guardedLeaf->getFullKeyLen(guardedLeaf->mNumSeps - 1));
guardedLeaf->copyFullKey(guardedLeaf->mNumSeps - 1, lastKey);
TXID last_key_tx_id;
utils::unfold(last_key, last_key_tx_id);
utils::unfold(lastKey, last_key_tx_id);
if (first_key_tx_id >= from_tx_id && to_tx_id >= last_key_tx_id) {
// Purge the whole page
removed_versions = removed_versions + guardedLeaf->mNumSeps;
Expand Down Expand Up @@ -320,11 +317,11 @@ void HistoryTree::visitRemoveVersions(
// [from, to]
BTreeLL* btree = remove_btrees[workerId];
auto keySize = sizeof(to_tx_id);
auto keyBuffer = utils::ArrayOnStack<u8>(PAGE_SIZE);
auto keyBuffer = utils::ArrayOnStack<u8>(FLAGS_page_size);
u64 offset = 0;
offset += utils::fold(keyBuffer + offset, from_tx_id);
Slice key(keyBuffer, keySize);
auto payload = utils::ArrayOnStack<u8>(PAGE_SIZE);
auto payload = utils::ArrayOnStack<u8>(FLAGS_page_size);
u16 payload_length;

JUMPMU_TRY() {
Expand Down
14 changes: 8 additions & 6 deletions source/concurrency-recovery/Recovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ using namespace leanstore::storage;
using namespace leanstore::storage::btree;

void Recovery::Analysis() {
Page page; // asume that each WALEntry is smaller than the page size
u8* walEntryPtr = (u8*)&page;
// asume that each WALEntry is smaller than the page size
utils::AlignedBuffer<512> alignedBuffer(FLAGS_page_size);
u8* walEntryPtr = alignedBuffer.Get();
u64 walEntrySize = sizeof(WALEntry);
for (auto offset = mWalStartOffset; offset < mWalSize;) {
auto bytesRead = ReadWalEntry(offset, walEntrySize, &page);
auto walEntry = reinterpret_cast<WALEntry*>(&page);
auto bytesRead = ReadWalEntry(offset, walEntrySize, walEntryPtr);
auto walEntry = reinterpret_cast<WALEntry*>(walEntryPtr);
switch (walEntry->type) {
case WALEntry::TYPE::TX_START: {
DCHECK_EQ(bytesRead, walEntry->size);
Expand Down Expand Up @@ -73,8 +74,9 @@ void Recovery::Analysis() {
}

void Recovery::Redo() {
Page page; // asume that each WALEntry is smaller than the page size
u8* walEntryPtr = (u8*)&page;
// asume that each WALEntry is smaller than the page size
utils::AlignedBuffer<512> alignedBuffer(FLAGS_page_size);
u8* walEntryPtr = alignedBuffer.Get();
u64 walEntrySize = sizeof(WALEntry);

for (auto offset = mWalStartOffset; offset < mWalSize;) {
Expand Down
21 changes: 10 additions & 11 deletions source/profiling/tables/BMTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@
#include "profiling/counters/PPCounters.hpp"
#include "profiling/counters/WorkerCounters.hpp"
#include "utils/ThreadLocalAggregator.hpp"
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------

using leanstore::utils::threadlocal::sum;

namespace leanstore {
namespace profiling {
// -------------------------------------------------------------------------------------

BMTable::BMTable(BufferManager& bm) : ProfilingTable(), bm(bm) {
}
// -------------------------------------------------------------------------------------

std::string BMTable::getName() {
return "bm";
}
// -------------------------------------------------------------------------------------

void BMTable::open() {
columns.emplace("key", [](Column& col) { col << 0; });
columns.emplace("space_usage_gib", [&](Column& col) {
const double gib =
bm.consumedPages() * 1.0 * PAGE_SIZE / 1024.0 / 1024.0 / 1024.0;
bm.consumedPages() * 1.0 * FLAGS_page_size / 1024.0 / 1024.0 / 1024.0;
col << gib;
});
columns.emplace("space_usage_kib", [&](Column& col) {
const double kib = bm.consumedPages() * 1.0 * PAGE_SIZE / 1024.0;
const double kib = bm.consumedPages() * 1.0 * FLAGS_page_size / 1024.0;
col << kib;
});
columns.emplace("consumed_pages",
Expand Down Expand Up @@ -65,7 +64,7 @@ void BMTable::open() {
});
columns.emplace("evicted_mib", [&](Column& col) {
col << (sum(PPCounters::pp_counters, &PPCounters::evicted_pages) *
EFFECTIVE_PAGE_SIZE / 1024.0 / 1024.0);
FLAGS_page_size / 1024.0 / 1024.0);
});
columns.emplace("rounds", [&](Column& col) {
col << (sum(PPCounters::pp_counters, &PPCounters::pp_thread_rounds));
Expand All @@ -86,7 +85,7 @@ void BMTable::open() {
});
columns.emplace("w_mib", [&](Column& col) {
col << (sum(PPCounters::pp_counters, &PPCounters::flushed_pages_counter) *
EFFECTIVE_PAGE_SIZE / 1024.0 / 1024.0);
FLAGS_page_size / 1024.0 / 1024.0);
});
// -------------------------------------------------------------------------------------
columns.emplace("allocate_ops", [&](Column& col) {
Expand All @@ -96,7 +95,7 @@ void BMTable::open() {
columns.emplace("r_mib", [&](Column& col) {
col << (sum(WorkerCounters::worker_counters,
&WorkerCounters::read_operations_counter) *
EFFECTIVE_PAGE_SIZE / 1024.0 / 1024.0);
FLAGS_page_size / 1024.0 / 1024.0);
});
}

Expand Down
22 changes: 11 additions & 11 deletions source/storage/btree/BTreeLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ OP_RESULT BTreeLL::insert(Slice key, Slice val) {
DCHECK(cr::Worker::my().IsTxStarted());
cr::activeTX().markAsWrite();
if (config.mEnableWal) {
cr::Worker::my().mLogging.walEnsureEnoughSpace(PAGE_SIZE * 1);
cr::Worker::my().mLogging.walEnsureEnoughSpace(FLAGS_page_size * 1);
}

JUMPMU_TRY() {
Expand Down Expand Up @@ -271,11 +271,11 @@ OP_RESULT BTreeLL::append(std::function<void(u8*)> o_key, u16 o_key_length,
OP_RESULT ret =
iterator.enoughSpaceInCurrentNode(o_key_length, o_value_length);
if (ret == OP_RESULT::OK) {
auto key_buffer = (u8*)alloca(o_key_length * sizeof(u8));
o_key(key_buffer);
auto keyBuffer = utils::ArrayOnStack<u8>(o_key_length);
o_key(keyBuffer);
const s32 pos = iterator.mGuardedLeaf->mNumSeps;
iterator.mGuardedLeaf->insertDoNotCopyPayload(
Slice(key_buffer, o_key_length), o_value_length, pos);
Slice(keyBuffer, o_key_length), o_value_length, pos);
iterator.mSlotId = pos;
o_value(iterator.mutableValue().data());
iterator.MarkAsDirty();
Expand All @@ -292,19 +292,19 @@ OP_RESULT BTreeLL::append(std::function<void(u8*)> o_key, u16 o_key_length,
while (true) {
JUMPMU_TRY() {
BTreeExclusiveIterator iterator(*static_cast<BTreeGeneric*>(this));
auto key_buffer = (u8*)alloca(o_key_length * sizeof(u8));
auto keyBuffer = utils::ArrayOnStack<u8>(o_key_length);
for (u64 i = 0; i < o_key_length; i++) {
key_buffer[i] = 255;
keyBuffer[i] = 255;
}
const Slice key(key_buffer, o_key_length);
const Slice key(keyBuffer, o_key_length);
OP_RESULT ret = iterator.seekToInsert(key);
RAISE_WHEN(ret == OP_RESULT::DUPLICATE);
ret = iterator.enoughSpaceInCurrentNode(key, o_value_length);
if (ret == OP_RESULT::NOT_ENOUGH_SPACE) {
iterator.splitForKey(key);
JUMPMU_CONTINUE;
}
o_key(key_buffer);
o_key(keyBuffer);
iterator.insertInCurrentNode(key, o_value_length);
o_value(iterator.mutableValue().data());
iterator.MarkAsDirty();
Expand Down Expand Up @@ -333,7 +333,7 @@ OP_RESULT BTreeLL::updateSameSizeInPlace(
UpdateSameSizeInPlaceDescriptor& update_descriptor) {
cr::activeTX().markAsWrite();
if (config.mEnableWal) {
cr::Worker::my().mLogging.walEnsureEnoughSpace(PAGE_SIZE * 1);
cr::Worker::my().mLogging.walEnsureEnoughSpace(FLAGS_page_size * 1);
}

JUMPMU_TRY() {
Expand Down Expand Up @@ -380,7 +380,7 @@ OP_RESULT BTreeLL::updateSameSizeInPlace(
OP_RESULT BTreeLL::remove(Slice key) {
cr::activeTX().markAsWrite();
if (config.mEnableWal) {
cr::Worker::my().mLogging.walEnsureEnoughSpace(PAGE_SIZE * 1);
cr::Worker::my().mLogging.walEnsureEnoughSpace(FLAGS_page_size * 1);
}
JUMPMU_TRY() {
BTreeExclusiveIterator iterator(*static_cast<BTreeGeneric*>(this));
Expand Down Expand Up @@ -414,7 +414,7 @@ OP_RESULT BTreeLL::rangeRemove(Slice startKey, Slice endKey, bool page_wise) {
BTreeExclusiveIterator iterator(*static_cast<BTreeGeneric*>(this));
iterator.exitLeafCallback([&](GuardedBufferFrame<BTreeNode>& guardedLeaf) {
if (guardedLeaf->freeSpaceAfterCompaction() >=
BTreeNodeHeader::sUnderFullSize) {
BTreeNode::UnderFullSize()) {
iterator.cleanUpCallback([&, to_find = guardedLeaf.mBf] {
JUMPMU_TRY() {
this->tryMerge(*to_find);
Expand Down
6 changes: 3 additions & 3 deletions source/storage/btree/BTreeVI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ OP_RESULT BTreeVI::updateSameSizeInPlace(
});

cr::activeTX().markAsWrite();
cr::Worker::my().mLogging.walEnsureEnoughSpace(PAGE_SIZE * 1);
cr::Worker::my().mLogging.walEnsureEnoughSpace(FLAGS_page_size * 1);
OP_RESULT ret;

// 20K instructions more
Expand Down Expand Up @@ -361,7 +361,7 @@ OP_RESULT BTreeVI::insert(Slice key, Slice val) {
});

cr::activeTX().markAsWrite();
cr::Worker::my().mLogging.walEnsureEnoughSpace(PAGE_SIZE * 1);
cr::Worker::my().mLogging.walEnsureEnoughSpace(FLAGS_page_size * 1);
u16 payloadSize = val.size() + sizeof(ChainedTuple);

while (true) {
Expand Down Expand Up @@ -428,7 +428,7 @@ OP_RESULT BTreeVI::remove(Slice key) {

// TODO: remove fat tuple
cr::activeTX().markAsWrite();
cr::Worker::my().mLogging.walEnsureEnoughSpace(PAGE_SIZE * 1);
cr::Worker::my().mLogging.walEnsureEnoughSpace(FLAGS_page_size * 1);

JUMPMU_TRY() {
BTreeExclusiveIterator iterator(*static_cast<BTreeGeneric*>(this));
Expand Down
2 changes: 1 addition & 1 deletion source/storage/btree/BTreeVI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ class BTreeVI : public BTreeLL {

private:
static inline u64 maxFatTupleLength() {
return EFFECTIVE_PAGE_SIZE - 1000;
return BTreeNode::Size() - 1000;
}

public:
Expand Down
Loading

0 comments on commit 8d5c6fc

Please sign in to comment.