Source code

Revision control

Other Tools

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
public:
42
LoggingIdString() {
43
using mozilla::ipc::BackgroundChildImpl;
44
45
if (IndexedDatabaseManager::GetLoggingMode() !=
46
IndexedDatabaseManager::Logging_Disabled) {
47
const BackgroundChildImpl::ThreadLocal* threadLocal =
48
BackgroundChildImpl::GetThreadLocalForCurrentThread();
49
if (threadLocal) {
50
const ThreadLocal* idbThreadLocal = threadLocal->mIndexedDBThreadLocal;
51
if (idbThreadLocal) {
52
Assign(idbThreadLocal->IdString());
53
}
54
}
55
}
56
}
57
58
explicit LoggingIdString(const nsID& aID) {
59
static_assert(NSID_LENGTH > 1, "NSID_LENGTH is set incorrectly!");
60
static_assert(NSID_LENGTH <= kStorageSize,
61
"nsID string won't fit in our storage!");
62
// Capacity() excludes the null terminator; NSID_LENGTH includes it.
63
MOZ_ASSERT(Capacity() + 1 == NSID_LENGTH);
64
65
if (IndexedDatabaseManager::GetLoggingMode() !=
66
IndexedDatabaseManager::Logging_Disabled) {
67
// NSID_LENGTH counts the null terminator, SetLength() does not.
68
SetLength(NSID_LENGTH - 1);
69
70
aID.ToProvidedString(
71
*reinterpret_cast<char(*)[NSID_LENGTH]>(BeginWriting()));
72
}
73
}
74
};
75
76
class MOZ_STACK_CLASS LoggingString final : public nsAutoCString {
77
static const char kQuote = '\"';
78
static const char kOpenBracket = '[';
79
static const char kCloseBracket = ']';
80
static const char kOpenParen = '(';
81
static const char kCloseParen = ')';
82
83
public:
84
explicit LoggingString(IDBDatabase* aDatabase) : nsAutoCString(kQuote) {
85
MOZ_ASSERT(aDatabase);
86
87
AppendUTF16toUTF8(aDatabase->Name(), *this);
88
Append(kQuote);
89
}
90
91
explicit LoggingString(IDBTransaction* aTransaction)
92
: nsAutoCString(kOpenBracket) {
93
MOZ_ASSERT(aTransaction);
94
95
NS_NAMED_LITERAL_CSTRING(kCommaSpace, ", ");
96
97
const nsTArray<nsString>& stores = aTransaction->ObjectStoreNamesInternal();
98
99
for (uint32_t count = stores.Length(), index = 0; index < count; index++) {
100
Append(kQuote);
101
AppendUTF16toUTF8(stores[index], *this);
102
Append(kQuote);
103
104
if (index != count - 1) {
105
Append(kCommaSpace);
106
}
107
}
108
109
Append(kCloseBracket);
110
Append(kCommaSpace);
111
112
switch (aTransaction->GetMode()) {
113
case IDBTransaction::Mode::ReadOnly:
114
AppendLiteral("\"readonly\"");
115
break;
116
case IDBTransaction::Mode::ReadWrite:
117
AppendLiteral("\"readwrite\"");
118
break;
119
case IDBTransaction::Mode::ReadWriteFlush:
120
AppendLiteral("\"readwriteflush\"");
121
break;
122
case IDBTransaction::Mode::Cleanup:
123
AppendLiteral("\"cleanup\"");
124
break;
125
case IDBTransaction::Mode::VersionChange:
126
AppendLiteral("\"versionchange\"");
127
break;
128
default:
129
MOZ_CRASH("Unknown mode!");
130
};
131
}
132
133
explicit LoggingString(IDBObjectStore* aObjectStore) : nsAutoCString(kQuote) {
134
MOZ_ASSERT(aObjectStore);
135
136
AppendUTF16toUTF8(aObjectStore->Name(), *this);
137
Append(kQuote);
138
}
139
140
explicit LoggingString(IDBIndex* aIndex) : nsAutoCString(kQuote) {
141
MOZ_ASSERT(aIndex);
142
143
AppendUTF16toUTF8(aIndex->Name(), *this);
144
Append(kQuote);
145
}
146
147
explicit LoggingString(IDBKeyRange* aKeyRange) {
148
if (aKeyRange) {
149
if (aKeyRange->IsOnly()) {
150
Assign(LoggingString(aKeyRange->Lower()));
151
} else {
152
if (aKeyRange->LowerOpen()) {
153
Assign(kOpenParen);
154
} else {
155
Assign(kOpenBracket);
156
}
157
158
Append(LoggingString(aKeyRange->Lower()));
159
AppendLiteral(", ");
160
Append(LoggingString(aKeyRange->Upper()));
161
162
if (aKeyRange->UpperOpen()) {
163
Append(kCloseParen);
164
} else {
165
Append(kCloseBracket);
166
}
167
}
168
} else {
169
AssignLiteral("<undefined>");
170
}
171
}
172
173
explicit LoggingString(const Key& aKey) {
174
if (aKey.IsUnset()) {
175
AssignLiteral("<undefined>");
176
} else if (aKey.IsFloat()) {
177
AppendPrintf("%g", aKey.ToFloat());
178
} else if (aKey.IsDate()) {
179
AppendPrintf("<Date %g>", aKey.ToDateMsec());
180
} else if (aKey.IsString()) {
181
nsAutoString str;
182
aKey.ToString(str);
183
AppendPrintf("\"%s\"", NS_ConvertUTF16toUTF8(str).get());
184
} else if (aKey.IsBinary()) {
185
AssignLiteral("[object ArrayBuffer]");
186
} else {
187
MOZ_ASSERT(aKey.IsArray());
188
AssignLiteral("[...]");
189
}
190
}
191
192
explicit LoggingString(const IDBCursor::Direction aDirection) {
193
switch (aDirection) {
194
case IDBCursor::NEXT:
195
AssignLiteral("\"next\"");
196
break;
197
case IDBCursor::NEXT_UNIQUE:
198
AssignLiteral("\"nextunique\"");
199
break;
200
case IDBCursor::PREV:
201
AssignLiteral("\"prev\"");
202
break;
203
case IDBCursor::PREV_UNIQUE:
204
AssignLiteral("\"prevunique\"");
205
break;
206
default:
207
MOZ_CRASH("Unknown direction!");
208
};
209
}
210
211
explicit LoggingString(const Optional<uint64_t>& aVersion) {
212
if (aVersion.WasPassed()) {
213
AppendInt(aVersion.Value());
214
} else {
215
AssignLiteral("<undefined>");
216
}
217
}
218
219
explicit LoggingString(const Optional<uint32_t>& aLimit) {
220
if (aLimit.WasPassed()) {
221
AppendInt(aLimit.Value());
222
} else {
223
AssignLiteral("<undefined>");
224
}
225
}
226
227
LoggingString(IDBObjectStore* aObjectStore, const Key& aKey) {
228
MOZ_ASSERT(aObjectStore);
229
230
if (!aObjectStore->HasValidKeyPath()) {
231
Append(LoggingString(aKey));
232
}
233
}
234
235
LoggingString(Event* aEvent, const char16_t* aDefault)
236
: nsAutoCString(kQuote) {
237
MOZ_ASSERT(aDefault);
238
239
nsAutoString eventType;
240
241
if (aEvent) {
242
aEvent->GetType(eventType);
243
} else {
244
eventType = nsDependentString(aDefault);
245
}
246
247
AppendUTF16toUTF8(eventType, *this);
248
Append(kQuote);
249
}
250
};
251
252
inline void MOZ_FORMAT_PRINTF(2, 3)
253
LoggingHelper(bool aUseProfiler, const char* aFmt, ...) {
254
MOZ_ASSERT(IndexedDatabaseManager::GetLoggingMode() !=
255
IndexedDatabaseManager::Logging_Disabled);
256
MOZ_ASSERT(aFmt);
257
258
mozilla::LogModule* logModule = IndexedDatabaseManager::GetLoggingModule();
259
MOZ_ASSERT(logModule);
260
261
static const mozilla::LogLevel logLevel = LogLevel::Warning;
262
263
if (MOZ_LOG_TEST(logModule, logLevel) ||
264
#ifdef MOZ_GECKO_PROFILER
265
(aUseProfiler && profiler_thread_is_being_profiled())
266
#else
267
false
268
#endif
269
) {
270
nsAutoCString message;
271
272
{
273
va_list args;
274
va_start(args, aFmt);
275
276
message.AppendPrintf(aFmt, args);
277
278
va_end(args);
279
}
280
281
MOZ_LOG(logModule, logLevel, ("%s", message.get()));
282
283
if (aUseProfiler) {
284
PROFILER_ADD_MARKER(message.get(), DOM);
285
}
286
}
287
}
288
289
} // namespace indexedDB
290
} // namespace dom
291
} // namespace mozilla
292
293
#define IDB_LOG_MARK2(_detailedFmt, _conciseFmt, ...) \
294
do { \
295
using namespace mozilla::dom::indexedDB; \
296
\
297
const IndexedDatabaseManager::LoggingMode mode = \
298
IndexedDatabaseManager::GetLoggingMode(); \
299
\
300
if (mode != IndexedDatabaseManager::Logging_Disabled) { \
301
const char* _fmt; \
302
if (mode == IndexedDatabaseManager::Logging_Concise || \
303
mode == IndexedDatabaseManager::Logging_ConciseProfilerMarks) { \
304
_fmt = _conciseFmt; \
305
} else { \
306
MOZ_ASSERT(mode == IndexedDatabaseManager::Logging_Detailed || \
307
mode == \
308
IndexedDatabaseManager::Logging_DetailedProfilerMarks); \
309
_fmt = _detailedFmt; \
310
} \
311
\
312
const bool _useProfiler = \
313
mode == IndexedDatabaseManager::Logging_ConciseProfilerMarks || \
314
mode == IndexedDatabaseManager::Logging_DetailedProfilerMarks; \
315
\
316
LoggingHelper(_useProfiler, _fmt, ##__VA_ARGS__); \
317
} \
318
} while (0)
319
320
#define IDB_LOG_MARK(_detailedFmt, _conciseFmt, _loggingId, ...) \
321
IDB_LOG_MARK2("IndexedDB %s: " _detailedFmt, "IndexedDB %s: " _conciseFmt, \
322
_loggingId, ##__VA_ARGS__)
323
324
#define IDB_LOG_ID_STRING(...) \
325
mozilla::dom::indexedDB::LoggingIdString(__VA_ARGS__).get()
326
327
#define IDB_LOG_STRINGIFY(...) \
328
mozilla::dom::indexedDB::LoggingString(__VA_ARGS__).get()
329
330
// IDB_LOG_MARK_DETAILED_PARENT and IDB_LOG_MARK_DETAILED_CHILD should have the
331
// same width.
332
#define IDB_LOG_MARK_DETAILED_PARENT "Parent"
333
#define IDB_LOG_MARK_DETAILED_CHILD "Child "
334
335
#define IDB_LOG_MARK_CONCISE_PARENT "P"
336
#define IDB_LOG_MARK_CONCISE_CHILD "C"
337
338
#define IDB_LOG_MARK_DETAILED_TRANSACTION "Transaction[%lld]"
339
#define IDB_LOG_MARK_DETAILED_REQUEST "Request[%llu]"
340
341
#define IDB_LOG_MARK_CONCISE_TRANSACTION "T[%lld]"
342
#define IDB_LOG_MARK_CONCISE_REQUEST "R[%llu]"
343
344
#define IDB_LOG_MARK_TRANSACTION_REQUEST( \
345
_detailedPeer, _concisePeer, _detailedFmt, _conciseFmt, _loggingId, \
346
_transactionSerialNumber, _requestSerialNumber, ...) \
347
IDB_LOG_MARK(_detailedPeer " " IDB_LOG_MARK_DETAILED_TRANSACTION \
348
" " IDB_LOG_MARK_DETAILED_REQUEST \
349
": " _detailedFmt, \
350
_concisePeer " " IDB_LOG_MARK_CONCISE_TRANSACTION \
351
" " IDB_LOG_MARK_CONCISE_REQUEST ": " _conciseFmt, \
352
_loggingId, _transactionSerialNumber, _requestSerialNumber, \
353
##__VA_ARGS__)
354
355
#define IDB_LOG_MARK_PARENT_TRANSACTION_REQUEST( \
356
_detailedFmt, _conciseFmt, _loggingId, _transactionSerialNumber, \
357
_requestSerialNumber, ...) \
358
IDB_LOG_MARK_TRANSACTION_REQUEST( \
359
IDB_LOG_MARK_DETAILED_PARENT, IDB_LOG_MARK_CONCISE_PARENT, _detailedFmt, \
360
_conciseFmt, _loggingId, _transactionSerialNumber, _requestSerialNumber, \
361
##__VA_ARGS__)
362
363
#define IDB_LOG_MARK_CHILD_TRANSACTION_REQUEST(_detailedFmt, _conciseFmt, \
364
_transactionSerialNumber, \
365
_requestSerialNumber, ...) \
366
IDB_LOG_MARK_TRANSACTION_REQUEST( \
367
IDB_LOG_MARK_DETAILED_CHILD, IDB_LOG_MARK_CONCISE_CHILD, _detailedFmt, \
368
_conciseFmt, IDB_LOG_ID_STRING(), _transactionSerialNumber, \
369
_requestSerialNumber, ##__VA_ARGS__)
370
371
#define IDB_LOG_MARK_CHILD_REQUEST(_detailedFmt, _conciseFmt, \
372
_requestSerialNumber, ...) \
373
IDB_LOG_MARK(IDB_LOG_MARK_DETAILED_CHILD " " IDB_LOG_MARK_DETAILED_REQUEST \
374
": " _detailedFmt, \
375
IDB_LOG_MARK_CONCISE_CHILD " " IDB_LOG_MARK_CONCISE_REQUEST \
376
": " _conciseFmt, \
377
IDB_LOG_ID_STRING(), _requestSerialNumber, ##__VA_ARGS__)
378
379
#define IDB_LOG_MARK_TRANSACTION(_detailedPeer, _concisePeer, _detailedFmt, \
380
_conciseFmt, _loggingId, \
381
_transactionSerialNumber, ...) \
382
IDB_LOG_MARK( \
383
_detailedPeer " " IDB_LOG_MARK_DETAILED_TRANSACTION ": " _detailedFmt, \
384
_concisePeer " " IDB_LOG_MARK_CONCISE_TRANSACTION ": " _conciseFmt, \
385
_loggingId, _transactionSerialNumber, ##__VA_ARGS__)
386
387
#define IDB_LOG_MARK_PARENT_TRANSACTION(_detailedFmt, _conciseFmt, _loggingId, \
388
_transactionSerialNumber, ...) \
389
IDB_LOG_MARK_TRANSACTION( \
390
IDB_LOG_MARK_DETAILED_PARENT, IDB_LOG_MARK_CONCISE_PARENT, _detailedFmt, \
391
_conciseFmt, _loggingId, _transactionSerialNumber, ##__VA_ARGS__)
392
393
#define IDB_LOG_MARK_CHILD_TRANSACTION(_detailedFmt, _conciseFmt, \
394
_transactionSerialNumber, ...) \
395
IDB_LOG_MARK_TRANSACTION(IDB_LOG_MARK_DETAILED_CHILD, \
396
IDB_LOG_MARK_CONCISE_CHILD, _detailedFmt, \
397
_conciseFmt, IDB_LOG_ID_STRING(), \
398
_transactionSerialNumber, ##__VA_ARGS__)
399
400
#endif // mozilla_dom_indexeddb_profilerhelpers_h__