summaryrefslogtreecommitdiff
path: root/chromium/net/disk_cache/tracing_cache_backend.cc
blob: 4966133f00db9073a40db40b8ff6f7fdbabdeef4 (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
// Copyright (c) 2013 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/tracing_cache_backend.h"

#include "net/base/net_errors.h"

namespace disk_cache {

// Proxies entry objects created by the real underlying backend. Backend users
// will only see the proxy entries. It is necessary for recording the backend
// operations since often non-trivial work is invoked directly on entries.
class EntryProxy : public Entry, public base::RefCountedThreadSafe<EntryProxy> {
 public:
  EntryProxy(Entry *entry, TracingCacheBackend* backend);
  virtual void Doom() OVERRIDE;
  virtual void Close() OVERRIDE;
  virtual std::string GetKey() const OVERRIDE;
  virtual base::Time GetLastUsed() const OVERRIDE;
  virtual base::Time GetLastModified() const OVERRIDE;
  virtual int32 GetDataSize(int index) const OVERRIDE;
  virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len,
                       const CompletionCallback& callback) OVERRIDE;
  virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len,
                        const CompletionCallback& callback,
                        bool truncate) OVERRIDE;
  virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
                             const CompletionCallback& callback) OVERRIDE;
  virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
                              const CompletionCallback& callback) OVERRIDE;
  virtual int GetAvailableRange(int64 offset, int len, int64* start,
                                const CompletionCallback& callback) OVERRIDE;
  virtual bool CouldBeSparse() const OVERRIDE;
  virtual void CancelSparseIO() OVERRIDE;
  virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE;

 private:
  friend class base::RefCountedThreadSafe<EntryProxy>;
  typedef TracingCacheBackend::Operation Operation;
  virtual ~EntryProxy();

  struct RwOpExtra {
    int index;
    int offset;
    int buf_len;
    bool truncate;
  };

  void RecordEvent(base::TimeTicks start_time, Operation op, RwOpExtra extra,
                   int result_to_record);
  void EntryOpComplete(base::TimeTicks start_time, Operation op,
                       RwOpExtra extra, const CompletionCallback& cb,
                       int result);
  Entry* entry_;
  base::WeakPtr<TracingCacheBackend> backend_;

  DISALLOW_COPY_AND_ASSIGN(EntryProxy);
};

EntryProxy::EntryProxy(Entry *entry, TracingCacheBackend* backend)
  : entry_(entry),
    backend_(backend->AsWeakPtr()) {
}

void EntryProxy::Doom() {
  // TODO(pasko): Record the event.
  entry_->Doom();
}

void EntryProxy::Close() {
  // TODO(pasko): Record the event.
  entry_->Close();
  Release();
}

std::string EntryProxy::GetKey() const {
  return entry_->GetKey();
}

base::Time EntryProxy::GetLastUsed() const {
  return entry_->GetLastUsed();
}

base::Time EntryProxy::GetLastModified() const {
  return entry_->GetLastModified();
}

int32 EntryProxy::GetDataSize(int index) const {
  return entry_->GetDataSize(index);
}

int EntryProxy::ReadData(int index, int offset, IOBuffer* buf, int buf_len,
                         const CompletionCallback& callback) {
  base::TimeTicks start_time = base::TimeTicks::Now();
  RwOpExtra extra;
  extra.index = index;
  extra.offset = offset;
  extra.buf_len = buf_len;
  extra.truncate = false;
  int rv = entry_->ReadData(
      index, offset, buf, buf_len,
      base::Bind(&EntryProxy::EntryOpComplete, this, start_time,
                 TracingCacheBackend::OP_READ, extra, callback));
  if (rv != net::ERR_IO_PENDING) {
    RecordEvent(start_time, TracingCacheBackend::OP_READ, extra, rv);
  }
  return rv;
}

int EntryProxy::WriteData(int index, int offset, IOBuffer* buf, int buf_len,
                      const CompletionCallback& callback,
                      bool truncate) {
  base::TimeTicks start_time = base::TimeTicks::Now();
  RwOpExtra extra;
  extra.index = index;
  extra.offset = offset;
  extra.buf_len = buf_len;
  extra.truncate = truncate;
  int rv = entry_->WriteData(index, offset, buf, buf_len,
      base::Bind(&EntryProxy::EntryOpComplete, this, start_time,
                 TracingCacheBackend::OP_WRITE, extra, callback),
      truncate);
  if (rv != net::ERR_IO_PENDING) {
    RecordEvent(start_time, TracingCacheBackend::OP_WRITE, extra, rv);
  }
  return rv;
}

int EntryProxy::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
                           const CompletionCallback& callback) {
  // TODO(pasko): Record the event.
  return entry_->ReadSparseData(offset, buf, buf_len, callback);
}

int EntryProxy::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
                            const CompletionCallback& callback) {
  // TODO(pasko): Record the event.
  return entry_->WriteSparseData(offset, buf, buf_len, callback);
}

int EntryProxy::GetAvailableRange(int64 offset, int len, int64* start,
                              const CompletionCallback& callback) {
  return entry_->GetAvailableRange(offset, len, start, callback);
}

bool EntryProxy::CouldBeSparse() const {
  return entry_->CouldBeSparse();
}

void EntryProxy::CancelSparseIO() {
  return entry_->CancelSparseIO();
}

int EntryProxy::ReadyForSparseIO(const CompletionCallback& callback) {
  return entry_->ReadyForSparseIO(callback);
}

void EntryProxy::RecordEvent(base::TimeTicks start_time, Operation op,
                             RwOpExtra extra, int result_to_record) {
  // TODO(pasko): Implement.
}

void EntryProxy::EntryOpComplete(base::TimeTicks start_time, Operation op,
                                 RwOpExtra extra, const CompletionCallback& cb,
                                 int result) {
  RecordEvent(start_time, op, extra, result);
  if (!cb.is_null()) {
    cb.Run(result);
  }
}

EntryProxy::~EntryProxy() {
  if (backend_.get()) {
    backend_->OnDeleteEntry(entry_);
  }
}

TracingCacheBackend::TracingCacheBackend(scoped_ptr<Backend> backend)
  : backend_(backend.Pass()) {
}

TracingCacheBackend::~TracingCacheBackend() {
}

net::CacheType TracingCacheBackend::GetCacheType() const {
  return backend_->GetCacheType();
}

int32 TracingCacheBackend::GetEntryCount() const {
  return backend_->GetEntryCount();
}

void TracingCacheBackend::RecordEvent(base::TimeTicks start_time, Operation op,
                                      std::string key, Entry* entry, int rv) {
  // TODO(pasko): Implement.
}

EntryProxy* TracingCacheBackend::FindOrCreateEntryProxy(Entry* entry) {
  EntryProxy* entry_proxy;
  EntryToProxyMap::iterator it = open_entries_.find(entry);
  if (it != open_entries_.end()) {
    entry_proxy = it->second;
    entry_proxy->AddRef();
    return entry_proxy;
  }
  entry_proxy = new EntryProxy(entry, this);
  entry_proxy->AddRef();
  open_entries_[entry] = entry_proxy;
  return entry_proxy;
}

void TracingCacheBackend::OnDeleteEntry(Entry* entry) {
  EntryToProxyMap::iterator it = open_entries_.find(entry);
  if (it != open_entries_.end()) {
    open_entries_.erase(it);
  }
}

void TracingCacheBackend::BackendOpComplete(base::TimeTicks start_time,
                                            Operation op,
                                            std::string key,
                                            Entry** entry,
                                            const CompletionCallback& callback,
                                            int result) {
  RecordEvent(start_time, op, key, *entry, result);
  if (*entry) {
    *entry = FindOrCreateEntryProxy(*entry);
  }
  if (!callback.is_null()) {
    callback.Run(result);
  }
}

net::CompletionCallback TracingCacheBackend::BindCompletion(
    Operation op, base::TimeTicks start_time, const std::string& key,
    Entry **entry, const net::CompletionCallback& cb) {
  return base::Bind(&TracingCacheBackend::BackendOpComplete,
                    AsWeakPtr(), start_time, op, key, entry, cb);
}

int TracingCacheBackend::OpenEntry(const std::string& key, Entry** entry,
                                   const CompletionCallback& callback) {
  DCHECK(*entry == NULL);
  base::TimeTicks start_time = base::TimeTicks::Now();
  int rv = backend_->OpenEntry(key, entry,
                               BindCompletion(OP_OPEN, start_time, key, entry,
                                              callback));
  if (rv != net::ERR_IO_PENDING) {
    RecordEvent(start_time, OP_OPEN, key, *entry, rv);
    if (*entry) {
      *entry = FindOrCreateEntryProxy(*entry);
    }
  }
  return rv;
}

int TracingCacheBackend::CreateEntry(const std::string& key, Entry** entry,
                                     const CompletionCallback& callback) {
  base::TimeTicks start_time = base::TimeTicks::Now();
  int rv = backend_->CreateEntry(key, entry,
                                 BindCompletion(OP_CREATE, start_time, key,
                                                entry, callback));
  if (rv != net::ERR_IO_PENDING) {
    RecordEvent(start_time, OP_CREATE, key, *entry, rv);
    if (*entry) {
      *entry = FindOrCreateEntryProxy(*entry);
    }
  }
  return rv;
}

int TracingCacheBackend::DoomEntry(const std::string& key,
                                   const CompletionCallback& callback) {
  base::TimeTicks start_time = base::TimeTicks::Now();
  int rv = backend_->DoomEntry(key, BindCompletion(OP_DOOM_ENTRY,
                                                   start_time, key, NULL,
                                                   callback));
  if (rv != net::ERR_IO_PENDING) {
    RecordEvent(start_time, OP_DOOM_ENTRY, key, NULL, rv);
  }
  return rv;
}

int TracingCacheBackend::DoomAllEntries(const CompletionCallback& callback) {
  return backend_->DoomAllEntries(callback);
}

int TracingCacheBackend::DoomEntriesBetween(base::Time initial_time,
                                            base::Time end_time,
                                            const CompletionCallback& cb) {
  return backend_->DoomEntriesBetween(initial_time, end_time, cb);
}

int TracingCacheBackend::DoomEntriesSince(base::Time initial_time,
                                          const CompletionCallback& callback) {
  return backend_->DoomEntriesSince(initial_time, callback);
}

int TracingCacheBackend::OpenNextEntry(void** iter, Entry** next_entry,
                                       const CompletionCallback& callback) {
  return backend_->OpenNextEntry(iter, next_entry, callback);
}

void TracingCacheBackend::EndEnumeration(void** iter) {
  return backend_->EndEnumeration(iter);
}

void TracingCacheBackend::GetStats(StatsItems* stats) {
  return backend_->GetStats(stats);
}

void TracingCacheBackend::OnExternalCacheHit(const std::string& key) {
  return backend_->OnExternalCacheHit(key);
}

}  // namespace disk_cache