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