/src/mozilla-central/dom/indexedDB/ProfilerHelpers.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef mozilla_dom_indexeddb_profilerhelpers_h__ |
8 | | #define mozilla_dom_indexeddb_profilerhelpers_h__ |
9 | | |
10 | | // This file is not exported and is only meant to be included in IndexedDB |
11 | | // source files. |
12 | | |
13 | | #include "BackgroundChildImpl.h" |
14 | | #include "GeckoProfiler.h" |
15 | | #include "IDBCursor.h" |
16 | | #include "IDBDatabase.h" |
17 | | #include "IDBIndex.h" |
18 | | #include "IDBKeyRange.h" |
19 | | #include "IDBObjectStore.h" |
20 | | #include "IDBTransaction.h" |
21 | | #include "IndexedDatabaseManager.h" |
22 | | #include "Key.h" |
23 | | #include "mozilla/Assertions.h" |
24 | | #include "mozilla/Attributes.h" |
25 | | #include "mozilla/dom/BindingDeclarations.h" |
26 | | #include "mozilla/dom/Event.h" |
27 | | #include "nsDebug.h" |
28 | | #include "nsID.h" |
29 | | #include "nsString.h" |
30 | | #include "mozilla/Logging.h" |
31 | | |
32 | | // Include this last to avoid path problems on Windows. |
33 | | #include "ActorsChild.h" |
34 | | |
35 | | namespace mozilla { |
36 | | namespace dom { |
37 | | namespace indexedDB { |
38 | | |
39 | | class MOZ_STACK_CLASS LoggingIdString final |
40 | | : public nsAutoCStringN<NSID_LENGTH> |
41 | | { |
42 | | public: |
43 | | LoggingIdString() |
44 | 0 | { |
45 | 0 | using mozilla::ipc::BackgroundChildImpl; |
46 | 0 |
|
47 | 0 | if (IndexedDatabaseManager::GetLoggingMode() != |
48 | 0 | IndexedDatabaseManager::Logging_Disabled) { |
49 | 0 | const BackgroundChildImpl::ThreadLocal* threadLocal = |
50 | 0 | BackgroundChildImpl::GetThreadLocalForCurrentThread(); |
51 | 0 | if (threadLocal) { |
52 | 0 | const ThreadLocal* idbThreadLocal = threadLocal->mIndexedDBThreadLocal; |
53 | 0 | if (idbThreadLocal) { |
54 | 0 | Assign(idbThreadLocal->IdString()); |
55 | 0 | } |
56 | 0 | } |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | explicit |
61 | | LoggingIdString(const nsID& aID) |
62 | 0 | { |
63 | 0 | static_assert(NSID_LENGTH > 1, "NSID_LENGTH is set incorrectly!"); |
64 | 0 | static_assert(NSID_LENGTH <= kStorageSize, |
65 | 0 | "nsID string won't fit in our storage!"); |
66 | 0 | // Capacity() excludes the null terminator; NSID_LENGTH includes it. |
67 | 0 | MOZ_ASSERT(Capacity() + 1 == NSID_LENGTH); |
68 | 0 |
|
69 | 0 | if (IndexedDatabaseManager::GetLoggingMode() != |
70 | 0 | IndexedDatabaseManager::Logging_Disabled) { |
71 | 0 | // NSID_LENGTH counts the null terminator, SetLength() does not. |
72 | 0 | SetLength(NSID_LENGTH - 1); |
73 | 0 |
|
74 | 0 | aID.ToProvidedString( |
75 | 0 | *reinterpret_cast<char(*)[NSID_LENGTH]>(BeginWriting())); |
76 | 0 | } |
77 | 0 | } |
78 | | }; |
79 | | |
80 | | class MOZ_STACK_CLASS LoggingString final |
81 | | : public nsAutoCString |
82 | | { |
83 | | static const char kQuote = '\"'; |
84 | | static const char kOpenBracket = '['; |
85 | | static const char kCloseBracket = ']'; |
86 | | static const char kOpenParen = '('; |
87 | | static const char kCloseParen = ')'; |
88 | | |
89 | | public: |
90 | | explicit |
91 | | LoggingString(IDBDatabase* aDatabase) |
92 | | : nsAutoCString(kQuote) |
93 | 0 | { |
94 | 0 | MOZ_ASSERT(aDatabase); |
95 | 0 |
|
96 | 0 | AppendUTF16toUTF8(aDatabase->Name(), *this); |
97 | 0 | Append(kQuote); |
98 | 0 | } |
99 | | |
100 | | explicit |
101 | | LoggingString(IDBTransaction* aTransaction) |
102 | | : nsAutoCString(kOpenBracket) |
103 | 0 | { |
104 | 0 | MOZ_ASSERT(aTransaction); |
105 | 0 |
|
106 | 0 | NS_NAMED_LITERAL_CSTRING(kCommaSpace, ", "); |
107 | 0 |
|
108 | 0 | const nsTArray<nsString>& stores = aTransaction->ObjectStoreNamesInternal(); |
109 | 0 |
|
110 | 0 | for (uint32_t count = stores.Length(), index = 0; index < count; index++) { |
111 | 0 | Append(kQuote); |
112 | 0 | AppendUTF16toUTF8(stores[index], *this); |
113 | 0 | Append(kQuote); |
114 | 0 |
|
115 | 0 | if (index != count - 1) { |
116 | 0 | Append(kCommaSpace); |
117 | 0 | } |
118 | 0 | } |
119 | 0 |
|
120 | 0 | Append(kCloseBracket); |
121 | 0 | Append(kCommaSpace); |
122 | 0 |
|
123 | 0 | switch (aTransaction->GetMode()) { |
124 | 0 | case IDBTransaction::READ_ONLY: |
125 | 0 | AppendLiteral("\"readonly\""); |
126 | 0 | break; |
127 | 0 | case IDBTransaction::READ_WRITE: |
128 | 0 | AppendLiteral("\"readwrite\""); |
129 | 0 | break; |
130 | 0 | case IDBTransaction::READ_WRITE_FLUSH: |
131 | 0 | AppendLiteral("\"readwriteflush\""); |
132 | 0 | break; |
133 | 0 | case IDBTransaction::CLEANUP: |
134 | 0 | AppendLiteral("\"cleanup\""); |
135 | 0 | break; |
136 | 0 | case IDBTransaction::VERSION_CHANGE: |
137 | 0 | AppendLiteral("\"versionchange\""); |
138 | 0 | break; |
139 | 0 | default: |
140 | 0 | MOZ_CRASH("Unknown mode!"); |
141 | 0 | }; |
142 | 0 | } |
143 | | |
144 | | explicit |
145 | | LoggingString(IDBObjectStore* aObjectStore) |
146 | | : nsAutoCString(kQuote) |
147 | 0 | { |
148 | 0 | MOZ_ASSERT(aObjectStore); |
149 | 0 |
|
150 | 0 | AppendUTF16toUTF8(aObjectStore->Name(), *this); |
151 | 0 | Append(kQuote); |
152 | 0 | } |
153 | | |
154 | | explicit |
155 | | LoggingString(IDBIndex* aIndex) |
156 | | : nsAutoCString(kQuote) |
157 | 0 | { |
158 | 0 | MOZ_ASSERT(aIndex); |
159 | 0 |
|
160 | 0 | AppendUTF16toUTF8(aIndex->Name(), *this); |
161 | 0 | Append(kQuote); |
162 | 0 | } |
163 | | |
164 | | explicit |
165 | | LoggingString(IDBKeyRange* aKeyRange) |
166 | 0 | { |
167 | 0 | if (aKeyRange) { |
168 | 0 | if (aKeyRange->IsOnly()) { |
169 | 0 | Assign(LoggingString(aKeyRange->Lower())); |
170 | 0 | } else { |
171 | 0 | if (aKeyRange->LowerOpen()) { |
172 | 0 | Assign(kOpenParen); |
173 | 0 | } else { |
174 | 0 | Assign(kOpenBracket); |
175 | 0 | } |
176 | 0 |
|
177 | 0 | Append(LoggingString(aKeyRange->Lower())); |
178 | 0 | AppendLiteral(", "); |
179 | 0 | Append(LoggingString(aKeyRange->Upper())); |
180 | 0 |
|
181 | 0 | if (aKeyRange->UpperOpen()) { |
182 | 0 | Append(kCloseParen); |
183 | 0 | } else { |
184 | 0 | Append(kCloseBracket); |
185 | 0 | } |
186 | 0 | } |
187 | 0 | } else { |
188 | 0 | AssignLiteral("<undefined>"); |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | explicit |
193 | | LoggingString(const Key& aKey) |
194 | 0 | { |
195 | 0 | if (aKey.IsUnset()) { |
196 | 0 | AssignLiteral("<undefined>"); |
197 | 0 | } else if (aKey.IsFloat()) { |
198 | 0 | AppendPrintf("%g", aKey.ToFloat()); |
199 | 0 | } else if (aKey.IsDate()) { |
200 | 0 | AppendPrintf("<Date %g>", aKey.ToDateMsec()); |
201 | 0 | } else if (aKey.IsString()) { |
202 | 0 | nsAutoString str; |
203 | 0 | aKey.ToString(str); |
204 | 0 | AppendPrintf("\"%s\"", NS_ConvertUTF16toUTF8(str).get()); |
205 | 0 | } else if (aKey.IsBinary()) { |
206 | 0 | AssignLiteral("[object ArrayBuffer]"); |
207 | 0 | } else { |
208 | 0 | MOZ_ASSERT(aKey.IsArray()); |
209 | 0 | AssignLiteral("[...]"); |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | explicit |
214 | | LoggingString(const IDBCursor::Direction aDirection) |
215 | 0 | { |
216 | 0 | switch (aDirection) { |
217 | 0 | case IDBCursor::NEXT: |
218 | 0 | AssignLiteral("\"next\""); |
219 | 0 | break; |
220 | 0 | case IDBCursor::NEXT_UNIQUE: |
221 | 0 | AssignLiteral("\"nextunique\""); |
222 | 0 | break; |
223 | 0 | case IDBCursor::PREV: |
224 | 0 | AssignLiteral("\"prev\""); |
225 | 0 | break; |
226 | 0 | case IDBCursor::PREV_UNIQUE: |
227 | 0 | AssignLiteral("\"prevunique\""); |
228 | 0 | break; |
229 | 0 | default: |
230 | 0 | MOZ_CRASH("Unknown direction!"); |
231 | 0 | }; |
232 | 0 | } |
233 | | |
234 | | explicit |
235 | | LoggingString(const Optional<uint64_t>& aVersion) |
236 | 0 | { |
237 | 0 | if (aVersion.WasPassed()) { |
238 | 0 | AppendInt(aVersion.Value()); |
239 | 0 | } else { |
240 | 0 | AssignLiteral("<undefined>"); |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | | explicit |
245 | | LoggingString(const Optional<uint32_t>& aLimit) |
246 | 0 | { |
247 | 0 | if (aLimit.WasPassed()) { |
248 | 0 | AppendInt(aLimit.Value()); |
249 | 0 | } else { |
250 | 0 | AssignLiteral("<undefined>"); |
251 | 0 | } |
252 | 0 | } |
253 | | |
254 | | LoggingString(IDBObjectStore* aObjectStore, const Key& aKey) |
255 | 0 | { |
256 | 0 | MOZ_ASSERT(aObjectStore); |
257 | 0 |
|
258 | 0 | if (!aObjectStore->HasValidKeyPath()) { |
259 | 0 | Append(LoggingString(aKey)); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | | LoggingString(Event* aEvent, const char16_t* aDefault) |
264 | | : nsAutoCString(kQuote) |
265 | 0 | { |
266 | 0 | MOZ_ASSERT(aDefault); |
267 | 0 |
|
268 | 0 | nsAutoString eventType; |
269 | 0 |
|
270 | 0 | if (aEvent) { |
271 | 0 | aEvent->GetType(eventType); |
272 | 0 | } else { |
273 | 0 | eventType = nsDependentString(aDefault); |
274 | 0 | } |
275 | 0 |
|
276 | 0 | AppendUTF16toUTF8(eventType, *this); |
277 | 0 | Append(kQuote); |
278 | 0 | } |
279 | | }; |
280 | | |
281 | | inline void MOZ_FORMAT_PRINTF(2, 3) |
282 | | LoggingHelper(bool aUseProfiler, const char* aFmt, ...) |
283 | 0 | { |
284 | 0 | MOZ_ASSERT(IndexedDatabaseManager::GetLoggingMode() != |
285 | 0 | IndexedDatabaseManager::Logging_Disabled); |
286 | 0 | MOZ_ASSERT(aFmt); |
287 | 0 |
|
288 | 0 | mozilla::LogModule* logModule = IndexedDatabaseManager::GetLoggingModule(); |
289 | 0 | MOZ_ASSERT(logModule); |
290 | 0 |
|
291 | 0 | static const mozilla::LogLevel logLevel = LogLevel::Warning; |
292 | 0 |
|
293 | 0 | if (MOZ_LOG_TEST(logModule, logLevel) || |
294 | 0 | #ifdef MOZ_GECKO_PROFILER |
295 | 0 | (aUseProfiler && profiler_is_active()) |
296 | | #else |
297 | | false |
298 | | #endif |
299 | 0 | ) { |
300 | 0 | nsAutoCString message; |
301 | 0 |
|
302 | 0 | { |
303 | 0 | va_list args; |
304 | 0 | va_start(args, aFmt); |
305 | 0 |
|
306 | 0 | message.AppendPrintf(aFmt, args); |
307 | 0 |
|
308 | 0 | va_end(args); |
309 | 0 | } |
310 | 0 |
|
311 | 0 | MOZ_LOG(logModule, logLevel, ("%s", message.get())); |
312 | 0 |
|
313 | 0 | if (aUseProfiler) { |
314 | 0 | PROFILER_ADD_MARKER(message.get()); |
315 | 0 | } |
316 | 0 | } |
317 | 0 | } |
318 | | |
319 | | } // namespace indexedDB |
320 | | } // namespace dom |
321 | | } // namespace mozilla |
322 | | |
323 | | #define IDB_LOG_MARK(_detailedFmt, _conciseFmt, ...) \ |
324 | 0 | do { \ |
325 | 0 | using namespace mozilla::dom::indexedDB; \ |
326 | 0 | \ |
327 | 0 | const IndexedDatabaseManager::LoggingMode mode = \ |
328 | 0 | IndexedDatabaseManager::GetLoggingMode(); \ |
329 | 0 | \ |
330 | 0 | if (mode != IndexedDatabaseManager::Logging_Disabled) { \ |
331 | 0 | const char* _fmt; \ |
332 | 0 | if (mode == IndexedDatabaseManager::Logging_Concise || \ |
333 | 0 | mode == IndexedDatabaseManager::Logging_ConciseProfilerMarks) { \ |
334 | 0 | _fmt = _conciseFmt; \ |
335 | 0 | } else { \ |
336 | 0 | MOZ_ASSERT( \ |
337 | 0 | mode == IndexedDatabaseManager::Logging_Detailed || \ |
338 | 0 | mode == IndexedDatabaseManager::Logging_DetailedProfilerMarks); \ |
339 | 0 | _fmt = _detailedFmt; \ |
340 | 0 | } \ |
341 | 0 | \ |
342 | 0 | const bool _useProfiler = \ |
343 | 0 | mode == IndexedDatabaseManager::Logging_ConciseProfilerMarks || \ |
344 | 0 | mode == IndexedDatabaseManager::Logging_DetailedProfilerMarks; \ |
345 | 0 | \ |
346 | 0 | LoggingHelper(_useProfiler, _fmt, ##__VA_ARGS__); \ |
347 | 0 | } \ |
348 | 0 | } while (0) |
349 | | |
350 | | #define IDB_LOG_ID_STRING(...) \ |
351 | 0 | mozilla::dom::indexedDB::LoggingIdString(__VA_ARGS__).get() |
352 | | |
353 | | #define IDB_LOG_STRINGIFY(...) \ |
354 | 0 | mozilla::dom::indexedDB::LoggingString(__VA_ARGS__).get() |
355 | | |
356 | | #endif // mozilla_dom_indexeddb_profilerhelpers_h__ |