diff options
author | Yi Kong <yikong@google.com> | 2020-09-09 13:38:36 +0800 |
---|---|---|
committer | Yi Kong <yikong@google.com> | 2020-09-11 05:57:23 +0800 |
commit | 05302b72f6bb08e4c84afff6c3b035a7c9833cb5 (patch) | |
tree | 05b7be561d7dcd3bb363154f5bed9882ec27a26c /services/profcollect | |
parent | b6bb23ce277e44fb36e3ccede7a6dfba674d917a (diff) |
profcollectd: trigger trace records processing during system idle
Add a JobService to trigger trace records processing when the device is
idle and connected to a charger, saving profile disk space that
accumulates over time.
Bug: 79161490
Test: build
Merged-In: Ibb50fb1d05e52be76a63dbf95884b788965eb979
Change-Id: I10ab56a8f0727b0660d1bcd9ba43d0b39a691084
Diffstat (limited to 'services/profcollect')
-rw-r--r-- | services/profcollect/src/com/android/server/profcollect/ProfcollectForwardingService.java | 87 |
1 files changed, 77 insertions, 10 deletions
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()); |