summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/res/AndroidManifest.xml4
-rw-r--r--services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java87
2 files changed, 81 insertions, 10 deletions
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 013249335268..f1e186813ad8 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -5465,6 +5465,10 @@
android:permission="android.permission.BIND_JOB_SERVICE" >
</service>
+ <service android:name="com.android.server.profcollect.ProfcollectForwardingService$ProfcollectBGJobService"
+ android:permission="android.permission.BIND_JOB_SERVICE" >
+ </service>
+
<service
android:name="com.android.server.autofill.AutofillCompatAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
diff --git a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
index 12c69eaa0f8d..ddd1f7568224 100644
--- a/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
+++ b/services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java
@@ -16,6 +16,11 @@
package com.android.server.profcollect;
+import android.app.job.JobInfo;
+import android.app.job.JobParameters;
+import android.app.job.JobScheduler;
+import android.app.job.JobService;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
@@ -34,6 +39,7 @@ import com.android.server.wm.ActivityMetricsLaunchObserverRegistry;
import com.android.server.wm.ActivityTaskManagerInternal;
import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
/**
* System-server-local proxy into the {@code IProfcollectd} native service.
@@ -41,28 +47,43 @@ import java.util.concurrent.ThreadLocalRandom;
public final class ProfcollectForwardingService extends SystemService {
public static final String LOG_TAG = "ProfcollectForwardingService";
+ private static final boolean DEBUG = Log.isLoggable(LOG_TAG, Log.DEBUG);
+
+ private static final long BG_PROCESS_PERIOD = DEBUG
+ ? TimeUnit.MINUTES.toMillis(1)
+ : TimeUnit.DAYS.toMillis(1);
+
private IProfCollectd mIProfcollect;
- private ProfcollectForwardingService mSelfService;
+ private static ProfcollectForwardingService sSelfService;
private final Handler mHandler = new ProfcollectdHandler(IoThread.getHandler().getLooper());
public ProfcollectForwardingService(Context context) {
super(context);
- if (mSelfService != null) {
+ if (sSelfService != null) {
throw new AssertionError("only one service instance allowed");
}
- mSelfService = this;
+ sSelfService = this;
}
@Override
public void onStart() {
- Log.i(LOG_TAG, "Profcollect forwarding service start");
- connectNativeService();
- if (mIProfcollect == null) {
- return;
+ if (DEBUG) {
+ Log.d(LOG_TAG, "Profcollect forwarding service start");
}
- if (serviceHasSupportedTraceProvider()) {
- registerObservers();
+ connectNativeService();
+ }
+
+ @Override
+ public void onBootPhase(int phase) {
+ if (phase == PHASE_BOOT_COMPLETED) {
+ if (mIProfcollect == null) {
+ return;
+ }
+ if (serviceHasSupportedTraceProvider()) {
+ registerObservers();
+ }
+ ProfcollectBGJobService.schedule(getContext());
}
}
@@ -130,6 +151,50 @@ public final class ProfcollectForwardingService extends SystemService {
}
}
+ /**
+ * Background trace process service.
+ */
+ public static class ProfcollectBGJobService extends JobService {
+ // Unique ID in system service
+ private static final int JOB_IDLE_PROCESS = 260817;
+ private static final ComponentName JOB_SERVICE_NAME = new ComponentName(
+ "android",
+ ProfcollectBGJobService.class.getName());
+
+ /**
+ * Attach the service to the system job scheduler.
+ */
+ public static void schedule(Context context) {
+ JobScheduler js = context.getSystemService(JobScheduler.class);
+
+ js.schedule(new JobInfo.Builder(JOB_IDLE_PROCESS, JOB_SERVICE_NAME)
+ .setRequiresDeviceIdle(true)
+ .setRequiresCharging(true)
+ .setPeriodic(BG_PROCESS_PERIOD)
+ .build());
+ }
+
+ @Override
+ public boolean onStartJob(JobParameters params) {
+ if (DEBUG) {
+ Log.d(LOG_TAG, "Starting background process job");
+ }
+
+ try {
+ sSelfService.mIProfcollect.ProcessProfile();
+ } catch (RemoteException e) {
+ Log.e(LOG_TAG, e.getMessage());
+ }
+ return true;
+ }
+
+ @Override
+ public boolean onStopJob(JobParameters params) {
+ // TODO: Handle this?
+ return false;
+ }
+ }
+
// Event observers
private void registerObservers() {
registerAppLaunchObserver();
@@ -155,7 +220,9 @@ public final class ProfcollectForwardingService extends SystemService {
int randomNum = ThreadLocalRandom.current().nextInt(100);
if (randomNum < traceFrequency) {
try {
- Log.i(LOG_TAG, "Tracing on app launch event: " + packageName);
+ if (DEBUG) {
+ Log.d(LOG_TAG, "Tracing on app launch event: " + packageName);
+ }
mIProfcollect.TraceOnce("applaunch");
} catch (RemoteException e) {
Log.e(LOG_TAG, e.getMessage());