-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.cpp
583 lines (518 loc) · 15.7 KB
/
service.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
* @file service.cpp
*
* @copyright 2021-2022 extratype
*/
#include "service.hpp"
#include <unordered_set>
#include <filesystem>
using std::bad_alloc;
namespace chunkdisk
{
// maximum number of cached pages (write through)
static constexpr auto MAX_PAGES = u32(2048);
ChunkDiskService::ChunkDiskService(std::vector<ChunkDiskBase> bases, SPD_STORAGE_UNIT* storage_unit,
bool trim_chunk, bool zero_chunk)
: bases(std::move(bases)), storage_unit(storage_unit),
trim_chunk(trim_chunk), zero_chunk(zero_chunk), max_pages_(MAX_PAGES)
{
}
DWORD ChunkDiskService::Start()
{
if (bases.empty()) return ERROR_INVALID_PARAMETER;
for (auto i = bases.size(); i > 0; --i)
{
auto& base = bases[i - 1];
auto err = base.Start();
if (err != ERROR_SUCCESS)
{
if (i > 1)
{
SpdLogErr(
L"error: failed to initialize disk #%llu (#1: argument, #2~: parents) with code %lu",
i, err);
}
else
{
SpdLogErr(L"error: failed to initialize disk with code %lu", err);
}
bases.clear();
return err;
}
}
return ERROR_SUCCESS;
}
DWORD ChunkDiskService::PeekPage(const u64 page_idx, SRWLock& lk, LPVOID& ptr)
{
auto lkx = SRWLock(mutex_pages_, true);
auto it = cached_pages_.find(page_idx);
if (it == cached_pages_.end()) return ERROR_NOT_FOUND;
auto* entry = &((*it).second);
if (entry->is_owned()) return ERROR_LOCKED;
cached_pages_.reinsert_back(it);
auto m = entry->mutex;
lkx.unlock();
lkx.switch_lock();
auto lkp = SRWLock(*m, false, std::defer_lock);
while (true)
{
std::lock(lkx, lkp);
// entry may be moved or replaced
it = cached_pages_.find(page_idx);
if (it == cached_pages_.end()) return ERROR_NOT_FOUND;
entry = &((*it).second);
if (m != entry->mutex)
{
lkp.unlock();
m = entry->mutex;
lkp = SRWLock(*m, false, std::defer_lock);
lkx.unlock();
continue;
}
lkp.release();
lk = SRWLock(*m, false, std::adopt_lock);
ptr = entry->ptr.get();
return ERROR_SUCCESS;
}
}
DWORD ChunkDiskService::LockPage(const u64 page_idx, LPVOID& ptr, LPVOID& user)
{
auto lk = SRWLock(mutex_pages_, true);
// entry to lock
auto it = cached_pages_.find(page_idx);
auto* entry = (it != cached_pages_.end()) ? &((*it).second) : nullptr;
auto find_entry = [this, page_idx, &it, &entry]() -> bool
{
it = cached_pages_.find(page_idx);
entry = (it != cached_pages_.end()) ? &((*it).second) : nullptr;
return entry != nullptr;
};
// try to keep < max_pages_
// lk: shared, resets lk
auto trim_pages = [this, &lk]()
{
while (cached_pages_.size() >= max_pages_)
{
// find entry to evict
auto it_evict = cached_pages_.end();
for (auto it = cached_pages_.begin(); it != cached_pages_.end();)
{
auto* entry = &((*it).second);
if (entry->is_owned())
{
// the added entry is skipped here
++it;
continue;
}
// avoid deadlock
if (!entry->mutex->try_lock())
{
++it;
continue;
}
entry->mutex->unlock();
it_evict = it;
break;
}
if (it_evict == cached_pages_.end()) break;
// resets lk, iterators may be invalidated
RemovePageEntry(lk, it_evict);
}
};
while (true)
{
if (entry != nullptr)
{
// page hit
if (entry->is_owned())
{
user = entry->user;
return ERROR_LOCKED;
}
cached_pages_.reinsert_back(it);
auto m = entry->mutex;
lk.unlock();
lk.switch_lock();
auto lkp = SRWLock(*m, true, std::defer_lock);
while (true)
{
std::lock(lk, lkp);
// entry may be moved or replaced
if (!find_entry())
{
break;
}
if (m != entry->mutex)
{
lkp.unlock();
m = entry->mutex;
lkp = SRWLock(*m, true, std::defer_lock);
lk.unlock();
continue;
}
break;
}
if (entry == nullptr)
{
lkp.unlock();
lk.switch_lock();
continue;
}
entry->set_owner();
lkp.release();
ptr = entry->ptr.get();
entry->user = user;
return ERROR_SUCCESS;
}
else
{
// page miss
try
{
auto new_ptr = Pages(VirtualAlloc(nullptr, bases[0].PageBytes(1),
MEM_COMMIT, PAGE_READWRITE));
if (new_ptr == nullptr) return ERROR_NOT_ENOUGH_MEMORY;
auto mutex = std::make_shared<std::shared_mutex>();
mutex->lock();
try
{
entry = &((*(cached_pages_.try_emplace(page_idx).first)).second);
}
catch (const bad_alloc&)
{
mutex->unlock();
throw;
}
entry->mutex = std::move(mutex);
entry->set_owner();
entry->ptr = std::move(new_ptr);
ptr = entry->ptr.get();
entry->user = user;
// entry may be moved
lk.switch_lock();
trim_pages();
return ERROR_NOT_FOUND;
}
catch (const bad_alloc&)
{
return ERROR_NOT_ENOUGH_MEMORY;
}
}
}
}
DWORD ChunkDiskService::ClaimPage(const u64 page_idx, LPVOID& ptr)
{
auto lk = SRWLock(mutex_pages_, false);
auto it = cached_pages_.find(page_idx);
if (it == cached_pages_.end()) return ERROR_NOT_FOUND;
auto* entry = &((*it).second);
if (!entry->is_owned()) return ERROR_INVALID_STATE;
ptr = entry->ptr.get();
return ERROR_SUCCESS;
}
DWORD ChunkDiskService::ClaimPage(const u64 page_idx, LPVOID& ptr, LPVOID& user)
{
auto lk = SRWLock(mutex_pages_, false);
auto it = cached_pages_.find(page_idx);
if (it == cached_pages_.end()) return ERROR_NOT_FOUND;
auto* entry = &((*it).second);
if (!entry->is_owned()) return ERROR_INVALID_STATE;
ptr = entry->ptr.get();
user = entry->user;
return ERROR_SUCCESS;
}
DWORD ChunkDiskService::RemovePageEntry(SRWLock& lk, Map<u64, PageEntry>::iterator it)
{
if (lk.is_exclusive() || !lk) return ERROR_INVALID_PARAMETER;
auto page_idx = (*it).first;
auto* entry = &((*it).second);
if (entry->is_owned()) return ERROR_LOCKED;
auto find_entry = [this, page_idx, &it, &entry]() -> bool
{
it = cached_pages_.find(page_idx);
entry = (it != cached_pages_.end()) ? &((*it).second) : nullptr;
return entry != nullptr;
};
auto m = entry->mutex;
lk.unlock();
lk.switch_lock();
auto lkp = SRWLock(*m, true, std::defer_lock);
std::lock(lk, lkp);
// entry may be moved or replaced
if (!find_entry())
{
lk.switch_lock();
return ERROR_SUCCESS;
}
if (m != entry->mutex)
{
// it deleted but then new entry added
lk.switch_lock();
return ERROR_SUCCESS;
}
entry->set_owner();
cached_pages_.erase(it);
lkp.unlock();
m.reset();
lk.switch_lock();
return ERROR_SUCCESS;
}
DWORD ChunkDiskService::UnlockPage(const u64 page_idx, const bool remove)
{
auto lk = SRWLock(mutex_pages_, false);
auto it = cached_pages_.find(page_idx);
if (it == cached_pages_.end()) return ERROR_NOT_FOUND;
auto* entry = &((*it).second);
if (!entry->is_owned()) return ERROR_INVALID_STATE;
entry->user = nullptr;
entry->clear_owner();
entry->mutex->unlock();
return remove ? RemovePageEntry(lk, it) : ERROR_SUCCESS;
}
DWORD ChunkDiskService::FlushPages(const PageRange& r, LPVOID& user)
{
auto lk = SRWLock(mutex_pages_, false);
if (cached_pages_.empty()) return ERROR_SUCCESS;
const auto size = cached_pages_.size();
if (size > r.end_idx - r.start_idx + 1)
{
for (auto i = r.start_idx; i <= r.end_idx; ++i)
{
auto it = cached_pages_.find(r.base_idx + i);
if (it == cached_pages_.end()) continue;
auto err = RemovePageEntry(lk, it);
if (err == ERROR_LOCKED)
{
// lk not reset if ERROR_LOCKED
user = (*it).second.user;
return ERROR_LOCKED;
}
else if (err != ERROR_SUCCESS)
{
return err;
}
}
}
else
{
// inverse search
// RemovePageEntry() resets lk
// Iterating over cached_pages_ is not thread safe
auto pages = std::vector<u64>();
try
{
for (auto&& p : cached_pages_)
{
auto idx = p.first;
if (r.base_idx + r.start_idx <= idx && idx <= r.base_idx + r.end_idx) pages.push_back(idx);
}
}
catch (const bad_alloc&)
{
return ERROR_NOT_ENOUGH_MEMORY;
}
for (auto idx : pages)
{
auto it = cached_pages_.find(idx);
if (it == cached_pages_.end()) continue;
auto err = RemovePageEntry(lk, it);
if (err == ERROR_LOCKED)
{
// lk not reset if ERROR_LOCKED
user = (*it).second.user;
return ERROR_LOCKED;
}
else if (err != ERROR_SUCCESS)
{
return err;
}
}
}
return ERROR_SUCCESS;
}
DWORD ChunkDiskService::FlushPages()
{
auto lk = SRWLock(mutex_pages_, false);
auto err = DWORD(ERROR_SUCCESS);
while (!cached_pages_.empty())
{
// RemovePageEntry() resets lk
// Iterating over cached_pages_ is not thread safe
const auto size = cached_pages_.size();
auto pages = std::vector<u64>();
pages.reserve(size);
try
{
for (auto&& p : cached_pages_) pages.push_back(p.first);
}
catch (const bad_alloc&)
{
err = ERROR_NOT_ENOUGH_MEMORY;
}
for (auto idx : pages)
{
auto it = cached_pages_.find(idx);
if (it == cached_pages_.end()) continue;
auto err1 = RemovePageEntry(lk, it);
if (err1 != ERROR_SUCCESS) err = err1;
}
// no progress
if (size == cached_pages_.size()) break;
}
// ERROR_SUCCESS anyway if empty, tried RemovePageEntry() otherwise
return cached_pages_.empty() ? ERROR_SUCCESS : err;
}
usize ChunkDiskService::FindChunk(const u64 chunk_idx)
{
auto i = usize(0);
const auto n = bases.size();
for (; i < n; ++i)
{
if (bases[i].CheckChunk(chunk_idx)) break;
}
return i;
}
DWORD ChunkDiskService::CreateChunk(
const u64 chunk_idx, FileHandle& handle_out, const bool is_write, const bool is_locked)
{
if (is_write)
{
return bases[0].CreateChunk(chunk_idx, handle_out, is_write, is_locked);
}
else
{
const auto i = FindChunk(chunk_idx);
if (i == bases.size())
{
handle_out = FileHandle();
return ERROR_SUCCESS;
}
return bases[i].CreateChunk(chunk_idx, handle_out, is_write, is_locked);
}
}
DWORD ChunkDiskService::LockChunk(const u64 chunk_idx, LPVOID& user)
{
auto lk = SRWLock(mutex_chunk_lock_, true);
auto [it, emplaced] = chunk_lock_.emplace(chunk_idx, user);
if (emplaced)
{
return ERROR_SUCCESS;
}
else
{
user = it->second;
return ERROR_LOCKED;
}
}
bool ChunkDiskService::CheckChunkLocked(const u64 chunk_idx)
{
auto lk = SRWLock(mutex_chunk_lock_, false);
if (chunk_lock_.empty()) return false;
return chunk_lock_.find(chunk_idx) != chunk_lock_.end();
}
bool ChunkDiskService::CheckChunkLocked(const u64 chunk_idx, LPVOID& user)
{
auto lk = SRWLock(mutex_chunk_lock_, false);
if (chunk_lock_.empty()) return false;
auto it = chunk_lock_.find(chunk_idx);
if (it == chunk_lock_.end())
{
return false;
}
else
{
user = it->second;
return true;
}
}
DWORD ChunkDiskService::UnlockChunk(const u64 chunk_idx)
{
auto lk = SRWLock(mutex_chunk_lock_, true);
return (chunk_lock_.erase(chunk_idx) != 0) ? ERROR_SUCCESS : ERROR_NOT_FOUND;
}
DWORD ChunkDiskService::UnmapRange(SRWLock& lk, const u64 chunk_idx, const u64 start_off, const u64 end_off)
{
if (lk) return ERROR_INVALID_PARAMETER;
if (start_off >= end_off) return ERROR_INVALID_PARAMETER;
if (end_off > bases[0].chunk_length) return ERROR_INVALID_PARAMETER;
lk = SRWLock(mutex_unmapped_, true);
try
{
auto [rit, emplaced] = chunk_unmapped_.try_emplace(chunk_idx);
auto& ranges = rit->second;
// add range [start_off, end_off)
auto start = ranges.upper_bound(start_off); // start_off < start->first
auto end = ranges.upper_bound(end_off); // end_off < end->first
auto new_start = (start == ranges.begin());
if (!new_start)
{
--start;
// check overlap on the left
if (start_off <= start->second)
{
start->second = max(start->second, end_off);
}
else
{
new_start = true;
}
}
if (new_start)
{
try
{
start = ranges.emplace(start_off, end_off).first;
}
catch (const bad_alloc&)
{
if (emplaced) chunk_unmapped_.erase(rit);
lk = SRWLock();
return ERROR_NOT_ENOUGH_MEMORY;
}
}
auto it = start;
for (++it; it != end; ++it)
{
if (it->second > end_off) break;
}
ranges.erase(std::next(start), it);
if (it != ranges.end())
{
// check overlap on the right
if (it->first <= start->second)
{
start->second = max(start->second, it->second);
ranges.erase(it);
}
}
if (ranges.size() != 1) return ERROR_IO_PENDING;
if (!bases[0].IsWholeChunk(ranges.begin()->first, ranges.begin()->second)) return ERROR_IO_PENDING;
chunk_unmapped_.erase(rit);
}
catch (const bad_alloc&)
{
lk = SRWLock();
return ERROR_NOT_ENOUGH_MEMORY;
}
return ERROR_SUCCESS;
}
void ChunkDiskService::FlushUnmapRanges(const u64 chunk_idx)
{
auto lk = SRWLock(mutex_unmapped_, false);
if (chunk_unmapped_.empty()) return;
if (chunk_unmapped_.find(chunk_idx) == chunk_unmapped_.end()) return;
lk.switch_lock();
auto it = chunk_unmapped_.find(chunk_idx);
if (it == chunk_unmapped_.end()) return;
chunk_unmapped_.erase(it);
}
void ChunkDiskService::FlushUnmapRanges()
{
auto lk = SRWLock(mutex_unmapped_, true);
chunk_unmapped_.clear();
}
void ChunkDiskService::SyncUnmapRanges()
{
auto lk = SRWLock(mutex_unmapped_, false);
}
}