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