1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/startupcache/StartupCache.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,226 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef StartupCache_h_ 1.10 +#define StartupCache_h_ 1.11 + 1.12 +#include "nsClassHashtable.h" 1.13 +#include "nsComponentManagerUtils.h" 1.14 +#include "nsZipArchive.h" 1.15 +#include "nsIStartupCache.h" 1.16 +#include "nsITimer.h" 1.17 +#include "nsIMemoryReporter.h" 1.18 +#include "nsIObserverService.h" 1.19 +#include "nsIObserver.h" 1.20 +#include "nsIOutputStream.h" 1.21 +#include "nsIFile.h" 1.22 +#include "mozilla/Attributes.h" 1.23 +#include "mozilla/MemoryReporting.h" 1.24 +#include "mozilla/StaticPtr.h" 1.25 + 1.26 +/** 1.27 + * The StartupCache is a persistent cache of simple key-value pairs, 1.28 + * where the keys are null-terminated c-strings and the values are 1.29 + * arbitrary data, passed as a (char*, size) tuple. 1.30 + * 1.31 + * Clients should use the GetSingleton() static method to access the cache. It 1.32 + * will be available from the end of XPCOM init (NS_InitXPCOM3 in nsXPComInit.cpp), 1.33 + * until XPCOM shutdown begins. The GetSingleton() method will return null if the cache 1.34 + * is unavailable. The cache is only provided for libxul builds -- 1.35 + * it will fail to link in non-libxul builds. The XPCOM interface is provided 1.36 + * only to allow compiled-code tests; clients should avoid using it. 1.37 + * 1.38 + * The API provided is very simple: GetBuffer() returns a buffer that was previously 1.39 + * stored in the cache (if any), and PutBuffer() inserts a buffer into the cache. 1.40 + * GetBuffer returns a new buffer, and the caller must take ownership of it. 1.41 + * PutBuffer will assert if the client attempts to insert a buffer with the same name as 1.42 + * an existing entry. The cache makes a copy of the passed-in buffer, so client 1.43 + * retains ownership. 1.44 + * 1.45 + * InvalidateCache() may be called if a client suspects data corruption 1.46 + * or wishes to invalidate for any other reason. This will remove all existing cache data. 1.47 + * Additionally, the static method IgnoreDiskCache() can be called if it is 1.48 + * believed that the on-disk cache file is itself corrupt. This call implicitly 1.49 + * calls InvalidateCache (if the singleton has been initialized) to ensure any 1.50 + * data already read from disk is discarded. The cache will not load data from 1.51 + * the disk file until a successful write occurs. 1.52 + * 1.53 + * Finally, getDebugObjectOutputStream() allows debug code to wrap an objectstream 1.54 + * with a debug objectstream, to check for multiply-referenced objects. These will 1.55 + * generally fail to deserialize correctly, unless they are stateless singletons or the 1.56 + * client maintains their own object data map for deserialization. 1.57 + * 1.58 + * Writes before the final-ui-startup notification are placed in an intermediate 1.59 + * cache in memory, then written out to disk at a later time, to get writes off the 1.60 + * startup path. In any case, clients should not rely on being able to GetBuffer() 1.61 + * data that is written to the cache, since it may not have been written to disk or 1.62 + * another client may have invalidated the cache. In other words, it should be used as 1.63 + * a cache only, and not a reliable persistent store. 1.64 + * 1.65 + * Some utility functions are provided in StartupCacheUtils. These functions wrap the 1.66 + * buffers into object streams, which may be useful for serializing objects. Note 1.67 + * the above caution about multiply-referenced objects, though -- the streams are just 1.68 + * as 'dumb' as the underlying buffers about multiply-referenced objects. They just 1.69 + * provide some convenience in writing out data. 1.70 + */ 1.71 + 1.72 +namespace mozilla { 1.73 + 1.74 +namespace scache { 1.75 + 1.76 +struct CacheEntry 1.77 +{ 1.78 + nsAutoArrayPtr<char> data; 1.79 + uint32_t size; 1.80 + 1.81 + CacheEntry() : data(nullptr), size(0) { } 1.82 + 1.83 + // Takes possession of buf 1.84 + CacheEntry(char* buf, uint32_t len) : data(buf), size(len) { } 1.85 + 1.86 + ~CacheEntry() 1.87 + { 1.88 + } 1.89 + 1.90 + size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) { 1.91 + return mallocSizeOf(data); 1.92 + } 1.93 +}; 1.94 + 1.95 +// We don't want to refcount StartupCache, and ObserverService wants to 1.96 +// refcount its listeners, so we'll let it refcount this instead. 1.97 +class StartupCacheListener MOZ_FINAL : public nsIObserver 1.98 +{ 1.99 + NS_DECL_THREADSAFE_ISUPPORTS 1.100 + NS_DECL_NSIOBSERVER 1.101 +}; 1.102 + 1.103 +class StartupCache : public nsIMemoryReporter 1.104 +{ 1.105 + 1.106 +friend class StartupCacheListener; 1.107 +friend class StartupCacheWrapper; 1.108 + 1.109 +public: 1.110 + NS_DECL_THREADSAFE_ISUPPORTS 1.111 + NS_DECL_NSIMEMORYREPORTER 1.112 + 1.113 + // StartupCache methods. See above comments for a more detailed description. 1.114 + 1.115 + // Returns a buffer that was previously stored, caller takes ownership. 1.116 + nsresult GetBuffer(const char* id, char** outbuf, uint32_t* length); 1.117 + 1.118 + // Stores a buffer. Caller keeps ownership, we make a copy. 1.119 + nsresult PutBuffer(const char* id, const char* inbuf, uint32_t length); 1.120 + 1.121 + // Removes the cache file. 1.122 + void InvalidateCache(); 1.123 + 1.124 + // Signal that data should not be loaded from the cache file 1.125 + static void IgnoreDiskCache(); 1.126 + 1.127 + // In DEBUG builds, returns a stream that will attempt to check for 1.128 + // and disallow multiple writes of the same object. 1.129 + nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream, 1.130 + nsIObjectOutputStream** outStream); 1.131 + 1.132 + nsresult RecordAgesAlways(); 1.133 + 1.134 + static StartupCache* GetSingleton(); 1.135 + static void DeleteSingleton(); 1.136 + 1.137 + // This measures all the heap memory used by the StartupCache, i.e. it 1.138 + // excludes the mapping. 1.139 + size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf); 1.140 + 1.141 + size_t SizeOfMapping(); 1.142 + 1.143 +private: 1.144 + StartupCache(); 1.145 + virtual ~StartupCache(); 1.146 + 1.147 + enum TelemetrifyAge { 1.148 + IGNORE_AGE = 0, 1.149 + RECORD_AGE = 1 1.150 + }; 1.151 + static enum TelemetrifyAge gPostFlushAgeAction; 1.152 + 1.153 + nsresult LoadArchive(enum TelemetrifyAge flag); 1.154 + nsresult Init(); 1.155 + void WriteToDisk(); 1.156 + nsresult ResetStartupWriteTimer(); 1.157 + void WaitOnWriteThread(); 1.158 + 1.159 + static nsresult InitSingleton(); 1.160 + static void WriteTimeout(nsITimer *aTimer, void *aClosure); 1.161 + static void ThreadedWrite(void *aClosure); 1.162 + 1.163 + static size_t SizeOfEntryExcludingThis(const nsACString& key, 1.164 + const nsAutoPtr<CacheEntry>& data, 1.165 + mozilla::MallocSizeOf mallocSizeOf, 1.166 + void *); 1.167 + 1.168 + nsClassHashtable<nsCStringHashKey, CacheEntry> mTable; 1.169 + nsRefPtr<nsZipArchive> mArchive; 1.170 + nsCOMPtr<nsIFile> mFile; 1.171 + 1.172 + nsCOMPtr<nsIObserverService> mObserverService; 1.173 + nsRefPtr<StartupCacheListener> mListener; 1.174 + nsCOMPtr<nsITimer> mTimer; 1.175 + 1.176 + bool mStartupWriteInitiated; 1.177 + 1.178 + static StaticRefPtr<StartupCache> gStartupCache; 1.179 + static bool gShutdownInitiated; 1.180 + static bool gIgnoreDiskCache; 1.181 + PRThread *mWriteThread; 1.182 +#ifdef DEBUG 1.183 + nsTHashtable<nsISupportsHashKey> mWriteObjectMap; 1.184 +#endif 1.185 +}; 1.186 + 1.187 +// This debug outputstream attempts to detect if clients are writing multiple 1.188 +// references to the same object. We only support that if that object 1.189 +// is a singleton. 1.190 +#ifdef DEBUG 1.191 +class StartupCacheDebugOutputStream MOZ_FINAL 1.192 + : public nsIObjectOutputStream 1.193 +{ 1.194 + NS_DECL_ISUPPORTS 1.195 + NS_DECL_NSIOBJECTOUTPUTSTREAM 1.196 + 1.197 + StartupCacheDebugOutputStream (nsIObjectOutputStream* binaryStream, 1.198 + nsTHashtable<nsISupportsHashKey>* objectMap) 1.199 + : mBinaryStream(binaryStream), mObjectMap(objectMap) { } 1.200 + 1.201 + NS_FORWARD_SAFE_NSIBINARYOUTPUTSTREAM(mBinaryStream) 1.202 + NS_FORWARD_SAFE_NSIOUTPUTSTREAM(mBinaryStream) 1.203 + 1.204 + bool CheckReferences(nsISupports* aObject); 1.205 + 1.206 + nsCOMPtr<nsIObjectOutputStream> mBinaryStream; 1.207 + nsTHashtable<nsISupportsHashKey> *mObjectMap; 1.208 +}; 1.209 +#endif // DEBUG 1.210 + 1.211 +// XPCOM wrapper interface provided for tests only. 1.212 +#define NS_STARTUPCACHE_CID \ 1.213 + {0xae4505a9, 0x87ab, 0x477c, \ 1.214 + {0xb5, 0x77, 0xf9, 0x23, 0x57, 0xed, 0xa8, 0x84}} 1.215 +// contract id: "@mozilla.org/startupcache/cache;1" 1.216 + 1.217 +class StartupCacheWrapper MOZ_FINAL 1.218 + : public nsIStartupCache 1.219 +{ 1.220 + NS_DECL_THREADSAFE_ISUPPORTS 1.221 + NS_DECL_NSISTARTUPCACHE 1.222 + 1.223 + static StartupCacheWrapper* GetSingleton(); 1.224 + static StartupCacheWrapper *gStartupCacheWrapper; 1.225 +}; 1.226 + 1.227 +} // namespace scache 1.228 +} // namespace mozilla 1.229 +#endif //StartupCache_h_