blob: 53bdbeaeccc418a45afa4039c31c2ac565063150 [file] [log] [blame]
Christopher Tate7060b042014-06-09 19:50:00 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.job;
18
19import java.io.FileDescriptor;
20import java.io.PrintWriter;
21import java.util.ArrayList;
22import java.util.Iterator;
23import java.util.List;
24
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070025import android.app.ActivityManager;
Christopher Tate5568f542014-06-18 13:53:31 -070026import android.app.AppGlobals;
Christopher Tate7060b042014-06-09 19:50:00 -070027import android.app.job.JobInfo;
28import android.app.job.JobScheduler;
29import android.app.job.JobService;
30import android.app.job.IJobScheduler;
31import android.content.BroadcastReceiver;
32import android.content.ComponentName;
33import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
Christopher Tate5568f542014-06-18 13:53:31 -070036import android.content.pm.IPackageManager;
Christopher Tate7060b042014-06-09 19:50:00 -070037import android.content.pm.PackageManager;
Christopher Tate7060b042014-06-09 19:50:00 -070038import android.content.pm.ServiceInfo;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070039import android.os.BatteryStats;
Christopher Tate7060b042014-06-09 19:50:00 -070040import android.os.Binder;
41import android.os.Handler;
42import android.os.Looper;
43import android.os.Message;
Dianne Hackborn88e98df2015-03-23 13:29:14 -070044import android.os.PowerManager;
Christopher Tate7060b042014-06-09 19:50:00 -070045import android.os.RemoteException;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070046import android.os.ServiceManager;
Christopher Tate7060b042014-06-09 19:50:00 -070047import android.os.SystemClock;
48import android.os.UserHandle;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070049import android.util.ArraySet;
Christopher Tate7060b042014-06-09 19:50:00 -070050import android.util.Slog;
51import android.util.SparseArray;
52
Dianne Hackbornfdb19562014-07-11 16:03:36 -070053import com.android.internal.app.IBatteryStats;
Christopher Tate7060b042014-06-09 19:50:00 -070054import com.android.server.job.controllers.BatteryController;
55import com.android.server.job.controllers.ConnectivityController;
56import com.android.server.job.controllers.IdleController;
57import com.android.server.job.controllers.JobStatus;
58import com.android.server.job.controllers.StateController;
59import com.android.server.job.controllers.TimeController;
60
Christopher Tate7060b042014-06-09 19:50:00 -070061/**
62 * Responsible for taking jobs representing work to be performed by a client app, and determining
63 * based on the criteria specified when that job should be run against the client application's
64 * endpoint.
65 * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing
66 * about constraints, or the state of active jobs. It receives callbacks from the various
67 * controllers and completed jobs and operates accordingly.
68 *
69 * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object.
70 * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}.
71 * @hide
72 */
73public class JobSchedulerService extends com.android.server.SystemService
Matthew Williams01ac45b2014-07-22 20:44:12 -070074 implements StateChangedListener, JobCompletedListener {
Georgi Nikolov326f2302014-10-08 11:50:50 -070075 static final boolean DEBUG = false;
Christopher Tate7060b042014-06-09 19:50:00 -070076 /** The number of concurrent jobs we run at one time. */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070077 private static final int MAX_JOB_CONTEXTS_COUNT
78 = ActivityManager.isLowRamDeviceStatic() ? 1 : 3;
Matthew Williamsbe0c4172014-08-06 18:14:16 -070079 static final String TAG = "JobSchedulerService";
Christopher Tate7060b042014-06-09 19:50:00 -070080 /** Master list of jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070081 final JobStore mJobs;
Christopher Tate7060b042014-06-09 19:50:00 -070082
83 static final int MSG_JOB_EXPIRED = 0;
84 static final int MSG_CHECK_JOB = 1;
85
86 // Policy constants
87 /**
88 * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things
89 * early.
90 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070091 static final int MIN_IDLE_COUNT = 1;
Christopher Tate7060b042014-06-09 19:50:00 -070092 /**
Matthew Williamsbe0c4172014-08-06 18:14:16 -070093 * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things
94 * early.
95 */
96 static final int MIN_CHARGING_COUNT = 1;
97 /**
Christopher Tate7060b042014-06-09 19:50:00 -070098 * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule
99 * things early.
100 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700101 static final int MIN_CONNECTIVITY_COUNT = 2;
Christopher Tate7060b042014-06-09 19:50:00 -0700102 /**
103 * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running
104 * some work early.
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700105 * This is correlated with the amount of batching we'll be able to do.
Christopher Tate7060b042014-06-09 19:50:00 -0700106 */
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700107 static final int MIN_READY_JOBS_COUNT = 2;
Christopher Tate7060b042014-06-09 19:50:00 -0700108
109 /**
110 * Track Services that have currently active or pending jobs. The index is provided by
111 * {@link JobStatus#getServiceToken()}
112 */
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700113 final List<JobServiceContext> mActiveServices = new ArrayList<>();
Christopher Tate7060b042014-06-09 19:50:00 -0700114 /** List of controllers that will notify this service of updates to jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700115 List<StateController> mControllers;
Christopher Tate7060b042014-06-09 19:50:00 -0700116 /**
117 * Queue of pending jobs. The JobServiceContext class will receive jobs from this list
118 * when ready to execute them.
119 */
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700120 final ArrayList<JobStatus> mPendingJobs = new ArrayList<>();
Christopher Tate7060b042014-06-09 19:50:00 -0700121
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700122 final ArrayList<Integer> mStartedUsers = new ArrayList<>();
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700123
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700124 final JobHandler mHandler;
125 final JobSchedulerStub mJobSchedulerStub;
126
127 IBatteryStats mBatteryStats;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700128 PowerManager mPowerManager;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700129
130 /**
131 * Set to true once we are allowed to run third party apps.
132 */
133 boolean mReadyToRock;
134
Christopher Tate7060b042014-06-09 19:50:00 -0700135 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700136 * True when in device idle mode, so we don't want to schedule any jobs.
137 */
138 boolean mDeviceIdleMode;
139
140 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700141 * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we
142 * still clean up. On reinstall the package will have a new uid.
143 */
144 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
145 @Override
146 public void onReceive(Context context, Intent intent) {
147 Slog.d(TAG, "Receieved: " + intent.getAction());
148 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
Christopher Tateaad67a32014-10-20 16:29:20 -0700149 // If this is an outright uninstall rather than the first half of an
150 // app update sequence, cancel the jobs associated with the app.
151 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
152 int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1);
153 if (DEBUG) {
154 Slog.d(TAG, "Removing jobs for uid: " + uidRemoved);
155 }
156 cancelJobsForUid(uidRemoved);
Christopher Tate7060b042014-06-09 19:50:00 -0700157 }
Christopher Tate7060b042014-06-09 19:50:00 -0700158 } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) {
159 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
160 if (DEBUG) {
161 Slog.d(TAG, "Removing jobs for user: " + userId);
162 }
163 cancelJobsForUser(userId);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700164 } else if (PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())) {
165 updateIdleMode(mPowerManager != null ? mPowerManager.isDeviceIdleMode() : false);
Christopher Tate7060b042014-06-09 19:50:00 -0700166 }
167 }
168 };
169
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700170 @Override
171 public void onStartUser(int userHandle) {
172 mStartedUsers.add(userHandle);
173 // Let's kick any outstanding jobs for this user.
174 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
175 }
176
177 @Override
178 public void onStopUser(int userHandle) {
179 mStartedUsers.remove(Integer.valueOf(userHandle));
180 }
181
Christopher Tate7060b042014-06-09 19:50:00 -0700182 /**
183 * Entry point from client to schedule the provided job.
184 * This cancels the job if it's already been scheduled, and replaces it with the one provided.
185 * @param job JobInfo object containing execution parameters
186 * @param uId The package identifier of the application this job is for.
Christopher Tate7060b042014-06-09 19:50:00 -0700187 * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes.
188 */
Matthew Williams900c67f2014-07-09 12:46:53 -0700189 public int schedule(JobInfo job, int uId) {
190 JobStatus jobStatus = new JobStatus(job, uId);
Christopher Tate7060b042014-06-09 19:50:00 -0700191 cancelJob(uId, job.getId());
192 startTrackingJob(jobStatus);
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700193 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700194 return JobScheduler.RESULT_SUCCESS;
195 }
196
197 public List<JobInfo> getPendingJobs(int uid) {
198 ArrayList<JobInfo> outList = new ArrayList<JobInfo>();
199 synchronized (mJobs) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700200 ArraySet<JobStatus> jobs = mJobs.getJobs();
201 for (int i=0; i<jobs.size(); i++) {
202 JobStatus job = jobs.valueAt(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700203 if (job.getUid() == uid) {
204 outList.add(job.getJob());
205 }
206 }
207 }
208 return outList;
209 }
210
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700211 void cancelJobsForUser(int userHandle) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700212 List<JobStatus> jobsForUser;
Christopher Tate7060b042014-06-09 19:50:00 -0700213 synchronized (mJobs) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700214 jobsForUser = mJobs.getJobsByUser(userHandle);
215 }
216 for (int i=0; i<jobsForUser.size(); i++) {
217 JobStatus toRemove = jobsForUser.get(i);
218 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700219 }
220 }
221
222 /**
223 * Entry point from client to cancel all jobs originating from their uid.
224 * This will remove the job from the master list, and cancel the job if it was staged for
225 * execution or being executed.
Matthew Williams48a30db2014-09-23 13:39:36 -0700226 * @param uid Uid to check against for removal of a job.
Christopher Tate7060b042014-06-09 19:50:00 -0700227 */
228 public void cancelJobsForUid(int uid) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700229 List<JobStatus> jobsForUid;
Christopher Tate7060b042014-06-09 19:50:00 -0700230 synchronized (mJobs) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700231 jobsForUid = mJobs.getJobsByUid(uid);
232 }
233 for (int i=0; i<jobsForUid.size(); i++) {
234 JobStatus toRemove = jobsForUid.get(i);
235 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700236 }
237 }
238
239 /**
240 * Entry point from client to cancel the job corresponding to the jobId provided.
241 * This will remove the job from the master list, and cancel the job if it was staged for
242 * execution or being executed.
243 * @param uid Uid of the calling client.
244 * @param jobId Id of the job, provided at schedule-time.
245 */
246 public void cancelJob(int uid, int jobId) {
247 JobStatus toCancel;
248 synchronized (mJobs) {
249 toCancel = mJobs.getJobByUidAndJobId(uid, jobId);
Matthew Williams48a30db2014-09-23 13:39:36 -0700250 }
251 if (toCancel != null) {
252 cancelJobImpl(toCancel);
Christopher Tate7060b042014-06-09 19:50:00 -0700253 }
254 }
255
Matthew Williams48a30db2014-09-23 13:39:36 -0700256 private void cancelJobImpl(JobStatus cancelled) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700257 if (DEBUG) {
258 Slog.d(TAG, "Cancelling: " + cancelled);
259 }
Christopher Tate7060b042014-06-09 19:50:00 -0700260 stopTrackingJob(cancelled);
Matthew Williams48a30db2014-09-23 13:39:36 -0700261 synchronized (mJobs) {
262 // Remove from pending queue.
263 mPendingJobs.remove(cancelled);
264 // Cancel if running.
265 stopJobOnServiceContextLocked(cancelled);
266 }
Christopher Tate7060b042014-06-09 19:50:00 -0700267 }
268
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700269 void updateIdleMode(boolean enabled) {
270 boolean changed = false;
271 boolean rocking;
272 synchronized (mJobs) {
273 if (mDeviceIdleMode != enabled) {
274 changed = true;
275 }
276 rocking = mReadyToRock;
277 }
278 if (changed) {
279 if (rocking) {
280 for (int i=0; i<mControllers.size(); i++) {
281 mControllers.get(i).deviceIdleModeChanged(enabled);
282 }
283 }
284 synchronized (mJobs) {
285 mDeviceIdleMode = enabled;
286 if (enabled) {
287 // When becoming idle, make sure no jobs are actively running.
288 for (int i=0; i<mActiveServices.size(); i++) {
289 JobServiceContext jsc = mActiveServices.get(i);
290 final JobStatus executing = jsc.getRunningJob();
291 if (executing != null) {
292 jsc.cancelExecutingJob();
293 }
294 }
295 } else {
296 // When coming out of idle, allow thing to start back up.
297 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
298 }
299 }
300 }
301 }
302
Christopher Tate7060b042014-06-09 19:50:00 -0700303 /**
304 * Initializes the system service.
305 * <p>
306 * Subclasses must define a single argument constructor that accepts the context
307 * and passes it to super.
308 * </p>
309 *
310 * @param context The system server context.
311 */
312 public JobSchedulerService(Context context) {
313 super(context);
314 // Create the controllers.
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700315 mControllers = new ArrayList<StateController>();
Christopher Tate7060b042014-06-09 19:50:00 -0700316 mControllers.add(ConnectivityController.get(this));
317 mControllers.add(TimeController.get(this));
318 mControllers.add(IdleController.get(this));
319 mControllers.add(BatteryController.get(this));
320
321 mHandler = new JobHandler(context.getMainLooper());
322 mJobSchedulerStub = new JobSchedulerStub();
Christopher Tate7060b042014-06-09 19:50:00 -0700323 mJobs = JobStore.initAndGet(this);
324 }
325
326 @Override
327 public void onStart() {
328 publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub);
329 }
330
331 @Override
332 public void onBootPhase(int phase) {
333 if (PHASE_SYSTEM_SERVICES_READY == phase) {
334 // Register br for package removals and user removals.
335 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
336 filter.addDataScheme("package");
337 getContext().registerReceiverAsUser(
338 mBroadcastReceiver, UserHandle.ALL, filter, null, null);
339 final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700340 userFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
Christopher Tate7060b042014-06-09 19:50:00 -0700341 getContext().registerReceiverAsUser(
342 mBroadcastReceiver, UserHandle.ALL, userFilter, null, null);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700343 mPowerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700344 } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
345 synchronized (mJobs) {
346 // Let's go!
347 mReadyToRock = true;
348 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
349 BatteryStats.SERVICE_NAME));
350 // Create the "runners".
351 for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) {
352 mActiveServices.add(
353 new JobServiceContext(this, mBatteryStats,
354 getContext().getMainLooper()));
355 }
356 // Attach jobs to their controllers.
357 ArraySet<JobStatus> jobs = mJobs.getJobs();
358 for (int i=0; i<jobs.size(); i++) {
359 JobStatus job = jobs.valueAt(i);
Christopher Tate4a79dae2014-07-18 17:01:40 -0700360 for (int controller=0; controller<mControllers.size(); controller++) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700361 mControllers.get(i).deviceIdleModeChanged(mDeviceIdleMode);
Christopher Tate4a79dae2014-07-18 17:01:40 -0700362 mControllers.get(controller).maybeStartTrackingJob(job);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700363 }
364 }
365 // GO GO GO!
366 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
367 }
Christopher Tate7060b042014-06-09 19:50:00 -0700368 }
369 }
370
371 /**
372 * Called when we have a job status object that we need to insert in our
373 * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know
374 * about.
375 */
376 private void startTrackingJob(JobStatus jobStatus) {
377 boolean update;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700378 boolean rocking;
Christopher Tate7060b042014-06-09 19:50:00 -0700379 synchronized (mJobs) {
380 update = mJobs.add(jobStatus);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700381 rocking = mReadyToRock;
Christopher Tate7060b042014-06-09 19:50:00 -0700382 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700383 if (rocking) {
384 for (int i=0; i<mControllers.size(); i++) {
385 StateController controller = mControllers.get(i);
386 if (update) {
387 controller.maybeStopTrackingJob(jobStatus);
388 }
389 controller.maybeStartTrackingJob(jobStatus);
Christopher Tate7060b042014-06-09 19:50:00 -0700390 }
Christopher Tate7060b042014-06-09 19:50:00 -0700391 }
392 }
393
394 /**
395 * Called when we want to remove a JobStatus object that we've finished executing. Returns the
396 * object removed.
397 */
398 private boolean stopTrackingJob(JobStatus jobStatus) {
399 boolean removed;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700400 boolean rocking;
Christopher Tate7060b042014-06-09 19:50:00 -0700401 synchronized (mJobs) {
402 // Remove from store as well as controllers.
403 removed = mJobs.remove(jobStatus);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700404 rocking = mReadyToRock;
Christopher Tate7060b042014-06-09 19:50:00 -0700405 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700406 if (removed && rocking) {
407 for (int i=0; i<mControllers.size(); i++) {
408 StateController controller = mControllers.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700409 controller.maybeStopTrackingJob(jobStatus);
410 }
411 }
412 return removed;
413 }
414
415 private boolean stopJobOnServiceContextLocked(JobStatus job) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700416 for (int i=0; i<mActiveServices.size(); i++) {
417 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700418 final JobStatus executing = jsc.getRunningJob();
419 if (executing != null && executing.matches(job.getUid(), job.getJobId())) {
420 jsc.cancelExecutingJob();
421 return true;
422 }
423 }
424 return false;
425 }
426
427 /**
428 * @param job JobStatus we are querying against.
429 * @return Whether or not the job represented by the status object is currently being run or
430 * is pending.
431 */
432 private boolean isCurrentlyActiveLocked(JobStatus job) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700433 for (int i=0; i<mActiveServices.size(); i++) {
434 JobServiceContext serviceContext = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700435 final JobStatus running = serviceContext.getRunningJob();
436 if (running != null && running.matches(job.getUid(), job.getJobId())) {
437 return true;
438 }
439 }
440 return false;
441 }
442
443 /**
444 * A job is rescheduled with exponential back-off if the client requests this from their
445 * execution logic.
446 * A caveat is for idle-mode jobs, for which the idle-mode constraint will usurp the
447 * timeliness of the reschedule. For an idle-mode job, no deadline is given.
448 * @param failureToReschedule Provided job status that we will reschedule.
449 * @return A newly instantiated JobStatus with the same constraints as the last job except
450 * with adjusted timing constraints.
451 */
452 private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) {
453 final long elapsedNowMillis = SystemClock.elapsedRealtime();
454 final JobInfo job = failureToReschedule.getJob();
455
456 final long initialBackoffMillis = job.getInitialBackoffMillis();
Matthew Williamsd1c06752014-08-22 14:15:28 -0700457 final int backoffAttempts = failureToReschedule.getNumFailures() + 1;
458 long delayMillis;
Christopher Tate7060b042014-06-09 19:50:00 -0700459
460 switch (job.getBackoffPolicy()) {
Matthew Williamsd1c06752014-08-22 14:15:28 -0700461 case JobInfo.BACKOFF_POLICY_LINEAR:
462 delayMillis = initialBackoffMillis * backoffAttempts;
Christopher Tate7060b042014-06-09 19:50:00 -0700463 break;
464 default:
465 if (DEBUG) {
466 Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential.");
467 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700468 case JobInfo.BACKOFF_POLICY_EXPONENTIAL:
469 delayMillis =
470 (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1);
Christopher Tate7060b042014-06-09 19:50:00 -0700471 break;
472 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700473 delayMillis =
474 Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS);
475 return new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis,
476 JobStatus.NO_LATEST_RUNTIME, backoffAttempts);
Christopher Tate7060b042014-06-09 19:50:00 -0700477 }
478
479 /**
480 * Called after a periodic has executed so we can to re-add it. We take the last execution time
481 * of the job to be the time of completion (i.e. the time at which this function is called).
482 * This could be inaccurate b/c the job can run for as long as
483 * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead
484 * to underscheduling at least, rather than if we had taken the last execution time to be the
485 * start of the execution.
486 * @return A new job representing the execution criteria for this instantiation of the
487 * recurring job.
488 */
489 private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) {
490 final long elapsedNow = SystemClock.elapsedRealtime();
491 // Compute how much of the period is remaining.
492 long runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0);
493 long newEarliestRunTimeElapsed = elapsedNow + runEarly;
494 long period = periodicToReschedule.getJob().getIntervalMillis();
495 long newLatestRuntimeElapsed = newEarliestRunTimeElapsed + period;
496
497 if (DEBUG) {
498 Slog.v(TAG, "Rescheduling executed periodic. New execution window [" +
499 newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s");
500 }
501 return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed,
502 newLatestRuntimeElapsed, 0 /* backoffAttempt */);
503 }
504
505 // JobCompletedListener implementations.
506
507 /**
508 * A job just finished executing. We fetch the
509 * {@link com.android.server.job.controllers.JobStatus} from the store and depending on
510 * whether we want to reschedule we readd it to the controllers.
511 * @param jobStatus Completed job.
512 * @param needsReschedule Whether the implementing class should reschedule this job.
513 */
514 @Override
515 public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) {
516 if (DEBUG) {
517 Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule);
518 }
519 if (!stopTrackingJob(jobStatus)) {
520 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700521 Slog.d(TAG, "Could not find job to remove. Was job removed while executing?");
Christopher Tate7060b042014-06-09 19:50:00 -0700522 }
523 return;
524 }
525 if (needsReschedule) {
526 JobStatus rescheduled = getRescheduleJobForFailure(jobStatus);
527 startTrackingJob(rescheduled);
528 } else if (jobStatus.getJob().isPeriodic()) {
529 JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus);
530 startTrackingJob(rescheduledPeriodic);
531 }
532 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
533 }
534
535 // StateChangedListener implementations.
536
537 /**
Matthew Williams48a30db2014-09-23 13:39:36 -0700538 * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that
539 * some controller's state has changed, so as to run through the list of jobs and start/stop
540 * any that are eligible.
Christopher Tate7060b042014-06-09 19:50:00 -0700541 */
542 @Override
543 public void onControllerStateChanged() {
Matthew Williams48a30db2014-09-23 13:39:36 -0700544 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700545 }
546
547 @Override
548 public void onRunJobNow(JobStatus jobStatus) {
549 mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget();
550 }
551
Christopher Tate7060b042014-06-09 19:50:00 -0700552 private class JobHandler extends Handler {
553
554 public JobHandler(Looper looper) {
555 super(looper);
556 }
557
558 @Override
559 public void handleMessage(Message message) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700560 synchronized (mJobs) {
561 if (!mReadyToRock) {
562 return;
563 }
564 }
Christopher Tate7060b042014-06-09 19:50:00 -0700565 switch (message.what) {
566 case MSG_JOB_EXPIRED:
567 synchronized (mJobs) {
568 JobStatus runNow = (JobStatus) message.obj;
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700569 // runNow can be null, which is a controller's way of indicating that its
570 // state is such that all ready jobs should be run immediately.
Matthew Williams48a30db2014-09-23 13:39:36 -0700571 if (runNow != null && !mPendingJobs.contains(runNow)
572 && mJobs.containsJob(runNow)) {
Christopher Tate7060b042014-06-09 19:50:00 -0700573 mPendingJobs.add(runNow);
574 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700575 queueReadyJobsForExecutionLockedH();
Christopher Tate7060b042014-06-09 19:50:00 -0700576 }
Christopher Tate7060b042014-06-09 19:50:00 -0700577 break;
578 case MSG_CHECK_JOB:
Matthew Williams48a30db2014-09-23 13:39:36 -0700579 synchronized (mJobs) {
580 // Check the list of jobs and run some of them if we feel inclined.
581 maybeQueueReadyJobsForExecutionLockedH();
582 }
Christopher Tate7060b042014-06-09 19:50:00 -0700583 break;
584 }
585 maybeRunPendingJobsH();
586 // Don't remove JOB_EXPIRED in case one came along while processing the queue.
587 removeMessages(MSG_CHECK_JOB);
588 }
589
590 /**
591 * Run through list of jobs and execute all possible - at least one is expired so we do
592 * as many as we can.
593 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700594 private void queueReadyJobsForExecutionLockedH() {
595 ArraySet<JobStatus> jobs = mJobs.getJobs();
596 if (DEBUG) {
597 Slog.d(TAG, "queuing all ready jobs for execution:");
598 }
599 for (int i=0; i<jobs.size(); i++) {
600 JobStatus job = jobs.valueAt(i);
601 if (isReadyToBeExecutedLocked(job)) {
602 if (DEBUG) {
603 Slog.d(TAG, " queued " + job.toShortString());
Christopher Tate7060b042014-06-09 19:50:00 -0700604 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700605 mPendingJobs.add(job);
606 } else if (isReadyToBeCancelledLocked(job)) {
607 stopJobOnServiceContextLocked(job);
Christopher Tate7060b042014-06-09 19:50:00 -0700608 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700609 }
610 if (DEBUG) {
611 final int queuedJobs = mPendingJobs.size();
612 if (queuedJobs == 0) {
613 Slog.d(TAG, "No jobs pending.");
614 } else {
615 Slog.d(TAG, queuedJobs + " jobs queued.");
Matthew Williams75fc5252014-09-02 16:17:53 -0700616 }
Christopher Tate7060b042014-06-09 19:50:00 -0700617 }
618 }
619
620 /**
621 * The state of at least one job has changed. Here is where we could enforce various
622 * policies on when we want to execute jobs.
623 * Right now the policy is such:
624 * If >1 of the ready jobs is idle mode we send all of them off
625 * if more than 2 network connectivity jobs are ready we send them all off.
626 * If more than 4 jobs total are ready we send them all off.
627 * TODO: It would be nice to consolidate these sort of high-level policies somewhere.
628 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700629 private void maybeQueueReadyJobsForExecutionLockedH() {
630 int chargingCount = 0;
631 int idleCount = 0;
632 int backoffCount = 0;
633 int connectivityCount = 0;
634 List<JobStatus> runnableJobs = new ArrayList<JobStatus>();
635 ArraySet<JobStatus> jobs = mJobs.getJobs();
636 for (int i=0; i<jobs.size(); i++) {
637 JobStatus job = jobs.valueAt(i);
638 if (isReadyToBeExecutedLocked(job)) {
639 if (job.getNumFailures() > 0) {
640 backoffCount++;
Christopher Tate7060b042014-06-09 19:50:00 -0700641 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700642 if (job.hasIdleConstraint()) {
643 idleCount++;
644 }
645 if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) {
646 connectivityCount++;
647 }
648 if (job.hasChargingConstraint()) {
649 chargingCount++;
650 }
651 runnableJobs.add(job);
652 } else if (isReadyToBeCancelledLocked(job)) {
653 stopJobOnServiceContextLocked(job);
Christopher Tate7060b042014-06-09 19:50:00 -0700654 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700655 }
656 if (backoffCount > 0 ||
657 idleCount >= MIN_IDLE_COUNT ||
658 connectivityCount >= MIN_CONNECTIVITY_COUNT ||
659 chargingCount >= MIN_CHARGING_COUNT ||
660 runnableJobs.size() >= MIN_READY_JOBS_COUNT) {
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700661 if (DEBUG) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700662 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs.");
Christopher Tate7060b042014-06-09 19:50:00 -0700663 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700664 for (int i=0; i<runnableJobs.size(); i++) {
665 mPendingJobs.add(runnableJobs.get(i));
666 }
667 } else {
668 if (DEBUG) {
669 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Not running anything.");
670 }
671 }
672 if (DEBUG) {
673 Slog.d(TAG, "idle=" + idleCount + " connectivity=" +
674 connectivityCount + " charging=" + chargingCount + " tot=" +
675 runnableJobs.size());
Christopher Tate7060b042014-06-09 19:50:00 -0700676 }
677 }
678
679 /**
680 * Criteria for moving a job into the pending queue:
681 * - It's ready.
682 * - It's not pending.
683 * - It's not already running on a JSC.
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700684 * - The user that requested the job is running.
Christopher Tate7060b042014-06-09 19:50:00 -0700685 */
686 private boolean isReadyToBeExecutedLocked(JobStatus job) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700687 final boolean jobReady = job.isReady();
688 final boolean jobPending = mPendingJobs.contains(job);
689 final boolean jobActive = isCurrentlyActiveLocked(job);
690 final boolean userRunning = mStartedUsers.contains(job.getUserId());
691
692 if (DEBUG) {
693 Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString()
694 + " ready=" + jobReady + " pending=" + jobPending
695 + " active=" + jobActive + " userRunning=" + userRunning);
696 }
697 return userRunning && jobReady && !jobPending && !jobActive;
Christopher Tate7060b042014-06-09 19:50:00 -0700698 }
699
700 /**
701 * Criteria for cancelling an active job:
702 * - It's not ready
703 * - It's running on a JSC.
704 */
705 private boolean isReadyToBeCancelledLocked(JobStatus job) {
706 return !job.isReady() && isCurrentlyActiveLocked(job);
707 }
708
709 /**
710 * Reconcile jobs in the pending queue against available execution contexts.
711 * A controller can force a job into the pending queue even if it's already running, but
712 * here is where we decide whether to actually execute it.
713 */
714 private void maybeRunPendingJobsH() {
715 synchronized (mJobs) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700716 if (mDeviceIdleMode) {
717 // If device is idle, we will not schedule jobs to run.
718 return;
719 }
Christopher Tate7060b042014-06-09 19:50:00 -0700720 Iterator<JobStatus> it = mPendingJobs.iterator();
Matthew Williams75fc5252014-09-02 16:17:53 -0700721 if (DEBUG) {
722 Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs.");
723 }
Christopher Tate7060b042014-06-09 19:50:00 -0700724 while (it.hasNext()) {
725 JobStatus nextPending = it.next();
726 JobServiceContext availableContext = null;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700727 for (int i=0; i<mActiveServices.size(); i++) {
728 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700729 final JobStatus running = jsc.getRunningJob();
730 if (running != null && running.matches(nextPending.getUid(),
731 nextPending.getJobId())) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700732 // Already running this job for this uId, skip.
Christopher Tate7060b042014-06-09 19:50:00 -0700733 availableContext = null;
734 break;
735 }
736 if (jsc.isAvailable()) {
737 availableContext = jsc;
738 }
739 }
740 if (availableContext != null) {
741 if (!availableContext.executeRunnableJob(nextPending)) {
742 if (DEBUG) {
743 Slog.d(TAG, "Error executing " + nextPending);
744 }
745 mJobs.remove(nextPending);
746 }
747 it.remove();
748 }
749 }
750 }
751 }
752 }
753
754 /**
755 * Binder stub trampoline implementation
756 */
757 final class JobSchedulerStub extends IJobScheduler.Stub {
758 /** Cache determination of whether a given app can persist jobs
759 * key is uid of the calling app; value is undetermined/true/false
760 */
761 private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>();
762
763 // Enforce that only the app itself (or shared uid participant) can schedule a
764 // job that runs one of the app's services, as well as verifying that the
765 // named service properly requires the BIND_JOB_SERVICE permission
766 private void enforceValidJobRequest(int uid, JobInfo job) {
Christopher Tate5568f542014-06-18 13:53:31 -0700767 final IPackageManager pm = AppGlobals.getPackageManager();
Christopher Tate7060b042014-06-09 19:50:00 -0700768 final ComponentName service = job.getService();
769 try {
Christopher Tate5568f542014-06-18 13:53:31 -0700770 ServiceInfo si = pm.getServiceInfo(service, 0, UserHandle.getUserId(uid));
771 if (si == null) {
772 throw new IllegalArgumentException("No such service " + service);
773 }
Christopher Tate7060b042014-06-09 19:50:00 -0700774 if (si.applicationInfo.uid != uid) {
775 throw new IllegalArgumentException("uid " + uid +
776 " cannot schedule job in " + service.getPackageName());
777 }
778 if (!JobService.PERMISSION_BIND.equals(si.permission)) {
779 throw new IllegalArgumentException("Scheduled service " + service
780 + " does not require android.permission.BIND_JOB_SERVICE permission");
781 }
Christopher Tate5568f542014-06-18 13:53:31 -0700782 } catch (RemoteException e) {
783 // Can't happen; the Package Manager is in this same process
Christopher Tate7060b042014-06-09 19:50:00 -0700784 }
785 }
786
787 private boolean canPersistJobs(int pid, int uid) {
788 // If we get this far we're good to go; all we need to do now is check
789 // whether the app is allowed to persist its scheduled work.
790 final boolean canPersist;
791 synchronized (mPersistCache) {
792 Boolean cached = mPersistCache.get(uid);
793 if (cached != null) {
794 canPersist = cached.booleanValue();
795 } else {
796 // Persisting jobs is tantamount to running at boot, so we permit
797 // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED
798 // permission
799 int result = getContext().checkPermission(
800 android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid);
801 canPersist = (result == PackageManager.PERMISSION_GRANTED);
802 mPersistCache.put(uid, canPersist);
803 }
804 }
805 return canPersist;
806 }
807
808 // IJobScheduler implementation
809 @Override
810 public int schedule(JobInfo job) throws RemoteException {
811 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700812 Slog.d(TAG, "Scheduling job: " + job.toString());
Christopher Tate7060b042014-06-09 19:50:00 -0700813 }
814 final int pid = Binder.getCallingPid();
815 final int uid = Binder.getCallingUid();
816
817 enforceValidJobRequest(uid, job);
Matthew Williams900c67f2014-07-09 12:46:53 -0700818 if (job.isPersisted()) {
819 if (!canPersistJobs(pid, uid)) {
820 throw new IllegalArgumentException("Error: requested job be persisted without"
821 + " holding RECEIVE_BOOT_COMPLETED permission.");
822 }
823 }
Christopher Tate7060b042014-06-09 19:50:00 -0700824
825 long ident = Binder.clearCallingIdentity();
826 try {
Matthew Williams900c67f2014-07-09 12:46:53 -0700827 return JobSchedulerService.this.schedule(job, uid);
Christopher Tate7060b042014-06-09 19:50:00 -0700828 } finally {
829 Binder.restoreCallingIdentity(ident);
830 }
831 }
832
833 @Override
834 public List<JobInfo> getAllPendingJobs() throws RemoteException {
835 final int uid = Binder.getCallingUid();
836
837 long ident = Binder.clearCallingIdentity();
838 try {
839 return JobSchedulerService.this.getPendingJobs(uid);
840 } finally {
841 Binder.restoreCallingIdentity(ident);
842 }
843 }
844
845 @Override
846 public void cancelAll() throws RemoteException {
847 final int uid = Binder.getCallingUid();
848
849 long ident = Binder.clearCallingIdentity();
850 try {
851 JobSchedulerService.this.cancelJobsForUid(uid);
852 } finally {
853 Binder.restoreCallingIdentity(ident);
854 }
855 }
856
857 @Override
858 public void cancel(int jobId) throws RemoteException {
859 final int uid = Binder.getCallingUid();
860
861 long ident = Binder.clearCallingIdentity();
862 try {
863 JobSchedulerService.this.cancelJob(uid, jobId);
864 } finally {
865 Binder.restoreCallingIdentity(ident);
866 }
867 }
868
869 /**
870 * "dumpsys" infrastructure
871 */
872 @Override
873 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
874 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
875
876 long identityToken = Binder.clearCallingIdentity();
877 try {
878 JobSchedulerService.this.dumpInternal(pw);
879 } finally {
880 Binder.restoreCallingIdentity(identityToken);
881 }
882 }
883 };
884
885 void dumpInternal(PrintWriter pw) {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700886 final long now = SystemClock.elapsedRealtime();
Christopher Tate7060b042014-06-09 19:50:00 -0700887 synchronized (mJobs) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700888 pw.print("Started users: ");
889 for (int i=0; i<mStartedUsers.size(); i++) {
890 pw.print("u" + mStartedUsers.get(i) + " ");
891 }
892 pw.println();
Christopher Tate7060b042014-06-09 19:50:00 -0700893 pw.println("Registered jobs:");
894 if (mJobs.size() > 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700895 ArraySet<JobStatus> jobs = mJobs.getJobs();
896 for (int i=0; i<jobs.size(); i++) {
897 JobStatus job = jobs.valueAt(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700898 job.dump(pw, " ");
899 }
900 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700901 pw.println(" None.");
Christopher Tate7060b042014-06-09 19:50:00 -0700902 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700903 for (int i=0; i<mControllers.size(); i++) {
Christopher Tate7060b042014-06-09 19:50:00 -0700904 pw.println();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700905 mControllers.get(i).dumpControllerState(pw);
Christopher Tate7060b042014-06-09 19:50:00 -0700906 }
907 pw.println();
Christopher Tatef973a7b2014-08-29 12:54:08 -0700908 pw.println("Pending:");
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700909 for (int i=0; i<mPendingJobs.size(); i++) {
910 pw.println(mPendingJobs.get(i).hashCode());
Christopher Tate7060b042014-06-09 19:50:00 -0700911 }
912 pw.println();
913 pw.println("Active jobs:");
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700914 for (int i=0; i<mActiveServices.size(); i++) {
915 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700916 if (jsc.isAvailable()) {
917 continue;
918 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700919 final long timeout = jsc.getTimeoutElapsed();
920 pw.print("Running for: ");
921 pw.print((now - jsc.getExecutionStartTimeElapsed())/1000);
922 pw.print("s timeout=");
923 pw.print(timeout);
924 pw.print(" fromnow=");
925 pw.println(timeout-now);
926 jsc.getRunningJob().dump(pw, " ");
Christopher Tate7060b042014-06-09 19:50:00 -0700927 }
928 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700929 pw.println();
930 pw.print("mReadyToRock="); pw.println(mReadyToRock);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700931 pw.print("mDeviceIdleMode="); pw.println(mDeviceIdleMode);
Christopher Tate7060b042014-06-09 19:50:00 -0700932 }
933 pw.println();
934 }
935}