update_engine: Add performance mode

Author: Gabriele M <moto.falcon.git@gmail.com>
Date:   Thu Jun 14 01:10:09 2018 +0200

    update_engine: Add performance mode

    Allow to move update_engine from the system-background cgroup
    to the foreground cgroup to speed up the installation of the
    updates.

    Change-Id: Iaa531a925f9e1a26e834d7448c4755151adcfea2

Author: Luca Stefani <luca.stefani.ge1@gmail.com>
Date:   Mon Mar 4 14:57:28 2019 +0100

    Move performance mode to top app

    Change-Id: I436102b4f8d046b8f3d897882613ec46acf9e2c5

luca020400: Move to TaskProfiles API

Change-Id: Id7e27b0c42d80c4a3ce1dc7f8540313a7042db53
diff --git a/Android.bp b/Android.bp
index cace5b6..b7b4cd4 100644
--- a/Android.bp
+++ b/Android.bp
@@ -194,6 +194,7 @@
         "libfec",
         "liblz4",
         "libziparchive",
+        "libprocessgroup",
     ],
 }
 
diff --git a/aosp/binder_service_android.cc b/aosp/binder_service_android.cc
index 84b5b7a..c2231fb 100644
--- a/aosp/binder_service_android.cc
+++ b/aosp/binder_service_android.cc
@@ -258,4 +258,11 @@
   return Status::ok();
 }
 
+Status BinderUpdateEngineAndroidService::setPerformanceMode(bool enable) {
+  brillo::ErrorPtr error;
+  if (!service_delegate_->SetPerformanceMode(enable, &error))
+    return ErrorPtrToStatus(error);
+  return Status::ok();
+}
+
 }  // namespace chromeos_update_engine
diff --git a/aosp/binder_service_android.h b/aosp/binder_service_android.h
index f1ce6b5..6bd1ee3 100644
--- a/aosp/binder_service_android.h
+++ b/aosp/binder_service_android.h
@@ -79,6 +79,7 @@
       int64_t* return_value) override;
   android::binder::Status cleanupSuccessfulUpdate(
       const android::sp<android::os::IUpdateEngineCallback>& callback) override;
+  android::binder::Status setPerformanceMode(bool enable) override;
 
  private:
   // Remove the passed |callback| from the list of registered callbacks. Called
diff --git a/aosp/service_delegate_android_interface.h b/aosp/service_delegate_android_interface.h
index b3660ab..b50f0f9 100644
--- a/aosp/service_delegate_android_interface.h
+++ b/aosp/service_delegate_android_interface.h
@@ -129,6 +129,8 @@
       std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface> callback,
       brillo::ErrorPtr* error) = 0;
 
+  virtual bool SetPerformanceMode(bool enable, brillo::ErrorPtr* error) = 0;
+
  protected:
   ServiceDelegateAndroidInterface() = default;
 };
diff --git a/aosp/update_attempter_android.cc b/aosp/update_attempter_android.cc
index a3485ea..7bef9db 100644
--- a/aosp/update_attempter_android.cc
+++ b/aosp/update_attempter_android.cc
@@ -31,6 +31,7 @@
 #include <brillo/message_loops/message_loop.h>
 #include <brillo/strings/string_utils.h>
 #include <log/log_safetynet.h>
+#include <processgroup/processgroup.h>
 
 #include "update_engine/aosp/cleanup_previous_update_action.h"
 #include "update_engine/common/constants.h"
@@ -145,6 +146,7 @@
   metrics_reporter_ = metrics::CreateMetricsReporter(
       boot_control_->GetDynamicPartitionControl(), &install_plan_);
   network_selector_ = network::CreateNetworkSelector();
+  SetTaskProfiles(0, {"OtaProfiles"});
 }
 
 UpdateAttempterAndroid::~UpdateAttempterAndroid() {
@@ -559,6 +561,23 @@
   return true;
 }
 
+bool UpdateAttempterAndroid::SetPerformanceMode(bool enable,
+                                                brillo::ErrorPtr* error) {
+  LOG(INFO) << (enable ? "Enabling" : "Disabling") << " performance mode.";
+
+  if (performance_mode_ == enable)
+    return true;
+  bool ret;
+  if (enable)
+    ret = SetTaskProfiles(0, {"ProcessCapacityMax", "HighIoPriority", "MaxPerformance"});
+  else
+    ret = SetTaskProfiles(0, {"OtaProfiles"});
+  if (!ret)
+    return LogAndSetError(error, FROM_HERE, "Could not change profiles");
+  performance_mode_ = enable;
+  return true;
+}
+
 void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor,
                                             ErrorCode code) {
   LOG(INFO) << "Processing Done.";
diff --git a/aosp/update_attempter_android.h b/aosp/update_attempter_android.h
index 5d832e0..4c5c70b 100644
--- a/aosp/update_attempter_android.h
+++ b/aosp/update_attempter_android.h
@@ -100,6 +100,8 @@
                                    brillo::ErrorPtr* error) override;
   bool resetShouldSwitchSlotOnReboot(brillo::ErrorPtr* error) override;
 
+  bool SetPerformanceMode(bool enable, brillo::ErrorPtr* error) override;
+
   // ActionProcessorDelegate methods:
   void ProcessingDone(const ActionProcessor* processor,
                       ErrorCode code) override;
@@ -282,6 +284,8 @@
   // The path to the zip file with X509 certificates.
   std::string update_certificates_path_{constants::kUpdateCertificatesPath};
 
+  bool performance_mode_ = false;
+
   DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
 };
 
diff --git a/aosp/update_engine_client_android.cc b/aosp/update_engine_client_android.cc
index c00e9f5..a350409 100644
--- a/aosp/update_engine_client_android.cc
+++ b/aosp/update_engine_client_android.cc
@@ -169,6 +169,7 @@
               false,
               "Wait for previous update to merge. "
               "Only available after rebooting to new slot.");
+  DEFINE_bool(perf_mode, false, "Enable perf mode.");
   // Boilerplate init commands.
   base::CommandLine::Init(argc_, argv_);
   brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client");
@@ -292,6 +293,10 @@
     keep_running = true;
   }
 
+  if (FLAGS_perf_mode) {
+    return ExitWhenIdle(service_->setPerformanceMode(true));
+  }
+
   if (FLAGS_update) {
     auto and_headers = ParseHeaders(FLAGS_headers);
     Status status = service_->applyPayload(
diff --git a/binder_bindings/android/os/IUpdateEngine.aidl b/binder_bindings/android/os/IUpdateEngine.aidl
index 4043b1a..061397c 100644
--- a/binder_bindings/android/os/IUpdateEngine.aidl
+++ b/binder_bindings/android/os/IUpdateEngine.aidl
@@ -76,4 +76,6 @@
    * but needs reboot). DEVICE_CORRUPTED for permanent errors.
    */
   void cleanupSuccessfulUpdate(IUpdateEngineCallback callback);
+  /** @hide */
+  void setPerformanceMode(in boolean enable);
 }
diff --git a/update_engine.rc b/update_engine.rc
index bc6447b..76072d5 100644
--- a/update_engine.rc
+++ b/update_engine.rc
@@ -2,7 +2,6 @@
     class late_start
     user root
     group root system wakelock inet cache media_rw
-    task_profiles OtaProfiles
     disabled
 
 on property:ro.boot.slot_suffix=*