1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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/. */
7 #include "mozilla/DebugOnly.h"
9 #include "VacuumManager.h"
11 #include "mozilla/Services.h"
12 #include "mozilla/Preferences.h"
13 #include "nsIObserverService.h"
15 #include "nsThreadUtils.h"
16 #include "mozilla/Logging.h"
18 #include "mozilla/StaticPrefs_storage.h"
20 #include "mozStorageConnection.h"
21 #include "mozIStorageStatement.h"
22 #include "mozIStorageStatementCallback.h"
23 #include "mozIStorageAsyncStatement.h"
24 #include "mozIStoragePendingStatement.h"
25 #include "mozIStorageError.h"
26 #include "mozStorageHelper.h"
27 #include "nsXULAppAPI.h"
29 #define OBSERVER_TOPIC_IDLE_DAILY "idle-daily"
30 #define OBSERVER_TOPIC_XPCOM_SHUTDOWN "xpcom-shutdown"
32 // Used to notify begin and end of a heavy IO task.
33 #define OBSERVER_TOPIC_HEAVY_IO "heavy-io-task"
34 #define OBSERVER_DATA_VACUUM_BEGIN u"vacuum-begin"
35 #define OBSERVER_DATA_VACUUM_END u"vacuum-end"
37 // This preferences root will contain last vacuum timestamps (in seconds) for
38 // each database. The database filename is used as a key.
39 #define PREF_VACUUM_BRANCH "storage.vacuum.last."
41 // Time between subsequent vacuum calls for a certain database.
42 #define VACUUM_INTERVAL_SECONDS 30 * 86400 // 30 days.
44 extern mozilla::LazyLogModule gStorageLog
;
51 ////////////////////////////////////////////////////////////////////////////////
54 class BaseCallback
: public mozIStorageStatementCallback
{
57 NS_DECL_MOZISTORAGESTATEMENTCALLBACK
61 virtual ~BaseCallback() {}
65 BaseCallback::HandleError(mozIStorageError
* aError
) {
68 nsresult rv
= aError
->GetResult(&result
);
69 NS_ENSURE_SUCCESS(rv
, rv
);
70 nsAutoCString message
;
71 rv
= aError
->GetMessage(message
);
72 NS_ENSURE_SUCCESS(rv
, rv
);
74 nsAutoCString warnMsg
;
75 warnMsg
.AppendLiteral("An error occured during async execution: ");
76 warnMsg
.AppendInt(result
);
78 warnMsg
.Append(message
);
79 NS_WARNING(warnMsg
.get());
85 BaseCallback::HandleResult(mozIStorageResultSet
* aResultSet
) {
86 // We could get results from PRAGMA statements, but we don't mind them.
91 BaseCallback::HandleCompletion(uint16_t aReason
) {
92 // By default BaseCallback will just be silent on completion.
96 NS_IMPL_ISUPPORTS(BaseCallback
, mozIStorageStatementCallback
)
98 ////////////////////////////////////////////////////////////////////////////////
99 //// Vacuumer declaration.
101 class Vacuumer
: public BaseCallback
{
103 NS_DECL_MOZISTORAGESTATEMENTCALLBACK
105 explicit Vacuumer(mozIStorageVacuumParticipant
* aParticipant
);
108 nsresult
notifyCompletion(bool aSucceeded
);
111 nsCOMPtr
<mozIStorageVacuumParticipant
> mParticipant
;
112 nsCString mDBFilename
;
113 nsCOMPtr
<mozIStorageConnection
> mDBConn
;
116 ////////////////////////////////////////////////////////////////////////////////
117 //// Vacuumer implementation.
119 Vacuumer::Vacuumer(mozIStorageVacuumParticipant
* aParticipant
)
120 : mParticipant(aParticipant
) {}
122 bool Vacuumer::execute() {
123 MOZ_ASSERT(NS_IsMainThread(), "Must be running on the main thread!");
125 // Get the connection and check its validity.
126 nsresult rv
= mParticipant
->GetDatabaseConnection(getter_AddRefs(mDBConn
));
127 NS_ENSURE_SUCCESS(rv
, false);
129 if (!mDBConn
|| NS_FAILED(mDBConn
->GetConnectionReady(&ready
)) || !ready
) {
130 NS_WARNING("Unable to get a connection to vacuum database");
134 // Ask for the expected page size. Vacuum can change the page size, unless
135 // the database is using WAL journaling.
136 // TODO Bug 634374: figure out a strategy to fix page size with WAL.
137 int32_t expectedPageSize
= 0;
138 rv
= mParticipant
->GetExpectedDatabasePageSize(&expectedPageSize
);
139 if (NS_FAILED(rv
) || !Service::pageSizeIsValid(expectedPageSize
)) {
140 NS_WARNING("Invalid page size requested for database, will use default ");
141 NS_WARNING(mDBFilename
.get());
142 expectedPageSize
= Service::kDefaultPageSize
;
145 // Get the database filename. Last vacuum time is stored under this name
146 // in PREF_VACUUM_BRANCH.
147 nsCOMPtr
<nsIFile
> databaseFile
;
148 mDBConn
->GetDatabaseFile(getter_AddRefs(databaseFile
));
150 NS_WARNING("Trying to vacuum a in-memory database!");
153 nsAutoString databaseFilename
;
154 rv
= databaseFile
->GetLeafName(databaseFilename
);
155 NS_ENSURE_SUCCESS(rv
, false);
156 CopyUTF16toUTF8(databaseFilename
, mDBFilename
);
157 MOZ_ASSERT(!mDBFilename
.IsEmpty(), "Database filename cannot be empty");
159 // Check interval from last vacuum.
160 int32_t now
= static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC
);
162 nsAutoCString
prefName(PREF_VACUUM_BRANCH
);
163 prefName
+= mDBFilename
;
164 rv
= Preferences::GetInt(prefName
.get(), &lastVacuum
);
165 if (NS_SUCCEEDED(rv
) && (now
- lastVacuum
) < VACUUM_INTERVAL_SECONDS
) {
166 // This database was vacuumed recently, skip it.
170 // Notify that we are about to start vacuuming. The participant can opt-out
171 // if it cannot handle a vacuum at this time, and then we'll move to the next
173 bool vacuumGranted
= false;
174 rv
= mParticipant
->OnBeginVacuum(&vacuumGranted
);
175 NS_ENSURE_SUCCESS(rv
, false);
176 if (!vacuumGranted
) {
180 // Notify a heavy IO task is about to start.
181 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
183 rv
= os
->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO
,
184 OBSERVER_DATA_VACUUM_BEGIN
);
185 MOZ_ASSERT(NS_SUCCEEDED(rv
), "Should be able to notify");
188 // Execute the statements separately, since the pragma may conflict with the
189 // vacuum, if they are executed in the same transaction.
190 nsCOMPtr
<mozIStorageAsyncStatement
> pageSizeStmt
;
191 nsAutoCString
pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
192 "PRAGMA page_size = ");
193 pageSizeQuery
.AppendInt(expectedPageSize
);
194 rv
= mDBConn
->CreateAsyncStatement(pageSizeQuery
,
195 getter_AddRefs(pageSizeStmt
));
196 NS_ENSURE_SUCCESS(rv
, false);
197 RefPtr
<BaseCallback
> callback
= new BaseCallback();
198 nsCOMPtr
<mozIStoragePendingStatement
> ps
;
199 rv
= pageSizeStmt
->ExecuteAsync(callback
, getter_AddRefs(ps
));
200 NS_ENSURE_SUCCESS(rv
, false);
202 nsCOMPtr
<mozIStorageAsyncStatement
> stmt
;
203 rv
= mDBConn
->CreateAsyncStatement("VACUUM"_ns
, getter_AddRefs(stmt
));
204 NS_ENSURE_SUCCESS(rv
, false);
205 rv
= stmt
->ExecuteAsync(this, getter_AddRefs(ps
));
206 NS_ENSURE_SUCCESS(rv
, false);
211 ////////////////////////////////////////////////////////////////////////////////
212 //// mozIStorageStatementCallback
215 Vacuumer::HandleError(mozIStorageError
* aError
) {
218 nsAutoCString message
;
221 rv
= aError
->GetResult(&result
);
222 NS_ENSURE_SUCCESS(rv
, rv
);
223 rv
= aError
->GetMessage(message
);
224 NS_ENSURE_SUCCESS(rv
, rv
);
226 nsAutoCString warnMsg
;
227 warnMsg
.AppendLiteral("Unable to vacuum database: ");
228 warnMsg
.Append(mDBFilename
);
229 warnMsg
.AppendLiteral(" - ");
230 warnMsg
.AppendInt(result
);
232 warnMsg
.Append(message
);
233 NS_WARNING(warnMsg
.get());
236 if (MOZ_LOG_TEST(gStorageLog
, LogLevel::Error
)) {
237 rv
= aError
->GetResult(&result
);
238 NS_ENSURE_SUCCESS(rv
, rv
);
239 rv
= aError
->GetMessage(message
);
240 NS_ENSURE_SUCCESS(rv
, rv
);
241 MOZ_LOG(gStorageLog
, LogLevel::Error
,
242 ("Vacuum failed with error: %d '%s'. Database was: '%s'", result
,
243 message
.get(), mDBFilename
.get()));
249 Vacuumer::HandleResult(mozIStorageResultSet
* aResultSet
) {
250 MOZ_ASSERT_UNREACHABLE("Got a resultset from a vacuum?");
255 Vacuumer::HandleCompletion(uint16_t aReason
) {
256 if (aReason
== REASON_FINISHED
) {
257 // Update last vacuum time.
258 int32_t now
= static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC
);
259 MOZ_ASSERT(!mDBFilename
.IsEmpty(), "Database filename cannot be empty");
260 nsAutoCString
prefName(PREF_VACUUM_BRANCH
);
261 prefName
+= mDBFilename
;
262 DebugOnly
<nsresult
> rv
= Preferences::SetInt(prefName
.get(), now
);
263 MOZ_ASSERT(NS_SUCCEEDED(rv
), "Should be able to set a preference");
266 notifyCompletion(aReason
== REASON_FINISHED
);
271 nsresult
Vacuumer::notifyCompletion(bool aSucceeded
) {
272 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
274 os
->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO
,
275 OBSERVER_DATA_VACUUM_END
);
278 nsresult rv
= mParticipant
->OnEndVacuum(aSucceeded
);
279 NS_ENSURE_SUCCESS(rv
, rv
);
286 ////////////////////////////////////////////////////////////////////////////////
289 NS_IMPL_ISUPPORTS(VacuumManager
, nsIObserver
)
291 VacuumManager
* VacuumManager::gVacuumManager
= nullptr;
293 already_AddRefed
<VacuumManager
> VacuumManager::getSingleton() {
294 // Don't allocate it in the child Process.
295 if (!XRE_IsParentProcess()) {
299 if (!gVacuumManager
) {
300 auto manager
= MakeRefPtr
<VacuumManager
>();
301 MOZ_ASSERT(gVacuumManager
== manager
.get());
302 return manager
.forget();
304 return do_AddRef(gVacuumManager
);
307 VacuumManager::VacuumManager() : mParticipants("vacuum-participant") {
308 MOZ_ASSERT(!gVacuumManager
,
309 "Attempting to create two instances of the service!");
310 gVacuumManager
= this;
313 VacuumManager::~VacuumManager() {
314 // Remove the static reference to the service. Check to make sure its us
315 // in case somebody creates an extra instance of the service.
316 MOZ_ASSERT(gVacuumManager
== this,
317 "Deleting a non-singleton instance of the service");
318 if (gVacuumManager
== this) {
319 gVacuumManager
= nullptr;
323 ////////////////////////////////////////////////////////////////////////////////
327 VacuumManager::Observe(nsISupports
* aSubject
, const char* aTopic
,
328 const char16_t
* aData
) {
329 if (strcmp(aTopic
, OBSERVER_TOPIC_IDLE_DAILY
) == 0) {
330 // Try to run vacuum on all registered entries. Will stop at the first
332 nsCOMArray
<mozIStorageVacuumParticipant
> entries
;
333 mParticipants
.GetEntries(entries
);
334 // If there are more entries than what a month can contain, we could end up
335 // skipping some, since we run daily. So we use a starting index.
336 static const char* kPrefName
= PREF_VACUUM_BRANCH
"index";
337 int32_t startIndex
= Preferences::GetInt(kPrefName
, 0);
338 if (startIndex
>= entries
.Count()) {
342 for (index
= startIndex
; index
< entries
.Count(); ++index
) {
343 RefPtr
<Vacuumer
> vacuum
= new Vacuumer(entries
[index
]);
344 // Only vacuum one database per day.
345 if (vacuum
->execute()) {
349 DebugOnly
<nsresult
> rv
= Preferences::SetInt(kPrefName
, index
);
350 MOZ_ASSERT(NS_SUCCEEDED(rv
), "Should be able to set a preference");
356 } // namespace storage
357 } // namespace mozilla