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

Side by Side Diff: net/disk_cache/simple/simple_entry_impl.cc

Issue 13907009: Support optimistic Create and Write operations on the SimpleCache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months 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 | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/simple/simple_entry_impl.h ('k') | net/http/http_cache.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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) {
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
270 // Since we don't know the correct values for |last_used_| and
271 // |last_modified_| yet, we make this approximation.
272 last_used_ = last_modified_ = base::Time::Now();
273
203 RunNextOperationIfNeeded(); 274 RunNextOperationIfNeeded();
204 // TODO(felipeg): Optimization: Add support for optimistic writes, quickly 275 return ret_value;
205 // returning net::OK here.
206 return net::ERR_IO_PENDING;
207 } 276 }
208 277
209 int SimpleEntryImpl::ReadSparseData(int64 offset, 278 int SimpleEntryImpl::ReadSparseData(int64 offset,
210 net::IOBuffer* buf, 279 net::IOBuffer* buf,
211 int buf_len, 280 int buf_len,
212 const CompletionCallback& callback) { 281 const CompletionCallback& callback) {
213 DCHECK(io_thread_checker_.CalledOnValidThread()); 282 DCHECK(io_thread_checker_.CalledOnValidThread());
214 // TODO(gavinp): Determine if the simple backend should support sparse data. 283 // TODO(gavinp): Determine if the simple backend should support sparse data.
215 NOTIMPLEMENTED(); 284 NOTIMPLEMENTED();
216 return net::ERR_FAILED; 285 return net::ERR_FAILED;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { 320 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) {
252 DCHECK(io_thread_checker_.CalledOnValidThread()); 321 DCHECK(io_thread_checker_.CalledOnValidThread());
253 // TODO(gavinp): Determine if the simple backend should support sparse data. 322 // TODO(gavinp): Determine if the simple backend should support sparse data.
254 NOTIMPLEMENTED(); 323 NOTIMPLEMENTED();
255 return net::ERR_FAILED; 324 return net::ERR_FAILED;
256 } 325 }
257 326
258 SimpleEntryImpl::~SimpleEntryImpl() { 327 SimpleEntryImpl::~SimpleEntryImpl() {
259 DCHECK(io_thread_checker_.CalledOnValidThread()); 328 DCHECK(io_thread_checker_.CalledOnValidThread());
260 DCHECK_EQ(0U, pending_operations_.size()); 329 DCHECK_EQ(0U, pending_operations_.size());
261 DCHECK_EQ(STATE_UNINITIALIZED, state_); 330 DCHECK(STATE_UNINITIALIZED == state_ || STATE_FAILURE == state_);
262 DCHECK(!synchronous_entry_); 331 DCHECK(!synchronous_entry_);
263 RemoveSelfFromBackend(); 332 RemoveSelfFromBackend();
264 } 333 }
265 334
266 void SimpleEntryImpl::MakeUninitialized() { 335 void SimpleEntryImpl::MakeUninitialized() {
267 state_ = STATE_UNINITIALIZED; 336 state_ = STATE_UNINITIALIZED;
268 std::memset(crc32s_end_offset_, 0, sizeof(crc32s_end_offset_)); 337 std::memset(crc32s_end_offset_, 0, sizeof(crc32s_end_offset_));
269 std::memset(crc32s_, 0, sizeof(crc32s_)); 338 std::memset(crc32s_, 0, sizeof(crc32s_));
270 std::memset(have_written_, 0, sizeof(have_written_)); 339 std::memset(have_written_, 0, sizeof(have_written_));
340 std::memset(data_size_, 0, sizeof(data_size_));
271 } 341 }
272 342
273 void SimpleEntryImpl::ReturnEntryToCaller(Entry** out_entry) { 343 void SimpleEntryImpl::ReturnEntryToCaller(Entry** out_entry) {
344 DCHECK(out_entry);
274 ++open_count_; 345 ++open_count_;
275 AddRef(); // Balanced in Close() 346 AddRef(); // Balanced in Close()
276 *out_entry = this; 347 *out_entry = this;
277 } 348 }
278 349
279 void SimpleEntryImpl::RemoveSelfFromBackend() { 350 void SimpleEntryImpl::RemoveSelfFromBackend() {
280 if (!backend_) 351 if (!backend_)
281 return; 352 return;
282 backend_->OnDeactivated(this); 353 backend_->OnDeactivated(this);
283 backend_.reset(); 354 backend_.reset();
284 } 355 }
285 356
286 void SimpleEntryImpl::MarkAsDoomed() { 357 void SimpleEntryImpl::MarkAsDoomed() {
287 if (!backend_) 358 if (!backend_)
288 return; 359 return;
289 backend_->index()->Remove(key_); 360 backend_->index()->Remove(key_);
290 RemoveSelfFromBackend(); 361 RemoveSelfFromBackend();
291 } 362 }
292 363
293 void SimpleEntryImpl::RunNextOperationIfNeeded() { 364 void SimpleEntryImpl::RunNextOperationIfNeeded() {
294 DCHECK(io_thread_checker_.CalledOnValidThread()); 365 DCHECK(io_thread_checker_.CalledOnValidThread());
295
296 UMA_HISTOGRAM_CUSTOM_COUNTS("SimpleCache.EntryOperationsPending", 366 UMA_HISTOGRAM_CUSTOM_COUNTS("SimpleCache.EntryOperationsPending",
297 pending_operations_.size(), 0, 100, 20); 367 pending_operations_.size(), 0, 100, 20);
298
299 if (!pending_operations_.empty() && state_ != STATE_IO_PENDING) { 368 if (!pending_operations_.empty() && state_ != STATE_IO_PENDING) {
300 base::Closure operation = pending_operations_.front(); 369 base::Closure operation = pending_operations_.front();
301 pending_operations_.pop(); 370 pending_operations_.pop();
302 operation.Run(); 371 operation.Run();
303 // |this| may have been deleted. 372 // |this| may have been deleted.
304 } 373 }
305 } 374 }
306 375
307 void SimpleEntryImpl::OpenEntryInternal(Entry** out_entry, 376 void SimpleEntryImpl::OpenEntryInternal(const CompletionCallback& callback,
308 const CompletionCallback& callback) { 377 Entry** out_entry) {
309 ScopedOperationRunner operation_runner(this); 378 ScopedOperationRunner operation_runner(this);
310
311 if (state_ == STATE_READY) { 379 if (state_ == STATE_READY) {
312 ReturnEntryToCaller(out_entry); 380 ReturnEntryToCaller(out_entry);
313 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(callback, 381 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(callback,
314 net::OK)); 382 net::OK));
315 return; 383 return;
384 } else if (state_ == STATE_FAILURE) {
385 if (!callback.is_null()) {
386 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(
387 callback, net::ERR_FAILED));
388 }
389 return;
316 } 390 }
317 DCHECK_EQ(STATE_UNINITIALIZED, state_); 391 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; 392 state_ = STATE_IO_PENDING;
343
344 const base::TimeTicks start_time = base::TimeTicks::Now(); 393 const base::TimeTicks start_time = base::TimeTicks::Now();
345 typedef SimpleSynchronousEntry* PointerToSimpleSynchronousEntry; 394 typedef SimpleSynchronousEntry* PointerToSimpleSynchronousEntry;
346 scoped_ptr<PointerToSimpleSynchronousEntry> sync_entry( 395 scoped_ptr<PointerToSimpleSynchronousEntry> sync_entry(
347 new PointerToSimpleSynchronousEntry()); 396 new PointerToSimpleSynchronousEntry());
348 scoped_ptr<int> result(new int()); 397 scoped_ptr<int> result(new int());
349 Closure task = base::Bind(&SimpleSynchronousEntry::OpenEntry, path_, key_, 398 Closure task = base::Bind(&SimpleSynchronousEntry::OpenEntry, path_, key_,
350 entry_hash_, sync_entry.get(), result.get()); 399 entry_hash_, sync_entry.get(), result.get());
351 Closure reply = base::Bind(&SimpleEntryImpl::CreationOperationComplete, this, 400 Closure reply = base::Bind(&SimpleEntryImpl::CreationOperationComplete, this,
352 callback, start_time, base::Passed(&sync_entry), 401 callback, start_time, base::Passed(&sync_entry),
353 base::Passed(&result), out_entry); 402 base::Passed(&result), out_entry);
354 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); 403 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true);
355 } 404 }
356 405
357 void SimpleEntryImpl::CreateEntryInternal(Entry** out_entry, 406 void SimpleEntryImpl::CreateEntryInternal(const CompletionCallback& callback,
358 const CompletionCallback& callback) { 407 Entry** out_entry) {
359 ScopedOperationRunner operation_runner(this); 408 ScopedOperationRunner operation_runner(this);
360 409 if (state_ != STATE_UNINITIALIZED) {
361 if (state_ == STATE_READY) {
362 // There is already an active normal entry. 410 // There is already an active normal entry.
363 MessageLoopProxy::current()->PostTask(FROM_HERE, 411 if (!callback.is_null()) {
364 base::Bind(callback, 412 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(
365 net::ERR_FAILED)); 413 callback, net::ERR_FAILED));
414 }
366 return; 415 return;
367 } 416 }
368 DCHECK_EQ(STATE_UNINITIALIZED, state_); 417 DCHECK_EQ(STATE_UNINITIALIZED, state_);
369 418
370 state_ = STATE_IO_PENDING; 419 state_ = STATE_IO_PENDING;
371 420
372 // If creation succeeds, we should mark all streams to be saved on close. 421 // If creation succeeds, we should mark all streams to be saved on close.
373 for (int i = 0; i < kSimpleEntryFileCount; ++i) 422 for (int i = 0; i < kSimpleEntryFileCount; ++i)
374 have_written_[i] = true; 423 have_written_[i] = true;
375 424
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(); 425 const base::TimeTicks start_time = base::TimeTicks::Now();
384 typedef SimpleSynchronousEntry* PointerToSimpleSynchronousEntry; 426 typedef SimpleSynchronousEntry* PointerToSimpleSynchronousEntry;
385 scoped_ptr<PointerToSimpleSynchronousEntry> sync_entry( 427 scoped_ptr<PointerToSimpleSynchronousEntry> sync_entry(
386 new PointerToSimpleSynchronousEntry()); 428 new PointerToSimpleSynchronousEntry());
387 scoped_ptr<int> result(new int()); 429 scoped_ptr<int> result(new int());
388 Closure task = base::Bind(&SimpleSynchronousEntry::CreateEntry, path_, key_, 430 Closure task = base::Bind(&SimpleSynchronousEntry::CreateEntry, path_, key_,
389 entry_hash_, sync_entry.get(), result.get()); 431 entry_hash_, sync_entry.get(), result.get());
390 Closure reply = base::Bind(&SimpleEntryImpl::CreationOperationComplete, this, 432 Closure reply = base::Bind(&SimpleEntryImpl::CreationOperationComplete, this,
391 callback, start_time, base::Passed(&sync_entry), 433 callback, start_time, base::Passed(&sync_entry),
392 base::Passed(&result), out_entry); 434 base::Passed(&result), out_entry);
393 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); 435 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true);
394 } 436 }
395 437
396 void SimpleEntryImpl::CloseInternal() { 438 void SimpleEntryImpl::CloseInternal() {
397 DCHECK(io_thread_checker_.CalledOnValidThread()); 439 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; 440 typedef SimpleSynchronousEntry::CRCRecord CRCRecord;
405
406 scoped_ptr<std::vector<CRCRecord> > 441 scoped_ptr<std::vector<CRCRecord> >
407 crc32s_to_write(new std::vector<CRCRecord>()); 442 crc32s_to_write(new std::vector<CRCRecord>());
408 for (int i = 0; i < kSimpleEntryFileCount; ++i) { 443
409 if (have_written_[i]) { 444 if (state_ == STATE_READY) {
410 if (data_size_[i] == crc32s_end_offset_[i]) { 445 DCHECK(synchronous_entry_);
411 int32 crc = data_size_[i] == 0 ? crc32(0, Z_NULL, 0) : crc32s_[i]; 446 state_ = STATE_IO_PENDING;
412 crc32s_to_write->push_back(CRCRecord(i, true, crc)); 447 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
413 } else { 448 if (have_written_[i]) {
414 crc32s_to_write->push_back(CRCRecord(i, false, 0)); 449 if (data_size_[i] == crc32s_end_offset_[i]) {
450 int32 crc = data_size_[i] == 0 ? crc32(0, Z_NULL, 0) : crc32s_[i];
451 crc32s_to_write->push_back(CRCRecord(i, true, crc));
452 } else {
453 crc32s_to_write->push_back(CRCRecord(i, false, 0));
454 }
415 } 455 }
416 } 456 }
457 } else {
458 DCHECK_EQ(STATE_FAILURE, state_);
417 } 459 }
418 Closure task = base::Bind(&SimpleSynchronousEntry::Close, 460
419 base::Unretained(synchronous_entry_), 461 if (synchronous_entry_) {
420 base::Passed(&crc32s_to_write)); 462 Closure task = base::Bind(&SimpleSynchronousEntry::Close,
421 Closure reply = base::Bind(&SimpleEntryImpl::CloseOperationComplete, this); 463 base::Unretained(synchronous_entry_),
422 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); 464 base::Passed(&crc32s_to_write));
423 synchronous_entry_ = NULL; 465 Closure reply = base::Bind(&SimpleEntryImpl::CloseOperationComplete, this);
466 synchronous_entry_ = NULL;
467 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true);
468 } else {
469 synchronous_entry_ = NULL;
470 CloseOperationComplete();
471 }
424 } 472 }
425 473
426 void SimpleEntryImpl::ReadDataInternal(int stream_index, 474 void SimpleEntryImpl::ReadDataInternal(int stream_index,
427 int offset, 475 int offset,
428 net::IOBuffer* buf, 476 net::IOBuffer* buf,
429 int buf_len, 477 int buf_len,
430 const CompletionCallback& callback) { 478 const CompletionCallback& callback) {
431 DCHECK(io_thread_checker_.CalledOnValidThread()); 479 DCHECK(io_thread_checker_.CalledOnValidThread());
480 ScopedOperationRunner operation_runner(this);
481
482 if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) {
483 if (!callback.is_null()) {
484 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(
485 callback, net::ERR_FAILED));
486 }
487 return;
488 }
432 DCHECK_EQ(STATE_READY, state_); 489 DCHECK_EQ(STATE_READY, state_);
490 buf_len = std::min(buf_len, GetDataSize(stream_index) - offset);
491 if (offset < 0 || buf_len <= 0) {
492 // If there is nothing to read, we bail out before setting state_ to
493 // STATE_IO_PENDING.
494 if (!callback.is_null())
495 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(
496 callback, 0));
497 return;
498 }
499
433 state_ = STATE_IO_PENDING; 500 state_ = STATE_IO_PENDING;
434 if (backend_) 501 if (backend_)
435 backend_->index()->UseIfExists(key_); 502 backend_->index()->UseIfExists(key_);
436 503
437 scoped_ptr<uint32> read_crc32(new uint32()); 504 scoped_ptr<uint32> read_crc32(new uint32());
438 scoped_ptr<int> result(new int()); 505 scoped_ptr<int> result(new int());
439 Closure task = base::Bind(&SimpleSynchronousEntry::ReadData, 506 Closure task = base::Bind(&SimpleSynchronousEntry::ReadData,
440 base::Unretained(synchronous_entry_), 507 base::Unretained(synchronous_entry_),
441 stream_index, offset, make_scoped_refptr(buf), 508 stream_index, offset, make_scoped_refptr(buf),
442 buf_len, read_crc32.get(), result.get()); 509 buf_len, read_crc32.get(), result.get());
443 Closure reply = base::Bind(&SimpleEntryImpl::ReadOperationComplete, this, 510 Closure reply = base::Bind(&SimpleEntryImpl::ReadOperationComplete, this,
444 stream_index, offset, callback, 511 stream_index, offset, callback,
445 base::Passed(&read_crc32), base::Passed(&result)); 512 base::Passed(&read_crc32), base::Passed(&result));
446 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true); 513 WorkerPool::PostTaskAndReply(FROM_HERE, task, reply, true);
447 } 514 }
448 515
449 void SimpleEntryImpl::WriteDataInternal(int stream_index, 516 void SimpleEntryImpl::WriteDataInternal(int stream_index,
450 int offset, 517 int offset,
451 net::IOBuffer* buf, 518 net::IOBuffer* buf,
452 int buf_len, 519 int buf_len,
453 const CompletionCallback& callback, 520 const CompletionCallback& callback,
454 bool truncate) { 521 bool truncate) {
455 DCHECK(io_thread_checker_.CalledOnValidThread()); 522 DCHECK(io_thread_checker_.CalledOnValidThread());
523 ScopedOperationRunner operation_runner(this);
524 if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) {
525 if (!callback.is_null()) {
526 // We need to posttask so that we don't go in a loop when we call the
527 // callback directly.
528 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(
529 callback, net::ERR_FAILED));
530 }
531 // |this| may be destroyed after return here.
532 return;
533 }
456 DCHECK_EQ(STATE_READY, state_); 534 DCHECK_EQ(STATE_READY, state_);
457 state_ = STATE_IO_PENDING; 535 state_ = STATE_IO_PENDING;
458 if (backend_) 536 if (backend_)
459 backend_->index()->UseIfExists(key_); 537 backend_->index()->UseIfExists(key_);
460 // It is easy to incrementally compute the CRC from [0 .. |offset + buf_len|) 538 // 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). 539 // 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 540 // 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 541 // 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. 542 // done a sequential write, we don't check the CRC on read.
465 if (offset == 0 || crc32s_end_offset_[stream_index] == offset) { 543 if (offset == 0 || crc32s_end_offset_[stream_index] == offset) {
(...skipping 22 matching lines...) Expand all
488 void SimpleEntryImpl::CreationOperationComplete( 566 void SimpleEntryImpl::CreationOperationComplete(
489 const CompletionCallback& completion_callback, 567 const CompletionCallback& completion_callback,
490 const base::TimeTicks& start_time, 568 const base::TimeTicks& start_time,
491 scoped_ptr<SimpleSynchronousEntry*> in_sync_entry, 569 scoped_ptr<SimpleSynchronousEntry*> in_sync_entry,
492 scoped_ptr<int> in_result, 570 scoped_ptr<int> in_result,
493 Entry** out_entry) { 571 Entry** out_entry) {
494 DCHECK(io_thread_checker_.CalledOnValidThread()); 572 DCHECK(io_thread_checker_.CalledOnValidThread());
495 DCHECK_EQ(state_, STATE_IO_PENDING); 573 DCHECK_EQ(state_, STATE_IO_PENDING);
496 DCHECK(in_sync_entry); 574 DCHECK(in_sync_entry);
497 DCHECK(in_result); 575 DCHECK(in_result);
498
499 ScopedOperationRunner operation_runner(this); 576 ScopedOperationRunner operation_runner(this);
500
501 UMA_HISTOGRAM_BOOLEAN( 577 UMA_HISTOGRAM_BOOLEAN(
502 "SimpleCache.EntryCreationResult", *in_result == net::OK); 578 "SimpleCache.EntryCreationResult", *in_result == net::OK);
503 if (*in_result != net::OK) { 579 if (*in_result != net::OK) {
504 if (*in_result!= net::ERR_FILE_EXISTS) 580 if (*in_result!= net::ERR_FILE_EXISTS)
505 MarkAsDoomed(); 581 MarkAsDoomed();
582 if (!completion_callback.is_null()) {
583 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(
584 completion_callback, net::ERR_FAILED));
585 }
506 MakeUninitialized(); 586 MakeUninitialized();
507 completion_callback.Run(net::ERR_FAILED); 587 state_ = STATE_FAILURE;
508 return; 588 return;
509 } 589 }
590 // If out_entry is NULL, it means we already called ReturnEntryToCaller from
591 // the optimistic Create case.
592 if (out_entry)
593 ReturnEntryToCaller(out_entry);
594
510 state_ = STATE_READY; 595 state_ = STATE_READY;
511 synchronous_entry_ = *in_sync_entry; 596 synchronous_entry_ = *in_sync_entry;
512 SetSynchronousData(); 597 SetSynchronousData();
513 ReturnEntryToCaller(out_entry);
514 UMA_HISTOGRAM_TIMES("SimpleCache.EntryCreationTime", 598 UMA_HISTOGRAM_TIMES("SimpleCache.EntryCreationTime",
515 (base::TimeTicks::Now() - start_time)); 599 (base::TimeTicks::Now() - start_time));
516 completion_callback.Run(net::OK); 600
601 if (!completion_callback.is_null()) {
602 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(
603 completion_callback, net::OK));
604 }
517 } 605 }
518 606
519 void SimpleEntryImpl::EntryOperationComplete( 607 void SimpleEntryImpl::EntryOperationComplete(
520 int stream_index, 608 int stream_index,
521 const CompletionCallback& completion_callback, 609 const CompletionCallback& completion_callback,
522 scoped_ptr<int> result) { 610 scoped_ptr<int> result) {
523 DCHECK(io_thread_checker_.CalledOnValidThread()); 611 DCHECK(io_thread_checker_.CalledOnValidThread());
524 DCHECK(synchronous_entry_); 612 DCHECK(synchronous_entry_);
525 DCHECK_EQ(STATE_IO_PENDING, state_); 613 DCHECK_EQ(STATE_IO_PENDING, state_);
526 DCHECK(result); 614 DCHECK(result);
527
528 state_ = STATE_READY; 615 state_ = STATE_READY;
529
530 if (*result < 0) { 616 if (*result < 0) {
531 MarkAsDoomed(); 617 MarkAsDoomed();
618 state_ = STATE_FAILURE;
532 crc32s_end_offset_[stream_index] = 0; 619 crc32s_end_offset_[stream_index] = 0;
620 } else {
621 SetSynchronousData();
533 } 622 }
534 SetSynchronousData(); 623
535 completion_callback.Run(*result); 624 if (!completion_callback.is_null()) {
625 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(
626 completion_callback, *result));
627 }
536 RunNextOperationIfNeeded(); 628 RunNextOperationIfNeeded();
537 } 629 }
538 630
539 void SimpleEntryImpl::ReadOperationComplete( 631 void SimpleEntryImpl::ReadOperationComplete(
540 int stream_index, 632 int stream_index,
541 int offset, 633 int offset,
542 const CompletionCallback& completion_callback, 634 const CompletionCallback& completion_callback,
543 scoped_ptr<uint32> read_crc32, 635 scoped_ptr<uint32> read_crc32,
544 scoped_ptr<int> result) { 636 scoped_ptr<int> result) {
545 DCHECK(io_thread_checker_.CalledOnValidThread()); 637 DCHECK(io_thread_checker_.CalledOnValidThread());
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 DCHECK_EQ(STATE_IO_PENDING, state_); 683 DCHECK_EQ(STATE_IO_PENDING, state_);
592 DCHECK(result); 684 DCHECK(result);
593 if (*result == net::OK) 685 if (*result == net::OK)
594 *result = orig_result; 686 *result = orig_result;
595 EntryOperationComplete(stream_index, completion_callback, result.Pass()); 687 EntryOperationComplete(stream_index, completion_callback, result.Pass());
596 } 688 }
597 689
598 void SimpleEntryImpl::CloseOperationComplete() { 690 void SimpleEntryImpl::CloseOperationComplete() {
599 DCHECK(!synchronous_entry_); 691 DCHECK(!synchronous_entry_);
600 DCHECK_EQ(0, open_count_); 692 DCHECK_EQ(0, open_count_);
601 DCHECK_EQ(STATE_IO_PENDING, state_); 693 DCHECK(STATE_IO_PENDING == state_ || STATE_FAILURE == state_);
602
603 MakeUninitialized(); 694 MakeUninitialized();
604 RunNextOperationIfNeeded(); 695 RunNextOperationIfNeeded();
605 } 696 }
606 697
607 void SimpleEntryImpl::SetSynchronousData() { 698 void SimpleEntryImpl::SetSynchronousData() {
608 DCHECK(io_thread_checker_.CalledOnValidThread()); 699 DCHECK(io_thread_checker_.CalledOnValidThread());
700 DCHECK(synchronous_entry_);
609 DCHECK_EQ(STATE_READY, state_); 701 DCHECK_EQ(STATE_READY, state_);
610 // TODO(felipeg): These copies to avoid data races are not optimal. While 702 // 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 703 // 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 704 // in that structure. This also solves problems with last_used() on ext4
613 // filesystems not being accurate. 705 // filesystems not being accurate.
614 last_used_ = synchronous_entry_->last_used(); 706 last_used_ = synchronous_entry_->last_used();
615 last_modified_ = synchronous_entry_->last_modified(); 707 last_modified_ = synchronous_entry_->last_modified();
616 for (int i = 0; i < kSimpleEntryFileCount; ++i) 708 for (int i = 0; i < kSimpleEntryFileCount; ++i)
617 data_size_[i] = synchronous_entry_->data_size(i); 709 data_size_[i] = synchronous_entry_->data_size(i);
618 if (backend_) 710 if (backend_)
619 backend_->index()->UpdateEntrySize(key_, synchronous_entry_->GetFileSize()); 711 backend_->index()->UpdateEntrySize(key_, synchronous_entry_->GetFileSize());
620 } 712 }
621 713
622 } // namespace disk_cache 714 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_entry_impl.h ('k') | net/http/http_cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698