-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
disk_manager_memory.h
179 lines (154 loc) · 5.11 KB
/
disk_manager_memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//===----------------------------------------------------------------------===//
//
// BusTub
//
// disk_manager_memory.h
//
// Identification: src/include/storage/disk/disk_manager_memory.h
//
// Copyright (c) 2015-2020, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include <array>
#include <chrono> // NOLINT
#include <cstring>
#include <fstream>
#include <future> // NOLINT
#include <memory>
#include <mutex> // NOLINT
#include <optional>
#include <shared_mutex>
#include <string>
#include <thread> // NOLINT
#include <utility>
#include <vector>
#include "common/config.h"
#include "common/exception.h"
#include "common/logger.h"
#include "fmt/core.h"
#include "storage/disk/disk_manager.h"
namespace bustub {
/**
* DiskManagerMemory replicates the utility of DiskManager on memory. It is primarily used for
* data structure performance testing.
*/
class DiskManagerMemory : public DiskManager {
public:
explicit DiskManagerMemory(size_t pages);
~DiskManagerMemory() override { delete[] memory_; }
/**
* Write a page to the database file.
* @param page_id id of the page
* @param page_data raw page data
*/
void WritePage(page_id_t page_id, const char *page_data) override;
/**
* Read a page from the database file.
* @param page_id id of the page
* @param[out] page_data output buffer
*/
void ReadPage(page_id_t page_id, char *page_data) override;
private:
char *memory_;
};
/**
* DiskManagerMemory replicates the utility of DiskManager on memory. It is primarily used for
* data structure performance testing.
*/
class DiskManagerUnlimitedMemory : public DiskManager {
public:
DiskManagerUnlimitedMemory() { std::fill(recent_access_.begin(), recent_access_.end(), -1); }
/**
* Write a page to the database file.
* @param page_id id of the page
* @param page_data raw page data
*/
void WritePage(page_id_t page_id, const char *page_data) override {
ProcessLatency(page_id);
std::unique_lock<std::mutex> l(mutex_);
if (!thread_id_.has_value()) {
thread_id_ = std::this_thread::get_id();
}
if (page_id >= static_cast<int>(data_.size())) {
data_.resize(page_id + 1);
}
if (data_[page_id] == nullptr) {
data_[page_id] = std::make_shared<ProtectedPage>();
}
std::shared_ptr<ProtectedPage> ptr = data_[page_id];
std::unique_lock<std::shared_mutex> l_page(ptr->second);
l.unlock();
memcpy(ptr->first.data(), page_data, BUSTUB_PAGE_SIZE);
PostProcessLatency(page_id);
}
/**
* Read a page from the database file.
* @param page_id id of the page
* @param[out] page_data output buffer
*/
void ReadPage(page_id_t page_id, char *page_data) override {
ProcessLatency(page_id);
std::unique_lock<std::mutex> l(mutex_);
if (!thread_id_.has_value()) {
thread_id_ = std::this_thread::get_id();
}
if (page_id >= static_cast<int>(data_.size()) || page_id < 0) {
fmt::println(stderr, "page {} not in range", page_id);
std::terminate();
return;
}
if (data_[page_id] == nullptr) {
fmt::println(stderr, "page {} not exist", page_id);
std::terminate();
return;
}
std::shared_ptr<ProtectedPage> ptr = data_[page_id];
std::shared_lock<std::shared_mutex> l_page(ptr->second);
l.unlock();
memcpy(page_data, ptr->first.data(), BUSTUB_PAGE_SIZE);
PostProcessLatency(page_id);
}
void ProcessLatency(page_id_t page_id) {
uint64_t sleep_micro_sec = 1000; // for random access, 1ms latency
if (latency_simulator_enabled_) {
std::unique_lock<std::mutex> lck(latency_processor_mutex_);
for (auto &recent_page_id : recent_access_) {
if ((recent_page_id & (~0x3)) == (page_id & (~0x3))) {
sleep_micro_sec = 100; // for access in the same "block", 0.1ms latency
break;
}
if (page_id >= recent_page_id && page_id <= recent_page_id + 3) {
sleep_micro_sec = 100; // for sequential access, 0.1ms latency
break;
}
}
lck.unlock();
std::this_thread::sleep_for(std::chrono::microseconds(sleep_micro_sec));
}
}
void PostProcessLatency(page_id_t page_id) {
if (latency_simulator_enabled_) {
std::scoped_lock<std::mutex> lck(latency_processor_mutex_);
recent_access_[access_ptr_] = page_id;
access_ptr_ = (access_ptr_ + 1) % recent_access_.size();
}
}
void EnableLatencySimulator(bool enabled) { latency_simulator_enabled_ = enabled; }
auto GetLastReadThreadAndClear() -> std::optional<std::thread::id> {
std::unique_lock<std::mutex> lck(mutex_);
auto t = thread_id_;
thread_id_ = std::nullopt;
return t;
}
private:
bool latency_simulator_enabled_{false};
std::mutex latency_processor_mutex_;
std::array<page_id_t, 4> recent_access_;
uint64_t access_ptr_{0};
using Page = std::array<char, BUSTUB_PAGE_SIZE>;
using ProtectedPage = std::pair<Page, std::shared_mutex>;
std::mutex mutex_;
std::optional<std::thread::id> thread_id_;
std::vector<std::shared_ptr<ProtectedPage>> data_;
};
} // namespace bustub