summaryrefslogtreecommitdiffstats
path: root/src/common/Readahead.h
blob: 716e58cd3a271a837960704a364b431d29af28db (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_READAHEAD_H
#define CEPH_READAHEAD_H

#include <list>
#include <vector>

#include "include/Context.h"
#include "common/ceph_mutex.h"

/**
   This class provides common state and logic for code that needs to perform readahead
   on linear things such as RBD images or files.
   Unless otherwise specified, all methods are thread-safe.

   Minimum and maximum readahead sizes may be violated by up to 50\% if alignment is enabled.
   Minimum readahead size may be violated if the end of the readahead target is reached.
 */
class Readahead {
public:
  typedef std::pair<uint64_t, uint64_t> extent_t;

  // equal to UINT64_MAX
  static const uint64_t NO_LIMIT = 18446744073709551615ULL;

  Readahead();

  ~Readahead();

  /**
     Update state with new reads and return readahead to be performed.
     If the length of the returned extent is 0, no readahead should be performed.
     The readahead extent is guaranteed not to pass \c limit.

     Note that passing in NO_LIMIT as the limit and truncating the returned extent
     is not the same as passing in the correct limit, because the internal state
     will differ in the two cases.

     @param extents read operations since last call to update
     @param limit size of the thing readahead is being applied to
   */
  extent_t update(const std::vector<extent_t>& extents, uint64_t limit);

  /**
     Update state with a new read and return readahead to be performed.
     If the length of the returned extent is 0, no readahead should be performed.
     The readahead extent is guaranteed not to pass \c limit.

     Note that passing in NO_LIMIT as the limit and truncating the returned extent
     is not the same as passing in the correct limit, because the internal state
     will differ in the two cases.

     @param offset offset of the read operation
     @param length length of the read operation
     @param limit size of the thing readahead is being applied to
   */
  extent_t update(uint64_t offset, uint64_t length, uint64_t limit);

  /**
     Increment the pending counter.
   */
  void inc_pending(int count = 1);

  /**
     Decrement the pending counter.
     The counter must not be decremented below 0.
   */
  void dec_pending(int count = 1);

  /**
     Waits until the pending count reaches 0.
   */
  void wait_for_pending();
  void wait_for_pending(Context *ctx);

  /**
     Sets the number of sequential requests necessary to trigger readahead.
   */
  void set_trigger_requests(int trigger_requests);

  /**
     Gets the minimum size of a readahead request, in bytes.
   */
  uint64_t get_min_readahead_size(void);

  /**
     Gets the maximum size of a readahead request, in bytes.
   */
  uint64_t get_max_readahead_size(void);

  /**
     Sets the minimum size of a readahead request, in bytes.
   */
  void set_min_readahead_size(uint64_t min_readahead_size);

  /**
     Sets the maximum size of a readahead request, in bytes.
   */
  void set_max_readahead_size(uint64_t max_readahead_size);

  /**
     Sets the alignment units.
     If the end point of a readahead request can be aligned to an alignment unit
     by increasing or decreasing the size of the request by 50\% or less, it will.
     Alignments are tested in order, so larger numbers should almost always come first.
   */
  void set_alignments(const std::vector<uint64_t> &alignments);

private:
  /**
     Records that a read request has been received.
     m_lock must be held while calling.
   */
  void _observe_read(uint64_t offset, uint64_t length);

  /**
     Computes the next readahead request.
     m_lock must be held while calling.
  */
  extent_t _compute_readahead(uint64_t limit);

  /// Number of sequential requests necessary to trigger readahead
  int m_trigger_requests;

  /// Minimum size of a readahead request, in bytes
  uint64_t m_readahead_min_bytes;

  /// Maximum size of a readahead request, in bytes
  uint64_t m_readahead_max_bytes;

  /// Alignment units, in bytes
  std::vector<uint64_t> m_alignments;

  /// Held while reading/modifying any state except m_pending
  ceph::mutex m_lock = ceph::make_mutex("Readahead::m_lock");

  /// Number of consecutive read requests in the current sequential stream
  int m_nr_consec_read;

  /// Number of bytes read in the current sequenial stream
  uint64_t m_consec_read_bytes;

  /// Position of the read stream
  uint64_t m_last_pos;

  /// Position of the readahead stream
  uint64_t m_readahead_pos;

  /// When readahead is already triggered and the read stream crosses this point, readahead is continued
  uint64_t m_readahead_trigger_pos;

  /// Size of the next readahead request (barring changes due to alignment, etc.)
  uint64_t m_readahead_size;

  /// Number of pending readahead requests, as determined by inc_pending() and dec_pending()
  int m_pending;

  /// Lock for m_pending
  ceph::mutex m_pending_lock = ceph::make_mutex("Readahead::m_pending_lock");

  /// Waiters for pending readahead
  std::list<Context *> m_pending_waiting;
};

#endif