OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/disk_cache/tracing_cache_backend.h" | |
6 | |
7 #include "net/base/net_errors.h" | |
8 | |
9 namespace disk_cache { | |
10 | |
11 TracingCacheBackend::TracingCacheBackend(Backend* backend) | |
12 : backend_(backend) { | |
13 } | |
14 | |
15 TracingCacheBackend::~TracingCacheBackend() { | |
16 } | |
17 | |
18 net::CacheType TracingCacheBackend::GetCacheType() const { | |
19 return backend_->GetCacheType(); | |
20 } | |
21 | |
22 int32 TracingCacheBackend::GetEntryCount() const { | |
23 return backend_->GetEntryCount(); | |
24 } | |
25 | |
26 class EntryProxy : public Entry, | |
rvargas (doing something else)
2013/04/05 19:25:13
don't declare a class in the middle of the definit
pasko-google - do not use
2013/04/09 11:53:17
Done.
| |
27 public base::RefCounted<EntryProxy> { | |
28 friend class base::RefCounted<EntryProxy>; | |
29 public: | |
30 EntryProxy(Entry *entry, TracingCacheBackend* be) | |
rvargas (doing something else)
2013/04/05 19:25:13
nit: backend
pasko-google - do not use
2013/04/09 11:53:17
Done.
| |
31 : entry_(entry), | |
32 backend_(be->AsWeakPtr()) { | |
33 } | |
34 virtual void Doom() OVERRIDE { | |
35 // TODO(pasko): Record the event. | |
36 entry_->Doom(); | |
37 } | |
38 virtual void Close() OVERRIDE { | |
39 // TODO(pasko): Record the event. | |
40 entry_->Close(); | |
41 Release(); | |
42 } | |
43 virtual std::string GetKey() const OVERRIDE { | |
rvargas (doing something else)
2013/04/05 19:25:13
why not track everything? To avoid overhead while
pasko-google - do not use
2013/04/08 15:37:40
Yes, I did not measure the CPU/memory overhead yet
| |
44 return entry_->GetKey(); | |
45 } | |
46 virtual base::Time GetLastUsed() const OVERRIDE { | |
47 return entry_->GetLastUsed(); | |
48 } | |
49 virtual base::Time GetLastModified() const OVERRIDE { | |
50 return entry_->GetLastModified(); | |
51 } | |
52 virtual int32 GetDataSize(int index) const OVERRIDE { | |
53 return entry_->GetDataSize(index); | |
54 } | |
55 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, | |
56 const CompletionCallback& callback) OVERRIDE { | |
57 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
rvargas (doing something else)
2013/04/05 19:25:13
separate class definition from class declaration.
pasko-google - do not use
2013/04/09 11:53:17
Done.
| |
58 RwOpExtra extra; | |
59 extra.index = index; | |
60 extra.offset = offset; | |
61 extra.buf_len = buf_len; | |
62 extra.truncate = false; | |
63 int rv = entry_->ReadData(index, offset, buf, buf_len, | |
64 base::Bind(&EntryProxy::EntryOpComplete, base::Unretained(this), | |
rvargas (doing something else)
2013/04/05 19:25:13
nit: indentation
rvargas (doing something else)
2013/04/05 19:25:13
Cannot use Unretained here
pasko-google - do not use
2013/04/09 11:53:17
Done.
| |
65 start_time, BackendIO::OP_READ, extra, callback)); | |
66 if (rv != net::ERR_IO_PENDING) { | |
67 RecordEvent(start_time, BackendIO::OP_READ, extra, rv); | |
68 } | |
69 return rv; | |
70 } | |
71 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, | |
72 const CompletionCallback& callback, | |
73 bool truncate) OVERRIDE { | |
74 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
75 RwOpExtra extra; | |
76 extra.index = index; | |
77 extra.offset = offset; | |
78 extra.buf_len = buf_len; | |
79 extra.truncate = truncate; | |
80 int rv = entry_->WriteData(index, offset, buf, buf_len, | |
81 base::Bind(&EntryProxy::EntryOpComplete, base::Unretained(this), | |
82 start_time, BackendIO::OP_WRITE, extra, callback), | |
83 truncate); | |
84 if (rv != net::ERR_IO_PENDING) { | |
85 RecordEvent(start_time, BackendIO::OP_WRITE, extra, rv); | |
86 } | |
87 return rv; | |
88 } | |
89 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
90 const CompletionCallback& callback) OVERRIDE { | |
91 return entry_->ReadSparseData(offset, buf, buf_len, callback); | |
rvargas (doing something else)
2013/04/05 19:25:13
definitely track.
pasko-google - do not use
2013/04/09 11:53:17
Added a TODO.
| |
92 } | |
93 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
94 const CompletionCallback& callback) OVERRIDE { | |
95 return entry_->WriteSparseData(offset, buf, buf_len, callback); | |
96 } | |
97 virtual int GetAvailableRange(int64 offset, int len, int64* start, | |
98 const CompletionCallback& callback) OVERRIDE { | |
99 return entry_->GetAvailableRange(offset, len, start, callback); | |
100 } | |
101 virtual bool CouldBeSparse() const OVERRIDE { | |
102 return entry_->CouldBeSparse(); | |
103 } | |
104 virtual void CancelSparseIO() OVERRIDE { | |
105 return entry_->CancelSparseIO(); | |
106 } | |
107 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE { | |
108 return entry_->ReadyForSparseIO(callback); | |
109 } | |
110 private: | |
111 struct RwOpExtra { | |
112 int index; | |
113 int offset; | |
114 int buf_len; | |
115 bool truncate; | |
116 }; | |
117 | |
118 void RecordEvent(int64 start_time, BackendIO::Operation op, RwOpExtra extra, | |
119 int rv) { | |
120 // TODO(pasko): Implement. | |
121 } | |
122 | |
123 void EntryOpComplete(int64 start_time, BackendIO::Operation op, | |
124 RwOpExtra extra, | |
125 const CompletionCallback& cb, int rv) { | |
126 RecordEvent(start_time, op, extra, rv); | |
127 if (!cb.is_null()) { | |
128 cb.Run(rv); | |
129 } | |
130 } | |
131 | |
132 virtual ~EntryProxy() { | |
133 if (backend_.get()) { | |
134 backend_->OnDeleteEntry(entry_); | |
135 } | |
136 } | |
137 | |
138 Entry* entry_; | |
139 // Shared with TracingCacheBackend. | |
140 base::WeakPtr<TracingCacheBackend> backend_; | |
141 | |
142 DISALLOW_COPY_AND_ASSIGN(EntryProxy); | |
143 }; | |
144 | |
145 void TracingCacheBackend::RecordEvent(int64 start_time, | |
146 BackendIO::Operation op, | |
147 std::string key, | |
148 Entry** entry, | |
rvargas (doing something else)
2013/04/05 19:25:13
Why Entry** and not Entry* (this method should not
pasko-google - do not use
2013/04/09 11:53:17
Just copied arguments from another function withou
| |
149 int rv) { | |
150 // TODO(pasko): Implement. | |
151 } | |
152 | |
153 EntryProxy* TracingCacheBackend::FindOrCreateEntryProxy(Entry* entry) { | |
154 EntryProxy* e; | |
rvargas (doing something else)
2013/04/05 19:25:13
nit: don't use single letter variable names (for a
pasko-google - do not use
2013/04/09 11:53:17
Done.
| |
155 if (open_entries_.find(entry) != open_entries_.end()) { | |
156 e = open_entries_[entry]; | |
rvargas (doing something else)
2013/04/05 19:25:13
don't search twice
pasko-google - do not use
2013/04/09 11:53:17
Done.
| |
157 e->AddRef(); | |
158 return e; | |
159 } | |
160 e = new EntryProxy(entry, this); | |
161 e->AddRef(); | |
162 open_entries_[entry] = e; | |
163 return e; | |
164 } | |
165 | |
166 void TracingCacheBackend::OnDeleteEntry(Entry* e) { | |
167 EntryToProxyMap::iterator it = open_entries_.find(e); | |
168 if (it != open_entries_.end()) { | |
169 open_entries_.erase(it); | |
170 } | |
171 } | |
172 | |
173 void TracingCacheBackend::BackendOpComplete(int64 start_time, | |
174 BackendIO::Operation op, | |
175 std::string key, | |
176 Entry** entry, | |
177 const CompletionCallback& callback, | |
178 int rv) { | |
179 RecordEvent(start_time, op, key, entry, rv); | |
180 if (*entry) { | |
181 *entry = FindOrCreateEntryProxy(*entry); | |
182 } | |
183 if (!callback.is_null()) { | |
184 callback.Run(rv); | |
185 } | |
186 } | |
187 | |
188 net::CompletionCallback TracingCacheBackend::BindCompletion( | |
189 BackendIO::Operation op, int64 start_time, const std::string& key, | |
190 Entry **entry, const net::CompletionCallback& cb) { | |
191 DCHECK(entry == NULL || *entry == NULL); | |
rvargas (doing something else)
2013/04/05 19:25:13
why *entry == null ? that's not part of the contra
pasko-google - do not use
2013/04/09 11:53:17
I was checking this invariant, but seems like it i
| |
192 return base::Bind(&TracingCacheBackend::BackendOpComplete, | |
rvargas (doing something else)
2013/04/05 19:25:13
nit: A method with a single line of implementation
pasko-google - do not use
2013/04/09 11:53:17
This would not work for, say, OpenEntry that retur
rvargas (doing something else)
2013/04/09 18:54:01
What would not work?
pasko-google - do not use
2013/04/10 17:06:38
in OpenEntry() we use start_time for two cases:
1.
| |
193 base::Unretained(this), start_time, op, key, entry, cb); | |
rvargas (doing something else)
2013/04/05 19:25:13
nit: indent under first arg
pasko-google - do not use
2013/04/09 11:53:17
Done.
| |
194 } | |
195 | |
196 int TracingCacheBackend::OpenEntry(const std::string& key, Entry** entry, | |
197 const CompletionCallback& callback) { | |
198 DCHECK(*entry == NULL); | |
199 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
200 int rv = backend_->OpenEntry(key, entry, BindCompletion(BackendIO::OP_OPEN, | |
201 start_time, key, | |
202 entry, callback)); | |
203 if (rv != net::ERR_IO_PENDING) { | |
204 RecordEvent(start_time, BackendIO::OP_OPEN, key, entry, rv); | |
205 if (*entry) { | |
206 *entry = FindOrCreateEntryProxy(*entry); | |
207 } | |
208 } | |
209 return rv; | |
210 } | |
211 | |
212 int TracingCacheBackend::CreateEntry(const std::string& key, Entry** entry, | |
213 const CompletionCallback& callback) { | |
214 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
rvargas (doing something else)
2013/04/05 19:25:13
why are you using int64 instead of Time?
pasko-google - do not use
2013/04/09 11:53:17
Because passing an object by value feels icky. Sur
rvargas (doing something else)
2013/04/09 18:54:01
That's not the convention on Chrome. Time was spec
pasko-google - do not use
2013/04/10 17:06:38
Done.
| |
215 int rv = backend_->CreateEntry( | |
216 key, entry, BindCompletion(BackendIO::OP_CREATE, start_time, key, entry, | |
rvargas (doing something else)
2013/04/05 19:25:13
BackendIO is going away... I would not recommend u
pasko-google - do not use
2013/04/08 15:37:40
I could create a different enum, though some enum
rvargas (doing something else)
2013/04/08 18:28:34
... and that show that the constants were never me
pasko-google - do not use
2013/04/09 11:53:17
I created an independent enum. Just copied. Some o
| |
217 callback)); | |
218 if (rv != net::ERR_IO_PENDING) { | |
219 RecordEvent(start_time, BackendIO::OP_CREATE, key, entry, rv); | |
220 if (*entry) { | |
221 *entry = FindOrCreateEntryProxy(*entry); | |
222 } | |
223 } | |
224 return rv; | |
225 } | |
226 | |
227 int TracingCacheBackend::DoomEntry(const std::string& key, | |
228 const CompletionCallback& callback) { | |
229 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
230 int rv = backend_->DoomEntry(key, BindCompletion(BackendIO::OP_DOOM_ENTRY, | |
231 start_time, key, NULL, | |
232 callback)); | |
233 if (rv != net::ERR_IO_PENDING) { | |
234 Entry* e; | |
rvargas (doing something else)
2013/04/05 19:25:13
ditto
pasko-google - do not use
2013/04/09 11:53:17
Done.
| |
235 RecordEvent(start_time, BackendIO::OP_DOOM_ENTRY, key, &e, rv); | |
236 } | |
237 return rv; | |
238 } | |
239 | |
240 int TracingCacheBackend::DoomAllEntries(const CompletionCallback& callback) { | |
241 return backend_->DoomAllEntries(callback); | |
rvargas (doing something else)
2013/04/05 19:25:13
why are we not tracking this? (and the other calls
pasko-google - do not use
2013/04/08 15:37:40
I was not measuring it in the initial experiments
| |
242 } | |
243 | |
244 int TracingCacheBackend::DoomEntriesBetween(base::Time initial_time, | |
245 base::Time end_time, | |
246 const CompletionCallback& cb) { | |
247 return backend_->DoomEntriesBetween(initial_time, end_time, cb); | |
248 } | |
249 | |
250 int TracingCacheBackend::DoomEntriesSince(base::Time initial_time, | |
251 const CompletionCallback& callback) { | |
252 return backend_->DoomEntriesSince(initial_time, callback); | |
253 } | |
254 | |
255 int TracingCacheBackend::OpenNextEntry(void** iter, Entry** next_entry, | |
256 const CompletionCallback& callback) { | |
257 return backend_->OpenNextEntry(iter, next_entry, callback); | |
258 } | |
259 | |
260 void TracingCacheBackend::EndEnumeration(void** iter) { | |
261 return backend_->EndEnumeration(iter); | |
262 } | |
263 | |
264 void TracingCacheBackend::GetStats(StatsItems* stats) { | |
265 return backend_->GetStats(stats); | |
266 } | |
267 | |
268 void TracingCacheBackend::OnExternalCacheHit(const std::string& key) { | |
269 return backend_->OnExternalCacheHit(key); | |
270 } | |
271 | |
272 } // namespace disk_cache | |
OLD | NEW |