Snap for 7110675 from 44176c75f7ee5b9f046abaa3ae513ad7f2a5ad6e to sdk-release

Change-Id: Ie45edc745ffcd08930f3b2e31467e3f32e7307ce
diff --git a/Android.bp b/Android.bp
index 138ba4b..56d8370 100644
--- a/Android.bp
+++ b/Android.bp
@@ -66,6 +66,11 @@
 
     // build all ioraps for host.
     host_supported: true,
+    target: {
+        darwin: {
+            enabled: false,
+        }
+    },
 }
 
 cc_defaults {
@@ -251,6 +256,11 @@
 cc_test_host {
     name: "iorapd-host-tests",
     gtest: false,  // we use gtest *and* gmock.
+    target: {
+        darwin: {
+            enabled: false,
+        }
+    },
     defaults: [
         "iorap-default-flags",
         "iorap-default-dependencies",
diff --git a/iorapd.rc b/iorapd.rc
index 8f8e014..cb887ac 100644
--- a/iorapd.rc
+++ b/iorapd.rc
@@ -14,6 +14,7 @@
 
 service iorapd /system/bin/iorapd
     class main
+    disabled
     user iorapd
     group iorapd
     capabilities DAC_READ_SEARCH
@@ -28,3 +29,10 @@
 on post-fs-data
     # Create directory for iorapd (see iorapd_data_file in selinux file_contexts).
     mkdir /data/misc/iorapd/ 0750 iorapd system
+
+# Start iorapd when either prefetching or tracing is enabled.
+on property:persist.device_config.runtime_native_boot.iorap_perfetto_enable=true && property:ro.iorapd.enable=true
+    start iorapd
+
+on property:persist.device_config.runtime_native_boot.iorap_readahead_enable=true && property:ro.iorapd.enable=true
+    start iorapd
diff --git a/src/binder/iiorap_impl.cc b/src/binder/iiorap_impl.cc
index de5a55d..13adcc0 100644
--- a/src/binder/iiorap_impl.cc
+++ b/src/binder/iiorap_impl.cc
@@ -291,8 +291,9 @@
     package_manager_ = PackageManagerRemote::Create();
 
     if (package_manager_ == nullptr) {
-      LOG(FATAL) << "Failed to get package manager service in IIorapImpl::Impl";
-      return;
+      LOG(ERROR) << "Failed to get package manager service in IIorapImpl::Impl."
+                 << " Is system_server down?";
+      exit(1);
     }
 
     package_change_observer_ =
diff --git a/src/binder/package_manager_remote.cc b/src/binder/package_manager_remote.cc
index daeb6af..46671eb 100644
--- a/src/binder/package_manager_remote.cc
+++ b/src/binder/package_manager_remote.cc
@@ -115,7 +115,7 @@
     package_service_ = GetPackageService();
     std::this_thread::sleep_for(interval);
     if (count * interval >= timeout) {
-      LOG(FATAL) << "Fail to create version map in "
+      LOG(ERROR) << "Fail to create version map in "
                  << timeout.count()
                  << " milliseconds."
                  << " Reason: Failed to connect to package manager service."
@@ -151,7 +151,8 @@
       });
 
   if (!status.isOk()) {
-    LOG(FATAL) << "Cannot register package change observer.";
+    LOG(ERROR) << "Cannot register package change observer. Is system_server down?";
+    exit(1);
   }
 }
 
@@ -181,7 +182,8 @@
   if (!ReconnectWithTimeout(kTimeoutMs) ||
       android::OK != android::IInterface::asBinder(
           package_service_.get())->linkToDeath(death_recipient)) {
-    LOG(FATAL) << "Failed to register package manager death recipient.";
+    LOG(ERROR) << "Failed to register package manager death recipient. Is system_server down?";
+    exit(1);
   }
 }
 
diff --git a/src/common/property.h b/src/common/property.h
new file mode 100644
index 0000000..214ba3c
--- /dev/null
+++ b/src/common/property.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef IORAP_UTILS_PROPERTY_H_
+#define IORAP_UTILS_PROPERTY_H_
+
+#include <android-base/properties.h>
+#include <server_configurable_flags/get_flags.h>
+
+namespace iorap::common {
+
+constexpr const char* ph_namespace = "runtime_native_boot";
+
+inline bool IsTracingEnabled(const std::string& default_value) {
+  return server_configurable_flags::GetServerConfigurableFlag(
+      ph_namespace,
+      "iorap_perfetto_enable",
+      ::android::base::GetProperty("iorapd.perfetto.enable", default_value)) == "true";
+}
+
+inline bool IsReadAheadEnabled(const std::string& default_value) {
+  return server_configurable_flags::GetServerConfigurableFlag(
+      ph_namespace,
+      "iorap_readahead_enable",
+      ::android::base::GetProperty("iorapd.readahead.enable", default_value)) == "true";
+}
+
+}  // namespace iorap::common
+
+#endif  // IORAP_UTILS_PROPERTY_H_
diff --git a/src/iorapd/main.cc b/src/iorapd/main.cc
index 8d806ba..a3a63f2 100644
--- a/src/iorapd/main.cc
+++ b/src/iorapd/main.cc
@@ -17,12 +17,14 @@
 #include "binder/iiorap_impl.h"
 #include "common/debug.h"
 #include "common/loggers.h"
+#include "common/property.h"
 #include "db/models.h"
 #include "manager/event_manager.h"
 
 #include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <binder/IPCThreadState.h>
+#include <server_configurable_flags/get_flags.h>
 #include <utils/Trace.h>
 
 #include <stdio.h>
@@ -30,6 +32,13 @@
 static constexpr const char* kServiceName = iorap::binder::IIorapImpl::getServiceName();
 
 int main(int /*argc*/, char** argv) {
+  bool tracing_allowed = iorap::common::IsTracingEnabled(/*default_value=*/"false");
+  bool readahead_allowed = iorap::common::IsReadAheadEnabled(/*default_value*/"false");
+  if (!tracing_allowed && !readahead_allowed) {
+    LOG(INFO) << "Turn off IORap because both tracing and prefetching are off.";
+    return 0;
+  }
+
   if (android::base::GetBoolProperty("iorapd.log.verbose", iorap::kIsDebugBuild)) {
     // Show verbose logs if the property is enabled or if we are a debug build.
     setenv("ANDROID_LOG_TAGS", "*:v", /*overwrite*/ 1);
diff --git a/src/manager/event_manager.cc b/src/manager/event_manager.cc
index bae676b..3ea7b12 100644
--- a/src/manager/event_manager.cc
+++ b/src/manager/event_manager.cc
@@ -19,6 +19,7 @@
 #include "common/expected.h"
 #include "common/printer.h"
 #include "common/rx_async.h"
+#include "common/property.h"
 #include "common/trace.h"
 #include "db/app_component_name.h"
 #include "db/file_models.h"
@@ -1226,18 +1227,11 @@
   void RefreshSystemProperties(::android::Printer& printer) {
     // TODO: read all properties from one config class.
     // PH properties do not work if they contain ".". "_" was instead used here.
-    const char* ph_namespace = "runtime_native_boot";
-    tracing_allowed_ = server_configurable_flags::GetServerConfigurableFlag(
-        ph_namespace,
-        "iorap_perfetto_enable",
-        ::android::base::GetProperty("iorapd.perfetto.enable", /*default*/"true")) == "true";
+    tracing_allowed_ = common::IsTracingEnabled(/*default_value=*/"false");
     s_tracing_allowed = tracing_allowed_;
     printer.printFormatLine("iorapd.perfetto.enable = %s", tracing_allowed_ ? "true" : "false");
 
-    readahead_allowed_ = server_configurable_flags::GetServerConfigurableFlag(
-        ph_namespace,
-        "iorap_readahead_enable",
-        ::android::base::GetProperty("iorapd.readahead.enable", /*default*/"true")) == "true";
+    readahead_allowed_ = common::IsReadAheadEnabled(/*default_value=*/"false");
     s_readahead_allowed = readahead_allowed_;
     printer.printFormatLine("iorapd.readahead.enable = %s", s_readahead_allowed ? "true" : "false");
 
@@ -1253,7 +1247,7 @@
          * Blacklisted packages are ignored by iorapd.
          */
         server_configurable_flags::GetServerConfigurableFlag(
-            ph_namespace,
+            common::ph_namespace,
             "iorap_blacklisted_packages",
             ::android::base::GetProperty("iorapd.blacklist_packages",
                                          /*default*/""))