Skip to content

Commit 719e735

Browse files
authored
test(sqllogictest): add test utils regarding bpm & disk (#764)
Introduce necessary test utils for checking students' external merge sort implementations, including: 1. Maximum number of pages written to disk 2. Minimum number of pages written to disk 3. Minimum number of pages deleted on disk
1 parent 32d7085 commit 719e735

File tree

5 files changed

+92
-24
lines changed

5 files changed

+92
-24
lines changed

src/include/storage/disk/disk_manager.h

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class DiskManager {
6868
*/
6969
virtual void ReadPage(page_id_t page_id, char *page_data);
7070

71+
/**
72+
* Delete a page from the database file. Reclaim the disk space.
73+
* @param page_id id of the page
74+
*/
75+
virtual void DeletePage(page_id_t page_id);
76+
7177
/**
7278
* Flush the entire log buffer into disk.
7379
* @param log_data raw log data
@@ -93,6 +99,9 @@ class DiskManager {
9399
/** @return the number of disk writes */
94100
auto GetNumWrites() const -> int;
95101

102+
/** @return the number of deletions */
103+
auto GetNumDeletes() const -> int;
104+
96105
/**
97106
* Sets the future which is used to check for non-blocking flushes.
98107
* @param f the non-blocking flush check
@@ -115,6 +124,7 @@ class DiskManager {
115124
std::filesystem::path file_name_;
116125
int num_flushes_{0};
117126
int num_writes_{0};
127+
int num_deletes_{0};
118128
bool flush_log_{false};
119129
std::future<void> *flush_log_f_{nullptr};
120130
// With multiple buffer pool instances, need to protect file access

src/include/storage/disk/disk_manager_memory.h

+9
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class DiskManagerUnlimitedMemory : public DiskManager {
123123
l.unlock();
124124

125125
memcpy(ptr->first.data(), page_data, BUSTUB_PAGE_SIZE);
126+
num_writes_ += 1;
126127

127128
PostProcessLatency(page_id);
128129
}
@@ -158,6 +159,14 @@ class DiskManagerUnlimitedMemory : public DiskManager {
158159
PostProcessLatency(page_id);
159160
}
160161

162+
/**
163+
* Delete a page from the database file. Reclaim the disk space.
164+
* Note: This is a no-op for now without a more complex data structure to
165+
* track deallocated pages.
166+
* @param page_id id of the page
167+
*/
168+
void DeletePage(page_id_t page_id) override { num_deletes_ += 1; }
169+
161170
void ProcessLatency(page_id_t page_id) {
162171
uint64_t sleep_micro_sec = 1000; // for random access, 1ms latency
163172
if (latency_simulator_enabled_) {

src/include/storage/disk/disk_scheduler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class DiskScheduler {
100100
*
101101
* @param page_id The page ID of the page to deallocate from disk.
102102
*/
103-
void DeallocatePage(page_id_t page_id) {}
103+
void DeallocatePage(page_id_t page_id) { disk_manager_->DeletePage(page_id); }
104104

105105
private:
106106
/** Pointer to the disk manager. */

src/storage/disk/disk_manager.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ void DiskManager::ReadPage(page_id_t page_id, char *page_data) {
148148
}
149149
}
150150

151+
/**
152+
* Note: This is a no-op for now without a more complex data structure to
153+
* track deallocated pages.
154+
*/
155+
void DiskManager::DeletePage(page_id_t page_id) { num_deletes_ += 1; }
156+
151157
/**
152158
* Write the contents of the log into disk file
153159
* Only return when sync is done, and only perform sequence write
@@ -215,15 +221,20 @@ auto DiskManager::ReadLog(char *log_data, int size, int offset) -> bool {
215221
*/
216222
auto DiskManager::GetNumFlushes() const -> int { return num_flushes_; }
217223

224+
/**
225+
* Returns true if the log is currently being flushed
226+
*/
227+
auto DiskManager::GetFlushState() const -> bool { return flush_log_; }
228+
218229
/**
219230
* Returns number of Writes made so far
220231
*/
221232
auto DiskManager::GetNumWrites() const -> int { return num_writes_; }
222233

223234
/**
224-
* Returns true if the log is currently being flushed
235+
* Returns number of deletions made so far
225236
*/
226-
auto DiskManager::GetFlushState() const -> bool { return flush_log_; }
237+
auto DiskManager::GetNumDeletes() const -> int { return num_deletes_; }
227238

228239
/**
229240
* Private helper function to get disk file size

tools/sqllogictest/sqllogictest.cpp

+59-21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "argparse/argparse.hpp"
1212
#include "common/bustub_instance.h"
13+
#include "common/config.h"
1314
#include "common/exception.h"
1415
#include "common/util/string_util.h"
1516
#include "execution/check_options.h"
@@ -137,30 +138,30 @@ auto ProcessExtraOptions(const std::string &sql, bustub::BusTubInstance &instanc
137138
auto expected_cols_proj = std::stoi(args[2]);
138139
auto expected_cols_agg = std::stoi(args[3]);
139140
// find agg & proj plan and test if the output schema has the expected number of columns
140-
auto lines = bustub::StringUtil::Split(result.str(), "\n");
141-
for (auto &line : lines) {
142-
bustub::StringUtil::LTrim(&line);
143-
if (bustub::StringUtil::StartsWith(line, "Agg")) {
144-
auto cols = bustub::StringUtil::Split(line, "],");
145-
if (cols.size() != 3) {
146-
fmt::print("Agg plan wrong formatting!\n");
147-
return false;
148-
}
149-
for (int i = 0; i < 2; i++) {
150-
if (bustub::StringUtil::Count(cols[i], "\",")+1 > static_cast<size_t>(expected_cols_agg)) {
151-
fmt::print("Agg wrong column pruning count!\n");
152-
return false;
153-
}
154-
}
155-
break;
141+
auto lines = bustub::StringUtil::Split(result.str(), "\n");
142+
for (auto &line : lines) {
143+
bustub::StringUtil::LTrim(&line);
144+
if (bustub::StringUtil::StartsWith(line, "Agg")) {
145+
auto cols = bustub::StringUtil::Split(line, "],");
146+
if (cols.size() != 3) {
147+
fmt::print("Agg plan wrong formatting!\n");
148+
return false;
156149
}
157-
if (bustub::StringUtil::StartsWith(line, "Projection")) {
158-
if (bustub::StringUtil::Count(line, "\",")+1 > static_cast<size_t>(expected_cols_proj)) {
159-
fmt::print("Projection wrong column pruning count!\n");
150+
for (int i = 0; i < 2; i++) {
151+
if (bustub::StringUtil::Count(cols[i], "\",") + 1 > static_cast<size_t>(expected_cols_agg)) {
152+
fmt::print("Agg wrong column pruning count!\n");
160153
return false;
161154
}
162155
}
156+
break;
163157
}
158+
if (bustub::StringUtil::StartsWith(line, "Projection")) {
159+
if (bustub::StringUtil::Count(line, "\",") + 1 > static_cast<size_t>(expected_cols_proj)) {
160+
fmt::print("Projection wrong column pruning count!\n");
161+
return false;
162+
}
163+
}
164+
}
164165
} else {
165166
throw bustub::NotImplementedException(fmt::format("unsupported extra option: {}", opt));
166167
}
@@ -224,6 +225,15 @@ auto main(int argc, char **argv) -> int { // NOLINT
224225
program.add_argument("--verbose").help("increase output verbosity").default_value(false).implicit_value(true);
225226
program.add_argument("-d", "--diff").help("write diff file").default_value(false).implicit_value(true);
226227
program.add_argument("--in-memory").help("use in-memory backend").default_value(false).implicit_value(true);
228+
program.add_argument("--bpm-size")
229+
.help("size of the buffer pool")
230+
.default_value(std::to_string(bustub::BUFFER_POOL_SIZE));
231+
program.add_argument("--check-min-disk-write")
232+
.help("the minimum disk write threshold to be checked at the end of the program");
233+
program.add_argument("--check-max-disk-write")
234+
.help("the maximum disk write threshold to be checked at the end of the program");
235+
program.add_argument("--check-min-disk-delete")
236+
.help("the maximum disk deletion threshold to be checked at the end of the program");
227237

228238
try {
229239
program.parse_args(argc, argv);
@@ -235,6 +245,8 @@ auto main(int argc, char **argv) -> int { // NOLINT
235245

236246
bool verbose = program.get<bool>("verbose");
237247
bool diff = program.get<bool>("diff");
248+
auto check_min_disk_write = program.present("check-min-disk-write");
249+
238250
std::string filename = program.get<std::string>("file");
239251
std::ifstream t(filename);
240252

@@ -253,11 +265,12 @@ auto main(int argc, char **argv) -> int { // NOLINT
253265
}
254266

255267
std::unique_ptr<bustub::BusTubInstance> bustub;
268+
size_t bpm_size = std::stoul(program.get<std::string>("bpm-size"));
256269

257270
if (program.get<bool>("--in-memory")) {
258-
bustub = std::make_unique<bustub::BusTubInstance>();
271+
bustub = std::make_unique<bustub::BusTubInstance>(bpm_size);
259272
} else {
260-
bustub = std::make_unique<bustub::BusTubInstance>("test.bustub");
273+
bustub = std::make_unique<bustub::BusTubInstance>("test.bustub", bpm_size);
261274
}
262275

263276
bustub->GenerateMockTable();
@@ -365,5 +378,30 @@ auto main(int argc, char **argv) -> int { // NOLINT
365378
}
366379
}
367380

381+
if (program.is_used("--check-min-disk-write")) {
382+
int min_disk_write_num = std::stoi(program.get("--check-min-disk-write"));
383+
int actual_disk_write_num = bustub->disk_manager_->GetNumWrites();
384+
if (actual_disk_write_num < min_disk_write_num) {
385+
fmt::print("test incurred {} times of disk write, which is too low\n", actual_disk_write_num);
386+
return 1;
387+
}
388+
}
389+
if (program.is_used("--check-max-disk-write")) {
390+
int max_disk_write_num = std::stoi(program.get("--check-max-disk-write"));
391+
int actual_disk_write_num = bustub->disk_manager_->GetNumWrites();
392+
if (actual_disk_write_num > max_disk_write_num) {
393+
fmt::print("test incurred {} times of disk write, which is too high\n", actual_disk_write_num);
394+
return 1;
395+
}
396+
}
397+
if (program.is_used("--check-min-disk-delete")) {
398+
int min_disk_delete_num = std::stoi(program.get("--check-min-disk-delete"));
399+
int actual_disk_delete_num = bustub->disk_manager_->GetNumDeletes();
400+
if (actual_disk_delete_num < min_disk_delete_num) {
401+
fmt::print("test incurred {} times of disk deletion, which is too low\n", actual_disk_delete_num);
402+
return 1;
403+
}
404+
}
405+
368406
return 0;
369407
}

0 commit comments

Comments
 (0)