]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/Readahead.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
9 : m_trigger_requests(10),
10 m_readahead_min_bytes(0),
11 m_readahead_max_bytes(NO_LIMIT
),
13 m_lock("Readahead::m_lock"),
15 m_consec_read_bytes(0),
18 m_readahead_trigger_pos(0),
21 m_pending_lock("Readahead::m_pending_lock") {
24 Readahead::~Readahead() {
27 Readahead::extent_t
Readahead::update(const vector
<extent_t
>& extents
, uint64_t limit
) {
29 for (vector
<extent_t
>::const_iterator p
= extents
.begin(); p
!= extents
.end(); ++p
) {
30 _observe_read(p
->first
, p
->second
);
32 if (m_readahead_pos
>= limit
|| m_last_pos
>= limit
) {
34 return extent_t(0, 0);
36 pair
<uint64_t, uint64_t> extent
= _compute_readahead(limit
);
41 Readahead::extent_t
Readahead::update(uint64_t offset
, uint64_t length
, uint64_t limit
) {
43 _observe_read(offset
, length
);
44 if (m_readahead_pos
>= limit
|| m_last_pos
>= limit
) {
46 return extent_t(0, 0);
48 extent_t extent
= _compute_readahead(limit
);
53 void Readahead::_observe_read(uint64_t offset
, uint64_t length
) {
54 if (offset
== m_last_pos
) {
56 m_consec_read_bytes
+= length
;
59 m_consec_read_bytes
= 0;
60 m_readahead_trigger_pos
= 0;
64 m_last_pos
= offset
+ length
;
67 Readahead::extent_t
Readahead::_compute_readahead(uint64_t limit
) {
68 uint64_t readahead_offset
= 0;
69 uint64_t readahead_length
= 0;
70 if (m_nr_consec_read
>= m_trigger_requests
) {
71 // currently reading sequentially
72 if (m_last_pos
>= m_readahead_trigger_pos
) {
74 if (m_readahead_size
== 0) {
75 // initial readahead trigger
76 m_readahead_size
= m_consec_read_bytes
;
77 m_readahead_pos
= m_last_pos
;
79 // continuing readahead trigger
80 m_readahead_size
*= 2;
81 if (m_last_pos
> m_readahead_pos
) {
82 m_readahead_pos
= m_last_pos
;
85 m_readahead_size
= MAX(m_readahead_size
, m_readahead_min_bytes
);
86 m_readahead_size
= MIN(m_readahead_size
, m_readahead_max_bytes
);
87 readahead_offset
= m_readahead_pos
;
88 readahead_length
= m_readahead_size
;
90 // Snap to the first alignment possible
91 uint64_t readahead_end
= readahead_offset
+ readahead_length
;
92 for (vector
<uint64_t>::iterator p
= m_alignments
.begin(); p
!= m_alignments
.end(); ++p
) {
93 // Align the readahead, if possible.
94 uint64_t alignment
= *p
;
95 uint64_t align_prev
= readahead_end
/ alignment
* alignment
;
96 uint64_t align_next
= align_prev
+ alignment
;
97 uint64_t dist_prev
= readahead_end
- align_prev
;
98 uint64_t dist_next
= align_next
- readahead_end
;
99 if (dist_prev
< readahead_length
/ 2 && dist_prev
< dist_next
) {
100 // we can snap to the previous alignment point by a less than 50% reduction in size
101 assert(align_prev
> readahead_offset
);
102 readahead_length
= align_prev
- readahead_offset
;
104 } else if(dist_next
< readahead_length
/ 2) {
105 // we can snap to the next alignment point by a less than 50% increase in size
106 assert(align_next
> readahead_offset
);
107 readahead_length
= align_next
- readahead_offset
;
110 // Note that m_readahead_size should remain unadjusted.
113 if (m_readahead_pos
+ readahead_length
> limit
) {
114 readahead_length
= limit
- m_readahead_pos
;
117 m_readahead_trigger_pos
= m_readahead_pos
+ readahead_length
/ 2;
118 m_readahead_pos
+= readahead_length
;
121 return extent_t(readahead_offset
, readahead_length
);
124 void Readahead::inc_pending(int count
) {
126 m_pending_lock
.Lock();
128 m_pending_lock
.Unlock();
131 void Readahead::dec_pending(int count
) {
133 m_pending_lock
.Lock();
134 assert(m_pending
>= count
);
136 if (m_pending
== 0) {
137 std::list
<Context
*> pending_waiting(std::move(m_pending_waiting
));
138 m_pending_lock
.Unlock();
140 for (auto ctx
: pending_waiting
) {
144 m_pending_lock
.Unlock();
148 void Readahead::wait_for_pending() {
150 wait_for_pending(&ctx
);
154 void Readahead::wait_for_pending(Context
*ctx
) {
155 m_pending_lock
.Lock();
157 m_pending_lock
.Unlock();
158 m_pending_waiting
.push_back(ctx
);
161 m_pending_lock
.Unlock();
165 void Readahead::set_trigger_requests(int trigger_requests
) {
167 m_trigger_requests
= trigger_requests
;
171 uint64_t Readahead::get_min_readahead_size(void) {
172 Mutex::Locker
lock(m_lock
);
173 return m_readahead_min_bytes
;
176 uint64_t Readahead::get_max_readahead_size(void) {
177 Mutex::Locker
lock(m_lock
);
178 return m_readahead_max_bytes
;
181 void Readahead::set_min_readahead_size(uint64_t min_readahead_size
) {
183 m_readahead_min_bytes
= min_readahead_size
;
187 void Readahead::set_max_readahead_size(uint64_t max_readahead_size
) {
189 m_readahead_max_bytes
= max_readahead_size
;
193 void Readahead::set_alignments(const vector
<uint64_t> &alignments
) {
195 m_alignments
= alignments
;