Skip to content

Commit fd2a01b

Browse files
fix(bpm-bench): concurrency issues (#519)
The return value of `FetchPage` can potentially be `nullptr` (e.g., when the BP is busy): add necessary checks. The reads and writes on individual pages happen concurrently: add necessary synchronization. Remove redundant `page_idx` addition (the addition result is not used). Closes #518
1 parent 71a0168 commit fd2a01b

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

tools/bpm_bench/bpm_bench.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,17 @@ auto main(int argc, char **argv) -> int {
168168

169169
while (!metrics.ShouldFinish()) {
170170
auto *page = bpm->FetchPage(page_ids[page_idx], AccessType::Scan);
171+
if (page == nullptr) {
172+
continue;
173+
}
171174

172175
char &ch = page->GetData()[page_idx % 1024];
176+
page->WLatch();
173177
ch += 1;
174178
if (ch == 0) {
175179
ch = 1;
176180
}
181+
page->WUnlatch();
177182

178183
bpm->UnpinPage(page->GetPageId(), true, AccessType::Scan);
179184
page_idx = (page_idx + 1) % BUSTUB_PAGE_CNT;
@@ -197,14 +202,18 @@ auto main(int argc, char **argv) -> int {
197202
while (!metrics.ShouldFinish()) {
198203
auto page_idx = dist(gen);
199204
auto *page = bpm->FetchPage(page_ids[page_idx], AccessType::Get);
205+
if (page == nullptr) {
206+
continue;
207+
}
200208

209+
page->RLatch();
201210
char ch = page->GetData()[page_idx % 1024];
211+
page->RUnlatch();
202212
if (ch == 0) {
203213
throw std::runtime_error("invalid data");
204214
}
205215

206216
bpm->UnpinPage(page->GetPageId(), false, AccessType::Get);
207-
page_idx += 1;
208217
metrics.Tick();
209218
metrics.Report();
210219
}

0 commit comments

Comments
 (0)