Bug 1613497 - Use the background thread pool for the predictor r=dragana
authorValentin Gosu <valentin.gosu@gmail.com>
Fri, 07 Feb 2020 10:55:24 +0000
changeset 512868 2f49bf59710be968997fff517292dff4ebab13c1
parent 512867 ed077a95bbc4cb8e6c7eb494ba767067ed1f6692
child 512869 5abf570c831e1597ebf762433863a049e6acf921
push id37100
push userapavel@mozilla.com
push dateFri, 07 Feb 2020 21:53:54 +0000
treeherdermozilla-central@882200a11bcf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdragana
bugs1613497
milestone74.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1613497 - Use the background thread pool for the predictor r=dragana Differential Revision: https://phabricator.services.mozilla.com/D61744
netwerk/base/Predictor.cpp
--- a/netwerk/base/Predictor.cpp
+++ b/netwerk/base/Predictor.cpp
@@ -58,23 +58,16 @@ namespace net {
 
 Predictor* Predictor::sSelf = nullptr;
 
 static LazyLogModule gPredictorLog("NetworkPredictor");
 
 #define PREDICTOR_LOG(args) \
   MOZ_LOG(gPredictorLog, mozilla::LogLevel::Debug, args)
 
-#define RETURN_IF_FAILED(_rv) \
-  do {                        \
-    if (NS_FAILED(_rv)) {     \
-      return;                 \
-    }                         \
-  } while (0)
-
 #define NOW_IN_SECONDS() static_cast<uint32_t>(PR_Now() / PR_USEC_PER_SEC)
 
 static const char PREDICTOR_CLEANED_UP_PREF[] = "network.predictor.cleaned-up";
 
 // All these time values are in sec
 static const uint32_t ONE_DAY = 86400U;
 static const uint32_t ONE_WEEK = 7U * ONE_DAY;
 static const uint32_t ONE_MONTH = 30U * ONE_DAY;
@@ -422,51 +415,46 @@ nsresult Predictor::Init() {
   mInitialized = true;
 
   return rv;
 }
 
 namespace {
 class PredictorThreadShutdownRunner : public Runnable {
  public:
-  PredictorThreadShutdownRunner(nsIThread* ioThread, bool success)
-      : Runnable("net::PredictorThreadShutdownRunner"),
-        mIOThread(ioThread),
-        mSuccess(success) {}
+  PredictorThreadShutdownRunner(bool success)
+      : Runnable("net::PredictorThreadShutdownRunner"), mSuccess(success) {}
   ~PredictorThreadShutdownRunner() = default;
 
   NS_IMETHOD Run() override {
     MOZ_ASSERT(NS_IsMainThread(), "Shutting down io thread off main thread!");
     if (mSuccess) {
       // This means the cleanup happened. Mark so we don't try in the
       // future.
       Preferences::SetBool(PREDICTOR_CLEANED_UP_PREF, true);
     }
-    return mIOThread->AsyncShutdown();
+    return NS_OK;
   }
 
  private:
-  nsCOMPtr<nsIThread> mIOThread;
   bool mSuccess;
 };
 
 class PredictorOldCleanupRunner : public Runnable {
  public:
-  PredictorOldCleanupRunner(nsIThread* ioThread, nsIFile* dbFile)
-      : Runnable("net::PredictorOldCleanupRunner"),
-        mIOThread(ioThread),
-        mDBFile(dbFile) {}
+  PredictorOldCleanupRunner(nsIFile* dbFile)
+      : Runnable("net::PredictorOldCleanupRunner"), mDBFile(dbFile) {}
 
   ~PredictorOldCleanupRunner() = default;
 
   NS_IMETHOD Run() override {
     MOZ_ASSERT(!NS_IsMainThread(), "Cleaning up old files on main thread!");
     nsresult rv = CheckForAndDeleteOldDBFiles();
     RefPtr<PredictorThreadShutdownRunner> runner =
-        new PredictorThreadShutdownRunner(mIOThread, NS_SUCCEEDED(rv));
+        new PredictorThreadShutdownRunner(NS_SUCCEEDED(rv));
     NS_DispatchToMainThread(runner);
     return NS_OK;
   }
 
  private:
   nsresult CheckForAndDeleteOldDBFiles() {
     nsCOMPtr<nsIFile> oldDBFile;
     nsresult rv = mDBFile->GetParent(getter_AddRefs(oldDBFile));
@@ -490,17 +478,16 @@ class PredictorOldCleanupRunner : public
 
     if (fileExists) {
       rv = mDBFile->Remove(false);
     }
 
     return rv;
   }
 
-  nsCOMPtr<nsIThread> mIOThread;
   nsCOMPtr<nsIFile> mDBFile;
 };
 
 class PredictorLearnRunnable final : public Runnable {
  public:
   PredictorLearnRunnable(nsIURI* targetURI, nsIURI* sourceURI,
                          PredictorLearnReason reason,
                          const OriginAttributes& oa)
@@ -551,27 +538,23 @@ void Predictor::MaybeCleanupOldDBFiles()
 
   mCleanedUp = true;
 
   // This is used for cleaning up junk left over from the old backend
   // built on top of sqlite, if necessary.
   nsCOMPtr<nsIFile> dbFile;
   nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
                                        getter_AddRefs(dbFile));
-  RETURN_IF_FAILED(rv);
+  NS_ENSURE_SUCCESS_VOID(rv);
   rv = dbFile->AppendNative(NS_LITERAL_CSTRING("netpredictions.sqlite"));
-  RETURN_IF_FAILED(rv);
-
-  nsCOMPtr<nsIThread> ioThread;
-  rv = NS_NewNamedThread("NetPredictClean", getter_AddRefs(ioThread));
-  RETURN_IF_FAILED(rv);
+  NS_ENSURE_SUCCESS_VOID(rv);
 
   RefPtr<PredictorOldCleanupRunner> runner =
-      new PredictorOldCleanupRunner(ioThread, dbFile);
-  ioThread->Dispatch(runner, NS_DISPATCH_NORMAL);
+      new PredictorOldCleanupRunner(dbFile);
+  NS_DispatchBackgroundTask(runner.forget());
 }
 
 void Predictor::Shutdown() {
   if (!NS_IsMainThread()) {
     MOZ_ASSERT(false, "Predictor::Shutdown called off the main thread!");
     return;
   }
 
@@ -1694,21 +1677,21 @@ void Predictor::SpaceCleaner::Finalize(n
 // helper functions above to update the database appropriately.
 void Predictor::LearnForSubresource(nsICacheEntry* entry, nsIURI* targetURI) {
   MOZ_ASSERT(NS_IsMainThread());
 
   PREDICTOR_LOG(("Predictor::LearnForSubresource"));
 
   uint32_t lastLoad;
   nsresult rv = entry->GetLastFetched(&lastLoad);
-  RETURN_IF_FAILED(rv);
+  NS_ENSURE_SUCCESS_VOID(rv);
 
   int32_t loadCount;
   rv = entry->GetFetchCount(&loadCount);
-  RETURN_IF_FAILED(rv);
+  NS_ENSURE_SUCCESS_VOID(rv);
 
   nsCString key;
   key.AssignLiteral(META_DATA_PREFIX);
   nsCString uri;
   targetURI->GetAsciiSpec(uri);
   key.Append(uri);
   if (uri.Length() > StaticPrefs::network_predictor_max_uri_length()) {
     // We do this to conserve space/prevent OOMs