OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 g// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
gavinp
2013/05/03 11:19:27
g// !!!
| |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/simple/simple_entry_impl.h" | 5 #include "net/disk_cache/simple/simple_entry_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 open_count_(0), | 72 open_count_(0), |
73 state_(STATE_UNINITIALIZED), | 73 state_(STATE_UNINITIALIZED), |
74 synchronous_entry_(NULL) { | 74 synchronous_entry_(NULL) { |
75 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); | 75 DCHECK_EQ(entry_hash, simple_util::GetEntryHashKey(key)); |
76 COMPILE_ASSERT(arraysize(data_size_) == arraysize(crc32s_end_offset_), | 76 COMPILE_ASSERT(arraysize(data_size_) == arraysize(crc32s_end_offset_), |
77 arrays_should_be_same_size); | 77 arrays_should_be_same_size); |
78 COMPILE_ASSERT(arraysize(data_size_) == arraysize(crc32s_), | 78 COMPILE_ASSERT(arraysize(data_size_) == arraysize(crc32s_), |
79 arrays_should_be_same_size2); | 79 arrays_should_be_same_size2); |
80 COMPILE_ASSERT(arraysize(data_size_) == arraysize(have_written_), | 80 COMPILE_ASSERT(arraysize(data_size_) == arraysize(have_written_), |
81 arrays_should_be_same_size3); | 81 arrays_should_be_same_size3); |
82 | |
83 MakeUninitialized(); | 82 MakeUninitialized(); |
84 } | 83 } |
85 | 84 |
86 int SimpleEntryImpl::OpenEntry(Entry** out_entry, | 85 int SimpleEntryImpl::OpenEntry(Entry** out_entry, |
87 const CompletionCallback& callback) { | 86 const CompletionCallback& callback) { |
88 DCHECK(backend_); | 87 DCHECK(backend_); |
88 // This enumeration is used in histograms, add entries only at end. | |
89 enum OpenEntryIndexEnum { | |
90 INDEX_NOEXIST = 0, | |
91 INDEX_MISS = 1, | |
92 INDEX_HIT = 2, | |
93 INDEX_MAX = 3, | |
94 }; | |
95 OpenEntryIndexEnum open_entry_index_enum = INDEX_NOEXIST; | |
96 if (backend_) { | |
97 if (backend_->index()->Has(key_)) | |
98 open_entry_index_enum = INDEX_HIT; | |
99 else | |
100 open_entry_index_enum = INDEX_MISS; | |
101 } | |
102 UMA_HISTOGRAM_ENUMERATION("SimpleCache.OpenEntryIndexState", | |
103 open_entry_index_enum, INDEX_MAX); | |
104 | |
105 // If entry is not known to the index, initiate fast failover to the network. | |
106 if (open_entry_index_enum == INDEX_MISS) | |
107 return net::ERR_FAILED; | |
89 | 108 |
90 pending_operations_.push(base::Bind(&SimpleEntryImpl::OpenEntryInternal, | 109 pending_operations_.push(base::Bind(&SimpleEntryImpl::OpenEntryInternal, |
91 this, out_entry, callback)); | 110 this, callback, out_entry)); |
92 RunNextOperationIfNeeded(); | 111 RunNextOperationIfNeeded(); |
93 return net::ERR_IO_PENDING; | 112 return net::ERR_IO_PENDING; |
94 } | 113 } |
95 | 114 |
96 int SimpleEntryImpl::CreateEntry(Entry** out_entry, | 115 int SimpleEntryImpl::CreateEntry(Entry** out_entry, |
97 const CompletionCallback& callback) { | 116 const CompletionCallback& callback) { |
98 DCHECK(backend_); | 117 DCHECK(backend_); |
99 pending_operations_.push(base::Bind(&SimpleEntryImpl::CreateEntryInternal, | 118 int ret_value = net::ERR_FAILED; |
100 this, out_entry, callback)); | 119 if (state_ == STATE_UNINITIALIZED && |
120 pending_operations_.size() == 0) { | |
121 ReturnEntryToCaller(out_entry); | |
122 // We can do optimistic Create. | |
123 pending_operations_.push(base::Bind(&SimpleEntryImpl::CreateEntryInternal, | |
124 this, | |
125 CompletionCallback(), | |
126 static_cast<Entry**>(NULL))); | |
127 ret_value = net::OK; | |
128 } else { | |
129 pending_operations_.push(base::Bind(&SimpleEntryImpl::CreateEntryInternal, | |
130 this, | |
131 callback, | |
132 out_entry)); | |
133 ret_value = net::ERR_IO_PENDING; | |
134 } | |
135 | |
136 // We insert the entry in the index before creating the entry files in the | |
137 // SimpleSynchronousEntry, because this way the worst scenario is when we | |
138 // have the entry in the index but we don't have the created files yet, this | |
139 // way we never leak files. CreationOperationComplete will remove the entry | |
140 // from the index if the creation fails. | |
141 if (backend_) | |
142 backend_->index()->Insert(key_); | |
143 | |
144 // Since we don't know the correct values for |last_used_| and | |
145 // |last_modified_| yet, we make this approximation. | |
146 last_used_ = last_modified_ = base::Time::Now(); | |
147 | |
101 RunNextOperationIfNeeded(); | 148 RunNextOperationIfNeeded(); |
102 return net::ERR_IO_PENDING; | 149 return ret_value; |
103 } | 150 } |
104 | 151 |
105 int SimpleEntryImpl::DoomEntry(const CompletionCallback& callback) { | 152 int SimpleEntryImpl::DoomEntry(const CompletionCallback& callback) { |
106 MarkAsDoomed(); | 153 MarkAsDoomed(); |
107 | |
108 scoped_ptr<int> result(new int()); | 154 scoped_ptr<int> result(new int()); |
109 Closure task = base::Bind(&SimpleSynchronousEntry::DoomEntry, path_, key_, | 155 Closure task = base::Bind(&SimpleSynchronousEntry::DoomEntry, path_, key_, |
110 entry_hash_, result.get()); | 156 entry_hash_, result.get()); |
111 Closure reply = base::Bind(&CallCompletionCallback, | 157 Closure reply = base::Bind(&CallCompletionCallback, |
112 callback, base::Passed(&result)); | 158 callback, base::Passed(&result)); |
113 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); | 159 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); |
114 return net::ERR_IO_PENDING; | 160 return net::ERR_IO_PENDING; |
115 } | 161 } |
116 | 162 |
117 | 163 |
(...skipping 27 matching lines...) Expand all Loading... | |
145 return last_used_; | 191 return last_used_; |
146 } | 192 } |
147 | 193 |
148 Time SimpleEntryImpl::GetLastModified() const { | 194 Time SimpleEntryImpl::GetLastModified() const { |
149 DCHECK(io_thread_checker_.CalledOnValidThread()); | 195 DCHECK(io_thread_checker_.CalledOnValidThread()); |
150 return last_modified_; | 196 return last_modified_; |
151 } | 197 } |
152 | 198 |
153 int32 SimpleEntryImpl::GetDataSize(int stream_index) const { | 199 int32 SimpleEntryImpl::GetDataSize(int stream_index) const { |
154 DCHECK(io_thread_checker_.CalledOnValidThread()); | 200 DCHECK(io_thread_checker_.CalledOnValidThread()); |
201 DCHECK_LE(0, data_size_[stream_index]); | |
155 return data_size_[stream_index]; | 202 return data_size_[stream_index]; |
156 } | 203 } |
157 | 204 |
158 int SimpleEntryImpl::ReadData(int stream_index, | 205 int SimpleEntryImpl::ReadData(int stream_index, |
159 int offset, | 206 int offset, |
160 net::IOBuffer* buf, | 207 net::IOBuffer* buf, |
161 int buf_len, | 208 int buf_len, |
162 const CompletionCallback& callback) { | 209 const CompletionCallback& callback) { |
163 DCHECK(io_thread_checker_.CalledOnValidThread()); | 210 DCHECK(io_thread_checker_.CalledOnValidThread()); |
164 if (stream_index < 0 || stream_index >= kSimpleEntryFileCount || buf_len < 0) | 211 if (stream_index < 0 || stream_index >= kSimpleEntryFileCount || buf_len < 0) |
165 return net::ERR_INVALID_ARGUMENT; | 212 return net::ERR_INVALID_ARGUMENT; |
166 if (offset >= data_size_[stream_index] || offset < 0 || !buf_len) | 213 if (offset >= data_size_[stream_index] || offset < 0 || !buf_len) |
167 return 0; | 214 return 0; |
168 buf_len = std::min(buf_len, data_size_[stream_index] - offset); | 215 |
169 // TODO(felipeg): Optimization: Add support for truly parallel read | 216 // TODO(felipeg): Optimization: Add support for truly parallel read |
170 // operations. | 217 // operations. |
171 pending_operations_.push( | 218 pending_operations_.push( |
172 base::Bind(&SimpleEntryImpl::ReadDataInternal, | 219 base::Bind(&SimpleEntryImpl::ReadDataInternal, |
173 this, | 220 this, |
174 stream_index, | 221 stream_index, |
175 offset, | 222 offset, |
176 make_scoped_refptr(buf), | 223 make_scoped_refptr(buf), |
177 buf_len, | 224 buf_len, |
178 callback)); | 225 callback)); |
179 RunNextOperationIfNeeded(); | 226 RunNextOperationIfNeeded(); |
180 return net::ERR_IO_PENDING; | 227 return net::ERR_IO_PENDING; |
181 } | 228 } |
182 | 229 |
183 int SimpleEntryImpl::WriteData(int stream_index, | 230 int SimpleEntryImpl::WriteData(int stream_index, |
184 int offset, | 231 int offset, |
185 net::IOBuffer* buf, | 232 net::IOBuffer* buf, |
186 int buf_len, | 233 int buf_len, |
187 const CompletionCallback& callback, | 234 const CompletionCallback& callback, |
188 bool truncate) { | 235 bool truncate) { |
189 DCHECK(io_thread_checker_.CalledOnValidThread()); | 236 DCHECK(io_thread_checker_.CalledOnValidThread()); |
190 if (stream_index < 0 || stream_index >= kSimpleEntryFileCount || offset < 0 || | 237 if (stream_index < 0 || stream_index >= kSimpleEntryFileCount || offset < 0 || |
191 buf_len < 0) { | 238 buf_len < 0) { |
192 return net::ERR_INVALID_ARGUMENT; | 239 return net::ERR_INVALID_ARGUMENT; |
193 } | 240 } |
194 pending_operations_.push( | 241 |
195 base::Bind(&SimpleEntryImpl::WriteDataInternal, | 242 int ret_value = net::ERR_FAILED; |
196 this, | 243 if (state_ == STATE_READY && pending_operations_.size() == 0) { |
197 stream_index, | 244 // We can only do optimistic Write if there is no pending operations, so |
198 offset, | 245 // that we are sure that the next call to RunNextOperationIfNeeded will |
199 make_scoped_refptr(buf), | 246 // actually run the write operation that sets the stream size. It also |
200 buf_len, | 247 // prevents from previous possibly-conflicting writes that could be stacked |
201 callback, | 248 // in the |pending_operations_|. We could optimize this for when we have |
202 truncate)); | 249 // only read operations enqueued. |
250 pending_operations_.push( | |
251 base::Bind(&SimpleEntryImpl::WriteDataInternal, this, stream_index, | |
252 offset, make_scoped_refptr(buf), buf_len, | |
253 CompletionCallback(), truncate)); | |
254 ret_value = buf_len; | |
255 } else { | |
256 pending_operations_.push( | |
257 base::Bind(&SimpleEntryImpl::WriteDataInternal, this, stream_index, | |
258 offset, make_scoped_refptr(buf), buf_len, callback, | |
259 truncate)); | |
260 ret_value = net::ERR_IO_PENDING; | |
261 } | |
262 | |
263 if (truncate) | |
gavinp
2013/05/03 11:19:27
Nit: needs braces.
felipeg
2013/05/03 11:47:28
Done.
| |
264 data_size_[stream_index] = offset + buf_len; | |
265 else | |
266 data_size_[stream_index] = std::max(offset + buf_len, | |
267 data_size_[stream_index]); | |
268 | |
269 // Since we don't know the correct values for |last_used_| and | |
270 // |last_modified_| yet, we make this approximation. | |
271 last_used_ = last_modified_ = base::Time::Now(); | |
272 | |
203 RunNextOperationIfNeeded(); | 273 RunNextOperationIfNeeded(); |
204 // TODO(felipeg): Optimization: Add support for optimistic writes, quickly | 274 return ret_value; |
205 // returning net::OK here. | |
206 return net::ERR_IO_PENDING; | |
207 } | 275 } |
208 | 276 |
209 int SimpleEntryImpl::ReadSparseData(int64 offset, | 277 int SimpleEntryImpl::ReadSparseData(int64 offset, |
210 net::IOBuffer* buf, | 278 net::IOBuffer* buf, |
211 int buf_len, | 279 int buf_len, |
212 const CompletionCallback& callback) { | 280 const CompletionCallback& callback) { |
213 DCHECK(io_thread_checker_.CalledOnValidThread()); | 281 DCHECK(io_thread_checker_.CalledOnValidThread()); |
214 // TODO(gavinp): Determine if the simple backend should support sparse data. | 282 // TODO(gavinp): Determine if the simple backend should support sparse data. |
215 NOTIMPLEMENTED(); | 283 NOTIMPLEMENTED(); |
216 return net::ERR_FAILED; | 284 return net::ERR_FAILED; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { | 319 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { |
252 DCHECK(io_thread_checker_.CalledOnValidThread()); | 320 DCHECK(io_thread_checker_.CalledOnValidThread()); |
253 // TODO(gavinp): Determine if the simple backend should support sparse data. | 321 // TODO(gavinp): Determine if the simple backend should support sparse data. |
254 NOTIMPLEMENTED(); | 322 NOTIMPLEMENTED(); |
255 return net::ERR_FAILED; | 323 return net::ERR_FAILED; |
256 } | 324 } |
257 | 325 |
258 SimpleEntryImpl::~SimpleEntryImpl() { | 326 SimpleEntryImpl::~SimpleEntryImpl() { |
259 DCHECK(io_thread_checker_.CalledOnValidThread()); | 327 DCHECK(io_thread_checker_.CalledOnValidThread()); |
260 DCHECK_EQ(0U, pending_operations_.size()); | 328 DCHECK_EQ(0U, pending_operations_.size()); |
261 DCHECK_EQ(STATE_UNINITIALIZED, state_); | 329 DCHECK(STATE_UNINITIALIZED == state_ || STATE_FAILURE == state_); |
262 DCHECK(!synchronous_entry_); | 330 DCHECK(!synchronous_entry_); |
263 RemoveSelfFromBackend(); | 331 RemoveSelfFromBackend(); |
264 } | 332 } |
265 | 333 |
266 void SimpleEntryImpl::MakeUninitialized() { | 334 void SimpleEntryImpl::MakeUninitialized() { |
267 state_ = STATE_UNINITIALIZED; | 335 state_ = STATE_UNINITIALIZED; |
268 std::memset(crc32s_end_offset_, 0, sizeof(crc32s_end_offset_)); | 336 std::memset(crc32s_end_offset_, 0, sizeof(crc32s_end_offset_)); |
269 std::memset(crc32s_, 0, sizeof(crc32s_)); | 337 std::memset(crc32s_, 0, sizeof(crc32s_)); |
270 std::memset(have_written_, 0, sizeof(have_written_)); | 338 std::memset(have_written_, 0, sizeof(have_written_)); |
339 std::memset(data_size_, 0, sizeof(data_size_)); | |
271 } | 340 } |
272 | 341 |
273 void SimpleEntryImpl::ReturnEntryToCaller(Entry** out_entry) { | 342 void SimpleEntryImpl::ReturnEntryToCaller(Entry** out_entry) { |
343 DCHECK(out_entry); | |
274 ++open_count_; | 344 ++open_count_; |
275 AddRef(); // Balanced in Close() | 345 AddRef(); // Balanced in Close() |
276 *out_entry = this; | 346 *out_entry = this; |
277 } | 347 } |
278 | 348 |
279 void SimpleEntryImpl::RemoveSelfFromBackend() { | 349 void SimpleEntryImpl::RemoveSelfFromBackend() { |
280 if (!backend_) | 350 if (!backend_) |
281 return; | 351 return; |
282 backend_->OnDeactivated(this); | 352 backend_->OnDeactivated(this); |
283 backend_.reset(); | 353 backend_.reset(); |
284 } | 354 } |
285 | 355 |
286 void SimpleEntryImpl::MarkAsDoomed() { | 356 void SimpleEntryImpl::MarkAsDoomed() { |
287 if (!backend_) | 357 if (!backend_) |
288 return; | 358 return; |
289 backend_->index()->Remove(key_); | 359 backend_->index()->Remove(key_); |
290 RemoveSelfFromBackend(); | 360 RemoveSelfFromBackend(); |
291 } | 361 } |
292 | 362 |
293 void SimpleEntryImpl::RunNextOperationIfNeeded() { | 363 void SimpleEntryImpl::RunNextOperationIfNeeded() { |
294 DCHECK(io_thread_checker_.CalledOnValidThread()); | 364 DCHECK(io_thread_checker_.CalledOnValidThread()); |
295 | |
296 UMA_HISTOGRAM_CUSTOM_COUNTS("SimpleCache.EntryOperationsPending", | 365 UMA_HISTOGRAM_CUSTOM_COUNTS("SimpleCache.EntryOperationsPending", |
297 pending_operations_.size(), 0, 100, 20); | 366 pending_operations_.size(), 0, 100, 20); |
298 | |
299 if (!pending_operations_.empty() && state_ != STATE_IO_PENDING) { | 367 if (!pending_operations_.empty() && state_ != STATE_IO_PENDING) { |
300 base::Closure operation = pending_operations_.front(); | 368 base::Closure operation = pending_operations_.front(); |
301 pending_operations_.pop(); | 369 pending_operations_.pop(); |
302 operation.Run(); | 370 operation.Run(); |
303 // |this| may have been deleted. | 371 // |this| may have been deleted. |
304 } | 372 } |
305 } | 373 } |
306 | 374 |
307 void SimpleEntryImpl::OpenEntryInternal(Entry** out_entry, | 375 void SimpleEntryImpl::OpenEntryInternal(const CompletionCallback& callback, |
308 const CompletionCallback& callback) { | 376 Entry** out_entry) { |
309 ScopedOperationRunner operation_runner(this); | 377 ScopedOperationRunner operation_runner(this); |
310 | |
311 if (state_ == STATE_READY) { | 378 if (state_ == STATE_READY) { |
312 ReturnEntryToCaller(out_entry); | 379 ReturnEntryToCaller(out_entry); |
313 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(callback, | 380 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(callback, |
314 net::OK)); | 381 net::OK)); |
315 return; | 382 return; |
383 } else if (state_ == STATE_FAILURE) { | |
384 if (!callback.is_null()) { | |
gavinp
2013/05/03 11:19:27
When is this used?
| |
385 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | |
386 callback, net::ERR_FAILED)); | |
387 } | |
388 return; | |
316 } | 389 } |
317 DCHECK_EQ(STATE_UNINITIALIZED, state_); | 390 DCHECK_EQ(STATE_UNINITIALIZED, state_); |
318 | |
319 // This enumeration is used in histograms, add entries only at end. | |
320 enum OpenEntryIndexEnum { | |
321 INDEX_NOEXIST = 0, | |
322 INDEX_MISS = 1, | |
323 INDEX_HIT = 2, | |
324 INDEX_MAX = 3, | |
325 }; | |
326 OpenEntryIndexEnum open_entry_index_enum = INDEX_NOEXIST; | |
327 if (backend_) { | |
328 if (backend_->index()->Has(key_)) | |
329 open_entry_index_enum = INDEX_HIT; | |
330 else | |
331 open_entry_index_enum = INDEX_MISS; | |
332 } | |
333 UMA_HISTOGRAM_ENUMERATION("SimpleCache.OpenEntryIndexState", | |
334 open_entry_index_enum, INDEX_MAX); | |
335 // If entry is not known to the index, initiate fast failover to the network. | |
336 if (open_entry_index_enum == INDEX_MISS) { | |
337 MessageLoopProxy::current()->PostTask(FROM_HERE, | |
338 base::Bind(callback, | |
339 net::ERR_FAILED)); | |
340 return; | |
341 } | |
342 state_ = STATE_IO_PENDING; | 391 state_ = STATE_IO_PENDING; |
343 | |
344 const base::TimeTicks start_time = base::TimeTicks::Now(); | 392 const base::TimeTicks start_time = base::TimeTicks::Now(); |
345 typedef SimpleSynchronousEntry* PointerToSimpleSynchronousEntry; | 393 typedef SimpleSynchronousEntry* PointerToSimpleSynchronousEntry; |
346 scoped_ptr<PointerToSimpleSynchronousEntry> sync_entry( | 394 scoped_ptr<PointerToSimpleSynchronousEntry> sync_entry( |
347 new PointerToSimpleSynchronousEntry()); | 395 new PointerToSimpleSynchronousEntry()); |
348 scoped_ptr<int> result(new int()); | 396 scoped_ptr<int> result(new int()); |
349 Closure task = base::Bind(&SimpleSynchronousEntry::OpenEntry, path_, key_, | 397 Closure task = base::Bind(&SimpleSynchronousEntry::OpenEntry, path_, key_, |
350 entry_hash_, sync_entry.get(), result.get()); | 398 entry_hash_, sync_entry.get(), result.get()); |
351 Closure reply = base::Bind(&SimpleEntryImpl::CreationOperationComplete, this, | 399 Closure reply = base::Bind(&SimpleEntryImpl::CreationOperationComplete, this, |
352 callback, start_time, base::Passed(&sync_entry), | 400 callback, start_time, base::Passed(&sync_entry), |
353 base::Passed(&result), out_entry); | 401 base::Passed(&result), out_entry); |
354 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); | 402 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); |
355 } | 403 } |
356 | 404 |
357 void SimpleEntryImpl::CreateEntryInternal(Entry** out_entry, | 405 void SimpleEntryImpl::CreateEntryInternal(const CompletionCallback& callback, |
358 const CompletionCallback& callback) { | 406 Entry** out_entry) { |
359 ScopedOperationRunner operation_runner(this); | 407 ScopedOperationRunner operation_runner(this); |
360 | 408 if (state_ != STATE_UNINITIALIZED) { |
361 if (state_ == STATE_READY) { | |
362 // There is already an active normal entry. | 409 // There is already an active normal entry. |
363 MessageLoopProxy::current()->PostTask(FROM_HERE, | 410 if (!callback.is_null()) { |
364 base::Bind(callback, | 411 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( |
365 net::ERR_FAILED)); | 412 callback, net::ERR_FAILED)); |
413 } | |
366 return; | 414 return; |
367 } | 415 } |
368 DCHECK_EQ(STATE_UNINITIALIZED, state_); | 416 DCHECK_EQ(STATE_UNINITIALIZED, state_); |
369 | 417 |
370 state_ = STATE_IO_PENDING; | 418 state_ = STATE_IO_PENDING; |
371 | 419 |
372 // If creation succeeds, we should mark all streams to be saved on close. | 420 // If creation succeeds, we should mark all streams to be saved on close. |
373 for (int i = 0; i < kSimpleEntryFileCount; ++i) | 421 for (int i = 0; i < kSimpleEntryFileCount; ++i) |
374 have_written_[i] = true; | 422 have_written_[i] = true; |
375 | 423 |
376 // We insert the entry in the index before creating the entry files in the | |
377 // SimpleSynchronousEntry, because this way the worst scenario is when we | |
378 // have the entry in the index but we don't have the created files yet, this | |
379 // way we never leak files. CreationOperationComplete will remove the entry | |
380 // from the index if the creation fails. | |
381 if (backend_) | |
382 backend_->index()->Insert(key_); | |
383 const base::TimeTicks start_time = base::TimeTicks::Now(); | 424 const base::TimeTicks start_time = base::TimeTicks::Now(); |
384 typedef SimpleSynchronousEntry* PointerToSimpleSynchronousEntry; | 425 typedef SimpleSynchronousEntry* PointerToSimpleSynchronousEntry; |
385 scoped_ptr<PointerToSimpleSynchronousEntry> sync_entry( | 426 scoped_ptr<PointerToSimpleSynchronousEntry> sync_entry( |
386 new PointerToSimpleSynchronousEntry()); | 427 new PointerToSimpleSynchronousEntry()); |
387 scoped_ptr<int> result(new int()); | 428 scoped_ptr<int> result(new int()); |
388 Closure task = base::Bind(&SimpleSynchronousEntry::CreateEntry, path_, key_, | 429 Closure task = base::Bind(&SimpleSynchronousEntry::CreateEntry, path_, key_, |
389 entry_hash_, sync_entry.get(), result.get()); | 430 entry_hash_, sync_entry.get(), result.get()); |
390 Closure reply = base::Bind(&SimpleEntryImpl::CreationOperationComplete, this, | 431 Closure reply = base::Bind(&SimpleEntryImpl::CreationOperationComplete, this, |
391 callback, start_time, base::Passed(&sync_entry), | 432 callback, start_time, base::Passed(&sync_entry), |
392 base::Passed(&result), out_entry); | 433 base::Passed(&result), out_entry); |
393 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); | 434 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); |
394 } | 435 } |
395 | 436 |
396 void SimpleEntryImpl::CloseInternal() { | 437 void SimpleEntryImpl::CloseInternal() { |
397 DCHECK(io_thread_checker_.CalledOnValidThread()); | 438 DCHECK(io_thread_checker_.CalledOnValidThread()); |
398 DCHECK_EQ(0U, pending_operations_.size()); | |
399 DCHECK_EQ(STATE_READY, state_); | |
400 DCHECK(synchronous_entry_); | |
401 | |
402 state_ = STATE_IO_PENDING; | |
403 | |
404 typedef SimpleSynchronousEntry::CRCRecord CRCRecord; | 439 typedef SimpleSynchronousEntry::CRCRecord CRCRecord; |
405 | |
406 scoped_ptr<std::vector<CRCRecord> > | 440 scoped_ptr<std::vector<CRCRecord> > |
407 crc32s_to_write(new std::vector<CRCRecord>()); | 441 crc32s_to_write(new std::vector<CRCRecord>()); |
408 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 442 |
409 if (have_written_[i]) { | 443 if (state_ == STATE_READY) { |
410 if (data_size_[i] == crc32s_end_offset_[i]) { | 444 DCHECK(synchronous_entry_); |
411 int32 crc = data_size_[i] == 0 ? crc32(0, Z_NULL, 0) : crc32s_[i]; | 445 state_ = STATE_IO_PENDING; |
412 crc32s_to_write->push_back(CRCRecord(i, true, crc)); | 446 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
413 } else { | 447 if (have_written_[i]) { |
414 crc32s_to_write->push_back(CRCRecord(i, false, 0)); | 448 if (data_size_[i] == crc32s_end_offset_[i]) { |
449 int32 crc = data_size_[i] == 0 ? crc32(0, Z_NULL, 0) : crc32s_[i]; | |
450 crc32s_to_write->push_back(CRCRecord(i, true, crc)); | |
451 } else { | |
452 crc32s_to_write->push_back(CRCRecord(i, false, 0)); | |
453 } | |
415 } | 454 } |
416 } | 455 } |
456 } else { | |
457 DCHECK_EQ(STATE_FAILURE, state_); | |
417 } | 458 } |
418 Closure task = base::Bind(&SimpleSynchronousEntry::Close, | 459 |
419 base::Unretained(synchronous_entry_), | 460 if (synchronous_entry_) { |
420 base::Passed(&crc32s_to_write)); | 461 Closure task = base::Bind(&SimpleSynchronousEntry::Close, |
421 Closure reply = base::Bind(&SimpleEntryImpl::CloseOperationComplete, this); | 462 base::Unretained(synchronous_entry_), |
422 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); | 463 base::Passed(&crc32s_to_write)); |
423 synchronous_entry_ = NULL; | 464 Closure reply = base::Bind(&SimpleEntryImpl::CloseOperationComplete, this); |
465 synchronous_entry_ = NULL; | |
466 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); | |
467 } else { | |
468 synchronous_entry_ = NULL; | |
469 CloseOperationComplete(); | |
470 } | |
424 } | 471 } |
425 | 472 |
426 void SimpleEntryImpl::ReadDataInternal(int stream_index, | 473 void SimpleEntryImpl::ReadDataInternal(int stream_index, |
427 int offset, | 474 int offset, |
428 net::IOBuffer* buf, | 475 net::IOBuffer* buf, |
429 int buf_len, | 476 int buf_len, |
430 const CompletionCallback& callback) { | 477 const CompletionCallback& callback) { |
431 DCHECK(io_thread_checker_.CalledOnValidThread()); | 478 DCHECK(io_thread_checker_.CalledOnValidThread()); |
479 ScopedOperationRunner operation_runner(this); | |
480 | |
481 if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) { | |
482 if (!callback.is_null()) { | |
483 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | |
484 callback, net::ERR_FAILED)); | |
485 } | |
486 return; | |
487 } | |
432 DCHECK_EQ(STATE_READY, state_); | 488 DCHECK_EQ(STATE_READY, state_); |
489 buf_len = std::min(buf_len, GetDataSize(stream_index) - offset); | |
490 if (offset < 0 || buf_len <= 0) { | |
491 // If there is nothing to read, we bail out before setting state_ to | |
492 // STATE_IO_PENDING. | |
493 if (!callback.is_null()) | |
494 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | |
495 callback, 0)); | |
496 return; | |
497 } | |
498 | |
433 state_ = STATE_IO_PENDING; | 499 state_ = STATE_IO_PENDING; |
434 if (backend_) | 500 if (backend_) |
435 backend_->index()->UseIfExists(key_); | 501 backend_->index()->UseIfExists(key_); |
436 | 502 |
437 scoped_ptr<uint32> read_crc32(new uint32()); | 503 scoped_ptr<uint32> read_crc32(new uint32()); |
438 scoped_ptr<int> result(new int()); | 504 scoped_ptr<int> result(new int()); |
439 Closure task = base::Bind(&SimpleSynchronousEntry::ReadData, | 505 Closure task = base::Bind(&SimpleSynchronousEntry::ReadData, |
440 base::Unretained(synchronous_entry_), | 506 base::Unretained(synchronous_entry_), |
441 stream_index, offset, make_scoped_refptr(buf), | 507 stream_index, offset, make_scoped_refptr(buf), |
442 buf_len, read_crc32.get(), result.get()); | 508 buf_len, read_crc32.get(), result.get()); |
443 Closure reply = base::Bind(&SimpleEntryImpl::ReadOperationComplete, this, | 509 Closure reply = base::Bind(&SimpleEntryImpl::ReadOperationComplete, this, |
444 stream_index, offset, callback, | 510 stream_index, offset, callback, |
445 base::Passed(&read_crc32), base::Passed(&result)); | 511 base::Passed(&read_crc32), base::Passed(&result)); |
446 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); | 512 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); |
447 } | 513 } |
448 | 514 |
449 void SimpleEntryImpl::WriteDataInternal(int stream_index, | 515 void SimpleEntryImpl::WriteDataInternal(int stream_index, |
450 int offset, | 516 int offset, |
451 net::IOBuffer* buf, | 517 net::IOBuffer* buf, |
452 int buf_len, | 518 int buf_len, |
453 const CompletionCallback& callback, | 519 const CompletionCallback& callback, |
454 bool truncate) { | 520 bool truncate) { |
455 DCHECK(io_thread_checker_.CalledOnValidThread()); | 521 DCHECK(io_thread_checker_.CalledOnValidThread()); |
522 ScopedOperationRunner operation_runner(this); | |
523 if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) { | |
524 if (!callback.is_null()) { | |
525 // We need to posttask so that we don't go in a loop when we call the | |
526 // callback directly. | |
527 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | |
528 callback, net::ERR_FAILED)); | |
529 } | |
530 // |this| may be destroyed after return here. | |
531 return; | |
532 } | |
456 DCHECK_EQ(STATE_READY, state_); | 533 DCHECK_EQ(STATE_READY, state_); |
457 state_ = STATE_IO_PENDING; | 534 state_ = STATE_IO_PENDING; |
458 if (backend_) | 535 if (backend_) |
459 backend_->index()->UseIfExists(key_); | 536 backend_->index()->UseIfExists(key_); |
460 // It is easy to incrementally compute the CRC from [0 .. |offset + buf_len|) | 537 // It is easy to incrementally compute the CRC from [0 .. |offset + buf_len|) |
461 // if |offset == 0| or we have already computed the CRC for [0 .. offset). | 538 // if |offset == 0| or we have already computed the CRC for [0 .. offset). |
462 // We rely on most write operations being sequential, start to end to compute | 539 // We rely on most write operations being sequential, start to end to compute |
463 // the crc of the data. When we write to an entry and close without having | 540 // the crc of the data. When we write to an entry and close without having |
464 // done a sequential write, we don't check the CRC on read. | 541 // done a sequential write, we don't check the CRC on read. |
465 if (offset == 0 || crc32s_end_offset_[stream_index] == offset) { | 542 if (offset == 0 || crc32s_end_offset_[stream_index] == offset) { |
(...skipping 22 matching lines...) Expand all Loading... | |
488 void SimpleEntryImpl::CreationOperationComplete( | 565 void SimpleEntryImpl::CreationOperationComplete( |
489 const CompletionCallback& completion_callback, | 566 const CompletionCallback& completion_callback, |
490 const base::TimeTicks& start_time, | 567 const base::TimeTicks& start_time, |
491 scoped_ptr<SimpleSynchronousEntry*> in_sync_entry, | 568 scoped_ptr<SimpleSynchronousEntry*> in_sync_entry, |
492 scoped_ptr<int> in_result, | 569 scoped_ptr<int> in_result, |
493 Entry** out_entry) { | 570 Entry** out_entry) { |
494 DCHECK(io_thread_checker_.CalledOnValidThread()); | 571 DCHECK(io_thread_checker_.CalledOnValidThread()); |
495 DCHECK_EQ(state_, STATE_IO_PENDING); | 572 DCHECK_EQ(state_, STATE_IO_PENDING); |
496 DCHECK(in_sync_entry); | 573 DCHECK(in_sync_entry); |
497 DCHECK(in_result); | 574 DCHECK(in_result); |
498 | |
499 ScopedOperationRunner operation_runner(this); | 575 ScopedOperationRunner operation_runner(this); |
500 | |
501 UMA_HISTOGRAM_BOOLEAN( | 576 UMA_HISTOGRAM_BOOLEAN( |
502 "SimpleCache.EntryCreationResult", *in_result == net::OK); | 577 "SimpleCache.EntryCreationResult", *in_result == net::OK); |
503 if (*in_result != net::OK) { | 578 if (*in_result != net::OK) { |
504 if (*in_result!= net::ERR_FILE_EXISTS) | 579 if (*in_result!= net::ERR_FILE_EXISTS) |
505 MarkAsDoomed(); | 580 MarkAsDoomed(); |
581 if (!completion_callback.is_null()) { | |
582 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | |
583 completion_callback, net::ERR_FAILED)); | |
584 } | |
506 MakeUninitialized(); | 585 MakeUninitialized(); |
507 completion_callback.Run(net::ERR_FAILED); | 586 state_ = STATE_FAILURE; |
508 return; | 587 return; |
509 } | 588 } |
589 // If out_entry is NULL, it means we already called ReturnEntryToCaller from | |
590 // the optimistic Create case. | |
591 if (out_entry) | |
592 ReturnEntryToCaller(out_entry); | |
593 | |
510 state_ = STATE_READY; | 594 state_ = STATE_READY; |
511 synchronous_entry_ = *in_sync_entry; | 595 synchronous_entry_ = *in_sync_entry; |
512 SetSynchronousData(); | 596 SetSynchronousData(); |
513 ReturnEntryToCaller(out_entry); | |
514 UMA_HISTOGRAM_TIMES("SimpleCache.EntryCreationTime", | 597 UMA_HISTOGRAM_TIMES("SimpleCache.EntryCreationTime", |
515 (base::TimeTicks::Now() - start_time)); | 598 (base::TimeTicks::Now() - start_time)); |
516 completion_callback.Run(net::OK); | 599 |
600 if (!completion_callback.is_null()) { | |
601 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | |
602 completion_callback, net::OK)); | |
603 } | |
517 } | 604 } |
518 | 605 |
519 void SimpleEntryImpl::EntryOperationComplete( | 606 void SimpleEntryImpl::EntryOperationComplete( |
520 int stream_index, | 607 int stream_index, |
521 const CompletionCallback& completion_callback, | 608 const CompletionCallback& completion_callback, |
522 scoped_ptr<int> result) { | 609 scoped_ptr<int> result) { |
523 DCHECK(io_thread_checker_.CalledOnValidThread()); | 610 DCHECK(io_thread_checker_.CalledOnValidThread()); |
524 DCHECK(synchronous_entry_); | 611 DCHECK(synchronous_entry_); |
525 DCHECK_EQ(STATE_IO_PENDING, state_); | 612 DCHECK_EQ(STATE_IO_PENDING, state_); |
526 DCHECK(result); | 613 DCHECK(result); |
527 | |
528 state_ = STATE_READY; | 614 state_ = STATE_READY; |
529 | |
530 if (*result < 0) { | 615 if (*result < 0) { |
531 MarkAsDoomed(); | 616 MarkAsDoomed(); |
617 state_ = STATE_FAILURE; | |
532 crc32s_end_offset_[stream_index] = 0; | 618 crc32s_end_offset_[stream_index] = 0; |
619 } else { | |
620 SetSynchronousData(); | |
533 } | 621 } |
534 SetSynchronousData(); | 622 |
535 completion_callback.Run(*result); | 623 if (!completion_callback.is_null()) { |
624 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | |
625 completion_callback, *result)); | |
626 } | |
536 RunNextOperationIfNeeded(); | 627 RunNextOperationIfNeeded(); |
537 } | 628 } |
538 | 629 |
539 void SimpleEntryImpl::ReadOperationComplete( | 630 void SimpleEntryImpl::ReadOperationComplete( |
540 int stream_index, | 631 int stream_index, |
541 int offset, | 632 int offset, |
542 const CompletionCallback& completion_callback, | 633 const CompletionCallback& completion_callback, |
543 scoped_ptr<uint32> read_crc32, | 634 scoped_ptr<uint32> read_crc32, |
544 scoped_ptr<int> result) { | 635 scoped_ptr<int> result) { |
545 DCHECK(io_thread_checker_.CalledOnValidThread()); | 636 DCHECK(io_thread_checker_.CalledOnValidThread()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
591 DCHECK_EQ(STATE_IO_PENDING, state_); | 682 DCHECK_EQ(STATE_IO_PENDING, state_); |
592 DCHECK(result); | 683 DCHECK(result); |
593 if (*result == net::OK) | 684 if (*result == net::OK) |
594 *result = orig_result; | 685 *result = orig_result; |
595 EntryOperationComplete(stream_index, completion_callback, result.Pass()); | 686 EntryOperationComplete(stream_index, completion_callback, result.Pass()); |
596 } | 687 } |
597 | 688 |
598 void SimpleEntryImpl::CloseOperationComplete() { | 689 void SimpleEntryImpl::CloseOperationComplete() { |
599 DCHECK(!synchronous_entry_); | 690 DCHECK(!synchronous_entry_); |
600 DCHECK_EQ(0, open_count_); | 691 DCHECK_EQ(0, open_count_); |
601 DCHECK_EQ(STATE_IO_PENDING, state_); | 692 DCHECK(STATE_IO_PENDING == state_ || STATE_FAILURE == state_); |
602 | |
603 MakeUninitialized(); | 693 MakeUninitialized(); |
604 RunNextOperationIfNeeded(); | 694 RunNextOperationIfNeeded(); |
605 } | 695 } |
606 | 696 |
607 void SimpleEntryImpl::SetSynchronousData() { | 697 void SimpleEntryImpl::SetSynchronousData() { |
608 DCHECK(io_thread_checker_.CalledOnValidThread()); | 698 DCHECK(io_thread_checker_.CalledOnValidThread()); |
699 DCHECK(synchronous_entry_); | |
609 DCHECK_EQ(STATE_READY, state_); | 700 DCHECK_EQ(STATE_READY, state_); |
610 // TODO(felipeg): These copies to avoid data races are not optimal. While | 701 // TODO(felipeg): These copies to avoid data races are not optimal. While |
611 // adding an IO thread index (for fast misses etc...), we can store this data | 702 // adding an IO thread index (for fast misses etc...), we can store this data |
612 // in that structure. This also solves problems with last_used() on ext4 | 703 // in that structure. This also solves problems with last_used() on ext4 |
613 // filesystems not being accurate. | 704 // filesystems not being accurate. |
614 last_used_ = synchronous_entry_->last_used(); | 705 last_used_ = synchronous_entry_->last_used(); |
615 last_modified_ = synchronous_entry_->last_modified(); | 706 last_modified_ = synchronous_entry_->last_modified(); |
616 for (int i = 0; i < kSimpleEntryFileCount; ++i) | 707 for (int i = 0; i < kSimpleEntryFileCount; ++i) |
617 data_size_[i] = synchronous_entry_->data_size(i); | 708 data_size_[i] = synchronous_entry_->data_size(i); |
618 if (backend_) | 709 if (backend_) |
619 backend_->index()->UpdateEntrySize(key_, synchronous_entry_->GetFileSize()); | 710 backend_->index()->UpdateEntrySize(key_, synchronous_entry_->GetFileSize()); |
620 } | 711 } |
621 | 712 |
622 } // namespace disk_cache | 713 } // namespace disk_cache |
OLD | NEW |