summaryrefslogtreecommitdiffstats
path: root/net/disk_cache/in_flight_backend_io.cc
blob: fe5382923c54df8d0b8033c100284797c70f0302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/disk_cache/in_flight_backend_io.h"

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/entry_impl.h"
#include "net/disk_cache/histogram_macros.h"

namespace disk_cache {

BackendIO::BackendIO(InFlightIO* controller, BackendImpl* backend,
                     net::CompletionCallback* callback)
    : BackgroundIO(controller), backend_(backend), callback_(callback),
      operation_(OP_NONE),
      ALLOW_THIS_IN_INITIALIZER_LIST(
          my_callback_(this, &BackendIO::OnIOComplete)) {
  start_time_ = base::TimeTicks::Now();
}

// Runs on the background thread.
void BackendIO::ExecuteOperation() {
  if (IsEntryOperation())
    return ExecuteEntryOperation();

  ExecuteBackendOperation();
}

// Runs on the background thread.
void BackendIO::OnIOComplete(int result) {
  DCHECK(IsEntryOperation());
  DCHECK_NE(result, net::ERR_IO_PENDING);
  result_ = result;
  controller_->OnIOComplete(this);
}

bool BackendIO::IsEntryOperation() {
  return operation_ > OP_MAX_BACKEND;
}

void BackendIO::ReleaseEntry() {
  entry_ = NULL;
}

base::TimeDelta BackendIO::ElapsedTime() const {
  return base::TimeTicks::Now() - start_time_;
}

void BackendIO::Init() {
  operation_ = OP_INIT;
}

void BackendIO::OpenEntry(const std::string& key, Entry** entry) {
  operation_ = OP_OPEN;
  key_ = key;
  entry_ptr_ = entry;
}

void BackendIO::CreateEntry(const std::string& key, Entry** entry) {
  operation_ = OP_CREATE;
  key_ = key;
  entry_ptr_ = entry;
}

void BackendIO::DoomEntry(const std::string& key) {
  operation_ = OP_DOOM;
  key_ = key;
}

void BackendIO::DoomAllEntries() {
  operation_ = OP_DOOM_ALL;
}

void BackendIO::DoomEntriesBetween(const base::Time initial_time,
                                   const base::Time end_time) {
  operation_ = OP_DOOM_BETWEEN;
  initial_time_ = initial_time;
  end_time_ = end_time;
}

void BackendIO::DoomEntriesSince(const base::Time initial_time) {
  operation_ = OP_DOOM_SINCE;
  initial_time_ = initial_time;
}

void BackendIO::OpenNextEntry(void** iter, Entry** next_entry) {
  operation_ = OP_OPEN_NEXT;
  iter_ptr_ = iter;
  entry_ptr_ = next_entry;
}

void BackendIO::OpenPrevEntry(void** iter, Entry** prev_entry) {
  operation_ = OP_OPEN_PREV;
  iter_ptr_ = iter;
  entry_ptr_ = prev_entry;
}

void BackendIO::EndEnumeration(void* iterator) {
  operation_ = OP_END_ENUMERATION;
  iter_ = iterator;
}

void BackendIO::CloseEntryImpl(EntryImpl* entry) {
  operation_ = OP_CLOSE_ENTRY;
  entry_ = entry;
}

void BackendIO::DoomEntryImpl(EntryImpl* entry) {
  operation_ = OP_DOOM_ENTRY;
  entry_ = entry;
}

void BackendIO::FlushQueue() {
  operation_ = OP_FLUSH_QUEUE;
}

void BackendIO::RunTask(Task* task) {
  operation_ = OP_RUN_TASK;
  task_ = task;
}

void BackendIO::ReadData(EntryImpl* entry, int index, int offset,
                         net::IOBuffer* buf, int buf_len) {
  operation_ = OP_READ;
  entry_ = entry;
  index_ = index;
  offset_ = offset;
  buf_ = buf;
  buf_len_ = buf_len;
}

void BackendIO::WriteData(EntryImpl* entry, int index, int offset,
                          net::IOBuffer* buf, int buf_len, bool truncate) {
  operation_ = OP_WRITE;
  entry_ = entry;
  index_ = index;
  offset_ = offset;
  buf_ = buf;
  buf_len_ = buf_len;
  truncate_ = truncate;
}

void BackendIO::ReadSparseData(EntryImpl* entry, int64 offset,
                               net::IOBuffer* buf, int buf_len) {
  operation_ = OP_READ_SPARSE;
  entry_ = entry;
  offset64_ = offset;
  buf_ = buf;
  buf_len_ = buf_len;
}

void BackendIO::WriteSparseData(EntryImpl* entry, int64 offset,
                                net::IOBuffer* buf, int buf_len) {
  operation_ = OP_WRITE_SPARSE;
  entry_ = entry;
  offset64_ = offset;
  buf_ = buf;
  buf_len_ = buf_len;
}

void BackendIO::GetAvailableRange(EntryImpl* entry, int64 offset, int len,
                                  int64* start) {
  operation_ = OP_GET_RANGE;
  entry_ = entry;
  offset64_ = offset;
  buf_len_ = len;
  start_ = start;
}

void BackendIO::CancelSparseIO(EntryImpl* entry) {
  operation_ = OP_CANCEL_IO;
  entry_ = entry;
}

void BackendIO::ReadyForSparseIO(EntryImpl* entry) {
  operation_ = OP_IS_READY;
  entry_ = entry;
}

BackendIO::~BackendIO() {}

// Runs on the background thread.
void BackendIO::ExecuteBackendOperation() {
  switch (operation_) {
    case OP_INIT:
      result_ = backend_->SyncInit();
      break;
    case OP_OPEN:
      result_ = backend_->SyncOpenEntry(key_, entry_ptr_);
      break;
    case OP_CREATE:
      result_ = backend_->SyncCreateEntry(key_, entry_ptr_);
      break;
    case OP_DOOM:
      result_ = backend_->SyncDoomEntry(key_);
      break;
    case OP_DOOM_ALL:
      result_ = backend_->SyncDoomAllEntries();
      break;
    case OP_DOOM_BETWEEN:
      result_ = backend_->SyncDoomEntriesBetween(initial_time_, end_time_);
      break;
    case OP_DOOM_SINCE:
      result_ = backend_->SyncDoomEntriesSince(initial_time_);
      break;
    case OP_OPEN_NEXT:
      result_ = backend_->SyncOpenNextEntry(iter_ptr_, entry_ptr_);
      break;
    case OP_OPEN_PREV:
      result_ = backend_->SyncOpenPrevEntry(iter_ptr_, entry_ptr_);
      break;
    case OP_END_ENUMERATION:
      backend_->SyncEndEnumeration(iter_);
      result_ = net::OK;
      break;
    case OP_CLOSE_ENTRY:
      entry_->Release();
      result_ = net::OK;
      break;
    case OP_DOOM_ENTRY:
      entry_->DoomImpl();
      result_ = net::OK;
      break;
    case OP_FLUSH_QUEUE:
      result_ = net::OK;
      break;
    case OP_RUN_TASK:
      task_->Run();
      delete task_;
      result_ = net::OK;
      break;
    default:
      NOTREACHED() << "Invalid Operation";
      result_ = net::ERR_UNEXPECTED;
  }
  DCHECK_NE(net::ERR_IO_PENDING, result_);
  controller_->OnIOComplete(this);
}

// Runs on the background thread.
void BackendIO::ExecuteEntryOperation() {
  switch (operation_) {
    case OP_READ:
      result_ = entry_->ReadDataImpl(index_, offset_, buf_, buf_len_,
                                     &my_callback_);
      break;
    case OP_WRITE:
      result_ = entry_->WriteDataImpl(index_, offset_, buf_, buf_len_,
                                      &my_callback_, truncate_);
      break;
    case OP_READ_SPARSE:
      result_ = entry_->ReadSparseDataImpl(offset64_, buf_, buf_len_,
                                           &my_callback_);
      break;
    case OP_WRITE_SPARSE:
      result_ = entry_->WriteSparseDataImpl(offset64_, buf_, buf_len_,
                                            &my_callback_);
      break;
    case OP_GET_RANGE:
      result_ = entry_->GetAvailableRangeImpl(offset64_, buf_len_, start_);
      break;
    case OP_CANCEL_IO:
      entry_->CancelSparseIOImpl();
      result_ = net::OK;
      break;
    case OP_IS_READY:
      result_ = entry_->ReadyForSparseIOImpl(&my_callback_);
      break;
    default:
      NOTREACHED() << "Invalid Operation";
      result_ = net::ERR_UNEXPECTED;
  }
  if (result_ != net::ERR_IO_PENDING)
    controller_->OnIOComplete(this);
}

// ---------------------------------------------------------------------------

InFlightBackendIO::InFlightBackendIO(BackendImpl* backend,
                    base::MessageLoopProxy* background_thread)
    : backend_(backend),
      background_thread_(background_thread),
      queue_entry_ops_(false) {
}

InFlightBackendIO::~InFlightBackendIO() {
}

void InFlightBackendIO::Init(CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->Init();
  QueueOperation(operation);
}

void InFlightBackendIO::OpenEntry(const std::string& key, Entry** entry,
                                  CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->OpenEntry(key, entry);
  QueueOperation(operation);
}

void InFlightBackendIO::CreateEntry(const std::string& key, Entry** entry,
                                    CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->CreateEntry(key, entry);
  QueueOperation(operation);
}

void InFlightBackendIO::DoomEntry(const std::string& key,
                                  CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->DoomEntry(key);
  QueueOperation(operation);
}

void InFlightBackendIO::DoomAllEntries(CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->DoomAllEntries();
  QueueOperation(operation);
}

void InFlightBackendIO::DoomEntriesBetween(const base::Time initial_time,
                        const base::Time end_time,
                        CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->DoomEntriesBetween(initial_time, end_time);
  QueueOperation(operation);
}

void InFlightBackendIO::DoomEntriesSince(const base::Time initial_time,
                                         CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->DoomEntriesSince(initial_time);
  QueueOperation(operation);
}

void InFlightBackendIO::OpenNextEntry(void** iter, Entry** next_entry,
                                      CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->OpenNextEntry(iter, next_entry);
  QueueOperation(operation);
}

void InFlightBackendIO::OpenPrevEntry(void** iter, Entry** prev_entry,
                                      CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->OpenPrevEntry(iter, prev_entry);
  QueueOperation(operation);
}

void InFlightBackendIO::EndEnumeration(void* iterator) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, NULL);
  operation->EndEnumeration(iterator);
  QueueOperation(operation);
}

void InFlightBackendIO::CloseEntryImpl(EntryImpl* entry) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, NULL);
  operation->CloseEntryImpl(entry);
  QueueOperation(operation);
}

void InFlightBackendIO::DoomEntryImpl(EntryImpl* entry) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, NULL);
  operation->DoomEntryImpl(entry);
  QueueOperation(operation);
}

void InFlightBackendIO::FlushQueue(net::CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->FlushQueue();
  QueueOperation(operation);
}

void InFlightBackendIO::RunTask(Task* task, net::CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->RunTask(task);
  QueueOperation(operation);
}

void InFlightBackendIO::ReadData(EntryImpl* entry, int index, int offset,
                                 net::IOBuffer* buf, int buf_len,
                                 CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->ReadData(entry, index, offset, buf, buf_len);
  QueueOperation(operation);
}

void InFlightBackendIO::WriteData(EntryImpl* entry, int index, int offset,
                                  net::IOBuffer* buf, int buf_len,
                                  bool truncate,
                                  CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->WriteData(entry, index, offset, buf, buf_len, truncate);
  QueueOperation(operation);
}

void InFlightBackendIO::ReadSparseData(EntryImpl* entry, int64 offset,
                                       net::IOBuffer* buf, int buf_len,
                                       CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->ReadSparseData(entry, offset, buf, buf_len);
  QueueOperation(operation);
}

void InFlightBackendIO::WriteSparseData(EntryImpl* entry, int64 offset,
                                        net::IOBuffer* buf, int buf_len,
                                        CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->WriteSparseData(entry, offset, buf, buf_len);
  QueueOperation(operation);
}

void InFlightBackendIO::GetAvailableRange(EntryImpl* entry, int64 offset,
                                          int len, int64* start,
                                          CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->GetAvailableRange(entry, offset, len, start);
  QueueOperation(operation);
}

void InFlightBackendIO::CancelSparseIO(EntryImpl* entry) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, NULL);
  operation->CancelSparseIO(entry);
  QueueOperation(operation);
}

void InFlightBackendIO::ReadyForSparseIO(EntryImpl* entry,
                                         CompletionCallback* callback) {
  scoped_refptr<BackendIO> operation = new BackendIO(this, backend_, callback);
  operation->ReadyForSparseIO(entry);
  QueueOperation(operation);
}

void InFlightBackendIO::WaitForPendingIO() {
  // We clear the list first so that we don't post more operations after this
  // point.
  pending_ops_.clear();
  InFlightIO::WaitForPendingIO();
}

void InFlightBackendIO::StartQueingOperations() {
  queue_entry_ops_ = true;
}

void InFlightBackendIO::StopQueingOperations() {
  queue_entry_ops_ = false;
}

void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation,
                                            bool cancel) {
  BackendIO* op = static_cast<BackendIO*>(operation);

  if (!op->IsEntryOperation() && !pending_ops_.empty()) {
    // Process the next request. Note that invoking the callback may result
    // in the backend destruction (and with it this object), so we should deal
    // with the next operation before invoking the callback.
    PostQueuedOperation(&pending_ops_);
  }

  if (op->IsEntryOperation()) {
    backend_->OnOperationCompleted(op->ElapsedTime());
    if (!pending_entry_ops_.empty()) {
      PostQueuedOperation(&pending_entry_ops_);

      // If we are not throttling requests anymore, dispatch the whole queue.
      if (!queue_entry_ops_) {
        CACHE_UMA(COUNTS_10000, "FinalQueuedOperations", 0,
                  pending_entry_ops_.size());
        while (!pending_entry_ops_.empty())
          PostQueuedOperation(&pending_entry_ops_);
      }
    }
  }

  if (op->callback() && (!cancel || op->IsEntryOperation()))
    op->callback()->Run(op->result());

  if (cancel)
    op->ReleaseEntry();
}

void InFlightBackendIO::QueueOperation(BackendIO* operation) {
  if (!operation->IsEntryOperation())
    return QueueOperationToList(operation, &pending_ops_);

  if (!queue_entry_ops_)
    return PostOperation(operation);

  CACHE_UMA(COUNTS_10000, "QueuedOperations", 0, pending_entry_ops_.size());

  QueueOperationToList(operation, &pending_entry_ops_);
}

void InFlightBackendIO::PostOperation(BackendIO* operation) {
  background_thread_->PostTask(FROM_HERE,
      NewRunnableMethod(operation, &BackendIO::ExecuteOperation));
  OnOperationPosted(operation);
}

void InFlightBackendIO::PostQueuedOperation(OperationList* from_list) {
  scoped_refptr<BackendIO> next_op = from_list->front();
  from_list->pop_front();
  PostOperation(next_op);
}

void InFlightBackendIO::QueueOperationToList(BackendIO* operation,
                                             OperationList* list) {
  if (list->empty())
    return PostOperation(operation);

  list->push_back(operation);
}

}  // namespace