Skip to content

Commit

Permalink
(lru_replacer): WIP: Refine LRUReplacer::Unpin, ::Pin
Browse files Browse the repository at this point in the history
FIXME: LRUReplacerTest still failed by AddressSanitizer:DEADLYSIGNAL.

$ ctest -R 'LRUReplacerTest' --output-on-failure
Test project /Users/unknowntpo/repo/unknowntpo/bustub/build
    Start 31: LRUReplacerTest.SampleTest
1/1 Test cmu-db#31: LRUReplacerTest.SampleTest .......Subprocess aborted***Exception:   0.18 sec
Running main() from gmock_main.cc
Note: Google Test filter = LRUReplacerTest.SampleTest
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from LRUReplacerTest
[ RUN      ] LRUReplacerTest.SampleTest
2023-06-16 16:00:47 [lru_replacer.cpp:59:Unpin] INFO  - hello
2023-06-16 16:00:47 [lru_replacer.cpp:72:Unpin] INFO  - Unpin is called for frame_id: 1
2023-06-16 16:00:47 [lru_replacer.cpp:59:Unpin] INFO  - hello
2023-06-16 16:00:47 [lru_replacer.cpp:72:Unpin] INFO  - Unpin is called for frame_id: 2
2023-06-16 16:00:47 [lru_replacer.cpp:59:Unpin] INFO  - hello
2023-06-16 16:00:47 [lru_replacer.cpp:72:Unpin] INFO  - Unpin is called for frame_id: 3
2023-06-16 16:00:47 [lru_replacer.cpp:59:Unpin] INFO  - hello
2023-06-16 16:00:47 [lru_replacer.cpp:72:Unpin] INFO  - Unpin is called for frame_id: 4
2023-06-16 16:00:47 [lru_replacer.cpp:59:Unpin] INFO  - hello
2023-06-16 16:00:47 [lru_replacer.cpp:72:Unpin] INFO  - Unpin is called for frame_id: 5
2023-06-16 16:00:47 [lru_replacer.cpp:59:Unpin] INFO  - hello
2023-06-16 16:00:47 [lru_replacer.cpp:72:Unpin] INFO  - Unpin is called for frame_id: 6
2023-06-16 16:00:47 [lru_replacer.cpp:59:Unpin] INFO  - hello
2023-06-16 16:00:47 [lru_replacer.cpp:72:Unpin] INFO  - Unpin is called for frame_id: 1
2023-06-16 16:00:47 [lru_replacer.cpp:39:Victim] INFO  - iter_count: 0
2023-06-16 16:00:47 [lru_replacer.cpp:39:Victim] INFO  - iter_count: 0
2023-06-16 16:00:47 [lru_replacer.cpp:39:Victim] INFO  - iter_count: 0
2023-06-16 16:00:47 [lru_replacer.cpp:48:Pin] INFO  - Pin is called for frame_id: 3
2023-06-16 16:00:47 [lru_replacer.cpp:55:Pin] WARN  - Pin is called with non-exist frame_id: 3
2023-06-16 16:00:47 [lru_replacer.cpp:48:Pin] INFO  - Pin is called for frame_id: 4
AddressSanitizer:DEADLYSIGNAL
=================================================================
==79229==ERROR: AddressSanitizer: SEGV on unknown address 0x00000104f02c (pc 0x000102816328 bp 0x00016d5f8fd0 sp 0x00016d5f8b80 T0)
==79229==The signal is caused by a UNKNOWN memory access.
    #0 0x102816328 in std::__1::pair<std::__1::__hash_iterator<std::__1::__hash_node<std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> >, void*>*>, bool> std::__1::__hash_table<std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> >, std::__1::__unordered_map_hasher<int, std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> >, std::__1::hash<int>, std::__1::equal_to<int>, true>, std::__1::__unordered_map_equal<int, std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> >, std::__1::equal_to<int>, std::__1::hash<int>, true>, std::__1::allocator<std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> > > >::__emplace_unique_key_args<int, std::__1::piecewise_construct_t const&, std::__1::tuple<int const&>, std::__1::tuple<> >(int const&, std::__1::piecewise_construct_t const&, std::__1::tuple<int const&>&&, std::__1::tuple<>&&) __hash_table:2083
    cmu-db#1 0x10280f548 in std::__1::unordered_map<int, std::__1::__list_iterator<bustub::node, void*>, std::__1::hash<int>, std::__1::equal_to<int>, std::__1::allocator<std::__1::pair<int const, std::__1::__list_iterator<bustub::node, void*> > > >::operator[](int const&) unordered_map:1743
    cmu-db#2 0x10280ec04 in bustub::LRUReplacer::Pin(int) lru_replacer.cpp:53
    cmu-db#3 0x102806af0 in bustub::LRUReplacerTest_SampleTest_Test::TestBody() lru_replacer_test.cpp:47
    cmu-db#4 0x102863f3c in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) gtest.cc:2657
    cmu-db#5 0x102863810 in testing::Test::Run() gtest.cc:2674
    cmu-db#6 0x1028660b0 in testing::TestInfo::Run() gtest.cc:2853
    cmu-db#7 0x102869044 in testing::TestSuite::Run() gtest.cc:3012
    cmu-db#8 0x10288d304 in testing::internal::UnitTestImpl::RunAllTests() gtest.cc:5870
    cmu-db#9 0x10288c6e4 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) gtest.cc:2657
    cmu-db#10 0x10288bed4 in testing::UnitTest::Run() gtest.cc:5444
    cmu-db#11 0x102929cd0 in RUN_ALL_TESTS() gtest.h:2293
    cmu-db#12 0x102929c2c in main gmock_main.cc:70
    cmu-db#13 0x1a564be4c  (<unknown module>)

==79229==Register values:
 x[0] = 0x0000000104c016c1   x[1] = 0x0000000000000004   x[2] = 0x000000010298dcc0   x[3] = 0x000000016d5f9020
 x[4] = 0x000000016d5f9040   x[5] = 0x000000016d5f9160   x[6] = 0x000000016d5f93b0   x[7] = 0x0000000000002900
 x[8] = 0xc00000000104f02c   x[9] = 0xc00000000104f02c  x[10] = 0x0000000000000001  x[11] = 0x000000702dadf178
x[12] = 0x000000016d5f8b88  x[13] = 0x000000016d5f8b80  x[14] = 0x000000016d5f8e20  x[15] = 0x0000000000000000
x[16] = 0x000000010280f380  x[17] = 0x000000010302c5d0  x[18] = 0x0000000000000000  x[19] = 0x000000016d5f8c80
x[20] = 0x0000000102929aa0  x[21] = 0x0000000102bedc60  x[22] = 0x000000016d5fb050  x[23] = 0x00000001a56c1000
x[24] = 0x00000002011e3340  x[25] = 0x0000000000000000  x[26] = 0x0000000000000000  x[27] = 0x0000000000000000
x[28] = 0x0000000000000000     fp = 0x000000016d5f8fd0     lr = 0x00000001028162b8     sp = 0x000000016d5f8b80
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV __hash_table:2083 in std::__1::pair<std::__1::__hash_iterator<std::__1::__hash_node<std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> >, void*>*>, bool> std::__1::__hash_table<std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> >, std::__1::__unordered_map_hasher<int, std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> >, std::__1::hash<int>, std::__1::equal_to<int>, true>, std::__1::__unordered_map_equal<int, std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> >, std::__1::equal_to<int>, std::__1::hash<int>, true>, std::__1::allocator<std::__1::__hash_value_type<int, std::__1::__list_iterator<bustub::node, void*> > > >::__emplace_unique_key_args<int, std::__1::piecewise_construct_t const&, std::__1::tuple<int const&>, std::__1::tuple<> >(int const&, std::__1::piecewise_construct_t const&, std::__1::tuple<int const&>&&, std::__1::tuple<>&&)
==79229==ABORTING

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.20 sec

The following tests FAILED:
	 31 - LRUReplacerTest.SampleTest (Subprocess aborted)
Errors while running CTest
  • Loading branch information
unknowntpo committed Jun 16, 2023
1 parent 4404d42 commit 28c8375
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/buffer/lru_replacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,52 @@ LRUReplacer::LRUReplacer(size_t num_pages) { capacity = num_pages; }

LRUReplacer::~LRUReplacer() = default;

// Victim set frame_id with victim's frame_id.
// RETURN VALUE: bool
// If Victim doesn't exist, return false, else return true.
auto LRUReplacer::Victim(frame_id_t *frame_id) -> bool {
auto it = um.find(*frame_id);
if (it == um.end()) {
LOG_INFO("frame_id: %d", it->second->frame_id);
auto it = dl.begin();
if (it == dl.end()) {
return false;
}
dl.splice(dl.begin(), dl, um[*frame_id]);
// get the first node which it->ref_cnt > 0
size_t iter_count = 0;
while (it->ref_cnt > 0 && iter_count < 1000) {
LOG_INFO("ref_cnt: %ld", it->ref_cnt);
it++;
iter_count++;
}
LOG_INFO("iter_count: %ld", iter_count);

*frame_id = it->frame_id;
dl.erase(it);
um.erase(*frame_id);
return true;
}

void LRUReplacer::Pin(frame_id_t frame_id) { LOG_INFO("Pin is called for frame_id: %d", frame_id); }
void LRUReplacer::Pin(frame_id_t frame_id) {
LOG_INFO("Pin is called for frame_id: %d", frame_id);
auto pair = um.find(frame_id);
if (pair != um.end()) {
auto it = pair->second;
it->ref_cnt++;
um[frame_id] = it;
}
LOG_WARN("Pin is called with non-exist frame_id: %d", frame_id);
}

void LRUReplacer::Unpin(frame_id_t frame_id) {
LOG_INFO("hello");
// // if exist -> move frame_id to front
// frame_id not exist, create new entry
if (um.find(frame_id) == um.end()) {
// add node to head of dl
dl.push_back((node){.frame_id = frame_id, .ref_cnt = 1});
auto end = dl.end();
end--;
um[frame_id] = end;
dl.push_back((node){.frame_id = frame_id, .ref_cnt = 0});
auto new_node = dl.rbegin().base();
um[frame_id] = new_node;
} else {
dl.splice();
// move target node to front
dl.splice(dl.begin(), dl, um[frame_id]);
}
// if doesn't -> add it
LOG_INFO("Unpin is called for frame_id: %d", frame_id);
Expand Down

0 comments on commit 28c8375

Please sign in to comment.