1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Startup Cache.
17 * The Initial Developer of the Original Code is
18 * The Mozilla Foundation <http://www.mozilla.org/>.
19 * Portions created by the Initial Developer are Copyright (C) 2009
20 * the Initial Developer. All Rights Reserved.
23 * Benedict Hsieh <bhsieh@mozilla.com>
24 * Taras Glek <tglek@mozilla.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
43 #include "mozilla/scache/StartupCache.h"
45 #include "nsAutoPtr.h"
46 #include "nsClassHashtable.h"
47 #include "nsComponentManagerUtils.h"
48 #include "nsDirectoryServiceUtils.h"
49 #include "nsIClassInfo.h"
51 #include "nsILocalFile.h"
52 #include "nsIObserver.h"
53 #include "nsIObserverService.h"
54 #include "nsIOutputStream.h"
55 #include "nsIStartupCache.h"
56 #include "nsIStorageStream.h"
57 #include "nsIStreamBufferAccess.h"
58 #include "nsIStringStream.h"
59 #include "nsISupports.h"
61 #include "nsIZipWriter.h"
62 #include "nsIZipReader.h"
63 #include "nsWeakReference.h"
64 #include "nsZipArchive.h"
65 #include "mozilla/Omnijar.h"
67 #include "mozilla/FunctionTimer.h"
70 #define SC_ENDIAN "big"
72 #define SC_ENDIAN "little"
75 #if PR_BYTES_PER_WORD == 4
76 #define SC_WORDSIZE "4"
78 #define SC_WORDSIZE "8"
84 static const char sStartupCacheName
[] = "startupCache." SC_WORDSIZE
"." SC_ENDIAN
;
85 static NS_DEFINE_CID(kZipReaderCID
, NS_ZIPREADER_CID
);
88 StartupCache::GetSingleton()
91 StartupCache::InitSingleton();
93 return StartupCache::gStartupCache
;
97 StartupCache::DeleteSingleton()
99 delete StartupCache::gStartupCache
;
103 StartupCache::InitSingleton()
106 StartupCache::gStartupCache
= new StartupCache();
108 rv
= StartupCache::gStartupCache
->Init();
110 delete StartupCache::gStartupCache
;
111 StartupCache::gStartupCache
= nsnull
;
116 StartupCache
* StartupCache::gStartupCache
;
117 PRBool
StartupCache::gShutdownInitiated
;
119 StartupCache::StartupCache()
120 : mArchive(NULL
), mStartupWriteInitiated(PR_FALSE
), mWriteThread(NULL
) {}
122 StartupCache::~StartupCache()
128 // Generally, the in-memory table should be empty here,
129 // but in special cases (like Talos Ts tests) we
130 // could shut down before we write.
131 // This mechanism will change when IO is moved off-thread
132 // (bug 586859) or when Talos first-run is changed to allow
133 // our timer to work (bug 591471).
135 gStartupCache
= nsnull
;
144 mWriteObjectMap
.Init();
147 // This allows to override the startup cache filename
148 // which is useful from xpcshell, when there is no ProfLDS directory to keep cache in.
149 char *env
= PR_GetEnv("MOZ_STARTUP_CACHE");
151 rv
= NS_NewLocalFile(NS_ConvertUTF8toUTF16(env
), PR_FALSE
, getter_AddRefs(mFile
));
153 nsCOMPtr
<nsIFile
> file
;
154 rv
= NS_GetSpecialDirectory("ProfLDS",
155 getter_AddRefs(file
));
157 // return silently, this will fail in mochitests's xpcshell process.
161 rv
= file
->AppendNative(NS_LITERAL_CSTRING("startupCache"));
162 NS_ENSURE_SUCCESS(rv
, rv
);
164 // Try to create the directory if it's not there yet
165 rv
= file
->Create(nsIFile::DIRECTORY_TYPE
, 0777);
166 if (NS_FAILED(rv
) && rv
!= NS_ERROR_FILE_ALREADY_EXISTS
)
169 rv
= file
->AppendNative(NS_LITERAL_CSTRING(sStartupCacheName
));
171 NS_ENSURE_SUCCESS(rv
, rv
);
173 mFile
= do_QueryInterface(file
);
176 NS_ENSURE_TRUE(mFile
, NS_ERROR_UNEXPECTED
);
178 mObserverService
= do_GetService("@mozilla.org/observer-service;1");
180 if (!mObserverService
) {
181 NS_WARNING("Could not get observerService.");
182 return NS_ERROR_UNEXPECTED
;
185 mListener
= new StartupCacheListener();
186 rv
= mObserverService
->AddObserver(mListener
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
,
188 NS_ENSURE_SUCCESS(rv
, rv
);
189 rv
= mObserverService
->AddObserver(mListener
, "startupcache-invalidate",
191 NS_ENSURE_SUCCESS(rv
, rv
);
195 // Sometimes we don't have a cache yet, that's ok.
196 // If it's corrupted, just remove it and start over.
197 if (NS_FAILED(rv
) && rv
!= NS_ERROR_FILE_NOT_FOUND
) {
198 NS_WARNING("Failed to load startupcache file correctly, removing!");
205 StartupCache::LoadArchive()
210 nsresult rv
= mFile
->Exists(&exists
);
211 if (NS_FAILED(rv
) || !exists
)
212 return NS_ERROR_FILE_NOT_FOUND
;
214 mArchive
= new nsZipArchive();
215 return mArchive
->OpenArchive(mFile
);
218 // NOTE: this will not find a new entry until it has been written to disk!
219 // Consumer should take ownership of the resulting buffer.
221 StartupCache::GetBuffer(const char* id
, char** outbuf
, PRUint32
* length
)
224 if (!mStartupWriteInitiated
) {
226 nsDependentCString
idStr(id
);
227 mTable
.Get(idStr
, &entry
);
229 *outbuf
= new char[entry
->size
];
230 memcpy(*outbuf
, entry
->data
, entry
->size
);
231 *length
= entry
->size
;
237 nsZipItemPtr
<char> zipItem(mArchive
, id
, true);
239 *outbuf
= zipItem
.Forget();
240 *length
= zipItem
.Length();
246 if (mozilla::OmnijarReader()) {
247 // no need to checksum omnijarred entries
248 nsZipItemPtr
<char> zipItem(mozilla::OmnijarReader(), id
);
250 *outbuf
= zipItem
.Forget();
251 *length
= zipItem
.Length();
256 return NS_ERROR_NOT_AVAILABLE
;
259 // Makes a copy of the buffer, client retains ownership of inbuf.
261 StartupCache::PutBuffer(const char* id
, const char* inbuf
, PRUint32 len
)
264 if (StartupCache::gShutdownInitiated
) {
265 return NS_ERROR_NOT_AVAILABLE
;
268 nsAutoArrayPtr
<char> data(new char[len
]);
269 memcpy(data
, inbuf
, len
);
271 nsDependentCString
idStr(id
);
272 // Cache it for now, we'll write all together later.
276 mTable
.Get(idStr
, &entry
);
277 NS_ASSERTION(entry
== nsnull
, "Existing entry in StartupCache.");
280 nsZipItem
* zipItem
= mArchive
->GetItem(id
);
281 NS_ASSERTION(zipItem
== nsnull
, "Existing entry in disk StartupCache.");
285 entry
= new CacheEntry(data
.forget(), len
);
286 mTable
.Put(idStr
, entry
);
287 return ResetStartupWriteTimer();
290 struct CacheWriteHolder
292 nsCOMPtr
<nsIZipWriter
> writer
;
293 nsCOMPtr
<nsIStringInputStream
> stream
;
298 CacheCloseHelper(const nsACString
& key
, nsAutoPtr
<CacheEntry
>& data
,
303 CacheWriteHolder
* holder
= (CacheWriteHolder
*) closure
;
304 nsIStringInputStream
* stream
= holder
->stream
;
305 nsIZipWriter
* writer
= holder
->writer
;
307 stream
->ShareData(data
->data
, data
->size
);
311 rv
= writer
->HasEntry(key
, &hasEntry
);
312 NS_ASSERTION(NS_SUCCEEDED(rv
) && hasEntry
== PR_FALSE
,
313 "Existing entry in disk StartupCache.");
315 rv
= writer
->AddEntryStream(key
, holder
->time
, PR_TRUE
, stream
, PR_FALSE
);
318 NS_WARNING("cache entry deleted but not written to disk.");
320 return PL_DHASH_REMOVE
;
324 StartupCache::WriteToDisk()
328 mStartupWriteInitiated
= PR_TRUE
;
330 if (mTable
.Count() == 0)
333 nsCOMPtr
<nsIZipWriter
> zipW
= do_CreateInstance("@mozilla.org/zipwriter;1");
337 rv
= zipW
->Open(mFile
, PR_RDWR
| PR_CREATE_FILE
);
339 NS_WARNING("could not open zipfile for write");
343 nsCOMPtr
<nsIStringInputStream
> stream
344 = do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv
);
346 NS_WARNING("Couldn't create string input stream.");
350 CacheWriteHolder holder
;
351 holder
.stream
= stream
;
352 holder
.writer
= zipW
;
353 holder
.time
= PR_Now();
355 mTable
.Enumerate(CacheCloseHelper
, &holder
);
357 // Close the archive so Windows doesn't choke.
361 // our reader's view of the archive is outdated now, reload it.
368 StartupCache::InvalidateCache()
373 mFile
->Remove(false);
378 * WaitOnWriteThread() is called from a main thread to wait for the worker
379 * thread to finish. However since the same code is used in the worker thread and
380 * main thread, the worker thread can also call WaitOnWriteThread() which is a no-op.
383 StartupCache::WaitOnWriteThread()
385 PRThread
* writeThread
= mWriteThread
;
386 if (!writeThread
|| writeThread
== PR_GetCurrentThread())
389 NS_TIME_FUNCTION_MIN(30);
390 //NS_WARNING("Waiting on startupcache write");
391 PR_JoinThread(writeThread
);
396 StartupCache::ThreadedWrite(void *aClosure
)
398 gStartupCache
->WriteToDisk();
402 * The write-thread is spawned on a timeout(which is reset with every write). This
403 * can avoid a slow shutdown. After writing out the cache, the zipreader is
404 * reloaded on the worker thread.
407 StartupCache::WriteTimeout(nsITimer
*aTimer
, void *aClosure
)
409 gStartupCache
->mWriteThread
= PR_CreateThread(PR_USER_THREAD
,
410 StartupCache::ThreadedWrite
,
418 // We don't want to refcount StartupCache, so we'll just
419 // hold a ref to this and pass it to observerService instead.
420 NS_IMPL_THREADSAFE_ISUPPORTS1(StartupCacheListener
, nsIObserver
)
423 StartupCacheListener::Observe(nsISupports
*subject
, const char* topic
, const PRUnichar
* data
)
425 StartupCache
* sc
= StartupCache::GetSingleton();
429 if (strcmp(topic
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
) == 0) {
430 // Do not leave the thread running past xpcom shutdown
431 sc
->WaitOnWriteThread();
432 StartupCache::gShutdownInitiated
= PR_TRUE
;
433 } else if (strcmp(topic
, "startupcache-invalidate") == 0) {
434 sc
->InvalidateCache();
440 StartupCache::GetDebugObjectOutputStream(nsIObjectOutputStream
* aStream
,
441 nsIObjectOutputStream
** aOutStream
)
443 NS_ENSURE_ARG_POINTER(aStream
);
445 StartupCacheDebugOutputStream
* stream
446 = new StartupCacheDebugOutputStream(aStream
, &mWriteObjectMap
);
447 NS_ADDREF(*aOutStream
= stream
);
449 NS_ADDREF(*aOutStream
= aStream
);
456 StartupCache::ResetStartupWriteTimer()
458 mStartupWriteInitiated
= PR_FALSE
;
461 mTimer
= do_CreateInstance("@mozilla.org/timer;1", &rv
);
463 rv
= mTimer
->Cancel();
464 NS_ENSURE_SUCCESS(rv
, rv
);
465 // Wait for 10 seconds, then write out the cache.
466 mTimer
->InitWithFuncCallback(StartupCache::WriteTimeout
, this, 60000,
467 nsITimer::TYPE_ONE_SHOT
);
471 // StartupCacheDebugOutputStream implementation
473 NS_IMPL_ISUPPORTS3(StartupCacheDebugOutputStream
, nsIObjectOutputStream
,
474 nsIBinaryOutputStream
, nsIOutputStream
)
477 StartupCacheDebugOutputStream::CheckReferences(nsISupports
* aObject
)
481 nsCOMPtr
<nsIClassInfo
> classInfo
= do_QueryInterface(aObject
);
483 NS_ERROR("aObject must implement nsIClassInfo");
488 rv
= classInfo
->GetFlags(&flags
);
489 NS_ENSURE_SUCCESS(rv
, rv
);
490 if (flags
& nsIClassInfo::SINGLETON
)
493 nsISupportsHashKey
* key
= mObjectMap
->GetEntry(aObject
);
495 NS_ERROR("non-singleton aObject is referenced multiple times in this"
496 "serialization, we don't support that.");
500 mObjectMap
->PutEntry(aObject
);
504 // nsIObjectOutputStream implementation
506 StartupCacheDebugOutputStream::WriteObject(nsISupports
* aObject
, PRBool aIsStrongRef
)
508 nsCOMPtr
<nsISupports
> rootObject(do_QueryInterface(aObject
));
510 NS_ASSERTION(rootObject
.get() == aObject
,
511 "bad call to WriteObject -- call WriteCompoundObject!");
512 PRBool check
= CheckReferences(aObject
);
513 NS_ENSURE_TRUE(check
, NS_ERROR_FAILURE
);
514 return mBinaryStream
->WriteObject(aObject
, aIsStrongRef
);
518 StartupCacheDebugOutputStream::WriteSingleRefObject(nsISupports
* aObject
)
520 nsCOMPtr
<nsISupports
> rootObject(do_QueryInterface(aObject
));
522 NS_ASSERTION(rootObject
.get() == aObject
,
523 "bad call to WriteSingleRefObject -- call WriteCompoundObject!");
524 PRBool check
= CheckReferences(aObject
);
525 NS_ENSURE_TRUE(check
, NS_ERROR_FAILURE
);
526 return mBinaryStream
->WriteSingleRefObject(aObject
);
530 StartupCacheDebugOutputStream::WriteCompoundObject(nsISupports
* aObject
,
534 nsCOMPtr
<nsISupports
> rootObject(do_QueryInterface(aObject
));
536 nsCOMPtr
<nsISupports
> roundtrip
;
537 rootObject
->QueryInterface(aIID
, getter_AddRefs(roundtrip
));
538 NS_ASSERTION(roundtrip
.get() == aObject
,
539 "bad aggregation or multiple inheritance detected by call to "
540 "WriteCompoundObject!");
542 PRBool check
= CheckReferences(aObject
);
543 NS_ENSURE_TRUE(check
, NS_ERROR_FAILURE
);
544 return mBinaryStream
->WriteCompoundObject(aObject
, aIID
, aIsStrongRef
);
548 StartupCacheDebugOutputStream::WriteID(nsID
const& aID
)
550 return mBinaryStream
->WriteID(aID
);
554 StartupCacheDebugOutputStream::GetBuffer(PRUint32 aLength
, PRUint32 aAlignMask
)
556 return mBinaryStream
->GetBuffer(aLength
, aAlignMask
);
560 StartupCacheDebugOutputStream::PutBuffer(char* aBuffer
, PRUint32 aLength
)
562 mBinaryStream
->PutBuffer(aBuffer
, aLength
);
566 StartupCacheWrapper
* StartupCacheWrapper::gStartupCacheWrapper
= nsnull
;
568 NS_IMPL_THREADSAFE_ISUPPORTS1(StartupCacheWrapper
, nsIStartupCache
)
570 StartupCacheWrapper
* StartupCacheWrapper::GetSingleton()
572 if (!gStartupCacheWrapper
)
573 gStartupCacheWrapper
= new StartupCacheWrapper();
575 NS_ADDREF(gStartupCacheWrapper
);
576 return gStartupCacheWrapper
;
580 StartupCacheWrapper::GetBuffer(const char* id
, char** outbuf
, PRUint32
* length
)
582 StartupCache
* sc
= StartupCache::GetSingleton();
584 return NS_ERROR_NOT_INITIALIZED
;
586 return sc
->GetBuffer(id
, outbuf
, length
);
590 StartupCacheWrapper::PutBuffer(const char* id
, char* inbuf
, PRUint32 length
)
592 StartupCache
* sc
= StartupCache::GetSingleton();
594 return NS_ERROR_NOT_INITIALIZED
;
596 return sc
->PutBuffer(id
, inbuf
, length
);
600 StartupCacheWrapper::InvalidateCache()
602 StartupCache
* sc
= StartupCache::GetSingleton();
604 return NS_ERROR_NOT_INITIALIZED
;
606 sc
->InvalidateCache();
611 StartupCacheWrapper::GetDebugObjectOutputStream(nsIObjectOutputStream
* stream
,
612 nsIObjectOutputStream
** outStream
)
614 StartupCache
* sc
= StartupCache::GetSingleton();
616 return NS_ERROR_NOT_INITIALIZED
;
618 return sc
->GetDebugObjectOutputStream(stream
, outStream
);
622 StartupCacheWrapper::StartupWriteComplete(PRBool
*complete
)
624 StartupCache
* sc
= StartupCache::GetSingleton();
626 return NS_ERROR_NOT_INITIALIZED
;
628 sc
->WaitOnWriteThread();
629 *complete
= sc
->mStartupWriteInitiated
&& sc
->mTable
.Count() == 0;
634 StartupCacheWrapper::ResetStartupWriteTimer()
636 StartupCache
* sc
= StartupCache::GetSingleton();
637 return sc
? sc
->ResetStartupWriteTimer() : NS_ERROR_NOT_INITIALIZED
;
641 StartupCacheWrapper::GetObserver(nsIObserver
** obv
) {
642 StartupCache
* sc
= StartupCache::GetSingleton();
644 return NS_ERROR_NOT_INITIALIZED
;
646 NS_ADDREF(*obv
= sc
->mListener
);
650 } // namespace scache
651 } // namespace mozilla