file_prefetch_buffer.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <atomic>
  11. #include <sstream>
  12. #include <string>
  13. #include "file/random_access_file_reader.h"
  14. #include "port/port.h"
  15. #include "rocksdb/env.h"
  16. #include "util/aligned_buffer.h"
  17. namespace ROCKSDB_NAMESPACE {
  18. // FilePrefetchBuffer is a smart buffer to store and read data from a file.
  19. class FilePrefetchBuffer {
  20. public:
  21. // Constructor.
  22. //
  23. // All arguments are optional.
  24. // file_reader : the file reader to use. Can be a nullptr.
  25. // readahead_size : the initial readahead size.
  26. // max_readahead_size : the maximum readahead size.
  27. // If max_readahead_size > readahead_size, the readahead size will be
  28. // doubled on every IO until max_readahead_size is hit.
  29. // Typically this is set as a multiple of readahead_size.
  30. // max_readahead_size should be greater than equal to readahead_size.
  31. // enable : controls whether reading from the buffer is enabled.
  32. // If false, TryReadFromCache() always return false, and we only take stats
  33. // for the minimum offset if track_min_offset = true.
  34. // track_min_offset : Track the minimum offset ever read and collect stats on
  35. // it. Used for adaptable readahead of the file footer/metadata.
  36. //
  37. // Automatic readhead is enabled for a file if file_reader, readahead_size,
  38. // and max_readahead_size are passed in.
  39. // If file_reader is a nullptr, setting readadhead_size and max_readahead_size
  40. // does not make any sense. So it does nothing.
  41. // A user can construct a FilePrefetchBuffer without any arguments, but use
  42. // `Prefetch` to load data into the buffer.
  43. FilePrefetchBuffer(RandomAccessFileReader* file_reader = nullptr,
  44. size_t readadhead_size = 0, size_t max_readahead_size = 0,
  45. bool enable = true, bool track_min_offset = false)
  46. : buffer_offset_(0),
  47. file_reader_(file_reader),
  48. readahead_size_(readadhead_size),
  49. max_readahead_size_(max_readahead_size),
  50. min_offset_read_(port::kMaxSizet),
  51. enable_(enable),
  52. track_min_offset_(track_min_offset) {}
  53. // Load data into the buffer from a file.
  54. // reader : the file reader.
  55. // offset : the file offset to start reading from.
  56. // n : the number of bytes to read.
  57. // for_compaction : if prefetch is done for compaction read.
  58. Status Prefetch(RandomAccessFileReader* reader, uint64_t offset, size_t n,
  59. bool for_compaction = false);
  60. // Tries returning the data for a file raed from this buffer, if that data is
  61. // in the buffer.
  62. // It handles tracking the minimum read offset if track_min_offset = true.
  63. // It also does the exponential readahead when readadhead_size is set as part
  64. // of the constructor.
  65. //
  66. // offset : the file offset.
  67. // n : the number of bytes.
  68. // result : output buffer to put the data into.
  69. // for_compaction : if cache read is done for compaction read.
  70. bool TryReadFromCache(uint64_t offset, size_t n, Slice* result,
  71. bool for_compaction = false);
  72. // The minimum `offset` ever passed to TryReadFromCache(). This will nly be
  73. // tracked if track_min_offset = true.
  74. size_t min_offset_read() const { return min_offset_read_; }
  75. private:
  76. AlignedBuffer buffer_;
  77. uint64_t buffer_offset_;
  78. RandomAccessFileReader* file_reader_;
  79. size_t readahead_size_;
  80. size_t max_readahead_size_;
  81. // The minimum `offset` ever passed to TryReadFromCache().
  82. size_t min_offset_read_;
  83. // if false, TryReadFromCache() always return false, and we only take stats
  84. // for track_min_offset_ if track_min_offset_ = true
  85. bool enable_;
  86. // If true, track minimum `offset` ever passed to TryReadFromCache(), which
  87. // can be fetched from min_offset_read().
  88. bool track_min_offset_;
  89. };
  90. } // namespace ROCKSDB_NAMESPACE