block_prefetcher.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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).
  5. //
  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.
  9. #pragma once
  10. #include "table/block_based/block_based_table_reader.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class BlockPrefetcher {
  13. public:
  14. explicit BlockPrefetcher(size_t compaction_readahead_size,
  15. size_t initial_auto_readahead_size)
  16. : compaction_readahead_size_(compaction_readahead_size),
  17. readahead_size_(initial_auto_readahead_size),
  18. initial_auto_readahead_size_(initial_auto_readahead_size) {}
  19. void PrefetchIfNeeded(
  20. const BlockBasedTable::Rep* rep, const BlockHandle& handle,
  21. size_t readahead_size, bool is_for_compaction,
  22. const bool no_sequential_checking, const ReadOptions& read_options,
  23. const std::function<void(bool, uint64_t&, uint64_t&)>& readaheadsize_cb,
  24. bool is_async_io_prefetch);
  25. FilePrefetchBuffer* prefetch_buffer() { return prefetch_buffer_.get(); }
  26. void UpdateReadPattern(const uint64_t& offset, const size_t& len) {
  27. prev_offset_ = offset;
  28. prev_len_ = len;
  29. }
  30. bool IsBlockSequential(const uint64_t& offset) {
  31. return (prev_len_ == 0 || (prev_offset_ + prev_len_ == offset));
  32. }
  33. void ResetValues(size_t initial_auto_readahead_size) {
  34. num_file_reads_ = 1;
  35. // Since initial_auto_readahead_size_ can be different from
  36. // the value passed to BlockBasedTableOptions.initial_auto_readahead_size in
  37. // case of adaptive_readahead, so fallback the readahead_size_ to that value
  38. // in case of reset.
  39. initial_auto_readahead_size_ = initial_auto_readahead_size;
  40. readahead_size_ = initial_auto_readahead_size_;
  41. readahead_limit_ = 0;
  42. return;
  43. }
  44. void SetReadaheadState(ReadaheadFileInfo::ReadaheadInfo* readahead_info) {
  45. num_file_reads_ = readahead_info->num_file_reads;
  46. initial_auto_readahead_size_ = readahead_info->readahead_size;
  47. TEST_SYNC_POINT_CALLBACK("BlockPrefetcher::SetReadaheadState",
  48. &initial_auto_readahead_size_);
  49. }
  50. private:
  51. // Readahead size used in compaction, its value is used only if
  52. // lookup_context_.caller = kCompaction.
  53. size_t compaction_readahead_size_;
  54. // readahead_size_ is used in non-compaction read if underlying FS supports
  55. // prefetching.
  56. size_t readahead_size_;
  57. size_t readahead_limit_ = 0;
  58. // initial_auto_readahead_size_ is used in non-compaction read if RocksDB uses
  59. // internal prefetch buffer.
  60. uint64_t initial_auto_readahead_size_;
  61. uint64_t num_file_reads_ = 0;
  62. uint64_t prev_offset_ = 0;
  63. size_t prev_len_ = 0;
  64. std::unique_ptr<FilePrefetchBuffer> prefetch_buffer_;
  65. };
  66. } // namespace ROCKSDB_NAMESPACE