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/. */
6 #ifndef StartupCache_h_
7 #define StartupCache_h_
9 #include "nsClassHashtable.h"
10 #include "nsComponentManagerUtils.h"
12 #include "nsZipArchive.h"
14 #include "nsIMemoryReporter.h"
15 #include "nsIObserverService.h"
16 #include "nsIObserver.h"
17 #include "nsIObjectOutputStream.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/AutoMemMap.h"
21 #include "mozilla/Compression.h"
22 #include "mozilla/MemoryReporting.h"
23 #include "mozilla/Pair.h"
24 #include "mozilla/Result.h"
25 #include "mozilla/UniquePtr.h"
28 * The StartupCache is a persistent cache of simple key-value pairs,
29 * where the keys are null-terminated c-strings and the values are
30 * arbitrary data, passed as a (char*, size) tuple.
32 * Clients should use the GetSingleton() static method to access the cache. It
33 * will be available from the end of XPCOM init (NS_InitXPCOM3 in
34 * XPCOMInit.cpp), until XPCOM shutdown begins. The GetSingleton() method will
35 * return null if the cache is unavailable. The cache is only provided for
36 * libxul builds -- it will fail to link in non-libxul builds. The XPCOM
37 * interface is provided only to allow compiled-code tests; clients should avoid
40 * The API provided is very simple: GetBuffer() returns a buffer that was
41 * previously stored in the cache (if any), and PutBuffer() inserts a buffer
42 * into the cache. GetBuffer returns a new buffer, and the caller must take
43 * ownership of it. PutBuffer will assert if the client attempts to insert a
44 * buffer with the same name as an existing entry. The cache makes a copy of the
45 * passed-in buffer, so client retains ownership.
47 * InvalidateCache() may be called if a client suspects data corruption
48 * or wishes to invalidate for any other reason. This will remove all existing
49 * cache data. Additionally, the static method IgnoreDiskCache() can be called
50 * if it is believed that the on-disk cache file is itself corrupt. This call
51 * implicitly calls InvalidateCache (if the singleton has been initialized) to
52 * ensure any data already read from disk is discarded. The cache will not load
53 * data from the disk file until a successful write occurs.
55 * Finally, getDebugObjectOutputStream() allows debug code to wrap an
56 * objectstream with a debug objectstream, to check for multiply-referenced
57 * objects. These will generally fail to deserialize correctly, unless they are
58 * stateless singletons or the client maintains their own object data map for
61 * Writes before the final-ui-startup notification are placed in an intermediate
62 * cache in memory, then written out to disk at a later time, to get writes off
63 * the startup path. In any case, clients should not rely on being able to
64 * GetBuffer() data that is written to the cache, since it may not have been
65 * written to disk or another client may have invalidated the cache. In other
66 * words, it should be used as a cache only, and not a reliable persistent
69 * Some utility functions are provided in StartupCacheUtils. These functions
70 * wrap the buffers into object streams, which may be useful for serializing
71 * objects. Note the above caution about multiply-referenced objects, though --
72 * the streams are just as 'dumb' as the underlying buffers about
73 * multiply-referenced objects. They just provide some convenience in writing
81 struct StartupCacheEntry
{
82 UniquePtr
<char[]> mData
;
84 uint32_t mCompressedSize
;
85 uint32_t mUncompressedSize
;
86 int32_t mHeaderOffsetInFile
;
87 int32_t mRequestedOrder
;
90 MOZ_IMPLICIT
StartupCacheEntry(uint32_t aOffset
, uint32_t aCompressedSize
,
91 uint32_t aUncompressedSize
)
94 mCompressedSize(aCompressedSize
),
95 mUncompressedSize(aUncompressedSize
),
96 mHeaderOffsetInFile(0),
100 StartupCacheEntry(UniquePtr
<char[]> aData
, size_t aLength
,
101 int32_t aRequestedOrder
)
102 : mData(std::move(aData
)),
105 mUncompressedSize(aLength
),
106 mHeaderOffsetInFile(0),
111 using Value
= Pair
<const nsCString
*, StartupCacheEntry
*>;
113 bool Equals(const Value
& a
, const Value
& b
) const {
114 return a
.second()->mRequestedOrder
== b
.second()->mRequestedOrder
;
117 bool LessThan(const Value
& a
, const Value
& b
) const {
118 return a
.second()->mRequestedOrder
< b
.second()->mRequestedOrder
;
123 struct nsCStringHasher
{
124 using Key
= nsCString
;
125 using Lookup
= nsCString
;
127 static HashNumber
hash(const Lookup
& aLookup
) {
128 return HashString(aLookup
.get());
131 static bool match(const Key
& aKey
, const Lookup
& aLookup
) {
132 return aKey
.Equals(aLookup
);
136 // We don't want to refcount StartupCache, and ObserverService wants to
137 // refcount its listeners, so we'll let it refcount this instead.
138 class StartupCacheListener final
: public nsIObserver
{
139 ~StartupCacheListener() {}
140 NS_DECL_THREADSAFE_ISUPPORTS
144 class StartupCache
: public nsIMemoryReporter
{
145 friend class StartupCacheListener
;
148 NS_DECL_THREADSAFE_ISUPPORTS
149 NS_DECL_NSIMEMORYREPORTER
151 // StartupCache methods. See above comments for a more detailed description.
153 // true if the archive has an entry for the buffer or not.
154 bool HasEntry(const char* id
);
156 // Returns a buffer that was previously stored, caller does not take ownership
157 nsresult
GetBuffer(const char* id
, const char** outbuf
, uint32_t* length
);
159 // Stores a buffer. Caller yields ownership.
160 nsresult
PutBuffer(const char* id
, UniquePtr
<char[]>&& inbuf
,
163 // Removes the cache file.
164 void InvalidateCache(bool memoryOnly
= false);
166 // For use during shutdown - this will write the startupcache's data
167 // to disk if the timer hasn't already gone off.
168 void MaybeInitShutdownWrite();
170 // Signal that data should not be loaded from the cache file
171 static void IgnoreDiskCache();
173 // In DEBUG builds, returns a stream that will attempt to check for
174 // and disallow multiple writes of the same object.
175 nsresult
GetDebugObjectOutputStream(nsIObjectOutputStream
* aStream
,
176 nsIObjectOutputStream
** outStream
);
178 static StartupCache
* GetSingletonNoInit();
179 static StartupCache
* GetSingleton();
180 static void DeleteSingleton();
182 // This measures all the heap memory used by the StartupCache, i.e. it
183 // excludes the mapping.
184 size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
186 bool ShouldCompactCache();
187 nsresult
ResetStartupWriteTimerCheckingReadCount();
188 nsresult
ResetStartupWriteTimer();
189 bool StartupWriteComplete();
193 virtual ~StartupCache();
195 Result
<Ok
, nsresult
> LoadArchive();
198 // Returns a file pointer for the cache file with the given name in the
200 Result
<nsCOMPtr
<nsIFile
>, nsresult
> GetCacheFile(const nsAString
& suffix
);
202 // Opens the cache file for reading.
203 Result
<Ok
, nsresult
> OpenCache();
205 // Writes the cache to disk
206 Result
<Ok
, nsresult
> WriteToDisk();
208 void WaitOnWriteThread();
209 void WaitOnPrefetchThread();
210 void StartPrefetchMemoryThread();
211 void MaybeSpawnWriteThread();
213 static nsresult
InitSingleton();
214 static void WriteTimeout(nsITimer
* aTimer
, void* aClosure
);
215 static void ThreadedWrite(void* aClosure
);
216 static void ThreadedPrefetch(void* aClosure
);
218 HashMap
<nsCString
, StartupCacheEntry
, nsCStringHasher
> mTable
;
219 // owns references to the contents of tables which have been invalidated.
220 // In theory grows forever if the cache is continually filled and then
221 // invalidated, but this should not happen in practice.
222 nsTArray
<decltype(mTable
)> mOldTables
;
223 nsCOMPtr
<nsIFile
> mFile
;
224 loader::AutoMemMap mCacheData
;
226 nsCOMPtr
<nsIObserverService
> mObserverService
;
227 RefPtr
<StartupCacheListener
> mListener
;
228 nsCOMPtr
<nsITimer
> mTimer
;
231 Atomic
<bool> mWrittenOnce
;
232 bool mStartupWriteInitiated
;
233 bool mCurTableReferenced
;
234 uint32_t mRequestedCount
;
235 size_t mCacheEntriesBaseOffset
;
237 static StaticRefPtr
<StartupCache
> gStartupCache
;
238 static bool gShutdownInitiated
;
239 static bool gIgnoreDiskCache
;
240 PRThread
* mWriteThread
;
241 PRThread
* mPrefetchThread
;
242 UniquePtr
<Compression::LZ4FrameDecompressionContext
> mDecompressionContext
;
244 nsTHashtable
<nsISupportsHashKey
> mWriteObjectMap
;
248 // This debug outputstream attempts to detect if clients are writing multiple
249 // references to the same object. We only support that if that object
252 class StartupCacheDebugOutputStream final
: public nsIObjectOutputStream
{
253 ~StartupCacheDebugOutputStream() {}
256 NS_DECL_NSIOBJECTOUTPUTSTREAM
258 StartupCacheDebugOutputStream(nsIObjectOutputStream
* binaryStream
,
259 nsTHashtable
<nsISupportsHashKey
>* objectMap
)
260 : mBinaryStream(binaryStream
), mObjectMap(objectMap
) {}
262 NS_FORWARD_SAFE_NSIBINARYOUTPUTSTREAM(mBinaryStream
)
263 NS_FORWARD_SAFE_NSIOUTPUTSTREAM(mBinaryStream
)
265 bool CheckReferences(nsISupports
* aObject
);
267 nsCOMPtr
<nsIObjectOutputStream
> mBinaryStream
;
268 nsTHashtable
<nsISupportsHashKey
>* mObjectMap
;
272 } // namespace scache
273 } // namespace mozilla
275 #endif // StartupCache_h_