Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: media/blink/multibuffer.h

Issue 1165903002: Multi reader/writer cache/buffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: refactor Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_BLINK_MULTIBUFFER_H_
6 #define MEDIA_BLINK_MULTIBUFFER_H_
7
8 #include <stdint.h>
9
10 #include <limits>
11 #include <map>
12 #include <set>
13 #include <vector>
14
15 #include "base/callback.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/macros.h"
18 #include "base/memory/ref_counted.h"
19 #include "media/base/data_buffer.h"
20 #include "media/blink/lru.h"
21 #include "media/blink/rangemap.h"
22
23 namespace media {
24
25 typedef int32_t MultiBufferBlockId;
26 class MultiBuffer;
27 typedef std::pair<MultiBuffer*, MultiBufferBlockId> MultiBufferGlobalBlockId;
28
29 } // namespace media
30
31 namespace BASE_HASH_NAMESPACE {
32
33 template<>
34 struct hash<media::MultiBufferGlobalBlockId> {
35 std::size_t operator()(const media::MultiBufferGlobalBlockId& key) const {
36 return base::HashPair(reinterpret_cast<uint64>(key.first), key.second);
37 }
38 };
39
40 } // namespace BASE_HASH_NAMESPACE
41
42 namespace media {
43
44 // Freeing a lot of blocks can be expensive, to keep thing
45 // flowing smoothly we only free a maximum of |kMaxFreesPerAdd|
46 // blocks when a new block is added to the cache.
47 const int kMaxFreesPerAdd = 10;
48
49 // There is a simple logic for creating, destroying and deferring
50 // data providers. Every data provider has a look-ahead region and
51 // a look-behind region. If there are readers in the look-ahead
52 // region, we keep reading. If not, but there are readers in the
53 // look-behind region, we defer. If there are no readers in either
54 // region, we destroy the data provider.
55
56 // When new readers are added, new data providers are created if
57 // the new reader doesn't fall into the look-ahead region of
58 // an existing data provider.
59
60 // This is the size of the look-ahead region.
61 const int kMaxWaitForWriterOffset = 5;
62
63 // This is the size of the look-behind region.
64 const int kMaxWaitForReaderOffset = 50;
65
66 class MultiBuffer;
67
68 // MultiBuffers are multi-reader multi-writer cache/buffers with
69 // prefetching and pinning. Data is stored internally in ref-counted
70 // blocks of identical size. |block_size_shift| is log2 of the block
71 // size.
72 //
73 // Users should inherit this class and implement CreateWriter().
74 // TODO(hubbe): Make the multibuffer respond to memory pressure.
75 class MEDIA_EXPORT MultiBuffer {
76 public:
77 // Interface for clients wishing to read data out of this cache.
78 // Note: It might look tempting to replace this with a callback,
79 // but we keep and compare pointers to Readers internally.
80 class Reader {
81 public:
82 Reader() {}
83 virtual ~Reader() {}
84 // Notifies the reader that the range of available blocks has changed.
85 // The reader must call MultiBuffer::Observe() to activate this callback.
86 virtual void NotifyAvailableRange(
87 const Range<MultiBufferBlockId>& range) = 0;
88 private:
89 DISALLOW_COPY_AND_ASSIGN(Reader);
90 };
91
92 // DataProvider is the interface that MultiBuffer
93 // uses to get data into the cache.
94 class DataProvider {
95 public:
96 virtual ~DataProvider() {}
97
98 // Returns the block number that is to be returned
99 // by the next Read() call.
100 virtual MultiBufferBlockId Tell() const = 0;
101
102 // Returns true if one (or more) blocks are
103 // availble to read.
104 virtual bool Available() const = 0;
105
106 // Returns the next block. Only valid if Available()
107 // returns true. Last block might be of a smaller size
108 // and after the last block we will get an end-of-stream
109 // DataBuffer.
110 virtual scoped_refptr<DataBuffer> Read() = 0;
111
112 // |cb| is called every time Available() becomes true.
113 virtual void SetAvailableCallback(const base::Closure& cb) = 0;
114
115 // Ask the data provider to stop giving us data.
116 // It's ok if the effect is not immediate.
117 virtual void SetDeferred(bool deferred) = 0;
118 };
119
120 // Multibuffers use a global shared LRU to free memory.
121 // This effectively means that recently used multibuffers can
122 // borrow memory from less recently used ones.
123 class GlobalLRU: public base::RefCounted<GlobalLRU> {
DaleCurtis 2015/10/28 23:06:13 space before :
hubbe 2015/10/29 17:52:44 Done.
124 public:
125 typedef MultiBufferGlobalBlockId GlobalBlockId;
126 GlobalLRU();
127
128 // Free elements from cache if needed and possible.
129 // Don't free more than |max_to_free| blocks.
130 // Virtual for testing purposes.
131 void Prune(size_t max_to_free);
132
133 void IncrementDataSize(int64_t blocks);
134 void IncrementMaxSize(int64_t blocks);
135
136 // LRU operations.
137 void Use(MultiBuffer* multibuffer, MultiBufferBlockId id);
138 void Remove(MultiBuffer* multibuffer, MultiBufferBlockId id);
139 void Insert(MultiBuffer* multibuffer, MultiBufferBlockId id);
140 bool Contains(MultiBuffer* multibuffer, MultiBufferBlockId id);
141
142 private:
143 friend class base::RefCounted<GlobalLRU>;
144 ~GlobalLRU();
145
146 // Max number of blocks.
147 int64_t max_size_;
148
149 // Sum of all multibuffer::data_.size().
150 int64_t data_size_;
151
152 // The LRU should contain all blocks which are not pinned from
153 // all multibuffers.
154 LRU<GlobalBlockId> lru_;
155 };
156
157 MultiBuffer(int32_t block_size_shift,
158 scoped_refptr<GlobalLRU> global_lru);
159 virtual ~MultiBuffer();
160
161 // Identifies a block in the cache.
162 // Block numbers can be calculated from byte positions as:
163 // block_num = byte_pos >> block_size_shift
164 typedef MultiBufferBlockId BlockId;
165 typedef base::hash_map<BlockId, scoped_refptr<DataBuffer> > DataMap;
166
167 // Registers a reader at the given position.
168 // If the cache does not already contain |pos|, it will activate
169 // or create data providers to make sure that the block becomes
170 // available soon. If |pos| is already in the cache, no action is
171 // taken, it simply lets the cache know that this reader is likely
172 // to read pos+1, pos+2.. soon.
173 //
174 // Registered readers will be notified when the available range
175 // at their position changes. The available range at |pos| is a range
176 // from A to B where: A <= |pos|, B >= |pos| and all blocks in [A..B)
177 // are present in the cache. When this changes, we will call
178 // NotifyAvailableRange() on the reader.
179 void AddReader(const BlockId& pos, Reader* reader);
180
181 // Unregister a reader at block |pos|.
182 // Often followed by a call to AddReader(pos + 1, ...);
183 // Idempotent.
184 void RemoveReader(const BlockId& pos, Reader* reader);
185
186 // Immediately remove writers at or before |pos| if nobody needs them.
187 // Note that we can't really do this in StopWaitFor(), because it's very
188 // likely that StopWaitFor() is immediately followed by a call to WaitFor().
189 // It is also a bad idea to wait for the writers to clean themselves up when
190 // they try to provide unwanted data to the cache. Besides the obvoius
191 // inefficiency, it will also cause the http_cache to bypass the disk/memory
192 // cache if we have multiple simultaneous requests going against the same
193 // url.
194 void CleanupWriters(const BlockId& pos);
195
196 // Returns true if block |pos| is available in the cache.
197 bool Contains(const BlockId& pos) const;
198
199 // Returns the next unavailable block at or after |pos|.
200 BlockId FindNextUnavailable(const BlockId& pos) const;
201
202 // Change the pin count for a range of data blocks.
203 // Note that blocks do not have to be present in the
204 // cache to be pinned.
205 // Examples:
206 // Pin block 3, 4 & 5: PinRange(3, 6, 1);
207 // Unpin block 4 & 5: PinRange(4, 6, -1);
208 void PinRange(const BlockId& from, const BlockId& to, int32_t how_much);
209
210 // Calls PinRange for each range in |ranges|, convenience
211 // function for applying multiple changes to the pinned ranges.
212 void PinRanges(const RangeMap<BlockId, int32_t>& ranges);
213
214 // Increment max cache size by |size| (counted in blocks).
215 void IncrementMaxSize(int32_t size);
216
217 // Caller takes ownership of 'provider', cache will
218 // not call it anymore.
219 scoped_ptr<DataProvider> RemoveProvider(DataProvider* provider);
220
221 // Add a writer to this cache. Cache takes ownership and
222 // may choose to destroy it.
223 void AddProvider(scoped_ptr<DataProvider> provider);
224
225 // Transfer all data from |other| to this.
226 void MergeFrom(MultiBuffer* other);
227
228 // Accessors.
229 const DataMap& map() const { return data_; }
230 int32_t block_size_shift() const { return block_size_shift_; }
231
232 protected:
233 // Create a new writer at |pos| and return it.
234 // Users needs to implemement this method.
235 virtual DataProvider* CreateWriter(const BlockId& pos) = 0;
236
237 virtual bool RangeSupported() const = 0;
238
239 private:
240 // For testing.
241 friend class TestMultiBuffer;
242
243 enum ProviderState {
244 ProviderStateDead,
245 ProviderStateDefer,
246 ProviderStateLoad
247 };
248
249 // Can be overriden for testing.
250 virtual void Prune(size_t max_to_free);
251
252 // Remove the given blocks from the multibuffer, called from
253 // GlobalLRU::Prune().
254 void ReleaseBlocks(const std::vector<MultiBufferBlockId> blocks);
255
256 // Figure out what state a writer at |pos| should be in.
257 ProviderState SuggestProviderState(const BlockId& pos) const;
258
259 // Returns true if a writer at |pos| is colliding with
260 // output of another writer.
261 bool ProviderCollision(const BlockId& pos) const;
262
263 // Call NotifyAvailableRange(new_range) on all readers waiting
264 // for a block in |observer_range|
265 void NotifyAvailableRange(
266 const Range<MultiBufferBlockId>& observer_range,
267 const Range<MultiBufferBlockId>& new_range);
268
269 // Callback which notifies us that a data provider has
270 // some data for us. Also called when it might be apprperiate
271 // for a provider in a deferred state to wake up.
272 void DataProviderEvent(DataProvider *provider);
273
274 // Max number of blocks.
275 int64_t max_size_;
276
277 // log2 of block size.
278 int32_t block_size_shift_;
279
280 // Stores the actual data.
281 DataMap data_;
282
283 // Keeps track of readers waiting for data.
284 std::map<MultiBufferBlockId, std::set<Reader*> > readers_;
285
286 // Keeps track of writers by their position.
287 // The writers are owned by this class.
288 std::map<BlockId, DataProvider*> writer_index_;
289
290 // Gloabally shared LRU, decides which block to free next.
291 scoped_refptr<GlobalLRU> lru_;
292
293 // Keeps track of what blocks are pinned. If block p is pinned,
294 // then pinned_[p] > 0. Pinned blocks cannot be freed and should not
295 // be present in |lru_|.
296 RangeMap<BlockId, int32_t> pinned_;
297
298 // present_[block] should be 1 for all blocks that are present
299 // and 0 for all blocks that are not. Used to quickly figure out
300 // ranges of available/unavailable blocks without iterating.
301 RangeMap<BlockId, int32_t> present_;
302 };
303
304 } // namespace media
305
306
DaleCurtis 2015/10/28 23:06:13 Remove extra line.
hubbe 2015/10/29 17:52:44 Done.
307 #endif // MEDIA_BLINK_MULTIBUFFER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698