Skip to content

Commit 1b40559

Browse files
thelongmarch-azxZixi Anxx01cyx
authored
p1: update cmake to include more submission files (#691)
* update cmake * remove page.h from p1 make list * tidy up files * update cow_buffer comment * fix format * p2 -> p1 * sync with private * remove unnecessary header * rename CoW buffer to write back cache * remove references to CoW buffer * update header order --------- Co-authored-by: Zixi An <[email protected]> Co-authored-by: xx01cyx <[email protected]>
1 parent 400d57c commit 1b40559

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ set(P1_FILES
275275
"src/buffer/buffer_pool_manager.cpp"
276276
"src/include/storage/disk/disk_scheduler.h"
277277
"src/storage/disk/disk_scheduler.cpp"
278+
"src/storage/page/page_guard.cpp"
279+
"src/include/storage/page/page_guard.h"
278280
)
279281
add_custom_target(check-clang-tidy-p1
280282
${BUSTUB_BUILD_SUPPORT_DIR}/run_clang_tidy.py # run LLVM's clang-tidy script

src/include/buffer/buffer_pool_manager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include "buffer/lru_k_replacer.h"
2121
#include "common/config.h"
2222
#include "recovery/log_manager.h"
23-
#include "storage/disk/cow_buffer.h"
2423
#include "storage/disk/disk_scheduler.h"
24+
#include "storage/disk/write_back_cache.h"
2525
#include "storage/page/page.h"
2626
#include "storage/page/page_guard.h"
2727

@@ -73,7 +73,7 @@ class BufferPoolManager {
7373
auto NewPage(page_id_t *page_id) -> Page *;
7474

7575
/**
76-
* TODO(P2): Add implementation
76+
* TODO(P1): Add implementation
7777
*
7878
* @brief PageGuard wrapper for NewPage
7979
*
@@ -106,7 +106,7 @@ class BufferPoolManager {
106106
auto FetchPage(page_id_t page_id, AccessType access_type = AccessType::Unknown) -> Page *;
107107

108108
/**
109-
* TODO(P2): Add implementation
109+
* TODO(P1): Add implementation
110110
*
111111
* @brief PageGuard wrappers for FetchPage
112112
*
@@ -194,7 +194,7 @@ class BufferPoolManager {
194194
/** This latch protects shared data structures. We recommend updating this comment to describe what it protects. */
195195
std::mutex latch_;
196196
/** This buffer is for the leaderboard task. You may want to use it to optimize the write requests. */
197-
CoWBuffer cow_buffer_ __attribute__((__unused__));
197+
WriteBackCache write_back_cache_ __attribute__((__unused__));
198198

199199
/**
200200
* @brief Allocate a page on disk. Caller should acquire the latch before calling this function.

src/include/storage/disk/cow_buffer.h renamed to src/include/storage/disk/write_back_cache.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@
2121
namespace bustub {
2222

2323
/**
24-
* CoWBuffer provides extra memory space other than the buffer pool to store the copy-on-write pages.
24+
* WriteBackCache provides extra memory space other than the buffer pool to store the copy-on-write pages.
2525
* It's purpose is to gather the copy of pages that are about to be written back to disk, so that the bpm
2626
* doesn't have to incur IO penality and wait for the write to be completed when evicting.
2727
* Spring 24: The buffer is limited to store a constant number of pages in total (8).
2828
* !! ANY ATTEMPTS TO ADD ANOTHER IN-MEMORY CACHE WILL BE REVIEWED MANUALLY AS PER LEADERBOARD POLICY !!
2929
*/
30-
class CoWBuffer {
30+
class WriteBackCache {
3131
public:
32-
CoWBuffer() : cow_pages_{new Page[8]} {}
33-
~CoWBuffer() { delete[] cow_pages_; }
34-
DISALLOW_COPY_AND_MOVE(CoWBuffer);
32+
WriteBackCache() : write_back_pages_{new Page[8]} {}
33+
~WriteBackCache() { delete[] write_back_pages_; }
34+
DISALLOW_COPY_AND_MOVE(WriteBackCache);
3535

3636
/**
37-
* @brief Adds a new page to the CoW buffer.
37+
* @brief Adds a new page to the write back cache.
3838
* @param page the page pointer from the bpm that is about to be evicted.
3939
* @return pointer to the copied page in the buffer, or nullptr if the buffer is full.
4040
*/
@@ -44,19 +44,19 @@ class CoWBuffer {
4444
}
4545

4646
uint32_t slot = FindFreeSlot();
47-
memcpy(cow_pages_[slot].GetData(), page->GetData(), BUSTUB_PAGE_SIZE);
47+
memcpy(write_back_pages_[slot].GetData(), page->GetData(), BUSTUB_PAGE_SIZE);
4848
MarkSlotUsed(slot);
4949

50-
return cow_pages_ + slot;
50+
return write_back_pages_ + slot;
5151
}
5252

5353
/**
54-
* @brief Removes a page from the CoW buffer.
54+
* @brief Removes a page from the write back cache.
5555
* @param page the pointer previously returned by Add.
5656
*/
5757
auto Remove(Page *page) -> void {
5858
if (page != nullptr) {
59-
MarkSlotFree(page - cow_pages_);
59+
MarkSlotFree(page - write_back_pages_);
6060
}
6161
}
6262

@@ -66,7 +66,7 @@ class CoWBuffer {
6666

6767
/** @brief Finds a free slot in the buffer, if not full. */
6868
auto FindFreeSlot() -> uint32_t {
69-
BUSTUB_ASSERT(!IsFull(), "no free slot in cow buffer");
69+
BUSTUB_ASSERT(!IsFull(), "no free slot in write back cache");
7070
uint32_t i = 0;
7171
uint8_t bitmap = free_slot_bitmap_;
7272
while ((bitmap & 1U) != 0) {
@@ -88,8 +88,8 @@ class CoWBuffer {
8888
free_slot_bitmap_ &= ~(1U << slot);
8989
}
9090

91-
/** The array of CoW buffer pages. */
92-
Page *cow_pages_;
91+
/** The array of write back cache pages. */
92+
Page *write_back_pages_;
9393
/** The bitmap that records which slots are free. */
9494
uint8_t free_slot_bitmap_{0};
9595
};

test/storage/cow_buffer_test.cpp renamed to test/storage/write_back_cache_test.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,46 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "storage/disk/cow_buffer.h"
13+
#include "storage/disk/write_back_cache.h"
1414
#include "gtest/gtest.h"
1515

1616
namespace bustub {
1717

1818
// NOLINTNEXTLINE
19-
TEST(CoWBufferTest, ScheduleWriteReadPageTest) {
20-
CoWBuffer cow{};
21-
std::vector<Page *> cow_pages{};
19+
TEST(WriteBackCacheTest, ScheduleWriteReadPageTest) {
20+
WriteBackCache wbc{};
21+
std::vector<Page *> wbc_pages{};
2222
for (size_t i{0}; i < 8; i++) {
2323
Page bpm_page{};
2424
auto content{"Meuh!: " + std::to_string(i) + " 🐄🐄🐄🐄"};
2525
std::strncpy(bpm_page.GetData(), content.data(), BUSTUB_PAGE_SIZE);
2626

27-
Page *cow_page{cow.Add(&bpm_page)};
28-
EXPECT_NE(cow_page, nullptr);
29-
EXPECT_NE(cow_page, &bpm_page);
30-
cow_pages.push_back(cow_page);
31-
EXPECT_EQ(std::memcmp(cow_page->GetData(), bpm_page.GetData(), BUSTUB_PAGE_SIZE), 0);
27+
Page *wbc_page{wbc.Add(&bpm_page)};
28+
EXPECT_NE(wbc_page, nullptr);
29+
EXPECT_NE(wbc_page, &bpm_page);
30+
wbc_pages.push_back(wbc_page);
31+
EXPECT_EQ(std::memcmp(wbc_page->GetData(), bpm_page.GetData(), BUSTUB_PAGE_SIZE), 0);
3232
}
3333

3434
Page extra_page{};
35-
Page *cow_extra_page{cow.Add(&extra_page)};
36-
EXPECT_EQ(cow_extra_page, nullptr);
35+
Page *wbc_extra_page{wbc.Add(&extra_page)};
36+
EXPECT_EQ(wbc_extra_page, nullptr);
3737

3838
for (size_t i{0}; i < 8; i++) {
3939
auto check{"Meuh!: " + std::to_string(i) + " 🐄🐄🐄🐄"};
40-
EXPECT_EQ(std::memcmp(cow_pages[i]->GetData(), check.data(), check.size()), 0);
40+
EXPECT_EQ(std::memcmp(wbc_pages[i]->GetData(), check.data(), check.size()), 0);
4141
}
4242

43-
cow.Remove(cow_pages[5]);
43+
wbc.Remove(wbc_pages[5]);
4444
Page replace_page{};
45-
cow_pages[5] = cow.Add(&replace_page);
46-
EXPECT_NE(cow_pages[5], nullptr);
45+
wbc_pages[5] = wbc.Add(&replace_page);
46+
EXPECT_NE(wbc_pages[5], nullptr);
4747

4848
for (size_t i{0}; i < 8; i++) {
49-
cow.Remove(cow_pages[i]); // Shouldn't crash.
49+
wbc.Remove(wbc_pages[i]); // Shouldn't crash.
5050
}
5151

52-
cow.Remove(nullptr); // Shouldn't crash.
52+
wbc.Remove(nullptr); // Shouldn't crash.
5353
}
5454

5555
} // namespace bustub

0 commit comments

Comments
 (0)