1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
10 #include "table/block_based/block_based_table_reader.h"
12 namespace ROCKSDB_NAMESPACE
{
13 class BlockPrefetcher
{
15 explicit BlockPrefetcher(size_t compaction_readahead_size
,
16 size_t initial_auto_readahead_size
)
17 : compaction_readahead_size_(compaction_readahead_size
),
18 readahead_size_(initial_auto_readahead_size
),
19 initial_auto_readahead_size_(initial_auto_readahead_size
) {}
21 void PrefetchIfNeeded(const BlockBasedTable::Rep
* rep
,
22 const BlockHandle
& handle
, size_t readahead_size
,
23 bool is_for_compaction
,
24 const bool no_sequential_checking
,
25 Env::IOPriority rate_limiter_priority
);
26 FilePrefetchBuffer
* prefetch_buffer() { return prefetch_buffer_
.get(); }
28 void UpdateReadPattern(const uint64_t& offset
, const size_t& len
) {
29 prev_offset_
= offset
;
33 bool IsBlockSequential(const uint64_t& offset
) {
34 return (prev_len_
== 0 || (prev_offset_
+ prev_len_
== offset
));
37 void ResetValues(size_t initial_auto_readahead_size
) {
39 // Since initial_auto_readahead_size_ can be different from
40 // the value passed to BlockBasedTableOptions.initial_auto_readahead_size in
41 // case of adaptive_readahead, so fallback the readahead_size_ to that value
43 initial_auto_readahead_size_
= initial_auto_readahead_size
;
44 readahead_size_
= initial_auto_readahead_size_
;
49 void SetReadaheadState(ReadaheadFileInfo::ReadaheadInfo
* readahead_info
) {
50 num_file_reads_
= readahead_info
->num_file_reads
;
51 initial_auto_readahead_size_
= readahead_info
->readahead_size
;
52 TEST_SYNC_POINT_CALLBACK("BlockPrefetcher::SetReadaheadState",
53 &initial_auto_readahead_size_
);
57 // Readahead size used in compaction, its value is used only if
58 // lookup_context_.caller = kCompaction.
59 size_t compaction_readahead_size_
;
61 // readahead_size_ is used if underlying FS supports prefetching.
62 size_t readahead_size_
;
63 size_t readahead_limit_
= 0;
64 // initial_auto_readahead_size_ is used if RocksDB uses internal prefetch
66 uint64_t initial_auto_readahead_size_
;
67 uint64_t num_file_reads_
= 0;
68 uint64_t prev_offset_
= 0;
70 std::unique_ptr
<FilePrefetchBuffer
> prefetch_buffer_
;
72 } // namespace ROCKSDB_NAMESPACE