Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1 | // Copyright (c) 2013 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 NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
| 6 | #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
| 7 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 8 | #include <queue> |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include "base/files/file_path.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 15 | #include "base/threading/thread_checker.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 16 | #include "net/base/net_log.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 17 | #include "net/disk_cache/disk_cache.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 18 | #include "net/disk_cache/simple/simple_entry_format.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 19 | #include "net/disk_cache/simple/simple_entry_operation.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 20 | |
| 21 | namespace base { |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 22 | class TaskRunner; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 23 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 24 | |
| 25 | namespace net { |
| 26 | class IOBuffer; |
| 27 | } |
| 28 | |
| 29 | namespace disk_cache { |
| 30 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 31 | class SimpleBackendImpl; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 32 | class SimpleSynchronousEntry; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 33 | struct SimpleEntryStat; |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 34 | struct SimpleEntryCreationResults; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 35 | |
| 36 | // SimpleEntryImpl is the IO thread interface to an entry in the very simple |
| 37 | // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO |
| 38 | // on the worker thread. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 39 | class SimpleEntryImpl : public Entry, public base::RefCounted<SimpleEntryImpl>, |
| 40 | public base::SupportsWeakPtr<SimpleEntryImpl> { |
| 41 | friend class base::RefCounted<SimpleEntryImpl>; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 42 | public: |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 43 | enum OperationsMode { |
| 44 | NON_OPTIMISTIC_OPERATIONS, |
| 45 | OPTIMISTIC_OPERATIONS, |
| 46 | }; |
| 47 | |
| 48 | SimpleEntryImpl(const base::FilePath& path, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 49 | uint64 entry_hash, |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 50 | OperationsMode operations_mode, |
| 51 | SimpleBackendImpl* backend, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 52 | net::NetLog* net_log); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 53 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 54 | // Adds another reader/writer to this entry, if possible, returning |this| to |
| 55 | // |entry|. |
| 56 | int OpenEntry(Entry** entry, const CompletionCallback& callback); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 57 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 58 | // Creates this entry, if possible. Returns |this| to |entry|. |
| 59 | int CreateEntry(Entry** entry, const CompletionCallback& callback); |
| 60 | |
| 61 | // Identical to Backend::Doom() except that it accepts a CompletionCallback. |
| 62 | int DoomEntry(const CompletionCallback& callback); |
| 63 | |
| 64 | const std::string& key() const { return key_; } |
| 65 | uint64 entry_hash() const { return entry_hash_; } |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 66 | void SetKey(const std::string& key); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 67 | |
| 68 | // From Entry: |
| 69 | virtual void Doom() OVERRIDE; |
| 70 | virtual void Close() OVERRIDE; |
| 71 | virtual std::string GetKey() const OVERRIDE; |
| 72 | virtual base::Time GetLastUsed() const OVERRIDE; |
| 73 | virtual base::Time GetLastModified() const OVERRIDE; |
| 74 | virtual int32 GetDataSize(int index) const OVERRIDE; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 75 | virtual int ReadData(int stream_index, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 76 | int offset, |
| 77 | net::IOBuffer* buf, |
| 78 | int buf_len, |
| 79 | const CompletionCallback& callback) OVERRIDE; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 80 | virtual int WriteData(int stream_index, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 81 | int offset, |
| 82 | net::IOBuffer* buf, |
| 83 | int buf_len, |
| 84 | const CompletionCallback& callback, |
| 85 | bool truncate) OVERRIDE; |
| 86 | virtual int ReadSparseData(int64 offset, |
| 87 | net::IOBuffer* buf, |
| 88 | int buf_len, |
| 89 | const CompletionCallback& callback) OVERRIDE; |
| 90 | virtual int WriteSparseData(int64 offset, |
| 91 | net::IOBuffer* buf, |
| 92 | int buf_len, |
| 93 | const CompletionCallback& callback) OVERRIDE; |
| 94 | virtual int GetAvailableRange(int64 offset, |
| 95 | int len, |
| 96 | int64* start, |
| 97 | const CompletionCallback& callback) OVERRIDE; |
| 98 | virtual bool CouldBeSparse() const OVERRIDE; |
| 99 | virtual void CancelSparseIO() OVERRIDE; |
| 100 | virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; |
| 101 | |
| 102 | private: |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 103 | class ScopedOperationRunner; |
| 104 | friend class ScopedOperationRunner; |
| 105 | |
| 106 | enum State { |
| 107 | // The state immediately after construction, but before |synchronous_entry_| |
| 108 | // has been assigned. This is the state at construction, and is the only |
| 109 | // legal state to destruct an entry in. |
| 110 | STATE_UNINITIALIZED, |
| 111 | |
| 112 | // This entry is available for regular IO. |
| 113 | STATE_READY, |
| 114 | |
| 115 | // IO is currently in flight, operations must wait for completion before |
| 116 | // launching. |
| 117 | STATE_IO_PENDING, |
| 118 | |
| 119 | // A failure occurred in the current or previous operation. All operations |
| 120 | // after that must fail, until we receive a Close(). |
| 121 | STATE_FAILURE, |
| 122 | }; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 123 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 124 | // Used in histograms, please only add entries at the end. |
| 125 | enum CheckCrcResult { |
| 126 | CRC_CHECK_NEVER_READ_TO_END = 0, |
| 127 | CRC_CHECK_NOT_DONE = 1, |
| 128 | CRC_CHECK_DONE = 2, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 129 | CRC_CHECK_NEVER_READ_AT_ALL = 3, |
| 130 | CRC_CHECK_MAX = 4, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 133 | virtual ~SimpleEntryImpl(); |
| 134 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 135 | // Sets entry to STATE_UNINITIALIZED. |
| 136 | void MakeUninitialized(); |
| 137 | |
| 138 | // Return this entry to a user of the API in |out_entry|. Increments the user |
| 139 | // count. |
| 140 | void ReturnEntryToCaller(Entry** out_entry); |
| 141 | |
| 142 | // Ensures that |this| is no longer referenced by our |backend_|, this |
| 143 | // guarantees that this entry cannot have OpenEntry/CreateEntry called again. |
| 144 | void RemoveSelfFromBackend(); |
| 145 | |
| 146 | // An error occured, and the SimpleSynchronousEntry should have Doomed |
| 147 | // us at this point. We need to remove |this| from the Backend and the |
| 148 | // index. |
| 149 | void MarkAsDoomed(); |
| 150 | |
| 151 | // Runs the next operation in the queue, if any and if there is no other |
| 152 | // operation running at the moment. |
| 153 | // WARNING: May delete |this|, as an operation in the queue can contain |
| 154 | // the last reference. |
| 155 | void RunNextOperationIfNeeded(); |
| 156 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 157 | void OpenEntryInternal(bool have_index, |
| 158 | const CompletionCallback& callback, |
| 159 | Entry** out_entry); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 160 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 161 | void CreateEntryInternal(bool have_index, |
| 162 | const CompletionCallback& callback, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 163 | Entry** out_entry); |
| 164 | |
| 165 | void CloseInternal(); |
| 166 | |
| 167 | void ReadDataInternal(int index, |
| 168 | int offset, |
| 169 | net::IOBuffer* buf, |
| 170 | int buf_len, |
| 171 | const CompletionCallback& callback); |
| 172 | |
| 173 | void WriteDataInternal(int index, |
| 174 | int offset, |
| 175 | net::IOBuffer* buf, |
| 176 | int buf_len, |
| 177 | const CompletionCallback& callback, |
| 178 | bool truncate); |
| 179 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 180 | // Called after a SimpleSynchronousEntry has completed CreateEntry() or |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 181 | // OpenEntry(). If |in_sync_entry| is non-NULL, creation is successful and we |
| 182 | // can return |this| SimpleEntryImpl to |*out_entry|. Runs |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 183 | // |completion_callback|. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 184 | void CreationOperationComplete( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 185 | const CompletionCallback& completion_callback, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 186 | const base::TimeTicks& start_time, |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 187 | scoped_ptr<SimpleEntryCreationResults> in_results, |
| 188 | Entry** out_entry, |
| 189 | net::NetLog::EventType end_event_type); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 190 | |
| 191 | // Called after we've closed and written the EOF record to our entry. Until |
| 192 | // this point it hasn't been safe to OpenEntry() the same entry, but from this |
| 193 | // point it is. |
| 194 | void CloseOperationComplete(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 195 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 196 | // Internal utility method used by other completion methods. Calls |
| 197 | // |completion_callback| after updating state and dooming on errors. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 198 | void EntryOperationComplete(int stream_index, |
| 199 | const CompletionCallback& completion_callback, |
| 200 | const SimpleEntryStat& entry_stat, |
| 201 | scoped_ptr<int> result); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 202 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 203 | // Called after an asynchronous read. Updates |crc32s_| if possible. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 204 | void ReadOperationComplete(int stream_index, |
| 205 | int offset, |
| 206 | const CompletionCallback& completion_callback, |
| 207 | scoped_ptr<uint32> read_crc32, |
| 208 | scoped_ptr<base::Time> last_used, |
| 209 | scoped_ptr<int> result); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 210 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 211 | // Called after an asynchronous write completes. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 212 | void WriteOperationComplete(int stream_index, |
| 213 | const CompletionCallback& completion_callback, |
| 214 | scoped_ptr<SimpleEntryStat> entry_stat, |
| 215 | scoped_ptr<int> result); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 216 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 217 | // Called after validating the checksums on an entry. Passes through the |
| 218 | // original result if successful, propogates the error if the checksum does |
| 219 | // not validate. |
| 220 | void ChecksumOperationComplete( |
| 221 | int stream_index, |
| 222 | int orig_result, |
| 223 | const CompletionCallback& completion_callback, |
| 224 | scoped_ptr<int> result); |
| 225 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 226 | // Called after completion of asynchronous IO and receiving file metadata for |
| 227 | // the entry in |entry_stat|. Updates the metadata in the entry and in the |
| 228 | // index to make them available on next IO operations. |
| 229 | void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); |
| 230 | |
| 231 | int64 GetDiskUsage() const; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 232 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 233 | // Used to report histograms. |
| 234 | void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; |
| 235 | void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; |
| 236 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 237 | // All nonstatic SimpleEntryImpl methods should always be called on the IO |
| 238 | // thread, in all cases. |io_thread_checker_| documents and enforces this. |
| 239 | base::ThreadChecker io_thread_checker_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 240 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 241 | base::WeakPtr<SimpleBackendImpl> backend_; |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 242 | const scoped_refptr<base::TaskRunner> worker_pool_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 243 | const base::FilePath path_; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 244 | const uint64 entry_hash_; |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 245 | const bool use_optimistic_operations_; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 246 | std::string key_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 247 | |
| 248 | // |last_used_|, |last_modified_| and |data_size_| are copied from the |
| 249 | // synchronous entry at the completion of each item of asynchronous IO. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 250 | // TODO(clamy): Unify last_used_ with data in the index. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 251 | base::Time last_used_; |
| 252 | base::Time last_modified_; |
| 253 | int32 data_size_[kSimpleEntryFileCount]; |
| 254 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 255 | // Number of times this object has been returned from Backend::OpenEntry() and |
| 256 | // Backend::CreateEntry() without subsequent Entry::Close() calls. Used to |
| 257 | // notify the backend when this entry not used by any callers. |
| 258 | int open_count_; |
| 259 | |
| 260 | State state_; |
| 261 | |
| 262 | // When possible, we compute a crc32, for the data in each entry as we read or |
| 263 | // write. For each stream, |crc32s_[index]| is the crc32 of that stream from |
| 264 | // [0 .. |crc32s_end_offset_|). If |crc32s_end_offset_[index] == 0| then the |
| 265 | // value of |crc32s_[index]| is undefined. |
| 266 | int32 crc32s_end_offset_[kSimpleEntryFileCount]; |
| 267 | uint32 crc32s_[kSimpleEntryFileCount]; |
| 268 | |
| 269 | // If |have_written_[index]| is true, we have written to the stream |index|. |
| 270 | bool have_written_[kSimpleEntryFileCount]; |
| 271 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 272 | // Reflects how much CRC checking has been done with the entry. This state is |
| 273 | // reported on closing each entry stream. |
| 274 | CheckCrcResult crc_check_state_[kSimpleEntryFileCount]; |
| 275 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 276 | // The |synchronous_entry_| is the worker thread object that performs IO on |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 277 | // entries. It's owned by this SimpleEntryImpl whenever |operation_running_| |
| 278 | // is false (i.e. when an operation is not pending on the worker pool). |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 279 | SimpleSynchronousEntry* synchronous_entry_; |
| 280 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 281 | std::queue<SimpleEntryOperation> pending_operations_; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 282 | |
| 283 | net::BoundNetLog net_log_; |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 284 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 285 | scoped_ptr<SimpleEntryOperation> executing_operation_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | } // namespace disk_cache |
| 289 | |
| 290 | #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |