block_prefetcher.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include "table/block_based/block_prefetcher.h"
  10. #include "rocksdb/file_system.h"
  11. #include "table/block_based/block_based_table_reader.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. void BlockPrefetcher::PrefetchIfNeeded(
  14. const BlockBasedTable::Rep* rep, const BlockHandle& handle,
  15. const size_t readahead_size, bool is_for_compaction,
  16. const bool no_sequential_checking, const ReadOptions& read_options,
  17. const std::function<void(bool, uint64_t&, uint64_t&)>& readaheadsize_cb,
  18. bool is_async_io_prefetch) {
  19. if (read_options.read_tier == ReadTier::kBlockCacheTier) {
  20. // Disable prefetching when IO disallowed. (Note that we haven't allocated
  21. // any buffers yet despite the various tracked settings.)
  22. return;
  23. }
  24. ReadaheadParams readahead_params;
  25. readahead_params.initial_readahead_size = readahead_size;
  26. readahead_params.max_readahead_size = readahead_size;
  27. readahead_params.num_buffers = is_async_io_prefetch ? 2 : 1;
  28. const size_t len = BlockBasedTable::BlockSizeWithTrailer(handle);
  29. const size_t offset = handle.offset();
  30. if (is_for_compaction) {
  31. if (!rep->file->use_direct_io() && compaction_readahead_size_ > 0) {
  32. // If FS supports prefetching (readahead_limit_ will be non zero in that
  33. // case) and current block exists in prefetch buffer then return.
  34. if (offset + len <= readahead_limit_) {
  35. return;
  36. }
  37. IOOptions opts;
  38. IODebugContext dbg;
  39. Status s = rep->file->PrepareIOOptions(read_options, opts, &dbg);
  40. if (!s.ok()) {
  41. return;
  42. }
  43. if (rep->fs_prefetch_support) {
  44. s = rep->file->Prefetch(opts, offset, len + compaction_readahead_size_);
  45. if (s.ok()) {
  46. readahead_limit_ = offset + len + compaction_readahead_size_;
  47. return;
  48. } else if (!s.IsNotSupported()) {
  49. return;
  50. }
  51. // If FS prefetch returned NotSupported despite feature bit being set,
  52. // fall through to use internal prefetch buffer.
  53. }
  54. }
  55. // If FS prefetch is not supported, fall back to use internal prefetch
  56. // buffer.
  57. //
  58. // num_file_reads is used by FilePrefetchBuffer only when
  59. // implicit_auto_readahead is set.
  60. readahead_params.initial_readahead_size = compaction_readahead_size_;
  61. readahead_params.max_readahead_size = compaction_readahead_size_;
  62. rep->CreateFilePrefetchBufferIfNotExists(
  63. readahead_params, &prefetch_buffer_,
  64. /*readaheadsize_cb=*/nullptr,
  65. /*usage=*/FilePrefetchBufferUsage::kCompactionPrefetch);
  66. return;
  67. }
  68. // Explicit user requested readahead.
  69. if (readahead_size > 0) {
  70. rep->CreateFilePrefetchBufferIfNotExists(
  71. readahead_params, &prefetch_buffer_, readaheadsize_cb,
  72. /*usage=*/FilePrefetchBufferUsage::kUserScanPrefetch);
  73. return;
  74. }
  75. // Implicit readahead.
  76. // If max_auto_readahead_size is set to be 0 by user, no data will be
  77. // prefetched.
  78. size_t max_auto_readahead_size = rep->table_options.max_auto_readahead_size;
  79. if (max_auto_readahead_size == 0 || initial_auto_readahead_size_ == 0) {
  80. return;
  81. }
  82. if (initial_auto_readahead_size_ > max_auto_readahead_size) {
  83. initial_auto_readahead_size_ = max_auto_readahead_size;
  84. }
  85. readahead_params.initial_readahead_size = initial_auto_readahead_size_;
  86. readahead_params.max_readahead_size = max_auto_readahead_size;
  87. readahead_params.implicit_auto_readahead = true;
  88. readahead_params.num_file_reads_for_auto_readahead =
  89. rep->table_options.num_file_reads_for_auto_readahead;
  90. // In case of no_sequential_checking, it will skip the num_file_reads_ and
  91. // will always creates the FilePrefetchBuffer.
  92. if (no_sequential_checking) {
  93. rep->CreateFilePrefetchBufferIfNotExists(
  94. readahead_params, &prefetch_buffer_, readaheadsize_cb,
  95. /*usage=*/FilePrefetchBufferUsage::kUserScanPrefetch);
  96. return;
  97. }
  98. // If FS supports prefetching (readahead_limit_ will be non zero in that case)
  99. // and current block exists in prefetch buffer then return.
  100. if (offset + len <= readahead_limit_) {
  101. UpdateReadPattern(offset, len);
  102. return;
  103. }
  104. if (!IsBlockSequential(offset)) {
  105. UpdateReadPattern(offset, len);
  106. ResetValues(rep->table_options.initial_auto_readahead_size);
  107. return;
  108. }
  109. UpdateReadPattern(offset, len);
  110. // Implicit auto readahead, which will be enabled if the number of reads
  111. // reached `table_options.num_file_reads_for_auto_readahead` (default: 2) and
  112. // scans are sequential.
  113. num_file_reads_++;
  114. if (num_file_reads_ <= rep->table_options.num_file_reads_for_auto_readahead) {
  115. return;
  116. }
  117. readahead_params.num_file_reads = num_file_reads_;
  118. if (rep->file->use_direct_io()) {
  119. rep->CreateFilePrefetchBufferIfNotExists(
  120. readahead_params, &prefetch_buffer_, readaheadsize_cb,
  121. /*usage=*/FilePrefetchBufferUsage::kUserScanPrefetch);
  122. return;
  123. }
  124. if (readahead_size_ > max_auto_readahead_size) {
  125. readahead_size_ = max_auto_readahead_size;
  126. }
  127. // If prefetch is not supported, fall back to use internal prefetch buffer.
  128. IOOptions opts;
  129. Status s = rep->file->PrepareIOOptions(read_options, opts);
  130. if (!s.ok()) {
  131. return;
  132. }
  133. if (rep->fs_prefetch_support) {
  134. s = rep->file->Prefetch(
  135. opts, handle.offset(),
  136. BlockBasedTable::BlockSizeWithTrailer(handle) + readahead_size_);
  137. if (s.ok()) {
  138. readahead_limit_ = offset + len + readahead_size_;
  139. // Keep exponentially increasing readahead size until
  140. // max_auto_readahead_size.
  141. readahead_size_ = std::min(max_auto_readahead_size, readahead_size_ * 2);
  142. return;
  143. }
  144. }
  145. // If FS prefetch is not supported or returned NotSupported, fall back to use
  146. // internal prefetch buffer.
  147. rep->CreateFilePrefetchBufferIfNotExists(
  148. readahead_params, &prefetch_buffer_, readaheadsize_cb,
  149. /*usage=*/FilePrefetchBufferUsage::kUserScanPrefetch);
  150. }
  151. } // namespace ROCKSDB_NAMESPACE