Utilize the new TracingServiceProxy for Traceur

Replaces the old sysprop based Traceur interface with an aidl based one.

Bug: 175591887
Test: Verified long trace ended as expected.
Change-Id: I596bddedb02f964abc76a2c63907ba30a8de82cb
diff --git a/Android.bp b/Android.bp
index 6654141..94faac6 100644
--- a/Android.bp
+++ b/Android.bp
@@ -624,6 +624,7 @@
     "src/android_internal/incident_service.cc",
     "src/android_internal/power_stats_hal.cc",
     "src/android_internal/statsd_logging.cc",
+    "src/android_internal/tracing_service_proxy.cc",
   ],
   shared_libs: [
     "android.hardware.atrace@1.0",
@@ -637,6 +638,7 @@
     "liblog",
     "libservices",
     "libstatssocket",
+    "libtracingproxy",
     "libutils",
   ],
   static_libs: [
diff --git a/BUILD b/BUILD
index 6b1204d..db77fd5 100644
--- a/BUILD
+++ b/BUILD
@@ -547,6 +547,7 @@
         "src/android_internal/incident_service.h",
         "src/android_internal/power_stats_hal.h",
         "src/android_internal/statsd_logging.h",
+        "src/android_internal/tracing_service_proxy.h",
     ],
 )
 
diff --git a/src/android_internal/BUILD.gn b/src/android_internal/BUILD.gn
index 9d196f6..6ef8f58 100644
--- a/src/android_internal/BUILD.gn
+++ b/src/android_internal/BUILD.gn
@@ -33,6 +33,7 @@
       "incident_service.cc",
       "power_stats_hal.cc",
       "statsd_logging.cc",
+      "tracing_service_proxy.cc",
     ]
     libs = [
       "android.hardware.health@2.0",
@@ -47,6 +48,7 @@
       "hidlbase",
       "incident",
       "services",
+      "tracingproxy",
       "utils",
     ]
 
@@ -71,6 +73,7 @@
     "incident_service.h",
     "power_stats_hal.h",
     "statsd_logging.h",
+    "tracing_service_proxy.h",
   ]
 }
 
diff --git a/src/android_internal/tracing_service_proxy.cc b/src/android_internal/tracing_service_proxy.cc
new file mode 100644
index 0000000..e297b75
--- /dev/null
+++ b/src/android_internal/tracing_service_proxy.cc
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+#include "src/android_internal/tracing_service_proxy.h"
+
+#include <android/tracing/ITracingServiceProxy.h>
+#include <binder/IBinder.h>
+#include <binder/IServiceManager.h>
+#include <binder/Status.h>
+
+namespace perfetto {
+namespace android_internal {
+
+using android::sp;
+using android::binder::Status;
+using android::tracing::ITracingServiceProxy;
+
+bool NotifyTraceSessionEnded(bool session_stolen) {
+  sp<ITracingServiceProxy> service = android::interface_cast<ITracingServiceProxy>(
+      android::defaultServiceManager()->getService(android::String16("tracing.proxy")));
+
+  if (service == nullptr) {
+    return false;
+  }
+
+  Status s = service->notifyTraceSessionEnded(session_stolen);
+  return s.isOk();
+}
+
+} // namespace android_internal
+} // namespace perfetto
diff --git a/src/android_internal/tracing_service_proxy.h b/src/android_internal/tracing_service_proxy.h
new file mode 100644
index 0000000..0b045e2
--- /dev/null
+++ b/src/android_internal/tracing_service_proxy.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2021 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 SRC_ANDROID_INTERNAL_TRACING_SERVICE_PROXY_H_
+#define SRC_ANDROID_INTERNAL_TRACING_SERVICE_PROXY_H_
+
+namespace perfetto {
+namespace android_internal {
+
+extern "C" {
+
+bool __attribute__((visibility("default")))
+NotifyTraceSessionEnded(bool session_stolen);
+
+} // extern "C"
+
+} // namespace android_internal
+} // namespace perfetto
+#endif  // SRC_ANDROID_INTERNAL_TRACING_SERVICE_PROXY_H_
diff --git a/src/tracing/core/BUILD.gn b/src/tracing/core/BUILD.gn
index 1a3197f..1c042d3 100644
--- a/src/tracing/core/BUILD.gn
+++ b/src/tracing/core/BUILD.gn
@@ -72,6 +72,12 @@
     "tracing_service_impl.cc",
     "tracing_service_impl.h",
   ]
+  if (is_android && perfetto_build_with_android) {
+    deps += [
+      "../../android_internal:lazy_library_loader",
+      "../../android_internal:headers",
+    ]
+  }
 }
 
 perfetto_unittest_source_set("unittests") {
diff --git a/src/tracing/core/tracing_service_impl.cc b/src/tracing/core/tracing_service_impl.cc
index 26da3a6..90fe4f0 100644
--- a/src/tracing/core/tracing_service_impl.cc
+++ b/src/tracing/core/tracing_service_impl.cc
@@ -34,7 +34,11 @@
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
 #include <sys/system_properties.h>
-#endif
+#if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
+#include "src/android_internal/lazy_library_loader.h"    // nogncheck
+#include "src/android_internal/tracing_service_proxy.h"  // nogncheck
+#endif // PERFETTO_ANDROID_BUILD
+#endif // PERFETTO_OS_ANDROID
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
     PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
@@ -116,6 +120,7 @@
 constexpr int64_t kMinSecondsBetweenTracesGuardrail = 5 * 60;
 
 constexpr uint32_t kMillisPerHour = 3600000;
+constexpr uint32_t kMillisPerDay = kMillisPerHour * 24;
 constexpr uint32_t kMaxTracingDurationMillis = 7 * 24 * kMillisPerHour;
 
 // These apply only if enable_extra_guardrails is true.
@@ -2133,18 +2138,27 @@
     buffers_.erase(buffer_id);
   }
   bool notify_traceur = tracing_session->config.notify_traceur();
+  bool is_long_trace = (tracing_session->config.write_into_file() &&
+          tracing_session->config.file_write_period_ms() < kMillisPerDay);
+  bool seized_for_bugreport = tracing_session->seized_for_bugreport;
   tracing_sessions_.erase(tsid);
+  tracing_session = nullptr;
   UpdateMemoryGuardrail();
 
   PERFETTO_LOG("Tracing session %" PRIu64 " ended, total sessions:%zu", tsid,
                tracing_sessions_.size());
 
-#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
-  static const char kTraceurProp[] = "sys.trace.trace_end_signal";
-  if (notify_traceur && __system_property_set(kTraceurProp, "1"))
-    PERFETTO_ELOG("Failed to setprop %s=1", kTraceurProp);
+#if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD) && \
+    PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
+  if (notify_traceur && (seized_for_bugreport || is_long_trace)) {
+    PERFETTO_LAZY_LOAD(android_internal::NotifyTraceSessionEnded, notify_fn);
+    if (!notify_fn || !notify_fn(seized_for_bugreport))
+      PERFETTO_ELOG("Failed to notify Traceur long tracing has ended");
+  }
 #else
   base::ignore_result(notify_traceur);
+  base::ignore_result(is_long_trace);
+  base::ignore_result(seized_for_bugreport);
 #endif
 }
 
diff --git a/tools/gen_android_bp b/tools/gen_android_bp
index a01096c..f309524 100755
--- a/tools/gen_android_bp
+++ b/tools/gen_android_bp
@@ -117,6 +117,7 @@
     'log',
     'services',
     'statssocket',
+    "tracingproxy",
     'utils',
 ]