Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef StartupCache_h_
7
#define StartupCache_h_
8
9
#include <utility>
10
11
#include "nsClassHashtable.h"
12
#include "nsComponentManagerUtils.h"
13
#include "nsTArray.h"
14
#include "nsZipArchive.h"
15
#include "nsITimer.h"
16
#include "nsIMemoryReporter.h"
17
#include "nsIObserverService.h"
18
#include "nsIObserver.h"
19
#include "nsIObjectOutputStream.h"
20
#include "nsIFile.h"
21
#include "mozilla/Attributes.h"
22
#include "mozilla/AutoMemMap.h"
23
#include "mozilla/Compression.h"
24
#include "mozilla/MemoryReporting.h"
25
#include "mozilla/Result.h"
26
#include "mozilla/UniquePtr.h"
27
28
/**
29
* The StartupCache is a persistent cache of simple key-value pairs,
30
* where the keys are null-terminated c-strings and the values are
31
* arbitrary data, passed as a (char*, size) tuple.
32
*
33
* Clients should use the GetSingleton() static method to access the cache. It
34
* will be available from the end of XPCOM init (NS_InitXPCOM3 in
35
* XPCOMInit.cpp), until XPCOM shutdown begins. The GetSingleton() method will
36
* return null if the cache is unavailable. The cache is only provided for
37
* libxul builds -- it will fail to link in non-libxul builds. The XPCOM
38
* interface is provided only to allow compiled-code tests; clients should avoid
39
* using it.
40
*
41
* The API provided is very simple: GetBuffer() returns a buffer that was
42
* previously stored in the cache (if any), and PutBuffer() inserts a buffer
43
* into the cache. GetBuffer returns a new buffer, and the caller must take
44
* ownership of it. PutBuffer will assert if the client attempts to insert a
45
* buffer with the same name as an existing entry. The cache makes a copy of the
46
* passed-in buffer, so client retains ownership.
47
*
48
* InvalidateCache() may be called if a client suspects data corruption
49
* or wishes to invalidate for any other reason. This will remove all existing
50
* cache data. Additionally, the static method IgnoreDiskCache() can be called
51
* if it is believed that the on-disk cache file is itself corrupt. This call
52
* implicitly calls InvalidateCache (if the singleton has been initialized) to
53
* ensure any data already read from disk is discarded. The cache will not load
54
* data from the disk file until a successful write occurs.
55
*
56
* Finally, getDebugObjectOutputStream() allows debug code to wrap an
57
* objectstream with a debug objectstream, to check for multiply-referenced
58
* objects. These will generally fail to deserialize correctly, unless they are
59
* stateless singletons or the client maintains their own object data map for
60
* deserialization.
61
*
62
* Writes before the final-ui-startup notification are placed in an intermediate
63
* cache in memory, then written out to disk at a later time, to get writes off
64
* the startup path. In any case, clients should not rely on being able to
65
* GetBuffer() data that is written to the cache, since it may not have been
66
* written to disk or another client may have invalidated the cache. In other
67
* words, it should be used as a cache only, and not a reliable persistent
68
* store.
69
*
70
* Some utility functions are provided in StartupCacheUtils. These functions
71
* wrap the buffers into object streams, which may be useful for serializing
72
* objects. Note the above caution about multiply-referenced objects, though --
73
* the streams are just as 'dumb' as the underlying buffers about
74
* multiply-referenced objects. They just provide some convenience in writing
75
* out data.
76
*/
77
78
namespace mozilla {
79
80
namespace scache {
81
82
struct StartupCacheEntry {
83
UniquePtr<char[]> mData;
84
uint32_t mOffset;
85
uint32_t mCompressedSize;
86
uint32_t mUncompressedSize;
87
int32_t mHeaderOffsetInFile;
88
int32_t mRequestedOrder;
89
bool mRequested;
90
91
MOZ_IMPLICIT StartupCacheEntry(uint32_t aOffset, uint32_t aCompressedSize,
92
uint32_t aUncompressedSize)
93
: mData(nullptr),
94
mOffset(aOffset),
95
mCompressedSize(aCompressedSize),
96
mUncompressedSize(aUncompressedSize),
97
mHeaderOffsetInFile(0),
98
mRequestedOrder(0),
99
mRequested(false) {}
100
101
StartupCacheEntry(UniquePtr<char[]> aData, size_t aLength,
102
int32_t aRequestedOrder)
103
: mData(std::move(aData)),
104
mOffset(0),
105
mCompressedSize(0),
106
mUncompressedSize(aLength),
107
mHeaderOffsetInFile(0),
108
mRequestedOrder(0),
109
mRequested(true) {}
110
111
struct Comparator {
112
using Value = std::pair<const nsCString*, StartupCacheEntry*>;
113
114
bool Equals(const Value& a, const Value& b) const {
115
return a.second->mRequestedOrder == b.second->mRequestedOrder;
116
}
117
118
bool LessThan(const Value& a, const Value& b) const {
119
return a.second->mRequestedOrder < b.second->mRequestedOrder;
120
}
121
};
122
};
123
124
struct nsCStringHasher {
125
using Key = nsCString;
126
using Lookup = nsCString;
127
128
static HashNumber hash(const Lookup& aLookup) {
129
return HashString(aLookup.get());
130
}
131
132
static bool match(const Key& aKey, const Lookup& aLookup) {
133
return aKey.Equals(aLookup);
134
}
135
};
136
137
// We don't want to refcount StartupCache, and ObserverService wants to
138
// refcount its listeners, so we'll let it refcount this instead.
139
class StartupCacheListener final : public nsIObserver {
140
~StartupCacheListener() = default;
141
NS_DECL_THREADSAFE_ISUPPORTS
142
NS_DECL_NSIOBSERVER
143
};
144
145
class StartupCache : public nsIMemoryReporter {
146
friend class StartupCacheListener;
147
148
public:
149
NS_DECL_THREADSAFE_ISUPPORTS
150
NS_DECL_NSIMEMORYREPORTER
151
152
// StartupCache methods. See above comments for a more detailed description.
153
154
// true if the archive has an entry for the buffer or not.
155
bool HasEntry(const char* id);
156
157
// Returns a buffer that was previously stored, caller does not take ownership
158
nsresult GetBuffer(const char* id, const char** outbuf, uint32_t* length);
159
160
// Stores a buffer. Caller yields ownership.
161
nsresult PutBuffer(const char* id, UniquePtr<char[]>&& inbuf,
162
uint32_t length);
163
164
// Removes the cache file.
165
void InvalidateCache(bool memoryOnly = false);
166
167
// For use during shutdown - this will write the startupcache's data
168
// to disk if the timer hasn't already gone off.
169
void MaybeInitShutdownWrite();
170
171
// Signal that data should not be loaded from the cache file
172
static void IgnoreDiskCache();
173
174
// In DEBUG builds, returns a stream that will attempt to check for
175
// and disallow multiple writes of the same object.
176
nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream,
177
nsIObjectOutputStream** outStream);
178
179
static StartupCache* GetSingletonNoInit();
180
static StartupCache* GetSingleton();
181
static void DeleteSingleton();
182
183
// This measures all the heap memory used by the StartupCache, i.e. it
184
// excludes the mapping.
185
size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
186
187
bool ShouldCompactCache();
188
nsresult ResetStartupWriteTimerCheckingReadCount();
189
nsresult ResetStartupWriteTimer();
190
bool StartupWriteComplete();
191
192
private:
193
StartupCache();
194
virtual ~StartupCache();
195
196
friend class StartupCacheInfo;
197
198
Result<Ok, nsresult> LoadArchive();
199
nsresult Init();
200
201
// Returns a file pointer for the cache file with the given name in the
202
// current profile.
203
Result<nsCOMPtr<nsIFile>, nsresult> GetCacheFile(const nsAString& suffix);
204
205
// Opens the cache file for reading.
206
Result<Ok, nsresult> OpenCache();
207
208
// Writes the cache to disk
209
Result<Ok, nsresult> WriteToDisk();
210
211
void WaitOnWriteThread();
212
void WaitOnPrefetchThread();
213
void StartPrefetchMemoryThread();
214
void MaybeSpawnWriteThread();
215
216
static nsresult InitSingleton();
217
static void WriteTimeout(nsITimer* aTimer, void* aClosure);
218
static void ThreadedWrite(void* aClosure);
219
static void ThreadedPrefetch(void* aClosure);
220
221
HashMap<nsCString, StartupCacheEntry, nsCStringHasher> mTable;
222
// owns references to the contents of tables which have been invalidated.
223
// In theory grows forever if the cache is continually filled and then
224
// invalidated, but this should not happen in practice.
225
nsTArray<decltype(mTable)> mOldTables;
226
nsCOMPtr<nsIFile> mFile;
227
loader::AutoMemMap mCacheData;
228
229
nsCOMPtr<nsIObserverService> mObserverService;
230
RefPtr<StartupCacheListener> mListener;
231
nsCOMPtr<nsITimer> mTimer;
232
233
Atomic<bool> mDirty;
234
Atomic<bool> mWrittenOnce;
235
bool mStartupWriteInitiated;
236
bool mCurTableReferenced;
237
uint32_t mRequestedCount;
238
size_t mCacheEntriesBaseOffset;
239
240
static StaticRefPtr<StartupCache> gStartupCache;
241
static bool gShutdownInitiated;
242
static bool gIgnoreDiskCache;
243
static bool gFoundDiskCacheOnInit;
244
PRThread* mWriteThread;
245
PRThread* mPrefetchThread;
246
UniquePtr<Compression::LZ4FrameDecompressionContext> mDecompressionContext;
247
#ifdef DEBUG
248
nsTHashtable<nsISupportsHashKey> mWriteObjectMap;
249
#endif
250
};
251
252
// This debug outputstream attempts to detect if clients are writing multiple
253
// references to the same object. We only support that if that object
254
// is a singleton.
255
#ifdef DEBUG
256
class StartupCacheDebugOutputStream final : public nsIObjectOutputStream {
257
~StartupCacheDebugOutputStream() = default;
258
259
NS_DECL_ISUPPORTS
260
NS_DECL_NSIOBJECTOUTPUTSTREAM
261
262
StartupCacheDebugOutputStream(nsIObjectOutputStream* binaryStream,
263
nsTHashtable<nsISupportsHashKey>* objectMap)
264
: mBinaryStream(binaryStream), mObjectMap(objectMap) {}
265
266
NS_FORWARD_SAFE_NSIBINARYOUTPUTSTREAM(mBinaryStream)
267
NS_FORWARD_SAFE_NSIOUTPUTSTREAM(mBinaryStream)
268
269
bool CheckReferences(nsISupports* aObject);
270
271
nsCOMPtr<nsIObjectOutputStream> mBinaryStream;
272
nsTHashtable<nsISupportsHashKey>* mObjectMap;
273
};
274
#endif // DEBUG
275
276
} // namespace scache
277
} // namespace mozilla
278
279
#endif // StartupCache_h_