Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.job; |
| 18 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 19 | import java.io.FileDescriptor; |
| 20 | import java.io.PrintWriter; |
| 21 | import java.util.ArrayList; |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 22 | import java.util.Arrays; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 23 | import java.util.Iterator; |
| 24 | import java.util.List; |
| 25 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 27 | import android.app.ActivityManagerNative; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 28 | import android.app.AppGlobals; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 29 | import android.app.IUidObserver; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 30 | import android.app.job.JobInfo; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 31 | import android.app.job.JobParameters; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 32 | import android.app.job.JobScheduler; |
| 33 | import android.app.job.JobService; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 34 | import android.app.job.IJobScheduler; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 35 | import android.content.BroadcastReceiver; |
| 36 | import android.content.ComponentName; |
| 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
| 39 | import android.content.IntentFilter; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 40 | import android.content.pm.IPackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 41 | import android.content.pm.PackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 42 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 43 | import android.os.BatteryStats; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 44 | import android.os.Binder; |
| 45 | import android.os.Handler; |
| 46 | import android.os.Looper; |
| 47 | import android.os.Message; |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 48 | import android.os.Process; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 49 | import android.os.PowerManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 50 | import android.os.RemoteException; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 51 | import android.os.ResultReceiver; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 52 | import android.os.ServiceManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 53 | import android.os.SystemClock; |
| 54 | import android.os.UserHandle; |
| 55 | import android.util.Slog; |
| 56 | import android.util.SparseArray; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 57 | import android.util.SparseIntArray; |
| 58 | import android.util.TimeUtils; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 59 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 60 | import com.android.internal.app.IBatteryStats; |
Joe Onorato | 4eb64fd | 2016-03-21 15:30:09 -0700 | [diff] [blame] | 61 | import com.android.internal.app.procstats.ProcessStats; |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 62 | import com.android.internal.util.ArrayUtils; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 63 | import com.android.server.DeviceIdleController; |
| 64 | import com.android.server.LocalServices; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 65 | import com.android.server.job.JobStore.JobStatusFunctor; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 66 | import com.android.server.job.controllers.AppIdleController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 67 | import com.android.server.job.controllers.BatteryController; |
| 68 | import com.android.server.job.controllers.ConnectivityController; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 69 | import com.android.server.job.controllers.ContentObserverController; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 70 | import com.android.server.job.controllers.DeviceIdleJobsController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 71 | import com.android.server.job.controllers.IdleController; |
| 72 | import com.android.server.job.controllers.JobStatus; |
| 73 | import com.android.server.job.controllers.StateController; |
| 74 | import com.android.server.job.controllers.TimeController; |
| 75 | |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 76 | import libcore.util.EmptyArray; |
| 77 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Responsible for taking jobs representing work to be performed by a client app, and determining |
| 80 | * based on the criteria specified when that job should be run against the client application's |
| 81 | * endpoint. |
| 82 | * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing |
| 83 | * about constraints, or the state of active jobs. It receives callbacks from the various |
| 84 | * controllers and completed jobs and operates accordingly. |
| 85 | * |
| 86 | * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object. |
| 87 | * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}. |
| 88 | * @hide |
| 89 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 90 | public final class JobSchedulerService extends com.android.server.SystemService |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 91 | implements StateChangedListener, JobCompletedListener { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 92 | static final String TAG = "JobSchedulerService"; |
Matthew Williams | aa98431 | 2015-10-15 16:08:05 -0700 | [diff] [blame] | 93 | public static final boolean DEBUG = false; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 94 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 95 | /** The maximum number of concurrent jobs we run at one time. */ |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 96 | private static final int MAX_JOB_CONTEXTS_COUNT = 12; |
| 97 | /** The number of MAX_JOB_CONTEXTS_COUNT we reserve for the foreground app. */ |
| 98 | private static final int FG_JOB_CONTEXTS_COUNT = 4; |
Christopher Tate | dabdf6f | 2016-02-24 12:30:22 -0800 | [diff] [blame] | 99 | /** Enforce a per-app limit on scheduled jobs? */ |
Christopher Tate | 0213ace0 | 2016-02-24 14:18:35 -0800 | [diff] [blame] | 100 | private static final boolean ENFORCE_MAX_JOBS = true; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 101 | /** The maximum number of jobs that we allow an unprivileged app to schedule */ |
| 102 | private static final int MAX_JOBS_PER_APP = 100; |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 103 | /** This is the job execution factor that is considered to be heavy use of the system. */ |
| 104 | private static final float HEAVY_USE_FACTOR = .9f; |
| 105 | /** This is the job execution factor that is considered to be moderate use of the system. */ |
| 106 | private static final float MODERATE_USE_FACTOR = .5f; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 107 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 108 | /** Global local for all job scheduler state. */ |
| 109 | final Object mLock = new Object(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 110 | /** Master list of jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 111 | final JobStore mJobs; |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 112 | /** Tracking amount of time each package runs for. */ |
| 113 | final JobPackageTracker mJobPackageTracker = new JobPackageTracker(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 114 | |
| 115 | static final int MSG_JOB_EXPIRED = 0; |
| 116 | static final int MSG_CHECK_JOB = 1; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 117 | static final int MSG_STOP_JOB = 2; |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 118 | static final int MSG_CHECK_JOB_GREEDY = 3; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 119 | |
| 120 | // Policy constants |
| 121 | /** |
| 122 | * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things |
| 123 | * early. |
| 124 | */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 125 | static final int MIN_IDLE_COUNT = 1; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 126 | /** |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 127 | * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things |
| 128 | * early. |
| 129 | */ |
| 130 | static final int MIN_CHARGING_COUNT = 1; |
| 131 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 132 | * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule |
| 133 | * things early. |
| 134 | */ |
Matthew Williams | aa98431 | 2015-10-15 16:08:05 -0700 | [diff] [blame] | 135 | static final int MIN_CONNECTIVITY_COUNT = 1; // Run connectivity jobs as soon as ready. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 136 | /** |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 137 | * Minimum # of content trigger jobs that must be ready in order to force the JMS to schedule |
| 138 | * things early. |
| 139 | */ |
| 140 | static final int MIN_CONTENT_COUNT = 1; |
| 141 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 142 | * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running |
| 143 | * some work early. |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 144 | * This is correlated with the amount of batching we'll be able to do. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 145 | */ |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 146 | static final int MIN_READY_JOBS_COUNT = 2; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Track Services that have currently active or pending jobs. The index is provided by |
| 150 | * {@link JobStatus#getServiceToken()} |
| 151 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 152 | final List<JobServiceContext> mActiveServices = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 153 | /** List of controllers that will notify this service of updates to jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 154 | List<StateController> mControllers; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Queue of pending jobs. The JobServiceContext class will receive jobs from this list |
| 157 | * when ready to execute them. |
| 158 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 159 | final ArrayList<JobStatus> mPendingJobs = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 160 | |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 161 | int[] mStartedUsers = EmptyArray.INT; |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 162 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 163 | final JobHandler mHandler; |
| 164 | final JobSchedulerStub mJobSchedulerStub; |
| 165 | |
| 166 | IBatteryStats mBatteryStats; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 167 | PowerManager mPowerManager; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 168 | DeviceIdleController.LocalService mLocalDeviceIdleController; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Set to true once we are allowed to run third party apps. |
| 172 | */ |
| 173 | boolean mReadyToRock; |
| 174 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 175 | /** |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 176 | * What we last reported to DeviceIdleController about whether we are active. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 177 | */ |
| 178 | boolean mReportedActive; |
| 179 | |
| 180 | /** |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 181 | * Current limit on the number of concurrent JobServiceContext entries we want to |
| 182 | * keep actively running a job. |
| 183 | */ |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 184 | int mMaxActiveJobs = MAX_JOB_CONTEXTS_COUNT - FG_JOB_CONTEXTS_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 187 | * Which uids are currently in the foreground. |
| 188 | */ |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 189 | final SparseIntArray mUidPriorityOverride = new SparseIntArray(); |
| 190 | |
| 191 | // -- Pre-allocated temporaries only for use in assignJobsToContextsLocked -- |
| 192 | |
| 193 | /** |
| 194 | * This array essentially stores the state of mActiveServices array. |
| 195 | * The ith index stores the job present on the ith JobServiceContext. |
| 196 | * We manipulate this array until we arrive at what jobs should be running on |
| 197 | * what JobServiceContext. |
| 198 | */ |
| 199 | JobStatus[] mTmpAssignContextIdToJobMap = new JobStatus[MAX_JOB_CONTEXTS_COUNT]; |
| 200 | /** |
| 201 | * Indicates whether we need to act on this jobContext id |
| 202 | */ |
| 203 | boolean[] mTmpAssignAct = new boolean[MAX_JOB_CONTEXTS_COUNT]; |
| 204 | /** |
| 205 | * The uid whose jobs we would like to assign to a context. |
| 206 | */ |
| 207 | int[] mTmpAssignPreferredUidForContext = new int[MAX_JOB_CONTEXTS_COUNT]; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 208 | |
| 209 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 210 | * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we |
| 211 | * still clean up. On reinstall the package will have a new uid. |
| 212 | */ |
| 213 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 214 | @Override |
| 215 | public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 216 | if (DEBUG) { |
| 217 | Slog.d(TAG, "Receieved: " + intent.getAction()); |
| 218 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 219 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Christopher Tate | aad67a3 | 2014-10-20 16:29:20 -0700 | [diff] [blame] | 220 | // If this is an outright uninstall rather than the first half of an |
| 221 | // app update sequence, cancel the jobs associated with the app. |
| 222 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 223 | int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 224 | if (DEBUG) { |
| 225 | Slog.d(TAG, "Removing jobs for uid: " + uidRemoved); |
| 226 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 227 | cancelJobsForUid(uidRemoved, true); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 228 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 229 | } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 230 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 231 | if (DEBUG) { |
| 232 | Slog.d(TAG, "Removing jobs for user: " + userId); |
| 233 | } |
| 234 | cancelJobsForUser(userId); |
| 235 | } |
| 236 | } |
| 237 | }; |
| 238 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 239 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
| 240 | @Override public void onUidStateChanged(int uid, int procState) throws RemoteException { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 241 | updateUidState(uid, procState); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override public void onUidGone(int uid) throws RemoteException { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 245 | updateUidState(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Override public void onUidActive(int uid) throws RemoteException { |
| 249 | } |
| 250 | |
| 251 | @Override public void onUidIdle(int uid) throws RemoteException { |
| 252 | cancelJobsForUid(uid, false); |
| 253 | } |
| 254 | }; |
| 255 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 256 | public Object getLock() { |
| 257 | return mLock; |
| 258 | } |
| 259 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame^] | 260 | public JobStore getJobStore() { |
| 261 | return mJobs; |
| 262 | } |
| 263 | |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 264 | @Override |
| 265 | public void onStartUser(int userHandle) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 266 | mStartedUsers = ArrayUtils.appendInt(mStartedUsers, userHandle); |
| 267 | // Let's kick any outstanding jobs for this user. |
| 268 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void onUnlockUser(int userHandle) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 273 | // Let's kick any outstanding jobs for this user. |
| 274 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void onStopUser(int userHandle) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 279 | mStartedUsers = ArrayUtils.removeInt(mStartedUsers, userHandle); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 282 | /** |
| 283 | * Entry point from client to schedule the provided job. |
| 284 | * This cancels the job if it's already been scheduled, and replaces it with the one provided. |
| 285 | * @param job JobInfo object containing execution parameters |
| 286 | * @param uId The package identifier of the application this job is for. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 287 | * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes. |
| 288 | */ |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 289 | public int schedule(JobInfo job, int uId) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 290 | return scheduleAsPackage(job, uId, null, -1, null); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 293 | public int scheduleAsPackage(JobInfo job, int uId, String packageName, int userId, |
| 294 | String tag) { |
| 295 | JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 296 | try { |
| 297 | if (ActivityManagerNative.getDefault().getAppStartMode(uId, |
| 298 | job.getService().getPackageName()) == ActivityManager.APP_START_MODE_DISABLED) { |
| 299 | Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString() |
| 300 | + " -- package not allowed to start"); |
| 301 | return JobScheduler.RESULT_FAILURE; |
| 302 | } |
| 303 | } catch (RemoteException e) { |
| 304 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 305 | if (DEBUG) Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString()); |
| 306 | JobStatus toCancel; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 307 | synchronized (mLock) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 308 | // Jobs on behalf of others don't apply to the per-app job cap |
Christopher Tate | dabdf6f | 2016-02-24 12:30:22 -0800 | [diff] [blame] | 309 | if (ENFORCE_MAX_JOBS && packageName == null) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 310 | if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) { |
| 311 | Slog.w(TAG, "Too many jobs for uid " + uId); |
| 312 | throw new IllegalStateException("Apps may not schedule more than " |
| 313 | + MAX_JOBS_PER_APP + " distinct jobs"); |
| 314 | } |
| 315 | } |
| 316 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 317 | toCancel = mJobs.getJobByUidAndJobId(uId, job.getId()); |
Christopher Tate | b1c1f9a | 2016-03-17 13:29:25 -0700 | [diff] [blame] | 318 | if (toCancel != null) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 319 | cancelJobImpl(toCancel, jobStatus); |
Christopher Tate | b1c1f9a | 2016-03-17 13:29:25 -0700 | [diff] [blame] | 320 | } |
| 321 | startTrackingJob(jobStatus, toCancel); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 322 | } |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 323 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 324 | return JobScheduler.RESULT_SUCCESS; |
| 325 | } |
| 326 | |
| 327 | public List<JobInfo> getPendingJobs(int uid) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 328 | synchronized (mLock) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 329 | List<JobStatus> jobs = mJobs.getJobsByUid(uid); |
| 330 | ArrayList<JobInfo> outList = new ArrayList<JobInfo>(jobs.size()); |
| 331 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 332 | JobStatus job = jobs.get(i); |
| 333 | outList.add(job.getJob()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 334 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 335 | return outList; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 336 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 339 | void cancelJobsForUser(int userHandle) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 340 | List<JobStatus> jobsForUser; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 341 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 342 | jobsForUser = mJobs.getJobsByUser(userHandle); |
| 343 | } |
| 344 | for (int i=0; i<jobsForUser.size(); i++) { |
| 345 | JobStatus toRemove = jobsForUser.get(i); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 346 | cancelJobImpl(toRemove, null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Entry point from client to cancel all jobs originating from their uid. |
| 352 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 353 | * execution or being executed. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 354 | * @param uid Uid to check against for removal of a job. |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 355 | * @param forceAll If true, all jobs for the uid will be canceled; if false, only those |
| 356 | * whose apps are stopped. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 357 | */ |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 358 | public void cancelJobsForUid(int uid, boolean forceAll) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 359 | List<JobStatus> jobsForUid; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 360 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 361 | jobsForUid = mJobs.getJobsByUid(uid); |
| 362 | } |
| 363 | for (int i=0; i<jobsForUid.size(); i++) { |
| 364 | JobStatus toRemove = jobsForUid.get(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 365 | if (!forceAll) { |
| 366 | String packageName = toRemove.getServiceComponent().getPackageName(); |
| 367 | try { |
| 368 | if (ActivityManagerNative.getDefault().getAppStartMode(uid, packageName) |
| 369 | != ActivityManager.APP_START_MODE_DISABLED) { |
| 370 | continue; |
| 371 | } |
| 372 | } catch (RemoteException e) { |
| 373 | } |
| 374 | } |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 375 | cancelJobImpl(toRemove, null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Entry point from client to cancel the job corresponding to the jobId provided. |
| 381 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 382 | * execution or being executed. |
| 383 | * @param uid Uid of the calling client. |
| 384 | * @param jobId Id of the job, provided at schedule-time. |
| 385 | */ |
| 386 | public void cancelJob(int uid, int jobId) { |
| 387 | JobStatus toCancel; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 388 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 389 | toCancel = mJobs.getJobByUidAndJobId(uid, jobId); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 390 | } |
| 391 | if (toCancel != null) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 392 | cancelJobImpl(toCancel, null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 396 | private void cancelJobImpl(JobStatus cancelled, JobStatus incomingJob) { |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 397 | if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString()); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 398 | stopTrackingJob(cancelled, incomingJob, true /* writeBack */); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 399 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 400 | // Remove from pending queue. |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 401 | if (mPendingJobs.remove(cancelled)) { |
| 402 | mJobPackageTracker.noteNonpending(cancelled); |
| 403 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 404 | // Cancel if running. |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 405 | stopJobOnServiceContextLocked(cancelled, JobParameters.REASON_CANCELED); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 406 | reportActive(); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 407 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 410 | void updateUidState(int uid, int procState) { |
| 411 | synchronized (mLock) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 412 | if (procState == ActivityManager.PROCESS_STATE_TOP) { |
| 413 | // Only use this if we are exactly the top app. All others can live |
| 414 | // with just the foreground priority. This means that persistent processes |
| 415 | // can never be the top app priority... that is fine. |
| 416 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_TOP_APP); |
| 417 | } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) { |
| 418 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_FOREGROUND_APP); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 419 | } else { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 420 | mUidPriorityOverride.delete(uid); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 425 | @Override |
| 426 | public void onDeviceIdleStateChanged(boolean deviceIdle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 427 | synchronized (mLock) { |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 428 | if (deviceIdle) { |
| 429 | // When becoming idle, make sure no jobs are actively running. |
| 430 | for (int i=0; i<mActiveServices.size(); i++) { |
| 431 | JobServiceContext jsc = mActiveServices.get(i); |
| 432 | final JobStatus executing = jsc.getRunningJob(); |
| 433 | if (executing != null) { |
| 434 | jsc.cancelExecutingJob(JobParameters.REASON_DEVICE_IDLE); |
| 435 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 436 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 437 | } else { |
| 438 | // When coming out of idle, allow thing to start back up. |
| 439 | if (mReadyToRock) { |
| 440 | if (mLocalDeviceIdleController != null) { |
| 441 | if (!mReportedActive) { |
| 442 | mReportedActive = true; |
| 443 | mLocalDeviceIdleController.setJobsActive(true); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 446 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 447 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 452 | void reportActive() { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 453 | // active is true if pending queue contains jobs OR some job is running. |
| 454 | boolean active = mPendingJobs.size() > 0; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 455 | if (mPendingJobs.size() <= 0) { |
| 456 | for (int i=0; i<mActiveServices.size(); i++) { |
| 457 | JobServiceContext jsc = mActiveServices.get(i); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 458 | if (jsc.getRunningJob() != null) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 459 | active = true; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 464 | |
| 465 | if (mReportedActive != active) { |
| 466 | mReportedActive = active; |
| 467 | if (mLocalDeviceIdleController != null) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 468 | mLocalDeviceIdleController.setJobsActive(active); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 473 | /** |
| 474 | * Initializes the system service. |
| 475 | * <p> |
| 476 | * Subclasses must define a single argument constructor that accepts the context |
| 477 | * and passes it to super. |
| 478 | * </p> |
| 479 | * |
| 480 | * @param context The system server context. |
| 481 | */ |
| 482 | public JobSchedulerService(Context context) { |
| 483 | super(context); |
| 484 | // Create the controllers. |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 485 | mControllers = new ArrayList<StateController>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 486 | mControllers.add(ConnectivityController.get(this)); |
| 487 | mControllers.add(TimeController.get(this)); |
| 488 | mControllers.add(IdleController.get(this)); |
| 489 | mControllers.add(BatteryController.get(this)); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 490 | mControllers.add(AppIdleController.get(this)); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 491 | mControllers.add(ContentObserverController.get(this)); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 492 | mControllers.add(DeviceIdleJobsController.get(this)); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 493 | |
| 494 | mHandler = new JobHandler(context.getMainLooper()); |
| 495 | mJobSchedulerStub = new JobSchedulerStub(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 496 | mJobs = JobStore.initAndGet(this); |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public void onStart() { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 501 | publishLocalService(JobSchedulerInternal.class, new LocalService()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 502 | publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub); |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public void onBootPhase(int phase) { |
| 507 | if (PHASE_SYSTEM_SERVICES_READY == phase) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 508 | // Register br for package removals and user removals. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 509 | final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); |
| 510 | filter.addDataScheme("package"); |
| 511 | getContext().registerReceiverAsUser( |
| 512 | mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
| 513 | final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED); |
| 514 | getContext().registerReceiverAsUser( |
| 515 | mBroadcastReceiver, UserHandle.ALL, userFilter, null, null); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 516 | mPowerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 517 | try { |
| 518 | ActivityManagerNative.getDefault().registerUidObserver(mUidObserver, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 519 | ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE |
| 520 | | ActivityManager.UID_OBSERVER_IDLE); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 521 | } catch (RemoteException e) { |
| 522 | // ignored; both services live in system_server |
| 523 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 524 | } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 525 | synchronized (mLock) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 526 | // Let's go! |
| 527 | mReadyToRock = true; |
| 528 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 529 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 530 | mLocalDeviceIdleController |
| 531 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 532 | // Create the "runners". |
| 533 | for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) { |
| 534 | mActiveServices.add( |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 535 | new JobServiceContext(this, mBatteryStats, mJobPackageTracker, |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 536 | getContext().getMainLooper())); |
| 537 | } |
| 538 | // Attach jobs to their controllers. |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 539 | mJobs.forEachJob(new JobStatusFunctor() { |
| 540 | @Override |
| 541 | public void process(JobStatus job) { |
| 542 | for (int controller = 0; controller < mControllers.size(); controller++) { |
| 543 | final StateController sc = mControllers.get(controller); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 544 | sc.maybeStartTrackingJobLocked(job, null); |
| 545 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 546 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 547 | }); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 548 | // GO GO GO! |
| 549 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 550 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Called when we have a job status object that we need to insert in our |
| 556 | * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know |
| 557 | * about. |
| 558 | */ |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 559 | private void startTrackingJob(JobStatus jobStatus, JobStatus lastJob) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 560 | synchronized (mLock) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 561 | final boolean update = mJobs.add(jobStatus); |
| 562 | if (mReadyToRock) { |
| 563 | for (int i = 0; i < mControllers.size(); i++) { |
| 564 | StateController controller = mControllers.get(i); |
| 565 | if (update) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 566 | controller.maybeStopTrackingJobLocked(jobStatus, null, true); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 567 | } |
| 568 | controller.maybeStartTrackingJobLocked(jobStatus, lastJob); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 569 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 570 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Called when we want to remove a JobStatus object that we've finished executing. Returns the |
| 576 | * object removed. |
| 577 | */ |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 578 | private boolean stopTrackingJob(JobStatus jobStatus, JobStatus incomingJob, |
| 579 | boolean writeBack) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 580 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 581 | // Remove from store as well as controllers. |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 582 | final boolean removed = mJobs.remove(jobStatus, writeBack); |
| 583 | if (removed && mReadyToRock) { |
| 584 | for (int i=0; i<mControllers.size(); i++) { |
| 585 | StateController controller = mControllers.get(i); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 586 | controller.maybeStopTrackingJobLocked(jobStatus, incomingJob, false); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 587 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 588 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 589 | return removed; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 590 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 593 | private boolean stopJobOnServiceContextLocked(JobStatus job, int reason) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 594 | for (int i=0; i<mActiveServices.size(); i++) { |
| 595 | JobServiceContext jsc = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 596 | final JobStatus executing = jsc.getRunningJob(); |
| 597 | if (executing != null && executing.matches(job.getUid(), job.getJobId())) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 598 | jsc.cancelExecutingJob(reason); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 599 | return true; |
| 600 | } |
| 601 | } |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * @param job JobStatus we are querying against. |
| 607 | * @return Whether or not the job represented by the status object is currently being run or |
| 608 | * is pending. |
| 609 | */ |
| 610 | private boolean isCurrentlyActiveLocked(JobStatus job) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 611 | for (int i=0; i<mActiveServices.size(); i++) { |
| 612 | JobServiceContext serviceContext = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 613 | final JobStatus running = serviceContext.getRunningJob(); |
| 614 | if (running != null && running.matches(job.getUid(), job.getJobId())) { |
| 615 | return true; |
| 616 | } |
| 617 | } |
| 618 | return false; |
| 619 | } |
| 620 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 621 | void noteJobsPending(List<JobStatus> jobs) { |
| 622 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 623 | JobStatus job = jobs.get(i); |
| 624 | mJobPackageTracker.notePending(job); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | void noteJobsNonpending(List<JobStatus> jobs) { |
| 629 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 630 | JobStatus job = jobs.get(i); |
| 631 | mJobPackageTracker.noteNonpending(job); |
| 632 | } |
| 633 | } |
| 634 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 635 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 636 | * Reschedules the given job based on the job's backoff policy. It doesn't make sense to |
| 637 | * specify an override deadline on a failed job (the failed job will run even though it's not |
| 638 | * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any |
| 639 | * ready job with {@link JobStatus#numFailures} > 0 will be executed. |
| 640 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 641 | * @param failureToReschedule Provided job status that we will reschedule. |
| 642 | * @return A newly instantiated JobStatus with the same constraints as the last job except |
| 643 | * with adjusted timing constraints. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 644 | * |
| 645 | * @see JobHandler#maybeQueueReadyJobsForExecutionLockedH |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 646 | */ |
| 647 | private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) { |
| 648 | final long elapsedNowMillis = SystemClock.elapsedRealtime(); |
| 649 | final JobInfo job = failureToReschedule.getJob(); |
| 650 | |
| 651 | final long initialBackoffMillis = job.getInitialBackoffMillis(); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 652 | final int backoffAttempts = failureToReschedule.getNumFailures() + 1; |
| 653 | long delayMillis; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 654 | |
| 655 | switch (job.getBackoffPolicy()) { |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 656 | case JobInfo.BACKOFF_POLICY_LINEAR: |
| 657 | delayMillis = initialBackoffMillis * backoffAttempts; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 658 | break; |
| 659 | default: |
| 660 | if (DEBUG) { |
| 661 | Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential."); |
| 662 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 663 | case JobInfo.BACKOFF_POLICY_EXPONENTIAL: |
| 664 | delayMillis = |
| 665 | (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 666 | break; |
| 667 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 668 | delayMillis = |
| 669 | Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 670 | JobStatus newJob = new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis, |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 671 | JobStatus.NO_LATEST_RUNTIME, backoffAttempts); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 672 | for (int ic=0; ic<mControllers.size(); ic++) { |
| 673 | StateController controller = mControllers.get(ic); |
| 674 | controller.rescheduleForFailure(newJob, failureToReschedule); |
| 675 | } |
| 676 | return newJob; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 680 | * Called after a periodic has executed so we can reschedule it. We take the last execution |
| 681 | * time of the job to be the time of completion (i.e. the time at which this function is |
| 682 | * called). |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 683 | * This could be inaccurate b/c the job can run for as long as |
| 684 | * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead |
| 685 | * to underscheduling at least, rather than if we had taken the last execution time to be the |
| 686 | * start of the execution. |
| 687 | * @return A new job representing the execution criteria for this instantiation of the |
| 688 | * recurring job. |
| 689 | */ |
| 690 | private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) { |
| 691 | final long elapsedNow = SystemClock.elapsedRealtime(); |
| 692 | // Compute how much of the period is remaining. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 693 | long runEarly = 0L; |
| 694 | |
| 695 | // If this periodic was rescheduled it won't have a deadline. |
| 696 | if (periodicToReschedule.hasDeadlineConstraint()) { |
| 697 | runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L); |
| 698 | } |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 699 | long flex = periodicToReschedule.getJob().getFlexMillis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 700 | long period = periodicToReschedule.getJob().getIntervalMillis(); |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 701 | long newLatestRuntimeElapsed = elapsedNow + runEarly + period; |
| 702 | long newEarliestRunTimeElapsed = newLatestRuntimeElapsed - flex; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 703 | |
| 704 | if (DEBUG) { |
| 705 | Slog.v(TAG, "Rescheduling executed periodic. New execution window [" + |
| 706 | newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s"); |
| 707 | } |
| 708 | return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed, |
| 709 | newLatestRuntimeElapsed, 0 /* backoffAttempt */); |
| 710 | } |
| 711 | |
| 712 | // JobCompletedListener implementations. |
| 713 | |
| 714 | /** |
| 715 | * A job just finished executing. We fetch the |
| 716 | * {@link com.android.server.job.controllers.JobStatus} from the store and depending on |
| 717 | * whether we want to reschedule we readd it to the controllers. |
| 718 | * @param jobStatus Completed job. |
| 719 | * @param needsReschedule Whether the implementing class should reschedule this job. |
| 720 | */ |
| 721 | @Override |
| 722 | public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) { |
| 723 | if (DEBUG) { |
| 724 | Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule); |
| 725 | } |
Shreyas Basarge | 73f1025 | 2016-02-11 17:06:13 +0000 | [diff] [blame] | 726 | // Do not write back immediately if this is a periodic job. The job may get lost if system |
| 727 | // shuts down before it is added back. |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 728 | if (!stopTrackingJob(jobStatus, null, !jobStatus.getJob().isPeriodic())) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 729 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 730 | Slog.d(TAG, "Could not find job to remove. Was job removed while executing?"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 731 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 732 | // We still want to check for jobs to execute, because this job may have |
| 733 | // scheduled a new job under the same job id, and now we can run it. |
| 734 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 735 | return; |
| 736 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 737 | // Note: there is a small window of time in here where, when rescheduling a job, |
| 738 | // we will stop monitoring its content providers. This should be fixed by stopping |
| 739 | // the old job after scheduling the new one, but since we have no lock held here |
| 740 | // that may cause ordering problems if the app removes jobStatus while in here. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 741 | if (needsReschedule) { |
| 742 | JobStatus rescheduled = getRescheduleJobForFailure(jobStatus); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 743 | startTrackingJob(rescheduled, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 744 | } else if (jobStatus.getJob().isPeriodic()) { |
| 745 | JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 746 | startTrackingJob(rescheduledPeriodic, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 747 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 748 | reportActive(); |
| 749 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // StateChangedListener implementations. |
| 753 | |
| 754 | /** |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 755 | * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that |
| 756 | * some controller's state has changed, so as to run through the list of jobs and start/stop |
| 757 | * any that are eligible. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 758 | */ |
| 759 | @Override |
| 760 | public void onControllerStateChanged() { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 761 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public void onRunJobNow(JobStatus jobStatus) { |
| 766 | mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget(); |
| 767 | } |
| 768 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 769 | private class JobHandler extends Handler { |
| 770 | |
| 771 | public JobHandler(Looper looper) { |
| 772 | super(looper); |
| 773 | } |
| 774 | |
| 775 | @Override |
| 776 | public void handleMessage(Message message) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 777 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 778 | if (!mReadyToRock) { |
| 779 | return; |
| 780 | } |
| 781 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 782 | switch (message.what) { |
| 783 | case MSG_JOB_EXPIRED: |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 784 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 785 | JobStatus runNow = (JobStatus) message.obj; |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 786 | // runNow can be null, which is a controller's way of indicating that its |
| 787 | // state is such that all ready jobs should be run immediately. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 788 | if (runNow != null && !mPendingJobs.contains(runNow) |
| 789 | && mJobs.containsJob(runNow)) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 790 | mJobPackageTracker.notePending(runNow); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 791 | mPendingJobs.add(runNow); |
| 792 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 793 | queueReadyJobsForExecutionLockedH(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 794 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 795 | break; |
| 796 | case MSG_CHECK_JOB: |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 797 | synchronized (mLock) { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 798 | if (mReportedActive) { |
| 799 | // if jobs are currently being run, queue all ready jobs for execution. |
| 800 | queueReadyJobsForExecutionLockedH(); |
| 801 | } else { |
| 802 | // Check the list of jobs and run some of them if we feel inclined. |
| 803 | maybeQueueReadyJobsForExecutionLockedH(); |
| 804 | } |
| 805 | } |
| 806 | break; |
| 807 | case MSG_CHECK_JOB_GREEDY: |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 808 | synchronized (mLock) { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 809 | queueReadyJobsForExecutionLockedH(); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 810 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 811 | break; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 812 | case MSG_STOP_JOB: |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 813 | cancelJobImpl((JobStatus)message.obj, null); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 814 | break; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 815 | } |
| 816 | maybeRunPendingJobsH(); |
| 817 | // Don't remove JOB_EXPIRED in case one came along while processing the queue. |
| 818 | removeMessages(MSG_CHECK_JOB); |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Run through list of jobs and execute all possible - at least one is expired so we do |
| 823 | * as many as we can. |
| 824 | */ |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 825 | private void queueReadyJobsForExecutionLockedH() { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 826 | if (DEBUG) { |
| 827 | Slog.d(TAG, "queuing all ready jobs for execution:"); |
| 828 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 829 | noteJobsNonpending(mPendingJobs); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 830 | mPendingJobs.clear(); |
| 831 | mJobs.forEachJob(mReadyQueueFunctor); |
| 832 | mReadyQueueFunctor.postProcess(); |
| 833 | |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 834 | if (DEBUG) { |
| 835 | final int queuedJobs = mPendingJobs.size(); |
| 836 | if (queuedJobs == 0) { |
| 837 | Slog.d(TAG, "No jobs pending."); |
| 838 | } else { |
| 839 | Slog.d(TAG, queuedJobs + " jobs queued."); |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 840 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 844 | class ReadyJobQueueFunctor implements JobStatusFunctor { |
| 845 | ArrayList<JobStatus> newReadyJobs; |
| 846 | |
| 847 | @Override |
| 848 | public void process(JobStatus job) { |
| 849 | if (isReadyToBeExecutedLocked(job)) { |
| 850 | if (DEBUG) { |
| 851 | Slog.d(TAG, " queued " + job.toShortString()); |
| 852 | } |
| 853 | if (newReadyJobs == null) { |
| 854 | newReadyJobs = new ArrayList<JobStatus>(); |
| 855 | } |
| 856 | newReadyJobs.add(job); |
| 857 | } else if (areJobConstraintsNotSatisfiedLocked(job)) { |
| 858 | stopJobOnServiceContextLocked(job, |
| 859 | JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | public void postProcess() { |
| 864 | if (newReadyJobs != null) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 865 | noteJobsPending(newReadyJobs); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 866 | mPendingJobs.addAll(newReadyJobs); |
| 867 | } |
| 868 | newReadyJobs = null; |
| 869 | } |
| 870 | } |
| 871 | private final ReadyJobQueueFunctor mReadyQueueFunctor = new ReadyJobQueueFunctor(); |
| 872 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 873 | /** |
| 874 | * The state of at least one job has changed. Here is where we could enforce various |
| 875 | * policies on when we want to execute jobs. |
| 876 | * Right now the policy is such: |
| 877 | * If >1 of the ready jobs is idle mode we send all of them off |
| 878 | * if more than 2 network connectivity jobs are ready we send them all off. |
| 879 | * If more than 4 jobs total are ready we send them all off. |
| 880 | * TODO: It would be nice to consolidate these sort of high-level policies somewhere. |
| 881 | */ |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 882 | class MaybeReadyJobQueueFunctor implements JobStatusFunctor { |
| 883 | int chargingCount; |
| 884 | int idleCount; |
| 885 | int backoffCount; |
| 886 | int connectivityCount; |
| 887 | int contentCount; |
| 888 | List<JobStatus> runnableJobs; |
| 889 | |
| 890 | public MaybeReadyJobQueueFunctor() { |
| 891 | reset(); |
| 892 | } |
| 893 | |
| 894 | // Functor method invoked for each job via JobStore.forEachJob() |
| 895 | @Override |
| 896 | public void process(JobStatus job) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 897 | if (isReadyToBeExecutedLocked(job)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 898 | try { |
| 899 | if (ActivityManagerNative.getDefault().getAppStartMode(job.getUid(), |
| 900 | job.getJob().getService().getPackageName()) |
| 901 | == ActivityManager.APP_START_MODE_DISABLED) { |
| 902 | Slog.w(TAG, "Aborting job " + job.getUid() + ":" |
| 903 | + job.getJob().toString() + " -- package not allowed to start"); |
| 904 | mHandler.obtainMessage(MSG_STOP_JOB, job).sendToTarget(); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 905 | return; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 906 | } |
| 907 | } catch (RemoteException e) { |
| 908 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 909 | if (job.getNumFailures() > 0) { |
| 910 | backoffCount++; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 911 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 912 | if (job.hasIdleConstraint()) { |
| 913 | idleCount++; |
| 914 | } |
| 915 | if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) { |
| 916 | connectivityCount++; |
| 917 | } |
| 918 | if (job.hasChargingConstraint()) { |
| 919 | chargingCount++; |
| 920 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 921 | if (job.hasContentTriggerConstraint()) { |
| 922 | contentCount++; |
| 923 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 924 | if (runnableJobs == null) { |
| 925 | runnableJobs = new ArrayList<>(); |
| 926 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 927 | runnableJobs.add(job); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 928 | } else if (areJobConstraintsNotSatisfiedLocked(job)) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 929 | stopJobOnServiceContextLocked(job, |
| 930 | JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 931 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 932 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 933 | |
| 934 | public void postProcess() { |
| 935 | if (backoffCount > 0 || |
| 936 | idleCount >= MIN_IDLE_COUNT || |
| 937 | connectivityCount >= MIN_CONNECTIVITY_COUNT || |
| 938 | chargingCount >= MIN_CHARGING_COUNT || |
| 939 | contentCount >= MIN_CONTENT_COUNT || |
| 940 | (runnableJobs != null && runnableJobs.size() >= MIN_READY_JOBS_COUNT)) { |
| 941 | if (DEBUG) { |
| 942 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs."); |
| 943 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 944 | noteJobsPending(runnableJobs); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 945 | mPendingJobs.addAll(runnableJobs); |
| 946 | } else { |
| 947 | if (DEBUG) { |
| 948 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Not running anything."); |
| 949 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 950 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 951 | |
| 952 | // Be ready for next time |
| 953 | reset(); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 954 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 955 | |
| 956 | private void reset() { |
| 957 | chargingCount = 0; |
| 958 | idleCount = 0; |
| 959 | backoffCount = 0; |
| 960 | connectivityCount = 0; |
| 961 | contentCount = 0; |
| 962 | runnableJobs = null; |
| 963 | } |
| 964 | } |
| 965 | private final MaybeReadyJobQueueFunctor mMaybeQueueFunctor = new MaybeReadyJobQueueFunctor(); |
| 966 | |
| 967 | private void maybeQueueReadyJobsForExecutionLockedH() { |
| 968 | if (DEBUG) Slog.d(TAG, "Maybe queuing ready jobs..."); |
| 969 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 970 | noteJobsNonpending(mPendingJobs); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 971 | mPendingJobs.clear(); |
| 972 | mJobs.forEachJob(mMaybeQueueFunctor); |
| 973 | mMaybeQueueFunctor.postProcess(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /** |
| 977 | * Criteria for moving a job into the pending queue: |
| 978 | * - It's ready. |
| 979 | * - It's not pending. |
| 980 | * - It's not already running on a JSC. |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 981 | * - The user that requested the job is running. |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 982 | * - The component is enabled and runnable. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 983 | */ |
| 984 | private boolean isReadyToBeExecutedLocked(JobStatus job) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 985 | final boolean jobReady = job.isReady(); |
| 986 | final boolean jobPending = mPendingJobs.contains(job); |
| 987 | final boolean jobActive = isCurrentlyActiveLocked(job); |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 988 | |
| 989 | final int userId = job.getUserId(); |
| 990 | final boolean userStarted = ArrayUtils.contains(mStartedUsers, userId); |
| 991 | final boolean componentPresent; |
| 992 | try { |
| 993 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 994 | job.getServiceComponent(), PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 995 | userId) != null); |
| 996 | } catch (RemoteException e) { |
| 997 | throw e.rethrowAsRuntimeException(); |
| 998 | } |
| 999 | |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 1000 | if (DEBUG) { |
| 1001 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1002 | + " ready=" + jobReady + " pending=" + jobPending |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 1003 | + " active=" + jobActive + " userStarted=" + userStarted |
| 1004 | + " componentPresent=" + componentPresent); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 1005 | } |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 1006 | return userStarted && componentPresent && jobReady && !jobPending && !jobActive; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * Criteria for cancelling an active job: |
| 1011 | * - It's not ready |
| 1012 | * - It's running on a JSC. |
| 1013 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1014 | private boolean areJobConstraintsNotSatisfiedLocked(JobStatus job) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1015 | return !job.isReady() && isCurrentlyActiveLocked(job); |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * Reconcile jobs in the pending queue against available execution contexts. |
| 1020 | * A controller can force a job into the pending queue even if it's already running, but |
| 1021 | * here is where we decide whether to actually execute it. |
| 1022 | */ |
| 1023 | private void maybeRunPendingJobsH() { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1024 | synchronized (mLock) { |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1025 | if (DEBUG) { |
| 1026 | Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs."); |
| 1027 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1028 | assignJobsToContextsLocked(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1029 | reportActive(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1034 | private int adjustJobPriority(int curPriority, JobStatus job) { |
| 1035 | if (curPriority < JobInfo.PRIORITY_TOP_APP) { |
| 1036 | float factor = mJobPackageTracker.getLoadFactor(job); |
| 1037 | if (factor >= HEAVY_USE_FACTOR) { |
| 1038 | curPriority += JobInfo.PRIORITY_ADJ_ALWAYS_RUNNING; |
| 1039 | } else if (factor >= MODERATE_USE_FACTOR) { |
| 1040 | curPriority += JobInfo.PRIORITY_ADJ_OFTEN_RUNNING; |
| 1041 | } |
| 1042 | } |
| 1043 | return curPriority; |
| 1044 | } |
| 1045 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1046 | private int evaluateJobPriorityLocked(JobStatus job) { |
| 1047 | int priority = job.getPriority(); |
| 1048 | if (priority >= JobInfo.PRIORITY_FOREGROUND_APP) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1049 | return adjustJobPriority(priority, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1050 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1051 | int override = mUidPriorityOverride.get(job.getSourceUid(), 0); |
| 1052 | if (override != 0) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1053 | return adjustJobPriority(override, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1054 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1055 | return adjustJobPriority(priority, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1058 | /** |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1059 | * Takes jobs from pending queue and runs them on available contexts. |
| 1060 | * If no contexts are available, preempts lower priority jobs to |
| 1061 | * run higher priority ones. |
| 1062 | * Lock on mJobs before calling this function. |
| 1063 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1064 | private void assignJobsToContextsLocked() { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1065 | if (DEBUG) { |
| 1066 | Slog.d(TAG, printPendingQueue()); |
| 1067 | } |
| 1068 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1069 | int memLevel; |
| 1070 | try { |
| 1071 | memLevel = ActivityManagerNative.getDefault().getMemoryTrimLevel(); |
| 1072 | } catch (RemoteException e) { |
| 1073 | memLevel = ProcessStats.ADJ_MEM_FACTOR_NORMAL; |
| 1074 | } |
| 1075 | switch (memLevel) { |
| 1076 | case ProcessStats.ADJ_MEM_FACTOR_MODERATE: |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1077 | mMaxActiveJobs = ((MAX_JOB_CONTEXTS_COUNT - FG_JOB_CONTEXTS_COUNT) * 2) / 3; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1078 | break; |
| 1079 | case ProcessStats.ADJ_MEM_FACTOR_LOW: |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1080 | mMaxActiveJobs = (MAX_JOB_CONTEXTS_COUNT - FG_JOB_CONTEXTS_COUNT) / 3; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1081 | break; |
| 1082 | case ProcessStats.ADJ_MEM_FACTOR_CRITICAL: |
| 1083 | mMaxActiveJobs = 1; |
| 1084 | break; |
| 1085 | default: |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1086 | mMaxActiveJobs = MAX_JOB_CONTEXTS_COUNT - FG_JOB_CONTEXTS_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1087 | break; |
| 1088 | } |
| 1089 | |
| 1090 | JobStatus[] contextIdToJobMap = mTmpAssignContextIdToJobMap; |
| 1091 | boolean[] act = mTmpAssignAct; |
| 1092 | int[] preferredUidForContext = mTmpAssignPreferredUidForContext; |
| 1093 | int numActive = 0; |
| 1094 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
| 1095 | final JobServiceContext js = mActiveServices.get(i); |
| 1096 | if ((contextIdToJobMap[i] = js.getRunningJob()) != null) { |
| 1097 | numActive++; |
| 1098 | } |
| 1099 | act[i] = false; |
| 1100 | preferredUidForContext[i] = js.getPreferredUid(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1101 | } |
| 1102 | if (DEBUG) { |
| 1103 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs initial")); |
| 1104 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1105 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 1106 | JobStatus nextPending = mPendingJobs.get(i); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1107 | |
| 1108 | // If job is already running, go to next job. |
| 1109 | int jobRunningContext = findJobContextIdFromMap(nextPending, contextIdToJobMap); |
| 1110 | if (jobRunningContext != -1) { |
| 1111 | continue; |
| 1112 | } |
| 1113 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1114 | final int priority = evaluateJobPriorityLocked(nextPending); |
| 1115 | nextPending.lastEvaluatedPriority = priority; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1116 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1117 | // Find a context for nextPending. The context should be available OR |
| 1118 | // it should have lowest priority among all running jobs |
| 1119 | // (sharing the same Uid as nextPending) |
| 1120 | int minPriority = Integer.MAX_VALUE; |
| 1121 | int minPriorityContextId = -1; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1122 | for (int j=0; j<MAX_JOB_CONTEXTS_COUNT; j++) { |
| 1123 | JobStatus job = contextIdToJobMap[j]; |
| 1124 | int preferredUid = preferredUidForContext[j]; |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1125 | if (job == null) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1126 | if ((numActive < mMaxActiveJobs || priority >= JobInfo.PRIORITY_TOP_APP) && |
| 1127 | (preferredUid == nextPending.getUid() || |
| 1128 | preferredUid == JobServiceContext.NO_PREFERRED_UID)) { |
| 1129 | // This slot is free, and we haven't yet hit the limit on |
| 1130 | // concurrent jobs... we can just throw the job in to here. |
| 1131 | minPriorityContextId = j; |
| 1132 | numActive++; |
| 1133 | break; |
| 1134 | } |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1135 | // No job on this context, but nextPending can't run here because |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1136 | // the context has a preferred Uid or we have reached the limit on |
| 1137 | // concurrent jobs. |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1138 | continue; |
| 1139 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1140 | if (job.getUid() != nextPending.getUid()) { |
| 1141 | continue; |
| 1142 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1143 | if (evaluateJobPriorityLocked(job) >= nextPending.lastEvaluatedPriority) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1144 | continue; |
| 1145 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1146 | if (minPriority > nextPending.lastEvaluatedPriority) { |
| 1147 | minPriority = nextPending.lastEvaluatedPriority; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1148 | minPriorityContextId = j; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | if (minPriorityContextId != -1) { |
| 1152 | contextIdToJobMap[minPriorityContextId] = nextPending; |
| 1153 | act[minPriorityContextId] = true; |
| 1154 | } |
| 1155 | } |
| 1156 | if (DEBUG) { |
| 1157 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs final")); |
| 1158 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1159 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1160 | boolean preservePreferredUid = false; |
| 1161 | if (act[i]) { |
| 1162 | JobStatus js = mActiveServices.get(i).getRunningJob(); |
| 1163 | if (js != null) { |
| 1164 | if (DEBUG) { |
| 1165 | Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJob()); |
| 1166 | } |
| 1167 | // preferredUid will be set to uid of currently running job. |
| 1168 | mActiveServices.get(i).preemptExecutingJob(); |
| 1169 | preservePreferredUid = true; |
| 1170 | } else { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1171 | final JobStatus pendingJob = contextIdToJobMap[i]; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1172 | if (DEBUG) { |
| 1173 | Slog.d(TAG, "About to run job on context " |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1174 | + String.valueOf(i) + ", job: " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1175 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1176 | for (int ic=0; ic<mControllers.size(); ic++) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1177 | mControllers.get(ic).prepareForExecutionLocked(pendingJob); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1178 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1179 | if (!mActiveServices.get(i).executeRunnableJob(pendingJob)) { |
| 1180 | Slog.d(TAG, "Error executing " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1181 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1182 | if (mPendingJobs.remove(pendingJob)) { |
| 1183 | mJobPackageTracker.noteNonpending(pendingJob); |
| 1184 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | if (!preservePreferredUid) { |
| 1188 | mActiveServices.get(i).clearPreferredUid(); |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | int findJobContextIdFromMap(JobStatus jobStatus, JobStatus[] map) { |
| 1194 | for (int i=0; i<map.length; i++) { |
| 1195 | if (map[i] != null && map[i].matches(jobStatus.getUid(), jobStatus.getJobId())) { |
| 1196 | return i; |
| 1197 | } |
| 1198 | } |
| 1199 | return -1; |
| 1200 | } |
| 1201 | |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1202 | final class LocalService implements JobSchedulerInternal { |
| 1203 | |
| 1204 | /** |
| 1205 | * Returns a list of all pending jobs. A running job is not considered pending. Periodic |
| 1206 | * jobs are always considered pending. |
| 1207 | */ |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1208 | @Override |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1209 | public List<JobInfo> getSystemScheduledPendingJobs() { |
| 1210 | synchronized (mLock) { |
| 1211 | final List<JobInfo> pendingJobs = new ArrayList<JobInfo>(); |
| 1212 | mJobs.forEachJob(Process.SYSTEM_UID, new JobStatusFunctor() { |
| 1213 | @Override |
| 1214 | public void process(JobStatus job) { |
| 1215 | if (job.getJob().isPeriodic() || !isCurrentlyActiveLocked(job)) { |
| 1216 | pendingJobs.add(job.getJob()); |
| 1217 | } |
| 1218 | } |
| 1219 | }); |
| 1220 | return pendingJobs; |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1225 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1226 | * Binder stub trampoline implementation |
| 1227 | */ |
| 1228 | final class JobSchedulerStub extends IJobScheduler.Stub { |
| 1229 | /** Cache determination of whether a given app can persist jobs |
| 1230 | * key is uid of the calling app; value is undetermined/true/false |
| 1231 | */ |
| 1232 | private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>(); |
| 1233 | |
| 1234 | // Enforce that only the app itself (or shared uid participant) can schedule a |
| 1235 | // job that runs one of the app's services, as well as verifying that the |
| 1236 | // named service properly requires the BIND_JOB_SERVICE permission |
| 1237 | private void enforceValidJobRequest(int uid, JobInfo job) { |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1238 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1239 | final ComponentName service = job.getService(); |
| 1240 | try { |
Jeff Sharkey | c7bacab | 2016-02-09 15:56:11 -0700 | [diff] [blame] | 1241 | ServiceInfo si = pm.getServiceInfo(service, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 1242 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1243 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Jeff Sharkey | 12c0da4 | 2016-02-25 17:10:50 -0700 | [diff] [blame] | 1244 | UserHandle.getUserId(uid)); |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1245 | if (si == null) { |
| 1246 | throw new IllegalArgumentException("No such service " + service); |
| 1247 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1248 | if (si.applicationInfo.uid != uid) { |
| 1249 | throw new IllegalArgumentException("uid " + uid + |
| 1250 | " cannot schedule job in " + service.getPackageName()); |
| 1251 | } |
| 1252 | if (!JobService.PERMISSION_BIND.equals(si.permission)) { |
| 1253 | throw new IllegalArgumentException("Scheduled service " + service |
| 1254 | + " does not require android.permission.BIND_JOB_SERVICE permission"); |
| 1255 | } |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1256 | } catch (RemoteException e) { |
| 1257 | // Can't happen; the Package Manager is in this same process |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | private boolean canPersistJobs(int pid, int uid) { |
| 1262 | // If we get this far we're good to go; all we need to do now is check |
| 1263 | // whether the app is allowed to persist its scheduled work. |
| 1264 | final boolean canPersist; |
| 1265 | synchronized (mPersistCache) { |
| 1266 | Boolean cached = mPersistCache.get(uid); |
| 1267 | if (cached != null) { |
| 1268 | canPersist = cached.booleanValue(); |
| 1269 | } else { |
| 1270 | // Persisting jobs is tantamount to running at boot, so we permit |
| 1271 | // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED |
| 1272 | // permission |
| 1273 | int result = getContext().checkPermission( |
| 1274 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid); |
| 1275 | canPersist = (result == PackageManager.PERMISSION_GRANTED); |
| 1276 | mPersistCache.put(uid, canPersist); |
| 1277 | } |
| 1278 | } |
| 1279 | return canPersist; |
| 1280 | } |
| 1281 | |
| 1282 | // IJobScheduler implementation |
| 1283 | @Override |
| 1284 | public int schedule(JobInfo job) throws RemoteException { |
| 1285 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 1286 | Slog.d(TAG, "Scheduling job: " + job.toString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1287 | } |
| 1288 | final int pid = Binder.getCallingPid(); |
| 1289 | final int uid = Binder.getCallingUid(); |
| 1290 | |
| 1291 | enforceValidJobRequest(uid, job); |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 1292 | if (job.isPersisted()) { |
| 1293 | if (!canPersistJobs(pid, uid)) { |
| 1294 | throw new IllegalArgumentException("Error: requested job be persisted without" |
| 1295 | + " holding RECEIVE_BOOT_COMPLETED permission."); |
| 1296 | } |
| 1297 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1298 | |
| 1299 | long ident = Binder.clearCallingIdentity(); |
| 1300 | try { |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 1301 | return JobSchedulerService.this.schedule(job, uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1302 | } finally { |
| 1303 | Binder.restoreCallingIdentity(ident); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | @Override |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1308 | public int scheduleAsPackage(JobInfo job, String packageName, int userId, String tag) |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1309 | throws RemoteException { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1310 | final int callerUid = Binder.getCallingUid(); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1311 | if (DEBUG) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1312 | Slog.d(TAG, "Caller uid " + callerUid + " scheduling job: " + job.toString() |
| 1313 | + " on behalf of " + packageName); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1314 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1315 | |
| 1316 | if (packageName == null) { |
| 1317 | throw new NullPointerException("Must specify a package for scheduleAsPackage()"); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1318 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1319 | |
| 1320 | int mayScheduleForOthers = getContext().checkCallingOrSelfPermission( |
| 1321 | android.Manifest.permission.UPDATE_DEVICE_STATS); |
| 1322 | if (mayScheduleForOthers != PackageManager.PERMISSION_GRANTED) { |
| 1323 | throw new SecurityException("Caller uid " + callerUid |
| 1324 | + " not permitted to schedule jobs for other apps"); |
| 1325 | } |
| 1326 | |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1327 | long ident = Binder.clearCallingIdentity(); |
| 1328 | try { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1329 | return JobSchedulerService.this.scheduleAsPackage(job, callerUid, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1330 | packageName, userId, tag); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1331 | } finally { |
| 1332 | Binder.restoreCallingIdentity(ident); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1337 | public List<JobInfo> getAllPendingJobs() throws RemoteException { |
| 1338 | final int uid = Binder.getCallingUid(); |
| 1339 | |
| 1340 | long ident = Binder.clearCallingIdentity(); |
| 1341 | try { |
| 1342 | return JobSchedulerService.this.getPendingJobs(uid); |
| 1343 | } finally { |
| 1344 | Binder.restoreCallingIdentity(ident); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | @Override |
| 1349 | public void cancelAll() throws RemoteException { |
| 1350 | final int uid = Binder.getCallingUid(); |
| 1351 | |
| 1352 | long ident = Binder.clearCallingIdentity(); |
| 1353 | try { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1354 | JobSchedulerService.this.cancelJobsForUid(uid, true); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1355 | } finally { |
| 1356 | Binder.restoreCallingIdentity(ident); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | @Override |
| 1361 | public void cancel(int jobId) throws RemoteException { |
| 1362 | final int uid = Binder.getCallingUid(); |
| 1363 | |
| 1364 | long ident = Binder.clearCallingIdentity(); |
| 1365 | try { |
| 1366 | JobSchedulerService.this.cancelJob(uid, jobId); |
| 1367 | } finally { |
| 1368 | Binder.restoreCallingIdentity(ident); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * "dumpsys" infrastructure |
| 1374 | */ |
| 1375 | @Override |
| 1376 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1377 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 1378 | |
| 1379 | long identityToken = Binder.clearCallingIdentity(); |
| 1380 | try { |
| 1381 | JobSchedulerService.this.dumpInternal(pw); |
| 1382 | } finally { |
| 1383 | Binder.restoreCallingIdentity(identityToken); |
| 1384 | } |
| 1385 | } |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1386 | |
| 1387 | @Override |
| 1388 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 1389 | String[] args, ResultReceiver resultReceiver) throws RemoteException { |
| 1390 | (new JobSchedulerShellCommand(JobSchedulerService.this)).exec( |
| 1391 | this, in, out, err, args, resultReceiver); |
| 1392 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1393 | }; |
| 1394 | |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1395 | // Shell command infrastructure: run the given job immediately |
| 1396 | int executeRunCommand(String pkgName, int userId, int jobId, boolean force) { |
| 1397 | if (DEBUG) { |
| 1398 | Slog.v(TAG, "executeRunCommand(): " + pkgName + "/" + userId |
| 1399 | + " " + jobId + " f=" + force); |
| 1400 | } |
| 1401 | |
| 1402 | try { |
| 1403 | final int uid = AppGlobals.getPackageManager().getPackageUid(pkgName, 0, userId); |
| 1404 | if (uid < 0) { |
| 1405 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 1406 | } |
| 1407 | |
| 1408 | synchronized (mLock) { |
| 1409 | final JobStatus js = mJobs.getJobByUidAndJobId(uid, jobId); |
| 1410 | if (js == null) { |
| 1411 | return JobSchedulerShellCommand.CMD_ERR_NO_JOB; |
| 1412 | } |
| 1413 | |
| 1414 | js.overrideState = (force) ? JobStatus.OVERRIDE_FULL : JobStatus.OVERRIDE_SOFT; |
| 1415 | if (!js.isConstraintsSatisfied()) { |
| 1416 | js.overrideState = 0; |
| 1417 | return JobSchedulerShellCommand.CMD_ERR_CONSTRAINTS; |
| 1418 | } |
| 1419 | |
| 1420 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
| 1421 | } |
| 1422 | } catch (RemoteException e) { |
| 1423 | // can't happen |
| 1424 | } |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1428 | private String printContextIdToJobMap(JobStatus[] map, String initial) { |
| 1429 | StringBuilder s = new StringBuilder(initial + ": "); |
| 1430 | for (int i=0; i<map.length; i++) { |
| 1431 | s.append("(") |
| 1432 | .append(map[i] == null? -1: map[i].getJobId()) |
| 1433 | .append(map[i] == null? -1: map[i].getUid()) |
| 1434 | .append(")" ); |
| 1435 | } |
| 1436 | return s.toString(); |
| 1437 | } |
| 1438 | |
| 1439 | private String printPendingQueue() { |
| 1440 | StringBuilder s = new StringBuilder("Pending queue: "); |
| 1441 | Iterator<JobStatus> it = mPendingJobs.iterator(); |
| 1442 | while (it.hasNext()) { |
| 1443 | JobStatus js = it.next(); |
| 1444 | s.append("(") |
| 1445 | .append(js.getJob().getId()) |
| 1446 | .append(", ") |
| 1447 | .append(js.getUid()) |
| 1448 | .append(") "); |
| 1449 | } |
| 1450 | return s.toString(); |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 1451 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1452 | |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1453 | void dumpInternal(final PrintWriter pw) { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1454 | final long now = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1455 | synchronized (mLock) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 1456 | pw.println("Started users: " + Arrays.toString(mStartedUsers)); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1457 | pw.println("Registered jobs:"); |
| 1458 | if (mJobs.size() > 0) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1459 | mJobs.forEachJob(new JobStatusFunctor() { |
| 1460 | private int index = 0; |
| 1461 | |
| 1462 | @Override |
| 1463 | public void process(JobStatus job) { |
| 1464 | pw.print(" Job #"); pw.print(index++); pw.print(": "); |
| 1465 | pw.println(job.toShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1466 | job.dump(pw, " ", true); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1467 | pw.print(" Ready: "); |
| 1468 | pw.print(mHandler.isReadyToBeExecutedLocked(job)); |
| 1469 | pw.print(" (job="); |
| 1470 | pw.print(job.isReady()); |
| 1471 | pw.print(" pending="); |
| 1472 | pw.print(mPendingJobs.contains(job)); |
| 1473 | pw.print(" active="); |
| 1474 | pw.print(isCurrentlyActiveLocked(job)); |
| 1475 | pw.print(" user="); |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 1476 | pw.print(ArrayUtils.contains(mStartedUsers, job.getUserId())); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1477 | pw.println(")"); |
| 1478 | } |
| 1479 | }); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1480 | } else { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1481 | pw.println(" None."); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1482 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1483 | for (int i=0; i<mControllers.size(); i++) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1484 | pw.println(); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1485 | mControllers.get(i).dumpControllerStateLocked(pw); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1486 | } |
| 1487 | pw.println(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1488 | pw.println("Uid priority overrides:"); |
| 1489 | for (int i=0; i< mUidPriorityOverride.size(); i++) { |
| 1490 | pw.print(" "); pw.print(UserHandle.formatUid(mUidPriorityOverride.keyAt(i))); |
| 1491 | pw.print(": "); pw.println(mUidPriorityOverride.valueAt(i)); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1492 | } |
| 1493 | pw.println(); |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1494 | mJobPackageTracker.dump(pw, ""); |
| 1495 | pw.println(); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1496 | pw.println("Pending queue:"); |
| 1497 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 1498 | JobStatus job = mPendingJobs.get(i); |
| 1499 | pw.print(" Pending #"); pw.print(i); pw.print(": "); |
| 1500 | pw.println(job.toShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1501 | job.dump(pw, " ", false); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1502 | int priority = evaluateJobPriorityLocked(job); |
| 1503 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 1504 | pw.print(" Evaluated priority: "); pw.println(priority); |
| 1505 | } |
| 1506 | pw.print(" Tag: "); pw.println(job.getTag()); |
| 1507 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1508 | pw.println(); |
| 1509 | pw.println("Active jobs:"); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1510 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1511 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1512 | pw.print(" Slot #"); pw.print(i); pw.print(": "); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1513 | if (jsc.getRunningJob() == null) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1514 | pw.println("inactive"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1515 | continue; |
| 1516 | } else { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1517 | pw.println(jsc.getRunningJob().toShortString()); |
| 1518 | pw.print(" Running for: "); |
| 1519 | TimeUtils.formatDuration(now - jsc.getExecutionStartTimeElapsed(), pw); |
| 1520 | pw.print(", timeout at: "); |
| 1521 | TimeUtils.formatDuration(jsc.getTimeoutElapsed() - now, pw); |
| 1522 | pw.println(); |
| 1523 | jsc.getRunningJob().dump(pw, " ", false); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1524 | int priority = evaluateJobPriorityLocked(jsc.getRunningJob()); |
| 1525 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1526 | pw.print(" Evaluated priority: "); pw.println(priority); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1527 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1528 | } |
| 1529 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1530 | pw.println(); |
| 1531 | pw.print("mReadyToRock="); pw.println(mReadyToRock); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1532 | pw.print("mReportedActive="); pw.println(mReportedActive); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1533 | pw.print("mMaxActiveJobs="); pw.println(mMaxActiveJobs); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1534 | } |
| 1535 | pw.println(); |
| 1536 | } |
| 1537 | } |