forked from arvidn/libtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_block_cache.cpp
508 lines (424 loc) · 14.5 KB
/
test_block_cache.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
/*
Copyright (c) 2012, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "libtorrent/block_cache.hpp"
#include "libtorrent/io_service.hpp"
#include "libtorrent/alert.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/disk_io_thread.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/aux_/path.hpp" // for bufs_size
#include <functional>
#include <memory>
using namespace lt;
struct test_storage_impl : storage_interface
{
explicit test_storage_impl(file_storage const& fs) : storage_interface(fs) {}
void initialize(storage_error& ec) override {}
int readv(span<iovec_t const> bufs
, piece_index_t piece, int offset, open_mode_t flags, storage_error& ec) override
{
return bufs_size(bufs);
}
int writev(span<iovec_t const> bufs
, piece_index_t piece, int offset, open_mode_t flags, storage_error& ec) override
{
return bufs_size(bufs);
}
bool has_any_file(storage_error& ec) override { return false; }
void set_file_priority(aux::vector<download_priority_t, file_index_t> const& prio
, storage_error& ec) override {}
status_t move_storage(std::string const& save_path, move_flags_t flags
, storage_error& ec) override { return status_t::no_error; }
bool verify_resume_data(add_torrent_params const& rd
, aux::vector<std::string, file_index_t> const& links
, storage_error& ec) override { return true; }
void release_files(storage_error& ec) override {}
void rename_file(file_index_t index, std::string const& new_filename
, storage_error& ec) override {}
void delete_files(remove_flags_t, storage_error& ec) override {}
};
struct allocator : buffer_allocator_interface
{
allocator(block_cache& bc, storage_interface* st)
: m_cache(bc), m_storage(st) {}
void free_disk_buffer(char* b) override
{ m_cache.free_buffer(b); }
void reclaim_blocks(span<aux::block_cache_reference> refs) override
{
for (auto ref : refs)
m_cache.reclaim_block(m_storage, ref);
}
private:
block_cache& m_cache;
storage_interface* m_storage;
};
static void nop() {}
#if TORRENT_USE_ASSERTS
#define INITIALIZE_JOB(j) j.in_use = true;
#else
#define INITIALIZE_JOB(j)
#endif
#define TEST_SETUP \
io_service ios; \
block_cache bc(0x4000, ios, std::bind(&nop)); \
aux::session_settings sett; \
file_storage fs; \
fs.add_file("a/test0", 0x4000); \
fs.add_file("a/test1", 0x4000); \
fs.add_file("a/test2", 0x4000); \
fs.add_file("a/test3", 0x4000); \
fs.add_file("a/test4", 0x4000); \
fs.add_file("a/test5", 0x4000); \
fs.add_file("a/test6", 0x4000); \
fs.add_file("a/test7", 0x4000); \
fs.set_piece_length(0x8000); \
fs.set_num_pieces(5); \
std::shared_ptr<storage_interface> pm \
= std::make_shared<test_storage_impl>(fs); \
allocator alloc(bc, pm.get()); \
bc.set_settings(sett); \
pm->m_settings = &sett; \
disk_io_job rj; \
disk_io_job wj; \
INITIALIZE_JOB(rj) \
INITIALIZE_JOB(wj) \
rj.storage = pm; \
wj.storage = pm; \
cached_piece_entry* pe = nullptr; \
int ret = 0; \
iovec_t iov; \
(void)iov; \
(void)ret; \
(void)pe
#define WRITE_BLOCK(p, b) \
wj.flags = disk_io_job::in_progress; \
wj.action = job_action_t::write; \
wj.d.io.offset = (b) * 0x4000; \
wj.d.io.buffer_size = 0x4000; \
wj.piece = piece_index_t(p); \
wj.argument = disk_buffer_holder(alloc, bc.allocate_buffer("write-test"), 0x4000); \
pe = bc.add_dirty_block(&wj)
#define READ_BLOCK(p, b, r) \
rj.action = job_action_t::read; \
rj.d.io.offset = (b) * 0x4000; \
rj.d.io.buffer_size = 0x4000; \
rj.piece = piece_index_t(p); \
rj.storage = pm; \
rj.argument = disk_buffer_holder(alloc, nullptr, 0); \
ret = bc.try_read(&rj, alloc)
#define FLUSH(flushing) \
for (int i = 0; i < int(sizeof(flushing)/sizeof((flushing)[0])); ++i) \
{ \
pe->blocks[(flushing)[i]].pending = true; \
bc.inc_block_refcount(pe, 0, block_cache::ref_flushing); \
} \
bc.blocks_flushed(pe, flushing, sizeof(flushing)/sizeof((flushing)[0]))
#define INSERT(p, b) \
wj.piece = piece_index_t(p); \
pe = bc.allocate_piece(&wj, cached_piece_entry::read_lru1); \
ret = bc.allocate_iovec(iov); \
TEST_EQUAL(ret, 0); \
bc.insert_blocks(pe, b, iov, &wj)
void test_write()
{
TEST_SETUP;
// write block (0,0)
WRITE_BLOCK(0, 0);
counters c;
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 1);
TEST_EQUAL(c[counters::read_cache_blocks], 0);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 0);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 1);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
// try to read it back
READ_BLOCK(0, 0, 1);
TEST_EQUAL(bc.pinned_blocks(), 1);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::pinned_blocks], 1);
// it's supposed to be a cache hit
TEST_CHECK(ret >= 0);
// return the reference to the buffer we just read
rj.argument = remove_flags_t{};
TEST_EQUAL(bc.pinned_blocks(), 0);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::pinned_blocks], 0);
// try to read block (1, 0)
READ_BLOCK(1, 0, 1);
// that's supposed to be a cache miss
TEST_CHECK(ret < 0);
TEST_EQUAL(bc.pinned_blocks(), 0);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::pinned_blocks], 0);
rj.argument = remove_flags_t{};
tailqueue<disk_io_job> jobs;
bc.clear(jobs);
}
void test_flush()
{
TEST_SETUP;
// write block (0,0)
WRITE_BLOCK(0, 0);
// pretend to flush to disk
int flushing[1] = {0};
FLUSH(flushing);
tailqueue<disk_io_job> jobs;
bc.clear(jobs);
}
void test_insert()
{
TEST_SETUP;
INSERT(0, 0);
counters c;
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 1);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 1);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
tailqueue<disk_io_job> jobs;
bc.clear(jobs);
}
void test_evict()
{
TEST_SETUP;
INSERT(0, 0);
counters c;
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 1);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 1);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
tailqueue<disk_io_job> jobs;
// this should make it not be evicted
// just free the buffers
++pe->piece_refcount;
bc.evict_piece(pe, jobs, block_cache::allow_ghost);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 0);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 1);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
--pe->piece_refcount;
bc.evict_piece(pe, jobs, block_cache::allow_ghost);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 0);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 0);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 1);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
bc.clear(jobs);
}
// test to have two different requestors read a block and
// make sure it moves into the MFU list
void test_arc_promote()
{
TEST_SETUP;
INSERT(0, 0);
counters c;
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 1);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 1);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
READ_BLOCK(0, 0, 1);
TEST_EQUAL(bc.pinned_blocks(), 1);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::pinned_blocks], 1);
// it's supposed to be a cache hit
TEST_CHECK(ret >= 0);
// return the reference to the buffer we just read
rj.argument = remove_flags_t{};
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 1);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 1);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
READ_BLOCK(0, 0, 2);
TEST_EQUAL(bc.pinned_blocks(), 1);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::pinned_blocks], 1);
// it's supposed to be a cache hit
TEST_CHECK(ret >= 0);
// return the reference to the buffer we just read
rj.argument = remove_flags_t{};
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 1);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 0);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 1);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
tailqueue<disk_io_job> jobs;
bc.clear(jobs);
}
void test_arc_unghost()
{
TEST_SETUP;
INSERT(0, 0);
counters c;
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 1);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 1);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
tailqueue<disk_io_job> jobs;
bc.evict_piece(pe, jobs, block_cache::allow_ghost);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
TEST_EQUAL(c[counters::read_cache_blocks], 0);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 0);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 1);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
// the block is now a ghost. If we cache-hit it,
// it should be promoted back to the main list
bc.cache_hit(pe, 0, false);
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::write_cache_blocks], 0);
// we didn't actually read in any blocks, so the cache size
// is still 0
TEST_EQUAL(c[counters::read_cache_blocks], 0);
TEST_EQUAL(c[counters::pinned_blocks], 0);
TEST_EQUAL(c[counters::arc_mru_size], 1);
TEST_EQUAL(c[counters::arc_mru_ghost_size], 0);
TEST_EQUAL(c[counters::arc_mfu_size], 0);
TEST_EQUAL(c[counters::arc_mfu_ghost_size], 0);
TEST_EQUAL(c[counters::arc_write_size], 0);
TEST_EQUAL(c[counters::arc_volatile_size], 0);
bc.clear(jobs);
}
void test_iovec()
{
TEST_SETUP;
ret = bc.allocate_iovec(iov);
bc.free_iovec(iov);
}
void test_unaligned_read()
{
TEST_SETUP;
INSERT(0, 0);
INSERT(0, 1);
rj.action = job_action_t::read;
rj.d.io.offset = 0x2000;
rj.d.io.buffer_size = 0x4000;
rj.piece = piece_index_t(0);
rj.storage = pm;
rj.argument = disk_buffer_holder(alloc, nullptr, 0);
ret = bc.try_read(&rj, alloc);
// unaligned reads copies the data into a new buffer
// rather than
TEST_EQUAL(bc.pinned_blocks(), 0);
counters c;
bc.update_stats_counters(c);
TEST_EQUAL(c[counters::pinned_blocks], 0);
// it's supposed to be a cache hit
TEST_CHECK(ret >= 0);
// return the reference to the buffer we just read
rj.argument = remove_flags_t{};
tailqueue<disk_io_job> jobs;
bc.clear(jobs);
}
TORRENT_TEST(block_cache)
{
test_write();
test_flush();
test_insert();
test_evict();
test_arc_promote();
test_arc_unghost();
test_iovec();
test_unaligned_read();
// TODO: test try_evict_blocks
// TODO: test evicting volatile pieces, to see them be removed
// TODO: test evicting dirty pieces
// TODO: test free_piece
// TODO: test abort_dirty
// TODO: test unaligned reads
}
TORRENT_TEST(delete_piece)
{
TEST_SETUP;
TEST_CHECK(bc.num_pieces() == 0);
INSERT(0, 0);
TEST_CHECK(bc.num_pieces() == 1);
rj.action = job_action_t::read;
rj.d.io.offset = 0x2000;
rj.d.io.buffer_size = 0x4000;
rj.piece = piece_index_t(0);
rj.storage = pm;
rj.argument = remove_flags_t{};
ret = bc.try_read(&rj, alloc);
cached_piece_entry* pe_ = bc.find_piece(pm.get(), piece_index_t(0));
bc.mark_for_eviction(pe_, block_cache::disallow_ghost);
TEST_CHECK(bc.num_pieces() == 0);
}