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"
19 #include "mozStorageConnection.h"
20 #include "mozIStorageStatement.h"
21 #include "mozIStorageAsyncStatement.h"
22 #include "mozIStoragePendingStatement.h"
23 #include "mozIStorageError.h"
24 #include "mozStorageHelper.h"
25 #include "nsXULAppAPI.h"
27 #define OBSERVER_TOPIC_IDLE_DAILY "idle-daily"
28 #define OBSERVER_TOPIC_XPCOM_SHUTDOWN "xpcom-shutdown"
30 // Used to notify begin and end of a heavy IO task.
31 #define OBSERVER_TOPIC_HEAVY_IO "heavy-io-task"
32 #define OBSERVER_DATA_VACUUM_BEGIN u"vacuum-begin"
33 #define OBSERVER_DATA_VACUUM_END u"vacuum-end"
35 // This preferences root will contain last vacuum timestamps (in seconds) for
36 // each database. The database filename is used as a key.
37 #define PREF_VACUUM_BRANCH "storage.vacuum.last."
39 // Time between subsequent vacuum calls for a certain database.
40 #define VACUUM_INTERVAL_SECONDS 30 * 86400 // 30 days.
42 extern mozilla::LazyLogModule gStorageLog
;
49 ////////////////////////////////////////////////////////////////////////////////
52 class BaseCallback
: public mozIStorageStatementCallback
56 NS_DECL_MOZISTORAGESTATEMENTCALLBACK
59 virtual ~BaseCallback() {}
63 BaseCallback::HandleError(mozIStorageError
*aError
)
67 nsresult rv
= aError
->GetResult(&result
);
68 NS_ENSURE_SUCCESS(rv
, rv
);
69 nsAutoCString message
;
70 rv
= aError
->GetMessage(message
);
71 NS_ENSURE_SUCCESS(rv
, rv
);
73 nsAutoCString warnMsg
;
74 warnMsg
.AppendLiteral("An error occured during async execution: ");
75 warnMsg
.AppendInt(result
);
77 warnMsg
.Append(message
);
78 NS_WARNING(warnMsg
.get());
84 BaseCallback::HandleResult(mozIStorageResultSet
*aResultSet
)
86 // We could get results from PRAGMA statements, but we don't mind them.
91 BaseCallback::HandleCompletion(uint16_t aReason
)
93 // By default BaseCallback will just be silent on completion.
99 , mozIStorageStatementCallback
102 ////////////////////////////////////////////////////////////////////////////////
103 //// Vacuumer declaration.
105 class Vacuumer
: public BaseCallback
108 NS_DECL_MOZISTORAGESTATEMENTCALLBACK
110 explicit Vacuumer(mozIStorageVacuumParticipant
*aParticipant
);
113 nsresult
notifyCompletion(bool aSucceeded
);
116 nsCOMPtr
<mozIStorageVacuumParticipant
> mParticipant
;
117 nsCString mDBFilename
;
118 nsCOMPtr
<mozIStorageConnection
> mDBConn
;
121 ////////////////////////////////////////////////////////////////////////////////
122 //// Vacuumer implementation.
124 Vacuumer::Vacuumer(mozIStorageVacuumParticipant
*aParticipant
)
125 : mParticipant(aParticipant
)
132 MOZ_ASSERT(NS_IsMainThread(), "Must be running on the main thread!");
134 // Get the connection and check its validity.
135 nsresult rv
= mParticipant
->GetDatabaseConnection(getter_AddRefs(mDBConn
));
136 NS_ENSURE_SUCCESS(rv
, false);
138 if (!mDBConn
|| NS_FAILED(mDBConn
->GetConnectionReady(&ready
)) || !ready
) {
139 NS_WARNING("Unable to get a connection to vacuum database");
143 // Ask for the expected page size. Vacuum can change the page size, unless
144 // the database is using WAL journaling.
145 // TODO Bug 634374: figure out a strategy to fix page size with WAL.
146 int32_t expectedPageSize
= 0;
147 rv
= mParticipant
->GetExpectedDatabasePageSize(&expectedPageSize
);
148 if (NS_FAILED(rv
) || !Service::pageSizeIsValid(expectedPageSize
)) {
149 NS_WARNING("Invalid page size requested for database, will use default ");
150 NS_WARNING(mDBFilename
.get());
151 expectedPageSize
= Service::getDefaultPageSize();
154 // Get the database filename. Last vacuum time is stored under this name
155 // in PREF_VACUUM_BRANCH.
156 nsCOMPtr
<nsIFile
> databaseFile
;
157 mDBConn
->GetDatabaseFile(getter_AddRefs(databaseFile
));
159 NS_WARNING("Trying to vacuum a in-memory database!");
162 nsAutoString databaseFilename
;
163 rv
= databaseFile
->GetLeafName(databaseFilename
);
164 NS_ENSURE_SUCCESS(rv
, false);
165 mDBFilename
= NS_ConvertUTF16toUTF8(databaseFilename
);
166 MOZ_ASSERT(!mDBFilename
.IsEmpty(), "Database filename cannot be empty");
168 // Check interval from last vacuum.
169 int32_t now
= static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC
);
171 nsAutoCString
prefName(PREF_VACUUM_BRANCH
);
172 prefName
+= mDBFilename
;
173 rv
= Preferences::GetInt(prefName
.get(), &lastVacuum
);
174 if (NS_SUCCEEDED(rv
) && (now
- lastVacuum
) < VACUUM_INTERVAL_SECONDS
) {
175 // This database was vacuumed recently, skip it.
179 // Notify that we are about to start vacuuming. The participant can opt-out
180 // if it cannot handle a vacuum at this time, and then we'll move to the next
182 bool vacuumGranted
= false;
183 rv
= mParticipant
->OnBeginVacuum(&vacuumGranted
);
184 NS_ENSURE_SUCCESS(rv
, false);
185 if (!vacuumGranted
) {
189 // Notify a heavy IO task is about to start.
190 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
193 os
->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO
,
194 OBSERVER_DATA_VACUUM_BEGIN
);
195 MOZ_ASSERT(NS_SUCCEEDED(rv
), "Should be able to notify");
198 // Execute the statements separately, since the pragma may conflict with the
199 // vacuum, if they are executed in the same transaction.
200 nsCOMPtr
<mozIStorageAsyncStatement
> pageSizeStmt
;
201 nsAutoCString
pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
202 "PRAGMA page_size = ");
203 pageSizeQuery
.AppendInt(expectedPageSize
);
204 rv
= mDBConn
->CreateAsyncStatement(pageSizeQuery
,
205 getter_AddRefs(pageSizeStmt
));
206 NS_ENSURE_SUCCESS(rv
, false);
207 RefPtr
<BaseCallback
> callback
= new BaseCallback();
208 nsCOMPtr
<mozIStoragePendingStatement
> ps
;
209 rv
= pageSizeStmt
->ExecuteAsync(callback
, getter_AddRefs(ps
));
210 NS_ENSURE_SUCCESS(rv
, false);
212 nsCOMPtr
<mozIStorageAsyncStatement
> stmt
;
213 rv
= mDBConn
->CreateAsyncStatement(NS_LITERAL_CSTRING(
215 ), getter_AddRefs(stmt
));
216 NS_ENSURE_SUCCESS(rv
, false);
217 rv
= stmt
->ExecuteAsync(this, getter_AddRefs(ps
));
218 NS_ENSURE_SUCCESS(rv
, false);
223 ////////////////////////////////////////////////////////////////////////////////
224 //// mozIStorageStatementCallback
227 Vacuumer::HandleError(mozIStorageError
*aError
)
231 nsAutoCString message
;
234 rv
= aError
->GetResult(&result
);
235 NS_ENSURE_SUCCESS(rv
, rv
);
236 rv
= aError
->GetMessage(message
);
237 NS_ENSURE_SUCCESS(rv
, rv
);
239 nsAutoCString warnMsg
;
240 warnMsg
.AppendLiteral("Unable to vacuum database: ");
241 warnMsg
.Append(mDBFilename
);
242 warnMsg
.AppendLiteral(" - ");
243 warnMsg
.AppendInt(result
);
245 warnMsg
.Append(message
);
246 NS_WARNING(warnMsg
.get());
249 if (MOZ_LOG_TEST(gStorageLog
, LogLevel::Error
)) {
250 rv
= aError
->GetResult(&result
);
251 NS_ENSURE_SUCCESS(rv
, rv
);
252 rv
= aError
->GetMessage(message
);
253 NS_ENSURE_SUCCESS(rv
, rv
);
254 MOZ_LOG(gStorageLog
, LogLevel::Error
,
255 ("Vacuum failed with error: %d '%s'. Database was: '%s'",
256 result
, message
.get(), mDBFilename
.get()));
262 Vacuumer::HandleResult(mozIStorageResultSet
*aResultSet
)
264 MOZ_ASSERT_UNREACHABLE("Got a resultset from a vacuum?");
269 Vacuumer::HandleCompletion(uint16_t aReason
)
271 if (aReason
== REASON_FINISHED
) {
272 // Update last vacuum time.
273 int32_t now
= static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC
);
274 MOZ_ASSERT(!mDBFilename
.IsEmpty(), "Database filename cannot be empty");
275 nsAutoCString
prefName(PREF_VACUUM_BRANCH
);
276 prefName
+= mDBFilename
;
277 DebugOnly
<nsresult
> rv
= Preferences::SetInt(prefName
.get(), now
);
278 MOZ_ASSERT(NS_SUCCEEDED(rv
), "Should be able to set a preference");
281 notifyCompletion(aReason
== REASON_FINISHED
);
287 Vacuumer::notifyCompletion(bool aSucceeded
)
289 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
291 os
->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO
,
292 OBSERVER_DATA_VACUUM_END
);
295 nsresult rv
= mParticipant
->OnEndVacuum(aSucceeded
);
296 NS_ENSURE_SUCCESS(rv
, rv
);
303 ////////////////////////////////////////////////////////////////////////////////
312 VacuumManager::gVacuumManager
= nullptr;
314 already_AddRefed
<VacuumManager
>
315 VacuumManager::getSingleton()
317 //Don't allocate it in the child Process.
318 if (!XRE_IsParentProcess()) {
322 if (!gVacuumManager
) {
323 auto manager
= MakeRefPtr
<VacuumManager
>();
324 MOZ_ASSERT(gVacuumManager
== manager
.get());
325 return manager
.forget();
327 return do_AddRef(gVacuumManager
);
330 VacuumManager::VacuumManager()
331 : mParticipants("vacuum-participant")
333 MOZ_ASSERT(!gVacuumManager
,
334 "Attempting to create two instances of the service!");
335 gVacuumManager
= this;
338 VacuumManager::~VacuumManager()
340 // Remove the static reference to the service. Check to make sure its us
341 // in case somebody creates an extra instance of the service.
342 MOZ_ASSERT(gVacuumManager
== this,
343 "Deleting a non-singleton instance of the service");
344 if (gVacuumManager
== this) {
345 gVacuumManager
= nullptr;
349 ////////////////////////////////////////////////////////////////////////////////
353 VacuumManager::Observe(nsISupports
*aSubject
,
355 const char16_t
*aData
)
357 if (strcmp(aTopic
, OBSERVER_TOPIC_IDLE_DAILY
) == 0) {
358 // Try to run vacuum on all registered entries. Will stop at the first
360 nsCOMArray
<mozIStorageVacuumParticipant
> entries
;
361 mParticipants
.GetEntries(entries
);
362 // If there are more entries than what a month can contain, we could end up
363 // skipping some, since we run daily. So we use a starting index.
364 static const char* kPrefName
= PREF_VACUUM_BRANCH
"index";
365 int32_t startIndex
= Preferences::GetInt(kPrefName
, 0);
366 if (startIndex
>= entries
.Count()) {
370 for (index
= startIndex
; index
< entries
.Count(); ++index
) {
371 RefPtr
<Vacuumer
> vacuum
= new Vacuumer(entries
[index
]);
372 // Only vacuum one database per day.
373 if (vacuum
->execute()) {
377 DebugOnly
<nsresult
> rv
= Preferences::SetInt(kPrefName
, index
);
378 MOZ_ASSERT(NS_SUCCEEDED(rv
), "Should be able to set a preference");
384 } // namespace storage
385 } // namespace mozilla