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 | |
Christopher Tate | 435c2f4 | 2018-01-18 12:59:15 -0800 | [diff] [blame] | 22 | import android.annotation.UserIdInt; |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 23 | import android.app.Activity; |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 25 | import android.app.ActivityManagerInternal; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 26 | import android.app.AppGlobals; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 27 | import android.app.IUidObserver; |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 28 | import android.app.job.IJobScheduler; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 29 | import android.app.job.JobInfo; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 30 | import android.app.job.JobParameters; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 31 | import android.app.job.JobScheduler; |
| 32 | import android.app.job.JobService; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 33 | import android.app.job.JobWorkItem; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 34 | import android.app.usage.UsageStatsManager; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 35 | import android.app.usage.UsageStatsManagerInternal; |
| 36 | import android.app.usage.UsageStatsManagerInternal.AppIdleStateChangeListener; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 37 | import android.content.BroadcastReceiver; |
| 38 | import android.content.ComponentName; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
| 42 | import android.content.IntentFilter; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 43 | import android.content.pm.IPackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 44 | import android.content.pm.PackageManager; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 45 | import android.content.pm.PackageManagerInternal; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 46 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 47 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 48 | import android.database.ContentObserver; |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 49 | import android.net.Uri; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 50 | import android.os.BatteryStats; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 51 | import android.os.Binder; |
| 52 | import android.os.Handler; |
| 53 | import android.os.Looper; |
| 54 | import android.os.Message; |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 55 | import android.os.Process; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 56 | import android.os.RemoteException; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 57 | import android.os.ResultReceiver; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 58 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 59 | import android.os.ShellCallback; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 60 | import android.os.SystemClock; |
| 61 | import android.os.UserHandle; |
Michael Wachenschwanz | 3f2b655 | 2017-05-15 13:53:09 -0700 | [diff] [blame] | 62 | import android.os.UserManagerInternal; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 63 | import android.provider.Settings; |
| 64 | import android.util.KeyValueListParser; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 65 | import android.util.Slog; |
| 66 | import android.util.SparseArray; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 67 | import android.util.SparseIntArray; |
Tej Singh | d5747a6 | 2018-01-08 20:57:35 -0800 | [diff] [blame] | 68 | import android.util.StatsLog; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 69 | import android.util.TimeUtils; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 70 | import android.util.proto.ProtoOutputStream; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 71 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 72 | import com.android.internal.annotations.VisibleForTesting; |
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; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 75 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 76 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 77 | import com.android.internal.util.DumpUtils; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 78 | import com.android.internal.util.Preconditions; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 79 | import com.android.server.DeviceIdleController; |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 80 | import com.android.server.FgThread; |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame^] | 81 | import com.android.server.AppStateTracker; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 82 | import com.android.server.LocalServices; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 83 | import com.android.server.job.JobSchedulerServiceDumpProto.ActiveJob; |
| 84 | import com.android.server.job.JobSchedulerServiceDumpProto.PendingJob; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 85 | import com.android.server.job.JobStore.JobStatusFunctor; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 86 | import com.android.server.job.controllers.AppIdleController; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 87 | import com.android.server.job.controllers.BackgroundJobsController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 88 | import com.android.server.job.controllers.BatteryController; |
| 89 | import com.android.server.job.controllers.ConnectivityController; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 90 | import com.android.server.job.controllers.ContentObserverController; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 91 | import com.android.server.job.controllers.DeviceIdleJobsController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 92 | import com.android.server.job.controllers.IdleController; |
| 93 | import com.android.server.job.controllers.JobStatus; |
| 94 | import com.android.server.job.controllers.StateController; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 95 | import com.android.server.job.controllers.StorageController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 96 | import com.android.server.job.controllers.TimeController; |
| 97 | |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 98 | import libcore.util.EmptyArray; |
| 99 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 100 | import java.io.FileDescriptor; |
| 101 | import java.io.PrintWriter; |
| 102 | import java.time.Clock; |
| 103 | import java.util.ArrayList; |
| 104 | import java.util.Arrays; |
| 105 | import java.util.Collections; |
| 106 | import java.util.Comparator; |
| 107 | import java.util.Iterator; |
| 108 | import java.util.List; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 109 | import java.util.function.Predicate; |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 110 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Responsible for taking jobs representing work to be performed by a client app, and determining |
| 113 | * based on the criteria specified when that job should be run against the client application's |
| 114 | * endpoint. |
| 115 | * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing |
| 116 | * about constraints, or the state of active jobs. It receives callbacks from the various |
| 117 | * controllers and completed jobs and operates accordingly. |
| 118 | * |
| 119 | * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object. |
| 120 | * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}. |
| 121 | * @hide |
| 122 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 123 | public final class JobSchedulerService extends com.android.server.SystemService |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 124 | implements StateChangedListener, JobCompletedListener { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 125 | static final String TAG = "JobSchedulerService"; |
Matthew Williams | aa98431 | 2015-10-15 16:08:05 -0700 | [diff] [blame] | 126 | public static final boolean DEBUG = false; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 127 | public static final boolean DEBUG_STANDBY = DEBUG || false; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 128 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 129 | /** The maximum number of concurrent jobs we run at one time. */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 130 | private static final int MAX_JOB_CONTEXTS_COUNT = 16; |
Christopher Tate | dabdf6f | 2016-02-24 12:30:22 -0800 | [diff] [blame] | 131 | /** Enforce a per-app limit on scheduled jobs? */ |
Christopher Tate | 0213ace0 | 2016-02-24 14:18:35 -0800 | [diff] [blame] | 132 | private static final boolean ENFORCE_MAX_JOBS = true; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 133 | /** The maximum number of jobs that we allow an unprivileged app to schedule */ |
| 134 | private static final int MAX_JOBS_PER_APP = 100; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 135 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 136 | @VisibleForTesting |
| 137 | public static Clock sSystemClock = Clock.systemUTC(); |
| 138 | @VisibleForTesting |
| 139 | public static Clock sUptimeMillisClock = SystemClock.uptimeMillisClock(); |
| 140 | @VisibleForTesting |
| 141 | public static Clock sElapsedRealtimeClock = SystemClock.elapsedRealtimeClock(); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 142 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 143 | /** Global local for all job scheduler state. */ |
| 144 | final Object mLock = new Object(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 145 | /** Master list of jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 146 | final JobStore mJobs; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 147 | /** Tracking the standby bucket state of each app */ |
| 148 | final StandbyTracker mStandbyTracker; |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 149 | /** Tracking amount of time each package runs for. */ |
| 150 | final JobPackageTracker mJobPackageTracker = new JobPackageTracker(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 151 | |
| 152 | static final int MSG_JOB_EXPIRED = 0; |
| 153 | static final int MSG_CHECK_JOB = 1; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 154 | static final int MSG_STOP_JOB = 2; |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 155 | static final int MSG_CHECK_JOB_GREEDY = 3; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 156 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Track Services that have currently active or pending jobs. The index is provided by |
| 159 | * {@link JobStatus#getServiceToken()} |
| 160 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 161 | final List<JobServiceContext> mActiveServices = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 162 | /** List of controllers that will notify this service of updates to jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 163 | List<StateController> mControllers; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 164 | /** Need direct access to this for testing. */ |
| 165 | BatteryController mBatteryController; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 166 | /** Need direct access to this for testing. */ |
| 167 | StorageController mStorageController; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 168 | /** Need directly for sending uid state changes */ |
| 169 | private BackgroundJobsController mBackgroundJobsController; |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 170 | private DeviceIdleJobsController mDeviceIdleJobsController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 171 | /** |
| 172 | * Queue of pending jobs. The JobServiceContext class will receive jobs from this list |
| 173 | * when ready to execute them. |
| 174 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 175 | final ArrayList<JobStatus> mPendingJobs = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 176 | |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 177 | int[] mStartedUsers = EmptyArray.INT; |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 178 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 179 | final JobHandler mHandler; |
| 180 | final JobSchedulerStub mJobSchedulerStub; |
| 181 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 182 | PackageManagerInternal mLocalPM; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 183 | ActivityManagerInternal mActivityManagerInternal; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 184 | IBatteryStats mBatteryStats; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 185 | DeviceIdleController.LocalService mLocalDeviceIdleController; |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame^] | 186 | AppStateTracker mAppStateTracker; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Set to true once we are allowed to run third party apps. |
| 190 | */ |
| 191 | boolean mReadyToRock; |
| 192 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 193 | /** |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 194 | * What we last reported to DeviceIdleController about whether we are active. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 195 | */ |
| 196 | boolean mReportedActive; |
| 197 | |
| 198 | /** |
Christopher Tate | a5a85bd | 2018-01-03 17:20:36 -0800 | [diff] [blame] | 199 | * Are we currently in device-wide standby parole? |
| 200 | */ |
| 201 | volatile boolean mInParole; |
| 202 | |
| 203 | /** |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 204 | * Current limit on the number of concurrent JobServiceContext entries we want to |
| 205 | * keep actively running a job. |
| 206 | */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 207 | int mMaxActiveJobs = 1; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 208 | |
| 209 | /** |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 210 | * A mapping of which uids are currently in the foreground to their effective priority. |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 211 | */ |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 212 | final SparseIntArray mUidPriorityOverride = new SparseIntArray(); |
| 213 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Which uids are currently performing backups, so we shouldn't allow their jobs to run. |
| 216 | */ |
| 217 | final SparseIntArray mBackingUpUids = new SparseIntArray(); |
| 218 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Count standby heartbeats, and keep track of which beat each bucket's jobs will |
| 221 | * next become runnable. Index into this array is by normalized bucket: |
| 222 | * { ACTIVE, WORKING, FREQUENT, RARE, NEVER }. The ACTIVE and NEVER bucket |
| 223 | * milestones are not updated: ACTIVE apps get jobs whenever they ask for them, |
| 224 | * and NEVER apps don't get them at all. |
| 225 | */ |
| 226 | final long[] mNextBucketHeartbeat = { 0, 0, 0, 0, Long.MAX_VALUE }; |
| 227 | long mHeartbeat = 0; |
| 228 | long mLastHeartbeatTime = 0; |
| 229 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 230 | // -- Pre-allocated temporaries only for use in assignJobsToContextsLocked -- |
| 231 | |
| 232 | /** |
| 233 | * This array essentially stores the state of mActiveServices array. |
| 234 | * The ith index stores the job present on the ith JobServiceContext. |
| 235 | * We manipulate this array until we arrive at what jobs should be running on |
| 236 | * what JobServiceContext. |
| 237 | */ |
| 238 | JobStatus[] mTmpAssignContextIdToJobMap = new JobStatus[MAX_JOB_CONTEXTS_COUNT]; |
| 239 | /** |
| 240 | * Indicates whether we need to act on this jobContext id |
| 241 | */ |
| 242 | boolean[] mTmpAssignAct = new boolean[MAX_JOB_CONTEXTS_COUNT]; |
| 243 | /** |
| 244 | * The uid whose jobs we would like to assign to a context. |
| 245 | */ |
| 246 | int[] mTmpAssignPreferredUidForContext = new int[MAX_JOB_CONTEXTS_COUNT]; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 247 | |
| 248 | /** |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 249 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 250 | * global Settings. Any access to this class or its fields should be done while |
| 251 | * holding the JobSchedulerService.mLock lock. |
| 252 | */ |
| 253 | private final class Constants extends ContentObserver { |
| 254 | // Key names stored in the settings value. |
| 255 | private static final String KEY_MIN_IDLE_COUNT = "min_idle_count"; |
| 256 | private static final String KEY_MIN_CHARGING_COUNT = "min_charging_count"; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 257 | private static final String KEY_MIN_BATTERY_NOT_LOW_COUNT = "min_battery_not_low_count"; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 258 | private static final String KEY_MIN_STORAGE_NOT_LOW_COUNT = "min_storage_not_low_count"; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 259 | private static final String KEY_MIN_CONNECTIVITY_COUNT = "min_connectivity_count"; |
| 260 | private static final String KEY_MIN_CONTENT_COUNT = "min_content_count"; |
| 261 | private static final String KEY_MIN_READY_JOBS_COUNT = "min_ready_jobs_count"; |
| 262 | private static final String KEY_HEAVY_USE_FACTOR = "heavy_use_factor"; |
| 263 | private static final String KEY_MODERATE_USE_FACTOR = "moderate_use_factor"; |
| 264 | private static final String KEY_FG_JOB_COUNT = "fg_job_count"; |
| 265 | private static final String KEY_BG_NORMAL_JOB_COUNT = "bg_normal_job_count"; |
| 266 | private static final String KEY_BG_MODERATE_JOB_COUNT = "bg_moderate_job_count"; |
| 267 | private static final String KEY_BG_LOW_JOB_COUNT = "bg_low_job_count"; |
| 268 | private static final String KEY_BG_CRITICAL_JOB_COUNT = "bg_critical_job_count"; |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 269 | private static final String KEY_MAX_STANDARD_RESCHEDULE_COUNT |
| 270 | = "max_standard_reschedule_count"; |
| 271 | private static final String KEY_MAX_WORK_RESCHEDULE_COUNT = "max_work_reschedule_count"; |
| 272 | private static final String KEY_MIN_LINEAR_BACKOFF_TIME = "min_linear_backoff_time"; |
| 273 | private static final String KEY_MIN_EXP_BACKOFF_TIME = "min_exp_backoff_time"; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 274 | private static final String KEY_STANDBY_HEARTBEAT_TIME = "standby_heartbeat_time"; |
| 275 | private static final String KEY_STANDBY_WORKING_BEATS = "standby_working_beats"; |
| 276 | private static final String KEY_STANDBY_FREQUENT_BEATS = "standby_frequent_beats"; |
| 277 | private static final String KEY_STANDBY_RARE_BEATS = "standby_rare_beats"; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 278 | |
| 279 | private static final int DEFAULT_MIN_IDLE_COUNT = 1; |
| 280 | private static final int DEFAULT_MIN_CHARGING_COUNT = 1; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 281 | private static final int DEFAULT_MIN_BATTERY_NOT_LOW_COUNT = 1; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 282 | private static final int DEFAULT_MIN_STORAGE_NOT_LOW_COUNT = 1; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 283 | private static final int DEFAULT_MIN_CONNECTIVITY_COUNT = 1; |
| 284 | private static final int DEFAULT_MIN_CONTENT_COUNT = 1; |
| 285 | private static final int DEFAULT_MIN_READY_JOBS_COUNT = 1; |
| 286 | private static final float DEFAULT_HEAVY_USE_FACTOR = .9f; |
| 287 | private static final float DEFAULT_MODERATE_USE_FACTOR = .5f; |
| 288 | private static final int DEFAULT_FG_JOB_COUNT = 4; |
| 289 | private static final int DEFAULT_BG_NORMAL_JOB_COUNT = 6; |
| 290 | private static final int DEFAULT_BG_MODERATE_JOB_COUNT = 4; |
Nancy Zheng | e39a8a4 | 2016-10-05 16:27:14 -0700 | [diff] [blame] | 291 | private static final int DEFAULT_BG_LOW_JOB_COUNT = 1; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 292 | private static final int DEFAULT_BG_CRITICAL_JOB_COUNT = 1; |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 293 | private static final int DEFAULT_MAX_STANDARD_RESCHEDULE_COUNT = Integer.MAX_VALUE; |
| 294 | private static final int DEFAULT_MAX_WORK_RESCHEDULE_COUNT = Integer.MAX_VALUE; |
| 295 | private static final long DEFAULT_MIN_LINEAR_BACKOFF_TIME = JobInfo.MIN_BACKOFF_MILLIS; |
| 296 | private static final long DEFAULT_MIN_EXP_BACKOFF_TIME = JobInfo.MIN_BACKOFF_MILLIS; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 297 | private static final long DEFAULT_STANDBY_HEARTBEAT_TIME = 11 * 60 * 1000L; |
Esteban Talavera | 6525404 | 2017-12-15 10:59:28 +0000 | [diff] [blame] | 298 | private static final int DEFAULT_STANDBY_WORKING_BEATS = 11; // ~ 2 hours, with 11min beats |
| 299 | private static final int DEFAULT_STANDBY_FREQUENT_BEATS = 43; // ~ 8 hours |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 300 | private static final int DEFAULT_STANDBY_RARE_BEATS = 130; // ~ 24 hours |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things |
| 304 | * early. |
| 305 | */ |
| 306 | int MIN_IDLE_COUNT = DEFAULT_MIN_IDLE_COUNT; |
| 307 | /** |
| 308 | * Minimum # of charging jobs that must be ready in order to force the JMS to schedule |
| 309 | * things early. |
| 310 | */ |
| 311 | int MIN_CHARGING_COUNT = DEFAULT_MIN_CHARGING_COUNT; |
| 312 | /** |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 313 | * Minimum # of "battery not low" jobs that must be ready in order to force the JMS to |
| 314 | * schedule things early. |
| 315 | */ |
| 316 | int MIN_BATTERY_NOT_LOW_COUNT = DEFAULT_MIN_BATTERY_NOT_LOW_COUNT; |
| 317 | /** |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 318 | * Minimum # of "storage not low" jobs that must be ready in order to force the JMS to |
| 319 | * schedule things early. |
| 320 | */ |
| 321 | int MIN_STORAGE_NOT_LOW_COUNT = DEFAULT_MIN_STORAGE_NOT_LOW_COUNT; |
| 322 | /** |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 323 | * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule |
| 324 | * things early. 1 == Run connectivity jobs as soon as ready. |
| 325 | */ |
| 326 | int MIN_CONNECTIVITY_COUNT = DEFAULT_MIN_CONNECTIVITY_COUNT; |
| 327 | /** |
| 328 | * Minimum # of content trigger jobs that must be ready in order to force the JMS to |
| 329 | * schedule things early. |
| 330 | */ |
| 331 | int MIN_CONTENT_COUNT = DEFAULT_MIN_CONTENT_COUNT; |
| 332 | /** |
| 333 | * Minimum # of jobs (with no particular constraints) for which the JMS will be happy |
| 334 | * running some work early. This (and thus the other min counts) is now set to 1, to |
| 335 | * prevent any batching at this level. Since we now do batching through doze, that is |
| 336 | * a much better mechanism. |
| 337 | */ |
| 338 | int MIN_READY_JOBS_COUNT = DEFAULT_MIN_READY_JOBS_COUNT; |
| 339 | /** |
| 340 | * This is the job execution factor that is considered to be heavy use of the system. |
| 341 | */ |
| 342 | float HEAVY_USE_FACTOR = DEFAULT_HEAVY_USE_FACTOR; |
| 343 | /** |
| 344 | * This is the job execution factor that is considered to be moderate use of the system. |
| 345 | */ |
| 346 | float MODERATE_USE_FACTOR = DEFAULT_MODERATE_USE_FACTOR; |
| 347 | /** |
| 348 | * The number of MAX_JOB_CONTEXTS_COUNT we reserve for the foreground app. |
| 349 | */ |
| 350 | int FG_JOB_COUNT = DEFAULT_FG_JOB_COUNT; |
| 351 | /** |
| 352 | * The maximum number of background jobs we allow when the system is in a normal |
| 353 | * memory state. |
| 354 | */ |
| 355 | int BG_NORMAL_JOB_COUNT = DEFAULT_BG_NORMAL_JOB_COUNT; |
| 356 | /** |
| 357 | * The maximum number of background jobs we allow when the system is in a moderate |
| 358 | * memory state. |
| 359 | */ |
| 360 | int BG_MODERATE_JOB_COUNT = DEFAULT_BG_MODERATE_JOB_COUNT; |
| 361 | /** |
| 362 | * The maximum number of background jobs we allow when the system is in a low |
| 363 | * memory state. |
| 364 | */ |
| 365 | int BG_LOW_JOB_COUNT = DEFAULT_BG_LOW_JOB_COUNT; |
| 366 | /** |
| 367 | * The maximum number of background jobs we allow when the system is in a critical |
| 368 | * memory state. |
| 369 | */ |
| 370 | int BG_CRITICAL_JOB_COUNT = DEFAULT_BG_CRITICAL_JOB_COUNT; |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 371 | /** |
| 372 | * The maximum number of times we allow a job to have itself rescheduled before |
| 373 | * giving up on it, for standard jobs. |
| 374 | */ |
| 375 | int MAX_STANDARD_RESCHEDULE_COUNT = DEFAULT_MAX_STANDARD_RESCHEDULE_COUNT; |
| 376 | /** |
| 377 | * The maximum number of times we allow a job to have itself rescheduled before |
| 378 | * giving up on it, for jobs that are executing work. |
| 379 | */ |
| 380 | int MAX_WORK_RESCHEDULE_COUNT = DEFAULT_MAX_WORK_RESCHEDULE_COUNT; |
| 381 | /** |
| 382 | * The minimum backoff time to allow for linear backoff. |
| 383 | */ |
| 384 | long MIN_LINEAR_BACKOFF_TIME = DEFAULT_MIN_LINEAR_BACKOFF_TIME; |
| 385 | /** |
| 386 | * The minimum backoff time to allow for exponential backoff. |
| 387 | */ |
| 388 | long MIN_EXP_BACKOFF_TIME = DEFAULT_MIN_EXP_BACKOFF_TIME; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 389 | /** |
| 390 | * How often we recalculate runnability based on apps' standby bucket assignment. |
| 391 | * This should be prime relative to common time interval lengths such as a quarter- |
| 392 | * hour or day, so that the heartbeat drifts relative to wall-clock milestones. |
| 393 | */ |
| 394 | long STANDBY_HEARTBEAT_TIME = DEFAULT_STANDBY_HEARTBEAT_TIME; |
| 395 | |
| 396 | /** |
| 397 | * Mapping: standby bucket -> number of heartbeats between each sweep of that |
| 398 | * bucket's jobs. |
| 399 | * |
| 400 | * Bucket assignments as recorded in the JobStatus objects are normalized to be |
| 401 | * indices into this array, rather than the raw constants used |
| 402 | * by AppIdleHistory. |
| 403 | */ |
| 404 | final int[] STANDBY_BEATS = { |
| 405 | 0, |
| 406 | DEFAULT_STANDBY_WORKING_BEATS, |
| 407 | DEFAULT_STANDBY_FREQUENT_BEATS, |
| 408 | DEFAULT_STANDBY_RARE_BEATS |
| 409 | }; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 410 | |
| 411 | private ContentResolver mResolver; |
| 412 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 413 | |
| 414 | public Constants(Handler handler) { |
| 415 | super(handler); |
| 416 | } |
| 417 | |
| 418 | public void start(ContentResolver resolver) { |
| 419 | mResolver = resolver; |
| 420 | mResolver.registerContentObserver(Settings.Global.getUriFor( |
| 421 | Settings.Global.JOB_SCHEDULER_CONSTANTS), false, this); |
| 422 | updateConstants(); |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void onChange(boolean selfChange, Uri uri) { |
| 427 | updateConstants(); |
| 428 | } |
| 429 | |
| 430 | private void updateConstants() { |
| 431 | synchronized (mLock) { |
| 432 | try { |
| 433 | mParser.setString(Settings.Global.getString(mResolver, |
Suprabh Shukla | 7a0a5c4 | 2017-07-25 21:07:13 +0000 | [diff] [blame] | 434 | Settings.Global.JOB_SCHEDULER_CONSTANTS)); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 435 | } catch (IllegalArgumentException e) { |
| 436 | // Failed to parse the settings string, log this and move on |
| 437 | // with defaults. |
Suprabh Shukla | 7a0a5c4 | 2017-07-25 21:07:13 +0000 | [diff] [blame] | 438 | Slog.e(TAG, "Bad jobscheduler settings", e); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | MIN_IDLE_COUNT = mParser.getInt(KEY_MIN_IDLE_COUNT, |
| 442 | DEFAULT_MIN_IDLE_COUNT); |
| 443 | MIN_CHARGING_COUNT = mParser.getInt(KEY_MIN_CHARGING_COUNT, |
| 444 | DEFAULT_MIN_CHARGING_COUNT); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 445 | MIN_BATTERY_NOT_LOW_COUNT = mParser.getInt(KEY_MIN_BATTERY_NOT_LOW_COUNT, |
| 446 | DEFAULT_MIN_BATTERY_NOT_LOW_COUNT); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 447 | MIN_STORAGE_NOT_LOW_COUNT = mParser.getInt(KEY_MIN_STORAGE_NOT_LOW_COUNT, |
| 448 | DEFAULT_MIN_STORAGE_NOT_LOW_COUNT); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 449 | MIN_CONNECTIVITY_COUNT = mParser.getInt(KEY_MIN_CONNECTIVITY_COUNT, |
| 450 | DEFAULT_MIN_CONNECTIVITY_COUNT); |
| 451 | MIN_CONTENT_COUNT = mParser.getInt(KEY_MIN_CONTENT_COUNT, |
| 452 | DEFAULT_MIN_CONTENT_COUNT); |
| 453 | MIN_READY_JOBS_COUNT = mParser.getInt(KEY_MIN_READY_JOBS_COUNT, |
| 454 | DEFAULT_MIN_READY_JOBS_COUNT); |
| 455 | HEAVY_USE_FACTOR = mParser.getFloat(KEY_HEAVY_USE_FACTOR, |
| 456 | DEFAULT_HEAVY_USE_FACTOR); |
| 457 | MODERATE_USE_FACTOR = mParser.getFloat(KEY_MODERATE_USE_FACTOR, |
| 458 | DEFAULT_MODERATE_USE_FACTOR); |
| 459 | FG_JOB_COUNT = mParser.getInt(KEY_FG_JOB_COUNT, |
| 460 | DEFAULT_FG_JOB_COUNT); |
| 461 | BG_NORMAL_JOB_COUNT = mParser.getInt(KEY_BG_NORMAL_JOB_COUNT, |
| 462 | DEFAULT_BG_NORMAL_JOB_COUNT); |
| 463 | if ((FG_JOB_COUNT+BG_NORMAL_JOB_COUNT) > MAX_JOB_CONTEXTS_COUNT) { |
| 464 | BG_NORMAL_JOB_COUNT = MAX_JOB_CONTEXTS_COUNT - FG_JOB_COUNT; |
| 465 | } |
| 466 | BG_MODERATE_JOB_COUNT = mParser.getInt(KEY_BG_MODERATE_JOB_COUNT, |
| 467 | DEFAULT_BG_MODERATE_JOB_COUNT); |
| 468 | if ((FG_JOB_COUNT+BG_MODERATE_JOB_COUNT) > MAX_JOB_CONTEXTS_COUNT) { |
| 469 | BG_MODERATE_JOB_COUNT = MAX_JOB_CONTEXTS_COUNT - FG_JOB_COUNT; |
| 470 | } |
| 471 | BG_LOW_JOB_COUNT = mParser.getInt(KEY_BG_LOW_JOB_COUNT, |
| 472 | DEFAULT_BG_LOW_JOB_COUNT); |
| 473 | if ((FG_JOB_COUNT+BG_LOW_JOB_COUNT) > MAX_JOB_CONTEXTS_COUNT) { |
| 474 | BG_LOW_JOB_COUNT = MAX_JOB_CONTEXTS_COUNT - FG_JOB_COUNT; |
| 475 | } |
| 476 | BG_CRITICAL_JOB_COUNT = mParser.getInt(KEY_BG_CRITICAL_JOB_COUNT, |
| 477 | DEFAULT_BG_CRITICAL_JOB_COUNT); |
| 478 | if ((FG_JOB_COUNT+BG_CRITICAL_JOB_COUNT) > MAX_JOB_CONTEXTS_COUNT) { |
| 479 | BG_CRITICAL_JOB_COUNT = MAX_JOB_CONTEXTS_COUNT - FG_JOB_COUNT; |
| 480 | } |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 481 | MAX_STANDARD_RESCHEDULE_COUNT = mParser.getInt(KEY_MAX_STANDARD_RESCHEDULE_COUNT, |
| 482 | DEFAULT_MAX_STANDARD_RESCHEDULE_COUNT); |
| 483 | MAX_WORK_RESCHEDULE_COUNT = mParser.getInt(KEY_MAX_WORK_RESCHEDULE_COUNT, |
| 484 | DEFAULT_MAX_WORK_RESCHEDULE_COUNT); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 485 | MIN_LINEAR_BACKOFF_TIME = mParser.getDurationMillis(KEY_MIN_LINEAR_BACKOFF_TIME, |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 486 | DEFAULT_MIN_LINEAR_BACKOFF_TIME); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 487 | MIN_EXP_BACKOFF_TIME = mParser.getDurationMillis(KEY_MIN_EXP_BACKOFF_TIME, |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 488 | DEFAULT_MIN_EXP_BACKOFF_TIME); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 489 | STANDBY_HEARTBEAT_TIME = mParser.getDurationMillis(KEY_STANDBY_HEARTBEAT_TIME, |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 490 | DEFAULT_STANDBY_HEARTBEAT_TIME); |
| 491 | STANDBY_BEATS[1] = mParser.getInt(KEY_STANDBY_WORKING_BEATS, |
| 492 | DEFAULT_STANDBY_WORKING_BEATS); |
| 493 | STANDBY_BEATS[2] = mParser.getInt(KEY_STANDBY_FREQUENT_BEATS, |
| 494 | DEFAULT_STANDBY_FREQUENT_BEATS); |
| 495 | STANDBY_BEATS[3] = mParser.getInt(KEY_STANDBY_RARE_BEATS, |
| 496 | DEFAULT_STANDBY_RARE_BEATS); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | void dump(PrintWriter pw) { |
| 501 | pw.println(" Settings:"); |
| 502 | |
| 503 | pw.print(" "); pw.print(KEY_MIN_IDLE_COUNT); pw.print("="); |
| 504 | pw.print(MIN_IDLE_COUNT); pw.println(); |
| 505 | |
| 506 | pw.print(" "); pw.print(KEY_MIN_CHARGING_COUNT); pw.print("="); |
| 507 | pw.print(MIN_CHARGING_COUNT); pw.println(); |
| 508 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 509 | pw.print(" "); pw.print(KEY_MIN_BATTERY_NOT_LOW_COUNT); pw.print("="); |
| 510 | pw.print(MIN_BATTERY_NOT_LOW_COUNT); pw.println(); |
| 511 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 512 | pw.print(" "); pw.print(KEY_MIN_STORAGE_NOT_LOW_COUNT); pw.print("="); |
| 513 | pw.print(MIN_STORAGE_NOT_LOW_COUNT); pw.println(); |
| 514 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 515 | pw.print(" "); pw.print(KEY_MIN_CONNECTIVITY_COUNT); pw.print("="); |
| 516 | pw.print(MIN_CONNECTIVITY_COUNT); pw.println(); |
| 517 | |
| 518 | pw.print(" "); pw.print(KEY_MIN_CONTENT_COUNT); pw.print("="); |
| 519 | pw.print(MIN_CONTENT_COUNT); pw.println(); |
| 520 | |
| 521 | pw.print(" "); pw.print(KEY_MIN_READY_JOBS_COUNT); pw.print("="); |
| 522 | pw.print(MIN_READY_JOBS_COUNT); pw.println(); |
| 523 | |
| 524 | pw.print(" "); pw.print(KEY_HEAVY_USE_FACTOR); pw.print("="); |
| 525 | pw.print(HEAVY_USE_FACTOR); pw.println(); |
| 526 | |
| 527 | pw.print(" "); pw.print(KEY_MODERATE_USE_FACTOR); pw.print("="); |
| 528 | pw.print(MODERATE_USE_FACTOR); pw.println(); |
| 529 | |
| 530 | pw.print(" "); pw.print(KEY_FG_JOB_COUNT); pw.print("="); |
| 531 | pw.print(FG_JOB_COUNT); pw.println(); |
| 532 | |
| 533 | pw.print(" "); pw.print(KEY_BG_NORMAL_JOB_COUNT); pw.print("="); |
| 534 | pw.print(BG_NORMAL_JOB_COUNT); pw.println(); |
| 535 | |
| 536 | pw.print(" "); pw.print(KEY_BG_MODERATE_JOB_COUNT); pw.print("="); |
| 537 | pw.print(BG_MODERATE_JOB_COUNT); pw.println(); |
| 538 | |
| 539 | pw.print(" "); pw.print(KEY_BG_LOW_JOB_COUNT); pw.print("="); |
| 540 | pw.print(BG_LOW_JOB_COUNT); pw.println(); |
| 541 | |
| 542 | pw.print(" "); pw.print(KEY_BG_CRITICAL_JOB_COUNT); pw.print("="); |
| 543 | pw.print(BG_CRITICAL_JOB_COUNT); pw.println(); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 544 | |
| 545 | pw.print(" "); pw.print(KEY_MAX_STANDARD_RESCHEDULE_COUNT); pw.print("="); |
| 546 | pw.print(MAX_STANDARD_RESCHEDULE_COUNT); pw.println(); |
| 547 | |
| 548 | pw.print(" "); pw.print(KEY_MAX_WORK_RESCHEDULE_COUNT); pw.print("="); |
| 549 | pw.print(MAX_WORK_RESCHEDULE_COUNT); pw.println(); |
| 550 | |
| 551 | pw.print(" "); pw.print(KEY_MIN_LINEAR_BACKOFF_TIME); pw.print("="); |
| 552 | pw.print(MIN_LINEAR_BACKOFF_TIME); pw.println(); |
| 553 | |
| 554 | pw.print(" "); pw.print(KEY_MIN_EXP_BACKOFF_TIME); pw.print("="); |
| 555 | pw.print(MIN_EXP_BACKOFF_TIME); pw.println(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 556 | |
| 557 | pw.print(" "); pw.print(KEY_STANDBY_HEARTBEAT_TIME); pw.print("="); |
| 558 | pw.print(STANDBY_HEARTBEAT_TIME); pw.println(); |
| 559 | |
| 560 | pw.print(" standby_beats={"); |
| 561 | pw.print(STANDBY_BEATS[0]); |
| 562 | for (int i = 1; i < STANDBY_BEATS.length; i++) { |
| 563 | pw.print(", "); |
| 564 | pw.print(STANDBY_BEATS[i]); |
| 565 | } |
| 566 | pw.println('}'); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 567 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 568 | |
| 569 | void dump(ProtoOutputStream proto, long fieldId) { |
| 570 | final long token = proto.start(fieldId); |
| 571 | |
| 572 | proto.write(ConstantsProto.MIN_IDLE_COUNT, MIN_IDLE_COUNT); |
| 573 | proto.write(ConstantsProto.MIN_CHARGING_COUNT, MIN_CHARGING_COUNT); |
| 574 | proto.write(ConstantsProto.MIN_BATTERY_NOT_LOW_COUNT, MIN_BATTERY_NOT_LOW_COUNT); |
| 575 | proto.write(ConstantsProto.MIN_STORAGE_NOT_LOW_COUNT, MIN_STORAGE_NOT_LOW_COUNT); |
| 576 | proto.write(ConstantsProto.MIN_CONNECTIVITY_COUNT, MIN_CONNECTIVITY_COUNT); |
| 577 | proto.write(ConstantsProto.MIN_CONTENT_COUNT, MIN_CONTENT_COUNT); |
| 578 | proto.write(ConstantsProto.MIN_READY_JOBS_COUNT, MIN_READY_JOBS_COUNT); |
| 579 | proto.write(ConstantsProto.HEAVY_USE_FACTOR, HEAVY_USE_FACTOR); |
| 580 | proto.write(ConstantsProto.MODERATE_USE_FACTOR, MODERATE_USE_FACTOR); |
| 581 | proto.write(ConstantsProto.FG_JOB_COUNT, FG_JOB_COUNT); |
| 582 | proto.write(ConstantsProto.BG_NORMAL_JOB_COUNT, BG_NORMAL_JOB_COUNT); |
| 583 | proto.write(ConstantsProto.BG_MODERATE_JOB_COUNT, BG_MODERATE_JOB_COUNT); |
| 584 | proto.write(ConstantsProto.BG_LOW_JOB_COUNT, BG_LOW_JOB_COUNT); |
| 585 | proto.write(ConstantsProto.BG_CRITICAL_JOB_COUNT, BG_CRITICAL_JOB_COUNT); |
| 586 | proto.write(ConstantsProto.MAX_STANDARD_RESCHEDULE_COUNT, MAX_STANDARD_RESCHEDULE_COUNT); |
| 587 | proto.write(ConstantsProto.MAX_WORK_RESCHEDULE_COUNT, MAX_WORK_RESCHEDULE_COUNT); |
| 588 | proto.write(ConstantsProto.MIN_LINEAR_BACKOFF_TIME_MS, MIN_LINEAR_BACKOFF_TIME); |
| 589 | proto.write(ConstantsProto.MIN_EXP_BACKOFF_TIME_MS, MIN_EXP_BACKOFF_TIME); |
| 590 | proto.write(ConstantsProto.STANDBY_HEARTBEAT_TIME_MS, STANDBY_HEARTBEAT_TIME); |
| 591 | |
| 592 | for (int period : STANDBY_BEATS) { |
| 593 | proto.write(ConstantsProto.STANDBY_BEATS, period); |
| 594 | } |
| 595 | |
| 596 | proto.end(token); |
| 597 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | final Constants mConstants; |
| 601 | |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 602 | static final Comparator<JobStatus> mEnqueueTimeComparator = (o1, o2) -> { |
| 603 | if (o1.enqueueTime < o2.enqueueTime) { |
| 604 | return -1; |
| 605 | } |
| 606 | return o1.enqueueTime > o2.enqueueTime ? 1 : 0; |
| 607 | }; |
| 608 | |
| 609 | static <T> void addOrderedItem(ArrayList<T> array, T newItem, Comparator<T> comparator) { |
| 610 | int where = Collections.binarySearch(array, newItem, comparator); |
| 611 | if (where < 0) { |
| 612 | where = ~where; |
| 613 | } |
| 614 | array.add(where, newItem); |
| 615 | } |
| 616 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 617 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 618 | * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we |
| 619 | * still clean up. On reinstall the package will have a new uid. |
| 620 | */ |
| 621 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 622 | @Override |
| 623 | public void onReceive(Context context, Intent intent) { |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 624 | final String action = intent.getAction(); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 625 | if (DEBUG) { |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 626 | Slog.d(TAG, "Receieved: " + action); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 627 | } |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 628 | final String pkgName = getPackageName(intent); |
| 629 | final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 630 | |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 631 | if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) { |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 632 | // Purge the app's jobs if the whole package was just disabled. When this is |
| 633 | // the case the component name will be a bare package name. |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 634 | if (pkgName != null && pkgUid != -1) { |
| 635 | final String[] changedComponents = intent.getStringArrayExtra( |
| 636 | Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST); |
| 637 | if (changedComponents != null) { |
| 638 | for (String component : changedComponents) { |
| 639 | if (component.equals(pkgName)) { |
| 640 | if (DEBUG) { |
| 641 | Slog.d(TAG, "Package state change: " + pkgName); |
| 642 | } |
| 643 | try { |
| 644 | final int userId = UserHandle.getUserId(pkgUid); |
| 645 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 646 | final int state = pm.getApplicationEnabledSetting(pkgName, userId); |
| 647 | if (state == COMPONENT_ENABLED_STATE_DISABLED |
| 648 | || state == COMPONENT_ENABLED_STATE_DISABLED_USER) { |
| 649 | if (DEBUG) { |
| 650 | Slog.d(TAG, "Removing jobs for package " + pkgName |
| 651 | + " in user " + userId); |
| 652 | } |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 653 | cancelJobsForPackageAndUid(pkgName, pkgUid, |
| 654 | "app disabled"); |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 655 | } |
Christopher Tate | 652c5ad | 2016-10-05 14:45:46 -0700 | [diff] [blame] | 656 | } catch (RemoteException|IllegalArgumentException e) { |
| 657 | /* |
| 658 | * IllegalArgumentException means that the package doesn't exist. |
| 659 | * This arises when PACKAGE_CHANGED broadcast delivery has lagged |
| 660 | * behind outright uninstall, so by the time we try to act it's gone. |
| 661 | * We don't need to act on this PACKAGE_CHANGED when this happens; |
| 662 | * we'll get a PACKAGE_REMOVED later and clean up then. |
| 663 | * |
| 664 | * RemoteException can't actually happen; the package manager is |
| 665 | * running in this same process. |
| 666 | */ |
| 667 | } |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 668 | break; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | } else { |
| 673 | Slog.w(TAG, "PACKAGE_CHANGED for " + pkgName + " / uid " + pkgUid); |
| 674 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 675 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
Christopher Tate | aad67a3 | 2014-10-20 16:29:20 -0700 | [diff] [blame] | 676 | // If this is an outright uninstall rather than the first half of an |
| 677 | // app update sequence, cancel the jobs associated with the app. |
| 678 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 679 | int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 680 | if (DEBUG) { |
| 681 | Slog.d(TAG, "Removing jobs for uid: " + uidRemoved); |
| 682 | } |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 683 | cancelJobsForPackageAndUid(pkgName, uidRemoved, "app uninstalled"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 684 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 685 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 686 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 687 | if (DEBUG) { |
| 688 | Slog.d(TAG, "Removing jobs for user: " + userId); |
| 689 | } |
| 690 | cancelJobsForUser(userId); |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 691 | } else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) { |
| 692 | // Has this package scheduled any jobs, such that we will take action |
| 693 | // if it were to be force-stopped? |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 694 | if (pkgUid != -1) { |
| 695 | List<JobStatus> jobsForUid; |
| 696 | synchronized (mLock) { |
| 697 | jobsForUid = mJobs.getJobsByUid(pkgUid); |
| 698 | } |
| 699 | for (int i = jobsForUid.size() - 1; i >= 0; i--) { |
| 700 | if (jobsForUid.get(i).getSourcePackageName().equals(pkgName)) { |
| 701 | if (DEBUG) { |
| 702 | Slog.d(TAG, "Restart query: package " + pkgName + " at uid " |
| 703 | + pkgUid + " has jobs"); |
| 704 | } |
| 705 | setResultCode(Activity.RESULT_OK); |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | } else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) { |
| 711 | // possible force-stop |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 712 | if (pkgUid != -1) { |
| 713 | if (DEBUG) { |
| 714 | Slog.d(TAG, "Removing jobs for pkg " + pkgName + " at uid " + pkgUid); |
| 715 | } |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 716 | cancelJobsForPackageAndUid(pkgName, pkgUid, "app force stopped"); |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 717 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | }; |
| 721 | |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 722 | private String getPackageName(Intent intent) { |
| 723 | Uri uri = intent.getData(); |
| 724 | String pkg = uri != null ? uri.getSchemeSpecificPart() : null; |
| 725 | return pkg; |
| 726 | } |
| 727 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 728 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 729 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 730 | updateUidState(uid, procState); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 733 | @Override public void onUidGone(int uid, boolean disabled) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 734 | updateUidState(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 735 | if (disabled) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 736 | cancelJobsForUid(uid, "uid gone"); |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 737 | } |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 738 | synchronized (mLock) { |
| 739 | mDeviceIdleJobsController.setUidActiveLocked(uid, false); |
| 740 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | @Override public void onUidActive(int uid) throws RemoteException { |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 744 | synchronized (mLock) { |
| 745 | mDeviceIdleJobsController.setUidActiveLocked(uid, true); |
| 746 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 749 | @Override public void onUidIdle(int uid, boolean disabled) { |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 750 | if (disabled) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 751 | cancelJobsForUid(uid, "app uid idle"); |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 752 | } |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 753 | synchronized (mLock) { |
| 754 | mDeviceIdleJobsController.setUidActiveLocked(uid, false); |
| 755 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 756 | } |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 757 | |
| 758 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
| 759 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 760 | }; |
| 761 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 762 | public Object getLock() { |
| 763 | return mLock; |
| 764 | } |
| 765 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 766 | public JobStore getJobStore() { |
| 767 | return mJobs; |
| 768 | } |
| 769 | |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 770 | @Override |
| 771 | public void onStartUser(int userHandle) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 772 | mStartedUsers = ArrayUtils.appendInt(mStartedUsers, userHandle); |
| 773 | // Let's kick any outstanding jobs for this user. |
| 774 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 775 | } |
| 776 | |
| 777 | @Override |
| 778 | public void onUnlockUser(int userHandle) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 779 | // Let's kick any outstanding jobs for this user. |
| 780 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 781 | } |
| 782 | |
| 783 | @Override |
| 784 | public void onStopUser(int userHandle) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 785 | mStartedUsers = ArrayUtils.removeInt(mStartedUsers, userHandle); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 788 | /** |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame^] | 789 | * Return whether an UID is active or idle. |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 790 | */ |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame^] | 791 | private boolean isUidActive(int uid) { |
| 792 | return mAppStateTracker.isUidActiveSynced(uid); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 793 | } |
| 794 | |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame^] | 795 | private final Predicate<Integer> mIsUidActivePredicate = this::isUidActive; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 796 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 797 | public int scheduleAsPackage(JobInfo job, JobWorkItem work, int uId, String packageName, |
| 798 | int userId, String tag) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 799 | try { |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 800 | if (ActivityManager.getService().isAppStartModeDisabled(uId, |
| 801 | job.getService().getPackageName())) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 802 | Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString() |
| 803 | + " -- package not allowed to start"); |
| 804 | return JobScheduler.RESULT_FAILURE; |
| 805 | } |
| 806 | } catch (RemoteException e) { |
| 807 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 808 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 809 | synchronized (mLock) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 810 | final JobStatus toCancel = mJobs.getJobByUidAndJobId(uId, job.getId()); |
| 811 | |
| 812 | if (work != null && toCancel != null) { |
| 813 | // Fast path: we are adding work to an existing job, and the JobInfo is not |
| 814 | // changing. We can just directly enqueue this work in to the job. |
| 815 | if (toCancel.getJob().equals(job)) { |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 816 | |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 817 | toCancel.enqueueWorkLocked(ActivityManager.getService(), work); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 818 | |
| 819 | // If any of work item is enqueued when the source is in the foreground, |
| 820 | // exempt the entire job. |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame^] | 821 | toCancel.maybeAddForegroundExemption(mIsUidActivePredicate); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 822 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 823 | return JobScheduler.RESULT_SUCCESS; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 828 | |
| 829 | // Give exemption if the source is in the foreground just now. |
| 830 | // Note if it's a sync job, this method is called on the handler so it's not exactly |
| 831 | // the state when requestSync() was called, but that should be fine because of the |
| 832 | // 1 minute foreground grace period. |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame^] | 833 | jobStatus.maybeAddForegroundExemption(mIsUidActivePredicate); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 834 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 835 | if (DEBUG) Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString()); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 836 | // 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] | 837 | if (ENFORCE_MAX_JOBS && packageName == null) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 838 | if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) { |
| 839 | Slog.w(TAG, "Too many jobs for uid " + uId); |
| 840 | throw new IllegalStateException("Apps may not schedule more than " |
| 841 | + MAX_JOBS_PER_APP + " distinct jobs"); |
| 842 | } |
| 843 | } |
| 844 | |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 845 | // This may throw a SecurityException. |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 846 | jobStatus.prepareLocked(ActivityManager.getService()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 847 | |
Christopher Tate | b1c1f9a | 2016-03-17 13:29:25 -0700 | [diff] [blame] | 848 | if (toCancel != null) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 849 | cancelJobImplLocked(toCancel, jobStatus, "job rescheduled by app"); |
Christopher Tate | b1c1f9a | 2016-03-17 13:29:25 -0700 | [diff] [blame] | 850 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 851 | if (work != null) { |
| 852 | // If work has been supplied, enqueue it into the new job. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 853 | jobStatus.enqueueWorkLocked(ActivityManager.getService(), work); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 854 | } |
| 855 | startTrackingJobLocked(jobStatus, toCancel); |
Tej Singh | d5747a6 | 2018-01-08 20:57:35 -0800 | [diff] [blame] | 856 | StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_STATE_CHANGED, |
Bookatz | 9086762 | 2018-01-31 15:05:57 -0800 | [diff] [blame] | 857 | uId, null, jobStatus.getBatteryName(), |
| 858 | StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__SCHEDULED); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 859 | |
| 860 | // If the job is immediately ready to run, then we can just immediately |
| 861 | // put it in the pending list and try to schedule it. This is especially |
| 862 | // important for jobs with a 0 deadline constraint, since they will happen a fair |
| 863 | // amount, we want to handle them as quickly as possible, and semantically we want to |
| 864 | // make sure we have started holding the wake lock for the job before returning to |
| 865 | // the caller. |
| 866 | // If the job is not yet ready to run, there is nothing more to do -- we are |
| 867 | // now just waiting for one of its controllers to change state and schedule |
| 868 | // the job appropriately. |
| 869 | if (isReadyToBeExecutedLocked(jobStatus)) { |
| 870 | // This is a new job, we can just immediately put it on the pending |
| 871 | // list and try to run it. |
| 872 | mJobPackageTracker.notePending(jobStatus); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 873 | addOrderedItem(mPendingJobs, jobStatus, mEnqueueTimeComparator); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 874 | maybeRunPendingJobsLocked(); |
| 875 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 876 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 877 | return JobScheduler.RESULT_SUCCESS; |
| 878 | } |
| 879 | |
| 880 | public List<JobInfo> getPendingJobs(int uid) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 881 | synchronized (mLock) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 882 | List<JobStatus> jobs = mJobs.getJobsByUid(uid); |
| 883 | ArrayList<JobInfo> outList = new ArrayList<JobInfo>(jobs.size()); |
| 884 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 885 | JobStatus job = jobs.get(i); |
| 886 | outList.add(job.getJob()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 887 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 888 | return outList; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 889 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 892 | public JobInfo getPendingJob(int uid, int jobId) { |
| 893 | synchronized (mLock) { |
| 894 | List<JobStatus> jobs = mJobs.getJobsByUid(uid); |
| 895 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 896 | JobStatus job = jobs.get(i); |
| 897 | if (job.getJobId() == jobId) { |
| 898 | return job.getJob(); |
| 899 | } |
| 900 | } |
| 901 | return null; |
| 902 | } |
| 903 | } |
| 904 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 905 | void cancelJobsForUser(int userHandle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 906 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 907 | final List<JobStatus> jobsForUser = mJobs.getJobsByUser(userHandle); |
| 908 | for (int i=0; i<jobsForUser.size(); i++) { |
| 909 | JobStatus toRemove = jobsForUser.get(i); |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 910 | cancelJobImplLocked(toRemove, null, "user removed"); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 911 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
Michael Wachenschwanz | 3f2b655 | 2017-05-15 13:53:09 -0700 | [diff] [blame] | 915 | private void cancelJobsForNonExistentUsers() { |
| 916 | UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class); |
| 917 | synchronized (mLock) { |
| 918 | mJobs.removeJobsOfNonUsers(umi.getUserIds()); |
| 919 | } |
| 920 | } |
| 921 | |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 922 | void cancelJobsForPackageAndUid(String pkgName, int uid, String reason) { |
| 923 | if ("android".equals(pkgName)) { |
| 924 | Slog.wtfStack(TAG, "Can't cancel all jobs for system package"); |
| 925 | return; |
| 926 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 927 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 928 | final List<JobStatus> jobsForUid = mJobs.getJobsByUid(uid); |
| 929 | for (int i = jobsForUid.size() - 1; i >= 0; i--) { |
| 930 | final JobStatus job = jobsForUid.get(i); |
| 931 | if (job.getSourcePackageName().equals(pkgName)) { |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 932 | cancelJobImplLocked(job, null, reason); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 933 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 938 | /** |
| 939 | * Entry point from client to cancel all jobs originating from their uid. |
| 940 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 941 | * execution or being executed. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 942 | * @param uid Uid to check against for removal of a job. |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 943 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 944 | */ |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 945 | public boolean cancelJobsForUid(int uid, String reason) { |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 946 | if (uid == Process.SYSTEM_UID) { |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 947 | Slog.wtfStack(TAG, "Can't cancel all jobs for system uid"); |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 948 | return false; |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 949 | } |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 950 | |
| 951 | boolean jobsCanceled = false; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 952 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 953 | final List<JobStatus> jobsForUid = mJobs.getJobsByUid(uid); |
| 954 | for (int i=0; i<jobsForUid.size(); i++) { |
| 955 | JobStatus toRemove = jobsForUid.get(i); |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 956 | cancelJobImplLocked(toRemove, null, reason); |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 957 | jobsCanceled = true; |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 958 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 959 | } |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 960 | return jobsCanceled; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Entry point from client to cancel the job corresponding to the jobId provided. |
| 965 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 966 | * execution or being executed. |
| 967 | * @param uid Uid of the calling client. |
| 968 | * @param jobId Id of the job, provided at schedule-time. |
| 969 | */ |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 970 | public boolean cancelJob(int uid, int jobId, int callingUid) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 971 | JobStatus toCancel; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 972 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 973 | toCancel = mJobs.getJobByUidAndJobId(uid, jobId); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 974 | if (toCancel != null) { |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 975 | cancelJobImplLocked(toCancel, null, |
| 976 | "cancel() called by app, callingUid=" + callingUid |
| 977 | + " uid=" + uid + " jobId=" + jobId); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 978 | } |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 979 | return (toCancel != null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 983 | private void cancelJobImplLocked(JobStatus cancelled, JobStatus incomingJob, String reason) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 984 | if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString()); |
| 985 | cancelled.unprepareLocked(ActivityManager.getService()); |
| 986 | stopTrackingJobLocked(cancelled, incomingJob, true /* writeBack */); |
| 987 | // Remove from pending queue. |
| 988 | if (mPendingJobs.remove(cancelled)) { |
| 989 | mJobPackageTracker.noteNonpending(cancelled); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 990 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 991 | // Cancel if running. |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 992 | stopJobOnServiceContextLocked(cancelled, JobParameters.REASON_CANCELED, reason); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 993 | reportActiveLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 996 | void updateUidState(int uid, int procState) { |
| 997 | synchronized (mLock) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 998 | if (procState == ActivityManager.PROCESS_STATE_TOP) { |
| 999 | // Only use this if we are exactly the top app. All others can live |
| 1000 | // with just the foreground priority. This means that persistent processes |
| 1001 | // can never be the top app priority... that is fine. |
| 1002 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_TOP_APP); |
Dianne Hackborn | 10fc4fd | 2017-12-19 17:23:13 -0800 | [diff] [blame] | 1003 | } else if (procState <= ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1004 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_FOREGROUND_APP); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1005 | } else { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1006 | mUidPriorityOverride.delete(uid); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1011 | @Override |
| 1012 | public void onDeviceIdleStateChanged(boolean deviceIdle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1013 | synchronized (mLock) { |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1014 | if (deviceIdle) { |
Jeff Sharkey | 34618b5 | 2016-06-01 15:51:19 -0600 | [diff] [blame] | 1015 | // When becoming idle, make sure no jobs are actively running, |
| 1016 | // except those using the idle exemption flag. |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1017 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1018 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1019 | final JobStatus executing = jsc.getRunningJobLocked(); |
Jeff Sharkey | 34618b5 | 2016-06-01 15:51:19 -0600 | [diff] [blame] | 1020 | if (executing != null |
| 1021 | && (executing.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) == 0) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1022 | jsc.cancelExecutingJobLocked(JobParameters.REASON_DEVICE_IDLE, |
| 1023 | "cancelled due to doze"); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1024 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1025 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1026 | } else { |
| 1027 | // When coming out of idle, allow thing to start back up. |
| 1028 | if (mReadyToRock) { |
| 1029 | if (mLocalDeviceIdleController != null) { |
| 1030 | if (!mReportedActive) { |
| 1031 | mReportedActive = true; |
| 1032 | mLocalDeviceIdleController.setJobsActive(true); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1035 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1041 | void reportActiveLocked() { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1042 | // active is true if pending queue contains jobs OR some job is running. |
| 1043 | boolean active = mPendingJobs.size() > 0; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1044 | if (mPendingJobs.size() <= 0) { |
| 1045 | for (int i=0; i<mActiveServices.size(); i++) { |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 1046 | final JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1047 | final JobStatus job = jsc.getRunningJobLocked(); |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 1048 | if (job != null |
| 1049 | && (job.getJob().getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) == 0 |
| 1050 | && !job.dozeWhitelisted) { |
| 1051 | // We will report active if we have a job running and it is not an exception |
| 1052 | // due to being in the foreground or whitelisted. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1053 | active = true; |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1058 | |
| 1059 | if (mReportedActive != active) { |
| 1060 | mReportedActive = active; |
| 1061 | if (mLocalDeviceIdleController != null) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1062 | mLocalDeviceIdleController.setJobsActive(active); |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 1067 | void reportAppUsage(String packageName, int userId) { |
| 1068 | // This app just transitioned into interactive use or near equivalent, so we should |
| 1069 | // take a look at its job state for feedback purposes. |
| 1070 | } |
| 1071 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1072 | /** |
| 1073 | * Initializes the system service. |
| 1074 | * <p> |
| 1075 | * Subclasses must define a single argument constructor that accepts the context |
| 1076 | * and passes it to super. |
| 1077 | * </p> |
| 1078 | * |
| 1079 | * @param context The system server context. |
| 1080 | */ |
| 1081 | public JobSchedulerService(Context context) { |
| 1082 | super(context); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1083 | |
| 1084 | mLocalPM = LocalServices.getService(PackageManagerInternal.class); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1085 | mActivityManagerInternal = Preconditions.checkNotNull( |
| 1086 | LocalServices.getService(ActivityManagerInternal.class)); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1087 | |
Dianne Hackborn | 970e3f4 | 2016-06-01 10:55:13 -0700 | [diff] [blame] | 1088 | mHandler = new JobHandler(context.getMainLooper()); |
| 1089 | mConstants = new Constants(mHandler); |
| 1090 | mJobSchedulerStub = new JobSchedulerStub(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1091 | |
| 1092 | // Set up the app standby bucketing tracker |
| 1093 | UsageStatsManagerInternal usageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
| 1094 | mStandbyTracker = new StandbyTracker(usageStats); |
| 1095 | usageStats.addAppIdleStateChangeListener(mStandbyTracker); |
| 1096 | |
| 1097 | // The job store needs to call back |
| 1098 | publishLocalService(JobSchedulerInternal.class, new LocalService()); |
| 1099 | |
| 1100 | // Initialize the job store and set up any persisted jobs |
Dianne Hackborn | 970e3f4 | 2016-06-01 10:55:13 -0700 | [diff] [blame] | 1101 | mJobs = JobStore.initAndGet(this); |
| 1102 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1103 | // Create the controllers. |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1104 | mControllers = new ArrayList<StateController>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1105 | mControllers.add(ConnectivityController.get(this)); |
| 1106 | mControllers.add(TimeController.get(this)); |
| 1107 | mControllers.add(IdleController.get(this)); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 1108 | mBatteryController = BatteryController.get(this); |
| 1109 | mControllers.add(mBatteryController); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 1110 | mStorageController = StorageController.get(this); |
| 1111 | mControllers.add(mStorageController); |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 1112 | mControllers.add(BackgroundJobsController.get(this)); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 1113 | mControllers.add(AppIdleController.get(this)); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1114 | mControllers.add(ContentObserverController.get(this)); |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 1115 | mDeviceIdleJobsController = DeviceIdleJobsController.get(this); |
| 1116 | mControllers.add(mDeviceIdleJobsController); |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 1117 | |
| 1118 | // If the job store determined that it can't yet reschedule persisted jobs, |
| 1119 | // we need to start watching the clock. |
| 1120 | if (!mJobs.jobTimesInflatedValid()) { |
| 1121 | Slog.w(TAG, "!!! RTC not yet good; tracking time updates for job scheduling"); |
| 1122 | context.registerReceiver(mTimeSetReceiver, new IntentFilter(Intent.ACTION_TIME_CHANGED)); |
| 1123 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 1126 | private final BroadcastReceiver mTimeSetReceiver = new BroadcastReceiver() { |
| 1127 | @Override |
| 1128 | public void onReceive(Context context, Intent intent) { |
| 1129 | if (Intent.ACTION_TIME_CHANGED.equals(intent.getAction())) { |
| 1130 | // When we reach clock sanity, recalculate the temporal windows |
| 1131 | // of all affected jobs. |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1132 | if (mJobs.clockNowValidToInflate(sSystemClock.millis())) { |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 1133 | Slog.i(TAG, "RTC now valid; recalculating persisted job windows"); |
| 1134 | |
| 1135 | // We've done our job now, so stop watching the time. |
| 1136 | context.unregisterReceiver(this); |
| 1137 | |
| 1138 | // And kick off the work to update the affected jobs, using a secondary |
| 1139 | // thread instead of chugging away here on the main looper thread. |
| 1140 | FgThread.getHandler().post(mJobTimeUpdater); |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | }; |
| 1145 | |
| 1146 | private final Runnable mJobTimeUpdater = () -> { |
| 1147 | final ArrayList<JobStatus> toRemove = new ArrayList<>(); |
| 1148 | final ArrayList<JobStatus> toAdd = new ArrayList<>(); |
| 1149 | synchronized (mLock) { |
| 1150 | // Note: we intentionally both look up the existing affected jobs and replace them |
| 1151 | // with recalculated ones inside the same lock lifetime. |
| 1152 | getJobStore().getRtcCorrectedJobsLocked(toAdd, toRemove); |
| 1153 | |
| 1154 | // Now, at each position [i], we have both the existing JobStatus |
| 1155 | // and the one that replaces it. |
| 1156 | final int N = toAdd.size(); |
| 1157 | for (int i = 0; i < N; i++) { |
| 1158 | final JobStatus oldJob = toRemove.get(i); |
| 1159 | final JobStatus newJob = toAdd.get(i); |
| 1160 | if (DEBUG) { |
| 1161 | Slog.v(TAG, " replacing " + oldJob + " with " + newJob); |
| 1162 | } |
| 1163 | cancelJobImplLocked(oldJob, newJob, "deferred rtc calculation"); |
| 1164 | } |
| 1165 | } |
| 1166 | }; |
| 1167 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1168 | @Override |
| 1169 | public void onStart() { |
| 1170 | publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub); |
| 1171 | } |
| 1172 | |
| 1173 | @Override |
| 1174 | public void onBootPhase(int phase) { |
| 1175 | if (PHASE_SYSTEM_SERVICES_READY == phase) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1176 | mConstants.start(getContext().getContentResolver()); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1177 | |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame^] | 1178 | mAppStateTracker = Preconditions.checkNotNull( |
| 1179 | LocalServices.getService(AppStateTracker.class)); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1180 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1181 | // Register br for package removals and user removals. |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 1182 | final IntentFilter filter = new IntentFilter(); |
| 1183 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1184 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 1185 | filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1186 | filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1187 | filter.addDataScheme("package"); |
| 1188 | getContext().registerReceiverAsUser( |
| 1189 | mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
| 1190 | final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED); |
| 1191 | getContext().registerReceiverAsUser( |
| 1192 | mBroadcastReceiver, UserHandle.ALL, userFilter, null, null); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1193 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1194 | ActivityManager.getService().registerUidObserver(mUidObserver, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1195 | ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1196 | | ActivityManager.UID_OBSERVER_IDLE | ActivityManager.UID_OBSERVER_ACTIVE, |
| 1197 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1198 | } catch (RemoteException e) { |
| 1199 | // ignored; both services live in system_server |
| 1200 | } |
Michael Wachenschwanz | 3f2b655 | 2017-05-15 13:53:09 -0700 | [diff] [blame] | 1201 | // Remove any jobs that are not associated with any of the current users. |
| 1202 | cancelJobsForNonExistentUsers(); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1203 | } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1204 | synchronized (mLock) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1205 | // Let's go! |
| 1206 | mReadyToRock = true; |
| 1207 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 1208 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1209 | mLocalDeviceIdleController |
| 1210 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1211 | // Create the "runners". |
| 1212 | for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) { |
| 1213 | mActiveServices.add( |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1214 | new JobServiceContext(this, mBatteryStats, mJobPackageTracker, |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1215 | getContext().getMainLooper())); |
| 1216 | } |
| 1217 | // Attach jobs to their controllers. |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1218 | mJobs.forEachJob(new JobStatusFunctor() { |
| 1219 | @Override |
| 1220 | public void process(JobStatus job) { |
| 1221 | for (int controller = 0; controller < mControllers.size(); controller++) { |
| 1222 | final StateController sc = mControllers.get(controller); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1223 | sc.maybeStartTrackingJobLocked(job, null); |
| 1224 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1225 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1226 | }); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1227 | // GO GO GO! |
| 1228 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 1229 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * Called when we have a job status object that we need to insert in our |
| 1235 | * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know |
| 1236 | * about. |
| 1237 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1238 | private void startTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) { |
| 1239 | if (!jobStatus.isPreparedLocked()) { |
| 1240 | Slog.wtf(TAG, "Not yet prepared when started tracking: " + jobStatus); |
| 1241 | } |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1242 | jobStatus.enqueueTime = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1243 | final boolean update = mJobs.add(jobStatus); |
| 1244 | if (mReadyToRock) { |
| 1245 | for (int i = 0; i < mControllers.size(); i++) { |
| 1246 | StateController controller = mControllers.get(i); |
| 1247 | if (update) { |
| 1248 | controller.maybeStopTrackingJobLocked(jobStatus, null, true); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1249 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1250 | controller.maybeStartTrackingJobLocked(jobStatus, lastJob); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1251 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Called when we want to remove a JobStatus object that we've finished executing. Returns the |
| 1257 | * object removed. |
| 1258 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1259 | private boolean stopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 1260 | boolean writeBack) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1261 | // Deal with any remaining work items in the old job. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 1262 | jobStatus.stopTrackingJobLocked(ActivityManager.getService(), incomingJob); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1263 | |
| 1264 | // Remove from store as well as controllers. |
| 1265 | final boolean removed = mJobs.remove(jobStatus, writeBack); |
| 1266 | if (removed && mReadyToRock) { |
| 1267 | for (int i=0; i<mControllers.size(); i++) { |
| 1268 | StateController controller = mControllers.get(i); |
| 1269 | controller.maybeStopTrackingJobLocked(jobStatus, incomingJob, false); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1270 | } |
| 1271 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1272 | return removed; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1275 | private boolean stopJobOnServiceContextLocked(JobStatus job, int reason, String debugReason) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1276 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1277 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1278 | final JobStatus executing = jsc.getRunningJobLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1279 | if (executing != null && executing.matches(job.getUid(), job.getJobId())) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1280 | jsc.cancelExecutingJobLocked(reason, debugReason); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1281 | return true; |
| 1282 | } |
| 1283 | } |
| 1284 | return false; |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * @param job JobStatus we are querying against. |
| 1289 | * @return Whether or not the job represented by the status object is currently being run or |
| 1290 | * is pending. |
| 1291 | */ |
| 1292 | private boolean isCurrentlyActiveLocked(JobStatus job) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1293 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1294 | JobServiceContext serviceContext = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1295 | final JobStatus running = serviceContext.getRunningJobLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1296 | if (running != null && running.matches(job.getUid(), job.getJobId())) { |
| 1297 | return true; |
| 1298 | } |
| 1299 | } |
| 1300 | return false; |
| 1301 | } |
| 1302 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1303 | void noteJobsPending(List<JobStatus> jobs) { |
| 1304 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 1305 | JobStatus job = jobs.get(i); |
| 1306 | mJobPackageTracker.notePending(job); |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | void noteJobsNonpending(List<JobStatus> jobs) { |
| 1311 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 1312 | JobStatus job = jobs.get(i); |
| 1313 | mJobPackageTracker.noteNonpending(job); |
| 1314 | } |
| 1315 | } |
| 1316 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1317 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1318 | * Reschedules the given job based on the job's backoff policy. It doesn't make sense to |
| 1319 | * specify an override deadline on a failed job (the failed job will run even though it's not |
| 1320 | * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any |
| 1321 | * ready job with {@link JobStatus#numFailures} > 0 will be executed. |
| 1322 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1323 | * @param failureToReschedule Provided job status that we will reschedule. |
| 1324 | * @return A newly instantiated JobStatus with the same constraints as the last job except |
| 1325 | * with adjusted timing constraints. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1326 | * |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1327 | * @see #maybeQueueReadyJobsForExecutionLocked |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1328 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1329 | private JobStatus getRescheduleJobForFailureLocked(JobStatus failureToReschedule) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1330 | final long elapsedNowMillis = sElapsedRealtimeClock.millis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1331 | final JobInfo job = failureToReschedule.getJob(); |
| 1332 | |
| 1333 | final long initialBackoffMillis = job.getInitialBackoffMillis(); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1334 | final int backoffAttempts = failureToReschedule.getNumFailures() + 1; |
| 1335 | long delayMillis; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1336 | |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1337 | if (failureToReschedule.hasWorkLocked()) { |
| 1338 | if (backoffAttempts > mConstants.MAX_WORK_RESCHEDULE_COUNT) { |
| 1339 | Slog.w(TAG, "Not rescheduling " + failureToReschedule + ": attempt #" |
| 1340 | + backoffAttempts + " > work limit " |
| 1341 | + mConstants.MAX_STANDARD_RESCHEDULE_COUNT); |
| 1342 | return null; |
| 1343 | } |
| 1344 | } else if (backoffAttempts > mConstants.MAX_STANDARD_RESCHEDULE_COUNT) { |
| 1345 | Slog.w(TAG, "Not rescheduling " + failureToReschedule + ": attempt #" |
| 1346 | + backoffAttempts + " > std limit " + mConstants.MAX_STANDARD_RESCHEDULE_COUNT); |
| 1347 | return null; |
| 1348 | } |
| 1349 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1350 | switch (job.getBackoffPolicy()) { |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1351 | case JobInfo.BACKOFF_POLICY_LINEAR: { |
| 1352 | long backoff = initialBackoffMillis; |
| 1353 | if (backoff < mConstants.MIN_LINEAR_BACKOFF_TIME) { |
| 1354 | backoff = mConstants.MIN_LINEAR_BACKOFF_TIME; |
| 1355 | } |
| 1356 | delayMillis = backoff * backoffAttempts; |
| 1357 | } break; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1358 | default: |
| 1359 | if (DEBUG) { |
| 1360 | Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential."); |
| 1361 | } |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1362 | case JobInfo.BACKOFF_POLICY_EXPONENTIAL: { |
| 1363 | long backoff = initialBackoffMillis; |
| 1364 | if (backoff < mConstants.MIN_EXP_BACKOFF_TIME) { |
| 1365 | backoff = mConstants.MIN_EXP_BACKOFF_TIME; |
| 1366 | } |
| 1367 | delayMillis = (long) Math.scalb(backoff, backoffAttempts - 1); |
| 1368 | } break; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1369 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1370 | delayMillis = |
| 1371 | Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1372 | JobStatus newJob = new JobStatus(failureToReschedule, getCurrentHeartbeat(), |
| 1373 | elapsedNowMillis + delayMillis, |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 1374 | JobStatus.NO_LATEST_RUNTIME, backoffAttempts, |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1375 | failureToReschedule.getLastSuccessfulRunTime(), sSystemClock.millis()); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1376 | for (int ic=0; ic<mControllers.size(); ic++) { |
| 1377 | StateController controller = mControllers.get(ic); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1378 | controller.rescheduleForFailureLocked(newJob, failureToReschedule); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1379 | } |
| 1380 | return newJob; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1384 | * Called after a periodic has executed so we can reschedule it. We take the last execution |
| 1385 | * time of the job to be the time of completion (i.e. the time at which this function is |
| 1386 | * called). |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1387 | * <p>This could be inaccurate b/c the job can run for as long as |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1388 | * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead |
| 1389 | * to underscheduling at least, rather than if we had taken the last execution time to be the |
| 1390 | * start of the execution. |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1391 | * <p>Unlike a reschedule prior to execution, in this case we advance the next-heartbeat |
| 1392 | * tracking as though the job were newly-scheduled. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1393 | * @return A new job representing the execution criteria for this instantiation of the |
| 1394 | * recurring job. |
| 1395 | */ |
| 1396 | private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1397 | final long elapsedNow = sElapsedRealtimeClock.millis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1398 | // Compute how much of the period is remaining. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1399 | long runEarly = 0L; |
| 1400 | |
| 1401 | // If this periodic was rescheduled it won't have a deadline. |
| 1402 | if (periodicToReschedule.hasDeadlineConstraint()) { |
| 1403 | runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L); |
| 1404 | } |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 1405 | long flex = periodicToReschedule.getJob().getFlexMillis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1406 | long period = periodicToReschedule.getJob().getIntervalMillis(); |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 1407 | long newLatestRuntimeElapsed = elapsedNow + runEarly + period; |
| 1408 | long newEarliestRunTimeElapsed = newLatestRuntimeElapsed - flex; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1409 | |
| 1410 | if (DEBUG) { |
| 1411 | Slog.v(TAG, "Rescheduling executed periodic. New execution window [" + |
| 1412 | newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s"); |
| 1413 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1414 | return new JobStatus(periodicToReschedule, getCurrentHeartbeat(), |
| 1415 | newEarliestRunTimeElapsed, newLatestRuntimeElapsed, |
| 1416 | 0 /* backoffAttempt */, |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1417 | sSystemClock.millis() /* lastSuccessfulRunTime */, |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 1418 | periodicToReschedule.getLastFailedRunTime()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | // JobCompletedListener implementations. |
| 1422 | |
| 1423 | /** |
| 1424 | * A job just finished executing. We fetch the |
| 1425 | * {@link com.android.server.job.controllers.JobStatus} from the store and depending on |
| 1426 | * whether we want to reschedule we readd it to the controllers. |
| 1427 | * @param jobStatus Completed job. |
| 1428 | * @param needsReschedule Whether the implementing class should reschedule this job. |
| 1429 | */ |
| 1430 | @Override |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1431 | public void onJobCompletedLocked(JobStatus jobStatus, boolean needsReschedule) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1432 | if (DEBUG) { |
| 1433 | Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule); |
| 1434 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1435 | |
| 1436 | // If the job wants to be rescheduled, we first need to make the next upcoming |
| 1437 | // job so we can transfer any appropriate state over from the previous job when |
| 1438 | // we stop it. |
| 1439 | final JobStatus rescheduledJob = needsReschedule |
| 1440 | ? getRescheduleJobForFailureLocked(jobStatus) : null; |
| 1441 | |
Shreyas Basarge | 73f1025 | 2016-02-11 17:06:13 +0000 | [diff] [blame] | 1442 | // Do not write back immediately if this is a periodic job. The job may get lost if system |
| 1443 | // shuts down before it is added back. |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1444 | if (!stopTrackingJobLocked(jobStatus, rescheduledJob, !jobStatus.getJob().isPeriodic())) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1445 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 1446 | 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] | 1447 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1448 | // We still want to check for jobs to execute, because this job may have |
| 1449 | // scheduled a new job under the same job id, and now we can run it. |
| 1450 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1451 | return; |
| 1452 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1453 | |
| 1454 | if (rescheduledJob != null) { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1455 | try { |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1456 | rescheduledJob.prepareLocked(ActivityManager.getService()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1457 | } catch (SecurityException e) { |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1458 | Slog.w(TAG, "Unable to regrant job permissions for " + rescheduledJob); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1459 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1460 | startTrackingJobLocked(rescheduledJob, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1461 | } else if (jobStatus.getJob().isPeriodic()) { |
| 1462 | JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1463 | try { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1464 | rescheduledPeriodic.prepareLocked(ActivityManager.getService()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1465 | } catch (SecurityException e) { |
| 1466 | Slog.w(TAG, "Unable to regrant job permissions for " + rescheduledPeriodic); |
| 1467 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1468 | startTrackingJobLocked(rescheduledPeriodic, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1469 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1470 | jobStatus.unprepareLocked(ActivityManager.getService()); |
| 1471 | reportActiveLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1472 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | // StateChangedListener implementations. |
| 1476 | |
| 1477 | /** |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1478 | * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that |
| 1479 | * some controller's state has changed, so as to run through the list of jobs and start/stop |
| 1480 | * any that are eligible. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1481 | */ |
| 1482 | @Override |
| 1483 | public void onControllerStateChanged() { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1484 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | @Override |
| 1488 | public void onRunJobNow(JobStatus jobStatus) { |
| 1489 | mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget(); |
| 1490 | } |
| 1491 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1492 | final private class JobHandler extends Handler { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1493 | |
| 1494 | public JobHandler(Looper looper) { |
| 1495 | super(looper); |
| 1496 | } |
| 1497 | |
| 1498 | @Override |
| 1499 | public void handleMessage(Message message) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1500 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1501 | if (!mReadyToRock) { |
| 1502 | return; |
| 1503 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1504 | switch (message.what) { |
| 1505 | case MSG_JOB_EXPIRED: { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1506 | JobStatus runNow = (JobStatus) message.obj; |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 1507 | // runNow can be null, which is a controller's way of indicating that its |
| 1508 | // state is such that all ready jobs should be run immediately. |
Christopher Tate | b1d6448 | 2017-03-15 12:01:22 -0700 | [diff] [blame] | 1509 | if (runNow != null && isReadyToBeExecutedLocked(runNow)) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1510 | mJobPackageTracker.notePending(runNow); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1511 | addOrderedItem(mPendingJobs, runNow, mEnqueueTimeComparator); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1512 | } else { |
| 1513 | queueReadyJobsForExecutionLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1514 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1515 | } break; |
| 1516 | case MSG_CHECK_JOB: |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1517 | if (mReportedActive) { |
| 1518 | // if jobs are currently being run, queue all ready jobs for execution. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1519 | queueReadyJobsForExecutionLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1520 | } else { |
| 1521 | // Check the list of jobs and run some of them if we feel inclined. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1522 | maybeQueueReadyJobsForExecutionLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1523 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1524 | break; |
| 1525 | case MSG_CHECK_JOB_GREEDY: |
| 1526 | queueReadyJobsForExecutionLocked(); |
| 1527 | break; |
| 1528 | case MSG_STOP_JOB: |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1529 | cancelJobImplLocked((JobStatus) message.obj, null, |
| 1530 | "app no longer allowed to run"); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1531 | break; |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1532 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1533 | maybeRunPendingJobsLocked(); |
| 1534 | // Don't remove JOB_EXPIRED in case one came along while processing the queue. |
| 1535 | removeMessages(MSG_CHECK_JOB); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1536 | } |
| 1537 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1538 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1539 | |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 1540 | private void stopNonReadyActiveJobsLocked() { |
| 1541 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1542 | JobServiceContext serviceContext = mActiveServices.get(i); |
| 1543 | final JobStatus running = serviceContext.getRunningJobLocked(); |
| 1544 | if (running != null && !running.isReady()) { |
| 1545 | serviceContext.cancelExecutingJobLocked( |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1546 | JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, |
| 1547 | "cancelled due to unsatisfied constraints"); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1552 | /** |
| 1553 | * Run through list of jobs and execute all possible - at least one is expired so we do |
| 1554 | * as many as we can. |
| 1555 | */ |
| 1556 | private void queueReadyJobsForExecutionLocked() { |
| 1557 | if (DEBUG) { |
| 1558 | Slog.d(TAG, "queuing all ready jobs for execution:"); |
| 1559 | } |
| 1560 | noteJobsNonpending(mPendingJobs); |
| 1561 | mPendingJobs.clear(); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 1562 | stopNonReadyActiveJobsLocked(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1563 | boolean updated = updateStandbyHeartbeatLocked(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1564 | mJobs.forEachJob(mReadyQueueFunctor); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1565 | if (updated) updateNextStandbyHeartbeatsLocked(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1566 | mReadyQueueFunctor.postProcess(); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1567 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1568 | if (DEBUG) { |
| 1569 | final int queuedJobs = mPendingJobs.size(); |
| 1570 | if (queuedJobs == 0) { |
| 1571 | Slog.d(TAG, "No jobs pending."); |
| 1572 | } else { |
| 1573 | Slog.d(TAG, queuedJobs + " jobs queued."); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1574 | } |
| 1575 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1576 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1577 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1578 | final class ReadyJobQueueFunctor implements JobStatusFunctor { |
| 1579 | ArrayList<JobStatus> newReadyJobs; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1580 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1581 | @Override |
| 1582 | public void process(JobStatus job) { |
| 1583 | if (isReadyToBeExecutedLocked(job)) { |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1584 | if (DEBUG) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1585 | Slog.d(TAG, " queued " + job.toShortString()); |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1586 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1587 | if (newReadyJobs == null) { |
| 1588 | newReadyJobs = new ArrayList<JobStatus>(); |
| 1589 | } |
| 1590 | newReadyJobs.add(job); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1591 | } |
| 1592 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1593 | |
| 1594 | public void postProcess() { |
| 1595 | if (newReadyJobs != null) { |
| 1596 | noteJobsPending(newReadyJobs); |
| 1597 | mPendingJobs.addAll(newReadyJobs); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1598 | if (mPendingJobs.size() > 1) { |
| 1599 | mPendingJobs.sort(mEnqueueTimeComparator); |
| 1600 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1601 | } |
| 1602 | newReadyJobs = null; |
| 1603 | } |
| 1604 | } |
| 1605 | private final ReadyJobQueueFunctor mReadyQueueFunctor = new ReadyJobQueueFunctor(); |
| 1606 | |
| 1607 | /** |
| 1608 | * The state of at least one job has changed. Here is where we could enforce various |
| 1609 | * policies on when we want to execute jobs. |
| 1610 | * Right now the policy is such: |
| 1611 | * If >1 of the ready jobs is idle mode we send all of them off |
| 1612 | * if more than 2 network connectivity jobs are ready we send them all off. |
| 1613 | * If more than 4 jobs total are ready we send them all off. |
| 1614 | * TODO: It would be nice to consolidate these sort of high-level policies somewhere. |
| 1615 | */ |
| 1616 | final class MaybeReadyJobQueueFunctor implements JobStatusFunctor { |
| 1617 | int chargingCount; |
| 1618 | int batteryNotLowCount; |
| 1619 | int storageNotLowCount; |
| 1620 | int idleCount; |
| 1621 | int backoffCount; |
| 1622 | int connectivityCount; |
| 1623 | int contentCount; |
| 1624 | List<JobStatus> runnableJobs; |
| 1625 | |
| 1626 | public MaybeReadyJobQueueFunctor() { |
| 1627 | reset(); |
| 1628 | } |
| 1629 | |
| 1630 | // Functor method invoked for each job via JobStore.forEachJob() |
| 1631 | @Override |
| 1632 | public void process(JobStatus job) { |
| 1633 | if (isReadyToBeExecutedLocked(job)) { |
| 1634 | try { |
| 1635 | if (ActivityManager.getService().isAppStartModeDisabled(job.getUid(), |
| 1636 | job.getJob().getService().getPackageName())) { |
| 1637 | Slog.w(TAG, "Aborting job " + job.getUid() + ":" |
| 1638 | + job.getJob().toString() + " -- package not allowed to start"); |
| 1639 | mHandler.obtainMessage(MSG_STOP_JOB, job).sendToTarget(); |
| 1640 | return; |
| 1641 | } |
| 1642 | } catch (RemoteException e) { |
| 1643 | } |
| 1644 | if (job.getNumFailures() > 0) { |
| 1645 | backoffCount++; |
| 1646 | } |
| 1647 | if (job.hasIdleConstraint()) { |
| 1648 | idleCount++; |
| 1649 | } |
| 1650 | if (job.hasConnectivityConstraint()) { |
| 1651 | connectivityCount++; |
| 1652 | } |
| 1653 | if (job.hasChargingConstraint()) { |
| 1654 | chargingCount++; |
| 1655 | } |
| 1656 | if (job.hasBatteryNotLowConstraint()) { |
| 1657 | batteryNotLowCount++; |
| 1658 | } |
| 1659 | if (job.hasStorageNotLowConstraint()) { |
| 1660 | storageNotLowCount++; |
| 1661 | } |
| 1662 | if (job.hasContentTriggerConstraint()) { |
| 1663 | contentCount++; |
| 1664 | } |
| 1665 | if (runnableJobs == null) { |
| 1666 | runnableJobs = new ArrayList<>(); |
| 1667 | } |
| 1668 | runnableJobs.add(job); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | public void postProcess() { |
| 1673 | if (backoffCount > 0 || |
| 1674 | idleCount >= mConstants.MIN_IDLE_COUNT || |
| 1675 | connectivityCount >= mConstants.MIN_CONNECTIVITY_COUNT || |
| 1676 | chargingCount >= mConstants.MIN_CHARGING_COUNT || |
| 1677 | batteryNotLowCount >= mConstants.MIN_BATTERY_NOT_LOW_COUNT || |
| 1678 | storageNotLowCount >= mConstants.MIN_STORAGE_NOT_LOW_COUNT || |
| 1679 | contentCount >= mConstants.MIN_CONTENT_COUNT || |
| 1680 | (runnableJobs != null |
| 1681 | && runnableJobs.size() >= mConstants.MIN_READY_JOBS_COUNT)) { |
| 1682 | if (DEBUG) { |
| 1683 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLocked: Running jobs."); |
| 1684 | } |
| 1685 | noteJobsPending(runnableJobs); |
| 1686 | mPendingJobs.addAll(runnableJobs); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1687 | if (mPendingJobs.size() > 1) { |
| 1688 | mPendingJobs.sort(mEnqueueTimeComparator); |
| 1689 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1690 | } else { |
| 1691 | if (DEBUG) { |
| 1692 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLocked: Not running anything."); |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | // Be ready for next time |
| 1697 | reset(); |
| 1698 | } |
| 1699 | |
| 1700 | private void reset() { |
| 1701 | chargingCount = 0; |
| 1702 | idleCount = 0; |
| 1703 | backoffCount = 0; |
| 1704 | connectivityCount = 0; |
| 1705 | batteryNotLowCount = 0; |
| 1706 | storageNotLowCount = 0; |
| 1707 | contentCount = 0; |
| 1708 | runnableJobs = null; |
| 1709 | } |
| 1710 | } |
| 1711 | private final MaybeReadyJobQueueFunctor mMaybeQueueFunctor = new MaybeReadyJobQueueFunctor(); |
| 1712 | |
| 1713 | private void maybeQueueReadyJobsForExecutionLocked() { |
| 1714 | if (DEBUG) Slog.d(TAG, "Maybe queuing ready jobs..."); |
| 1715 | |
| 1716 | noteJobsNonpending(mPendingJobs); |
| 1717 | mPendingJobs.clear(); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 1718 | stopNonReadyActiveJobsLocked(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1719 | boolean updated = updateStandbyHeartbeatLocked(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1720 | mJobs.forEachJob(mMaybeQueueFunctor); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1721 | if (updated) updateNextStandbyHeartbeatsLocked(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1722 | mMaybeQueueFunctor.postProcess(); |
| 1723 | } |
| 1724 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1725 | private boolean updateStandbyHeartbeatLocked() { |
| 1726 | final long sinceLast = sElapsedRealtimeClock.millis() - mLastHeartbeatTime; |
| 1727 | final long beatsElapsed = sinceLast / mConstants.STANDBY_HEARTBEAT_TIME; |
| 1728 | if (beatsElapsed > 0) { |
| 1729 | mHeartbeat += beatsElapsed; |
| 1730 | mLastHeartbeatTime += beatsElapsed * mConstants.STANDBY_HEARTBEAT_TIME; |
| 1731 | if (DEBUG_STANDBY) { |
| 1732 | Slog.v(TAG, "Advancing standby heartbeat by " + beatsElapsed + " to " + mHeartbeat); |
| 1733 | } |
| 1734 | return true; |
| 1735 | } |
| 1736 | return false; |
| 1737 | } |
| 1738 | |
| 1739 | private void updateNextStandbyHeartbeatsLocked() { |
| 1740 | // don't update ACTIVE or NEVER bucket milestones |
| 1741 | for (int i = 1; i < mNextBucketHeartbeat.length - 1; i++) { |
| 1742 | while (mHeartbeat >= mNextBucketHeartbeat[i]) { |
| 1743 | mNextBucketHeartbeat[i] += mConstants.STANDBY_BEATS[i]; |
| 1744 | } |
| 1745 | if (DEBUG_STANDBY) { |
| 1746 | Slog.v(TAG, " Bucket " + i + " next heartbeat " + mNextBucketHeartbeat[i]); |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1751 | /** |
| 1752 | * Criteria for moving a job into the pending queue: |
| 1753 | * - It's ready. |
| 1754 | * - It's not pending. |
| 1755 | * - It's not already running on a JSC. |
| 1756 | * - The user that requested the job is running. |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1757 | * - The job's standby bucket has come due to be runnable. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1758 | * - The component is enabled and runnable. |
| 1759 | */ |
| 1760 | private boolean isReadyToBeExecutedLocked(JobStatus job) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1761 | final boolean jobReady = job.isReady(); |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 1762 | |
| 1763 | if (DEBUG) { |
| 1764 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1765 | + " ready=" + jobReady); |
| 1766 | } |
| 1767 | |
| 1768 | // This is a condition that is very likely to be false (most jobs that are |
| 1769 | // scheduled are sitting there, not ready yet) and very cheap to check (just |
| 1770 | // a few conditions on data in JobStatus). |
| 1771 | if (!jobReady) { |
| 1772 | return false; |
| 1773 | } |
| 1774 | |
| 1775 | final boolean jobExists = mJobs.containsJob(job); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1776 | |
| 1777 | final int userId = job.getUserId(); |
| 1778 | final boolean userStarted = ArrayUtils.contains(mStartedUsers, userId); |
| 1779 | |
| 1780 | if (DEBUG) { |
| 1781 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 1782 | + " exists=" + jobExists + " userStarted=" + userStarted); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 1785 | // These are also fairly cheap to check, though they typically will not |
| 1786 | // be conditions we fail. |
| 1787 | if (!jobExists || !userStarted) { |
| 1788 | return false; |
| 1789 | } |
| 1790 | |
| 1791 | final boolean jobPending = mPendingJobs.contains(job); |
| 1792 | final boolean jobActive = isCurrentlyActiveLocked(job); |
| 1793 | |
| 1794 | if (DEBUG) { |
| 1795 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1796 | + " pending=" + jobPending + " active=" + jobActive); |
| 1797 | } |
| 1798 | |
| 1799 | // These can be a little more expensive (especially jobActive, since we need to |
| 1800 | // go through the array of all potentially active jobs), so we are doing them |
| 1801 | // later... but still before checking with the package manager! |
| 1802 | if (jobPending || jobActive) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1803 | return false; |
| 1804 | } |
| 1805 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1806 | // If the app is in a non-active standby bucket, make sure we've waited |
Christopher Tate | a5a85bd | 2018-01-03 17:20:36 -0800 | [diff] [blame] | 1807 | // an appropriate amount of time since the last invocation. During device- |
| 1808 | // wide parole, standby bucketing is ignored. |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 1809 | // |
| 1810 | // But if a job has FLAG_EXEMPT_FROM_APP_STANDBY, don't check it. |
| 1811 | if (!mInParole && !job.getJob().isExemptedFromAppStandby()) { |
Christopher Tate | a5a85bd | 2018-01-03 17:20:36 -0800 | [diff] [blame] | 1812 | final int bucket = job.getStandbyBucket(); |
| 1813 | if (mHeartbeat < mNextBucketHeartbeat[bucket]) { |
| 1814 | // Only skip this job if it's still waiting for the end of its (initial) nominal |
| 1815 | // bucket interval. Once it's waited that long, we let it go ahead and clear. |
| 1816 | // The final (NEVER) bucket is special; we never age those apps' jobs into |
| 1817 | // runnability. |
| 1818 | if (bucket >= mConstants.STANDBY_BEATS.length |
| 1819 | || (mHeartbeat < job.getBaseHeartbeat() + mConstants.STANDBY_BEATS[bucket])) { |
| 1820 | // TODO: log/trace that we're deferring the job due to bucketing if we hit this |
| 1821 | if (job.getWhenStandbyDeferred() == 0) { |
| 1822 | if (DEBUG_STANDBY) { |
| 1823 | Slog.v(TAG, "Bucket deferral: " + mHeartbeat + " < " |
| 1824 | + mNextBucketHeartbeat[job.getStandbyBucket()] + " for " + job); |
| 1825 | } |
| 1826 | job.setWhenStandbyDeferred(sElapsedRealtimeClock.millis()); |
Christopher Tate | 0c4d768 | 2017-12-06 15:10:22 -0800 | [diff] [blame] | 1827 | } |
Christopher Tate | a5a85bd | 2018-01-03 17:20:36 -0800 | [diff] [blame] | 1828 | return false; |
| 1829 | } else { |
| 1830 | if (DEBUG_STANDBY) { |
| 1831 | Slog.v(TAG, "Bucket deferred job aged into runnability at " |
| 1832 | + mHeartbeat + " : " + job); |
| 1833 | } |
Christopher Tate | 0c4d768 | 2017-12-06 15:10:22 -0800 | [diff] [blame] | 1834 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1835 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | // The expensive check last: validate that the defined package+service is |
| 1839 | // still present & viable. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1840 | final boolean componentPresent; |
| 1841 | try { |
| 1842 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 1843 | job.getServiceComponent(), PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 1844 | userId) != null); |
| 1845 | } catch (RemoteException e) { |
| 1846 | throw e.rethrowAsRuntimeException(); |
| 1847 | } |
| 1848 | |
| 1849 | if (DEBUG) { |
| 1850 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1851 | + " componentPresent=" + componentPresent); |
| 1852 | } |
| 1853 | |
| 1854 | // Everything else checked out so far, so this is the final yes/no check |
| 1855 | return componentPresent; |
| 1856 | } |
| 1857 | |
| 1858 | /** |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1859 | * Reconcile jobs in the pending queue against available execution contexts. |
| 1860 | * A controller can force a job into the pending queue even if it's already running, but |
| 1861 | * here is where we decide whether to actually execute it. |
| 1862 | */ |
| 1863 | private void maybeRunPendingJobsLocked() { |
| 1864 | if (DEBUG) { |
| 1865 | Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs."); |
| 1866 | } |
| 1867 | assignJobsToContextsLocked(); |
| 1868 | reportActiveLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1871 | private int adjustJobPriority(int curPriority, JobStatus job) { |
| 1872 | if (curPriority < JobInfo.PRIORITY_TOP_APP) { |
| 1873 | float factor = mJobPackageTracker.getLoadFactor(job); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1874 | if (factor >= mConstants.HEAVY_USE_FACTOR) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1875 | curPriority += JobInfo.PRIORITY_ADJ_ALWAYS_RUNNING; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1876 | } else if (factor >= mConstants.MODERATE_USE_FACTOR) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1877 | curPriority += JobInfo.PRIORITY_ADJ_OFTEN_RUNNING; |
| 1878 | } |
| 1879 | } |
| 1880 | return curPriority; |
| 1881 | } |
| 1882 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1883 | private int evaluateJobPriorityLocked(JobStatus job) { |
| 1884 | int priority = job.getPriority(); |
| 1885 | if (priority >= JobInfo.PRIORITY_FOREGROUND_APP) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1886 | return adjustJobPriority(priority, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1887 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1888 | int override = mUidPriorityOverride.get(job.getSourceUid(), 0); |
| 1889 | if (override != 0) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1890 | return adjustJobPriority(override, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1891 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1892 | return adjustJobPriority(priority, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1893 | } |
| 1894 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1895 | /** |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1896 | * Takes jobs from pending queue and runs them on available contexts. |
| 1897 | * If no contexts are available, preempts lower priority jobs to |
| 1898 | * run higher priority ones. |
| 1899 | * Lock on mJobs before calling this function. |
| 1900 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1901 | private void assignJobsToContextsLocked() { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1902 | if (DEBUG) { |
| 1903 | Slog.d(TAG, printPendingQueue()); |
| 1904 | } |
| 1905 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1906 | int memLevel; |
| 1907 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1908 | memLevel = ActivityManager.getService().getMemoryTrimLevel(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1909 | } catch (RemoteException e) { |
| 1910 | memLevel = ProcessStats.ADJ_MEM_FACTOR_NORMAL; |
| 1911 | } |
| 1912 | switch (memLevel) { |
| 1913 | case ProcessStats.ADJ_MEM_FACTOR_MODERATE: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1914 | mMaxActiveJobs = mConstants.BG_MODERATE_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1915 | break; |
| 1916 | case ProcessStats.ADJ_MEM_FACTOR_LOW: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1917 | mMaxActiveJobs = mConstants.BG_LOW_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1918 | break; |
| 1919 | case ProcessStats.ADJ_MEM_FACTOR_CRITICAL: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1920 | mMaxActiveJobs = mConstants.BG_CRITICAL_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1921 | break; |
| 1922 | default: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1923 | mMaxActiveJobs = mConstants.BG_NORMAL_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1924 | break; |
| 1925 | } |
| 1926 | |
| 1927 | JobStatus[] contextIdToJobMap = mTmpAssignContextIdToJobMap; |
| 1928 | boolean[] act = mTmpAssignAct; |
| 1929 | int[] preferredUidForContext = mTmpAssignPreferredUidForContext; |
| 1930 | int numActive = 0; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1931 | int numForeground = 0; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1932 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
| 1933 | final JobServiceContext js = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1934 | final JobStatus status = js.getRunningJobLocked(); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1935 | if ((contextIdToJobMap[i] = status) != null) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1936 | numActive++; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1937 | if (status.lastEvaluatedPriority >= JobInfo.PRIORITY_TOP_APP) { |
| 1938 | numForeground++; |
| 1939 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1940 | } |
| 1941 | act[i] = false; |
| 1942 | preferredUidForContext[i] = js.getPreferredUid(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1943 | } |
| 1944 | if (DEBUG) { |
| 1945 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs initial")); |
| 1946 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1947 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 1948 | JobStatus nextPending = mPendingJobs.get(i); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1949 | |
| 1950 | // If job is already running, go to next job. |
| 1951 | int jobRunningContext = findJobContextIdFromMap(nextPending, contextIdToJobMap); |
| 1952 | if (jobRunningContext != -1) { |
| 1953 | continue; |
| 1954 | } |
| 1955 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1956 | final int priority = evaluateJobPriorityLocked(nextPending); |
| 1957 | nextPending.lastEvaluatedPriority = priority; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1958 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1959 | // Find a context for nextPending. The context should be available OR |
| 1960 | // it should have lowest priority among all running jobs |
| 1961 | // (sharing the same Uid as nextPending) |
| 1962 | int minPriority = Integer.MAX_VALUE; |
| 1963 | int minPriorityContextId = -1; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1964 | for (int j=0; j<MAX_JOB_CONTEXTS_COUNT; j++) { |
| 1965 | JobStatus job = contextIdToJobMap[j]; |
| 1966 | int preferredUid = preferredUidForContext[j]; |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1967 | if (job == null) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1968 | if ((numActive < mMaxActiveJobs || |
| 1969 | (priority >= JobInfo.PRIORITY_TOP_APP && |
| 1970 | numForeground < mConstants.FG_JOB_COUNT)) && |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1971 | (preferredUid == nextPending.getUid() || |
| 1972 | preferredUid == JobServiceContext.NO_PREFERRED_UID)) { |
| 1973 | // This slot is free, and we haven't yet hit the limit on |
| 1974 | // concurrent jobs... we can just throw the job in to here. |
| 1975 | minPriorityContextId = j; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1976 | break; |
| 1977 | } |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1978 | // No job on this context, but nextPending can't run here because |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1979 | // the context has a preferred Uid or we have reached the limit on |
| 1980 | // concurrent jobs. |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1981 | continue; |
| 1982 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1983 | if (job.getUid() != nextPending.getUid()) { |
| 1984 | continue; |
| 1985 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1986 | if (evaluateJobPriorityLocked(job) >= nextPending.lastEvaluatedPriority) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1987 | continue; |
| 1988 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1989 | if (minPriority > nextPending.lastEvaluatedPriority) { |
| 1990 | minPriority = nextPending.lastEvaluatedPriority; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1991 | minPriorityContextId = j; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | if (minPriorityContextId != -1) { |
| 1995 | contextIdToJobMap[minPriorityContextId] = nextPending; |
| 1996 | act[minPriorityContextId] = true; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1997 | numActive++; |
| 1998 | if (priority >= JobInfo.PRIORITY_TOP_APP) { |
| 1999 | numForeground++; |
| 2000 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | if (DEBUG) { |
| 2004 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs final")); |
| 2005 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2006 | mJobPackageTracker.noteConcurrency(numActive, numForeground); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2007 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2008 | boolean preservePreferredUid = false; |
| 2009 | if (act[i]) { |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2010 | JobStatus js = mActiveServices.get(i).getRunningJobLocked(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2011 | if (js != null) { |
| 2012 | if (DEBUG) { |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2013 | Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJobLocked()); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2014 | } |
| 2015 | // preferredUid will be set to uid of currently running job. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 2016 | mActiveServices.get(i).preemptExecutingJobLocked(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2017 | preservePreferredUid = true; |
| 2018 | } else { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2019 | final JobStatus pendingJob = contextIdToJobMap[i]; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2020 | if (DEBUG) { |
| 2021 | Slog.d(TAG, "About to run job on context " |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2022 | + String.valueOf(i) + ", job: " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2023 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 2024 | for (int ic=0; ic<mControllers.size(); ic++) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2025 | mControllers.get(ic).prepareForExecutionLocked(pendingJob); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 2026 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2027 | if (!mActiveServices.get(i).executeRunnableJob(pendingJob)) { |
| 2028 | Slog.d(TAG, "Error executing " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2029 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 2030 | if (mPendingJobs.remove(pendingJob)) { |
| 2031 | mJobPackageTracker.noteNonpending(pendingJob); |
| 2032 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | if (!preservePreferredUid) { |
| 2036 | mActiveServices.get(i).clearPreferredUid(); |
| 2037 | } |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | int findJobContextIdFromMap(JobStatus jobStatus, JobStatus[] map) { |
| 2042 | for (int i=0; i<map.length; i++) { |
| 2043 | if (map[i] != null && map[i].matches(jobStatus.getUid(), jobStatus.getJobId())) { |
| 2044 | return i; |
| 2045 | } |
| 2046 | } |
| 2047 | return -1; |
| 2048 | } |
| 2049 | |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2050 | final class LocalService implements JobSchedulerInternal { |
| 2051 | |
| 2052 | /** |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2053 | * The current bucket heartbeat ordinal |
| 2054 | */ |
| 2055 | public long currentHeartbeat() { |
| 2056 | return getCurrentHeartbeat(); |
| 2057 | } |
| 2058 | |
| 2059 | /** |
| 2060 | * Heartbeat ordinal at which the given standby bucket's jobs next become runnable |
| 2061 | */ |
| 2062 | public long nextHeartbeatForBucket(int bucket) { |
| 2063 | synchronized (mLock) { |
| 2064 | return mNextBucketHeartbeat[bucket]; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | /** |
Christopher Tate | 435c2f4 | 2018-01-18 12:59:15 -0800 | [diff] [blame] | 2069 | * Heartbeat ordinal for the given app. This is typically the heartbeat at which |
| 2070 | * the app last ran jobs, so that a newly-scheduled job in an app that hasn't run |
| 2071 | * jobs in a long time is immediately runnable even if the app is bucketed into |
| 2072 | * an infrequent time allocation. |
| 2073 | */ |
| 2074 | public long baseHeartbeatForApp(String packageName, @UserIdInt int userId, |
| 2075 | final int appStandbyBucket) { |
| 2076 | if (appStandbyBucket == 0 || |
| 2077 | appStandbyBucket >= mConstants.STANDBY_BEATS.length) { |
| 2078 | // ACTIVE => everything can be run right away |
| 2079 | // NEVER => we won't run them anyway, so let them go in the future |
| 2080 | // as soon as the app enters normal use |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
| 2084 | final long timeSinceLastJob = mStandbyTracker.getTimeSinceLastJobRun( |
| 2085 | packageName, userId); |
| 2086 | final long bucketLength = mConstants.STANDBY_BEATS[appStandbyBucket]; |
| 2087 | final long bucketsAgo = timeSinceLastJob / bucketLength; |
| 2088 | |
| 2089 | // If we haven't run any jobs for more than the app's current bucket period, just |
| 2090 | // consider anything new to be immediately runnable. Otherwise, base it on the |
| 2091 | // bucket at which we last ran jobs. |
| 2092 | return (bucketsAgo > bucketLength) ? 0 : (getCurrentHeartbeat() - bucketsAgo); |
| 2093 | } |
| 2094 | |
| 2095 | /** |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2096 | * Returns a list of all pending jobs. A running job is not considered pending. Periodic |
| 2097 | * jobs are always considered pending. |
| 2098 | */ |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 2099 | @Override |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2100 | public List<JobInfo> getSystemScheduledPendingJobs() { |
| 2101 | synchronized (mLock) { |
| 2102 | final List<JobInfo> pendingJobs = new ArrayList<JobInfo>(); |
| 2103 | mJobs.forEachJob(Process.SYSTEM_UID, new JobStatusFunctor() { |
| 2104 | @Override |
| 2105 | public void process(JobStatus job) { |
| 2106 | if (job.getJob().isPeriodic() || !isCurrentlyActiveLocked(job)) { |
| 2107 | pendingJobs.add(job.getJob()); |
| 2108 | } |
| 2109 | } |
| 2110 | }); |
| 2111 | return pendingJobs; |
| 2112 | } |
| 2113 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2114 | |
| 2115 | @Override |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 2116 | public void cancelJobsForUid(int uid, String reason) { |
| 2117 | JobSchedulerService.this.cancelJobsForUid(uid, reason); |
| 2118 | } |
| 2119 | |
| 2120 | @Override |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2121 | public void addBackingUpUid(int uid) { |
| 2122 | synchronized (mLock) { |
| 2123 | // No need to actually do anything here, since for a full backup the |
| 2124 | // activity manager will kill the process which will kill the job (and |
| 2125 | // cause it to restart, but now it can't run). |
| 2126 | mBackingUpUids.put(uid, uid); |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | @Override |
| 2131 | public void removeBackingUpUid(int uid) { |
| 2132 | synchronized (mLock) { |
| 2133 | mBackingUpUids.delete(uid); |
| 2134 | // If there are any jobs for this uid, we need to rebuild the pending list |
| 2135 | // in case they are now ready to run. |
| 2136 | if (mJobs.countJobsForUid(uid) > 0) { |
| 2137 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 2138 | } |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | @Override |
| 2143 | public void clearAllBackingUpUids() { |
| 2144 | synchronized (mLock) { |
| 2145 | if (mBackingUpUids.size() > 0) { |
| 2146 | mBackingUpUids.clear(); |
| 2147 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 2148 | } |
| 2149 | } |
| 2150 | } |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 2151 | |
| 2152 | @Override |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 2153 | public void reportAppUsage(String packageName, int userId) { |
| 2154 | JobSchedulerService.this.reportAppUsage(packageName, userId); |
| 2155 | } |
| 2156 | |
| 2157 | @Override |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 2158 | public JobStorePersistStats getPersistStats() { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 2159 | synchronized (mLock) { |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 2160 | return new JobStorePersistStats(mJobs.getPersistStats()); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 2161 | } |
| 2162 | } |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2165 | /** |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2166 | * Tracking of app assignments to standby buckets |
| 2167 | */ |
| 2168 | final class StandbyTracker extends AppIdleStateChangeListener { |
| 2169 | final UsageStatsManagerInternal mUsageStats; |
| 2170 | |
| 2171 | StandbyTracker(UsageStatsManagerInternal usageStats) { |
| 2172 | mUsageStats = usageStats; |
| 2173 | } |
| 2174 | |
Christopher Tate | 435c2f4 | 2018-01-18 12:59:15 -0800 | [diff] [blame] | 2175 | public long getTimeSinceLastJobRun(String packageName, final @UserIdInt int userId) { |
| 2176 | return mUsageStats.getTimeSinceLastJobRun(packageName, userId); |
| 2177 | } |
| 2178 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2179 | // AppIdleStateChangeListener interface for live updates |
| 2180 | |
| 2181 | @Override |
Christopher Tate | 435c2f4 | 2018-01-18 12:59:15 -0800 | [diff] [blame] | 2182 | public void onAppIdleStateChanged(final String packageName, final @UserIdInt int userId, |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2183 | boolean idle, int bucket) { |
| 2184 | final int uid = mLocalPM.getPackageUid(packageName, |
| 2185 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 2186 | if (uid < 0) { |
| 2187 | if (DEBUG_STANDBY) { |
| 2188 | Slog.i(TAG, "App idle state change for unknown app " |
| 2189 | + packageName + "/" + userId); |
| 2190 | } |
| 2191 | return; |
| 2192 | } |
| 2193 | |
| 2194 | final int bucketIndex = standbyBucketToBucketIndex(bucket); |
| 2195 | // update job bookkeeping out of band |
| 2196 | BackgroundThread.getHandler().post(() -> { |
| 2197 | if (DEBUG_STANDBY) { |
| 2198 | Slog.i(TAG, "Moving uid " + uid + " to bucketIndex " + bucketIndex); |
| 2199 | } |
| 2200 | synchronized (mLock) { |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 2201 | mJobs.forEachJobForSourceUid(uid, job -> job.setStandbyBucket(bucketIndex)); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2202 | onControllerStateChanged(); |
| 2203 | } |
| 2204 | }); |
| 2205 | } |
| 2206 | |
| 2207 | @Override |
| 2208 | public void onParoleStateChanged(boolean isParoleOn) { |
Christopher Tate | a5a85bd | 2018-01-03 17:20:36 -0800 | [diff] [blame] | 2209 | if (DEBUG_STANDBY) { |
| 2210 | Slog.i(TAG, "Global parole state now " + (isParoleOn ? "ON" : "OFF")); |
| 2211 | } |
| 2212 | mInParole = isParoleOn; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2213 | } |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 2214 | |
| 2215 | @Override |
| 2216 | public void onUserInteractionStarted(String packageName, int userId) { |
| 2217 | final int uid = mLocalPM.getPackageUid(packageName, |
| 2218 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 2219 | if (uid < 0) { |
| 2220 | // Quietly ignore; the case is already logged elsewhere |
| 2221 | return; |
| 2222 | } |
| 2223 | |
| 2224 | final long sinceLast = sElapsedRealtimeClock.millis() - |
| 2225 | mUsageStats.getTimeSinceLastJobRun(packageName, userId); |
| 2226 | final DeferredJobCounter counter = new DeferredJobCounter(); |
| 2227 | synchronized (mLock) { |
| 2228 | mJobs.forEachJobForSourceUid(uid, counter); |
| 2229 | } |
| 2230 | |
| 2231 | mUsageStats.reportAppJobState(packageName, userId, counter.numDeferred(), sinceLast); |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | static class DeferredJobCounter implements JobStatusFunctor { |
| 2236 | private int mDeferred = 0; |
| 2237 | |
| 2238 | public int numDeferred() { |
| 2239 | return mDeferred; |
| 2240 | } |
| 2241 | |
| 2242 | @Override |
| 2243 | public void process(JobStatus job) { |
| 2244 | if (job.getWhenStandbyDeferred() > 0) { |
| 2245 | mDeferred++; |
| 2246 | } |
| 2247 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | public static int standbyBucketToBucketIndex(int bucket) { |
| 2251 | // Normalize AppStandby constants to indices into our bookkeeping |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 2252 | if (bucket == UsageStatsManager.STANDBY_BUCKET_NEVER) return 4; |
| 2253 | else if (bucket >= UsageStatsManager.STANDBY_BUCKET_RARE) return 3; |
| 2254 | else if (bucket >= UsageStatsManager.STANDBY_BUCKET_FREQUENT) return 2; |
| 2255 | else if (bucket >= UsageStatsManager.STANDBY_BUCKET_WORKING_SET) return 1; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2256 | else return 0; |
| 2257 | } |
| 2258 | |
| 2259 | public static int standbyBucketForPackage(String packageName, int userId, long elapsedNow) { |
| 2260 | UsageStatsManagerInternal usageStats = LocalServices.getService( |
| 2261 | UsageStatsManagerInternal.class); |
| 2262 | int bucket = usageStats != null |
| 2263 | ? usageStats.getAppStandbyBucket(packageName, userId, elapsedNow) |
| 2264 | : 0; |
| 2265 | |
| 2266 | bucket = standbyBucketToBucketIndex(bucket); |
| 2267 | |
| 2268 | if (DEBUG_STANDBY) { |
| 2269 | Slog.v(TAG, packageName + "/" + userId + " standby bucket index: " + bucket); |
| 2270 | } |
| 2271 | return bucket; |
| 2272 | } |
| 2273 | |
| 2274 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2275 | * Binder stub trampoline implementation |
| 2276 | */ |
| 2277 | final class JobSchedulerStub extends IJobScheduler.Stub { |
| 2278 | /** Cache determination of whether a given app can persist jobs |
| 2279 | * key is uid of the calling app; value is undetermined/true/false |
| 2280 | */ |
| 2281 | private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>(); |
| 2282 | |
| 2283 | // Enforce that only the app itself (or shared uid participant) can schedule a |
| 2284 | // job that runs one of the app's services, as well as verifying that the |
| 2285 | // named service properly requires the BIND_JOB_SERVICE permission |
| 2286 | private void enforceValidJobRequest(int uid, JobInfo job) { |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 2287 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2288 | final ComponentName service = job.getService(); |
| 2289 | try { |
Jeff Sharkey | c7bacab | 2016-02-09 15:56:11 -0700 | [diff] [blame] | 2290 | ServiceInfo si = pm.getServiceInfo(service, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2291 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 2292 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Jeff Sharkey | 12c0da4 | 2016-02-25 17:10:50 -0700 | [diff] [blame] | 2293 | UserHandle.getUserId(uid)); |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 2294 | if (si == null) { |
| 2295 | throw new IllegalArgumentException("No such service " + service); |
| 2296 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2297 | if (si.applicationInfo.uid != uid) { |
| 2298 | throw new IllegalArgumentException("uid " + uid + |
| 2299 | " cannot schedule job in " + service.getPackageName()); |
| 2300 | } |
| 2301 | if (!JobService.PERMISSION_BIND.equals(si.permission)) { |
| 2302 | throw new IllegalArgumentException("Scheduled service " + service |
| 2303 | + " does not require android.permission.BIND_JOB_SERVICE permission"); |
| 2304 | } |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 2305 | } catch (RemoteException e) { |
| 2306 | // Can't happen; the Package Manager is in this same process |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | private boolean canPersistJobs(int pid, int uid) { |
| 2311 | // If we get this far we're good to go; all we need to do now is check |
| 2312 | // whether the app is allowed to persist its scheduled work. |
| 2313 | final boolean canPersist; |
| 2314 | synchronized (mPersistCache) { |
| 2315 | Boolean cached = mPersistCache.get(uid); |
| 2316 | if (cached != null) { |
| 2317 | canPersist = cached.booleanValue(); |
| 2318 | } else { |
| 2319 | // Persisting jobs is tantamount to running at boot, so we permit |
| 2320 | // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED |
| 2321 | // permission |
| 2322 | int result = getContext().checkPermission( |
| 2323 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid); |
| 2324 | canPersist = (result == PackageManager.PERMISSION_GRANTED); |
| 2325 | mPersistCache.put(uid, canPersist); |
| 2326 | } |
| 2327 | } |
| 2328 | return canPersist; |
| 2329 | } |
| 2330 | |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2331 | private void validateJobFlags(JobInfo job, int callingUid) { |
| 2332 | if ((job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0) { |
| 2333 | getContext().enforceCallingOrSelfPermission( |
| 2334 | android.Manifest.permission.CONNECTIVITY_INTERNAL, TAG); |
| 2335 | } |
| 2336 | if ((job.getFlags() & JobInfo.FLAG_EXEMPT_FROM_APP_STANDBY) != 0) { |
| 2337 | if (callingUid != Process.SYSTEM_UID) { |
| 2338 | throw new SecurityException("Job has invalid flags"); |
| 2339 | } |
| 2340 | if (job.hasLateConstraint() || job.hasEarlyConstraint()) { |
| 2341 | Slog.wtf(TAG, "Jobs with time-constraints mustn't have" |
| 2342 | +" FLAG_EXEMPT_FROM_APP_STANDBY. Job=" + job); |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2347 | // IJobScheduler implementation |
| 2348 | @Override |
| 2349 | public int schedule(JobInfo job) throws RemoteException { |
| 2350 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 2351 | Slog.d(TAG, "Scheduling job: " + job.toString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2352 | } |
| 2353 | final int pid = Binder.getCallingPid(); |
| 2354 | final int uid = Binder.getCallingUid(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2355 | final int userId = UserHandle.getUserId(uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2356 | |
| 2357 | enforceValidJobRequest(uid, job); |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 2358 | if (job.isPersisted()) { |
| 2359 | if (!canPersistJobs(pid, uid)) { |
| 2360 | throw new IllegalArgumentException("Error: requested job be persisted without" |
| 2361 | + " holding RECEIVE_BOOT_COMPLETED permission."); |
| 2362 | } |
| 2363 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2364 | |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2365 | validateJobFlags(job, uid); |
Jeff Sharkey | 785f494 | 2016-07-14 10:31:15 -0600 | [diff] [blame] | 2366 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2367 | long ident = Binder.clearCallingIdentity(); |
| 2368 | try { |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2369 | return JobSchedulerService.this.scheduleAsPackage(job, null, uid, null, userId, |
| 2370 | null); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2371 | } finally { |
| 2372 | Binder.restoreCallingIdentity(ident); |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | // IJobScheduler implementation |
| 2377 | @Override |
| 2378 | public int enqueue(JobInfo job, JobWorkItem work) throws RemoteException { |
| 2379 | if (DEBUG) { |
| 2380 | Slog.d(TAG, "Enqueueing job: " + job.toString() + " work: " + work); |
| 2381 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2382 | final int uid = Binder.getCallingUid(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2383 | final int userId = UserHandle.getUserId(uid); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2384 | |
| 2385 | enforceValidJobRequest(uid, job); |
| 2386 | if (job.isPersisted()) { |
| 2387 | throw new IllegalArgumentException("Can't enqueue work for persisted jobs"); |
| 2388 | } |
| 2389 | if (work == null) { |
| 2390 | throw new NullPointerException("work is null"); |
| 2391 | } |
| 2392 | |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2393 | validateJobFlags(job, uid); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2394 | |
| 2395 | long ident = Binder.clearCallingIdentity(); |
| 2396 | try { |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2397 | return JobSchedulerService.this.scheduleAsPackage(job, work, uid, null, userId, |
| 2398 | null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2399 | } finally { |
| 2400 | Binder.restoreCallingIdentity(ident); |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | @Override |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2405 | public int scheduleAsPackage(JobInfo job, String packageName, int userId, String tag) |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2406 | throws RemoteException { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2407 | final int callerUid = Binder.getCallingUid(); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2408 | if (DEBUG) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2409 | Slog.d(TAG, "Caller uid " + callerUid + " scheduling job: " + job.toString() |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2410 | + " on behalf of " + packageName + "/"); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2411 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2412 | |
| 2413 | if (packageName == null) { |
| 2414 | throw new NullPointerException("Must specify a package for scheduleAsPackage()"); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2415 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2416 | |
| 2417 | int mayScheduleForOthers = getContext().checkCallingOrSelfPermission( |
| 2418 | android.Manifest.permission.UPDATE_DEVICE_STATS); |
| 2419 | if (mayScheduleForOthers != PackageManager.PERMISSION_GRANTED) { |
| 2420 | throw new SecurityException("Caller uid " + callerUid |
| 2421 | + " not permitted to schedule jobs for other apps"); |
| 2422 | } |
| 2423 | |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2424 | validateJobFlags(job, callerUid); |
Jeff Sharkey | 4f10040 | 2016-05-03 17:44:23 -0600 | [diff] [blame] | 2425 | |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2426 | long ident = Binder.clearCallingIdentity(); |
| 2427 | try { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2428 | return JobSchedulerService.this.scheduleAsPackage(job, null, callerUid, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2429 | packageName, userId, tag); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2430 | } finally { |
| 2431 | Binder.restoreCallingIdentity(ident); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2436 | public List<JobInfo> getAllPendingJobs() throws RemoteException { |
| 2437 | final int uid = Binder.getCallingUid(); |
| 2438 | |
| 2439 | long ident = Binder.clearCallingIdentity(); |
| 2440 | try { |
| 2441 | return JobSchedulerService.this.getPendingJobs(uid); |
| 2442 | } finally { |
| 2443 | Binder.restoreCallingIdentity(ident); |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | @Override |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 2448 | public JobInfo getPendingJob(int jobId) throws RemoteException { |
| 2449 | final int uid = Binder.getCallingUid(); |
| 2450 | |
| 2451 | long ident = Binder.clearCallingIdentity(); |
| 2452 | try { |
| 2453 | return JobSchedulerService.this.getPendingJob(uid, jobId); |
| 2454 | } finally { |
| 2455 | Binder.restoreCallingIdentity(ident); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2460 | public void cancelAll() throws RemoteException { |
| 2461 | final int uid = Binder.getCallingUid(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2462 | long ident = Binder.clearCallingIdentity(); |
| 2463 | try { |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 2464 | JobSchedulerService.this.cancelJobsForUid(uid, |
| 2465 | "cancelAll() called by app, callingUid=" + uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2466 | } finally { |
| 2467 | Binder.restoreCallingIdentity(ident); |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | @Override |
| 2472 | public void cancel(int jobId) throws RemoteException { |
| 2473 | final int uid = Binder.getCallingUid(); |
| 2474 | |
| 2475 | long ident = Binder.clearCallingIdentity(); |
| 2476 | try { |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 2477 | JobSchedulerService.this.cancelJob(uid, jobId, uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2478 | } finally { |
| 2479 | Binder.restoreCallingIdentity(ident); |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | /** |
| 2484 | * "dumpsys" infrastructure |
| 2485 | */ |
| 2486 | @Override |
| 2487 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2488 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2489 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2490 | int filterUid = -1; |
| 2491 | boolean proto = false; |
| 2492 | if (!ArrayUtils.isEmpty(args)) { |
| 2493 | int opti = 0; |
| 2494 | while (opti < args.length) { |
| 2495 | String arg = args[opti]; |
| 2496 | if ("-h".equals(arg)) { |
| 2497 | dumpHelp(pw); |
| 2498 | return; |
| 2499 | } else if ("-a".equals(arg)) { |
| 2500 | // Ignore, we always dump all. |
| 2501 | } else if ("--proto".equals(arg)) { |
| 2502 | proto = true; |
| 2503 | } else if (arg.length() > 0 && arg.charAt(0) == '-') { |
| 2504 | pw.println("Unknown option: " + arg); |
| 2505 | return; |
| 2506 | } else { |
| 2507 | break; |
| 2508 | } |
| 2509 | opti++; |
| 2510 | } |
| 2511 | if (opti < args.length) { |
| 2512 | String pkg = args[opti]; |
| 2513 | try { |
| 2514 | filterUid = getContext().getPackageManager().getPackageUid(pkg, |
| 2515 | PackageManager.MATCH_ANY_USER); |
| 2516 | } catch (NameNotFoundException ignored) { |
| 2517 | pw.println("Invalid package: " + pkg); |
| 2518 | return; |
| 2519 | } |
| 2520 | } |
| 2521 | } |
| 2522 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2523 | long identityToken = Binder.clearCallingIdentity(); |
| 2524 | try { |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2525 | if (proto) { |
| 2526 | JobSchedulerService.this.dumpInternalProto(fd, filterUid); |
| 2527 | } else { |
| 2528 | JobSchedulerService.this.dumpInternal(pw, filterUid); |
| 2529 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2530 | } finally { |
| 2531 | Binder.restoreCallingIdentity(identityToken); |
| 2532 | } |
| 2533 | } |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2534 | |
| 2535 | @Override |
| 2536 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2537 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2538 | (new JobSchedulerShellCommand(JobSchedulerService.this)).exec( |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2539 | this, in, out, err, args, callback, resultReceiver); |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2540 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2541 | }; |
| 2542 | |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2543 | // Shell command infrastructure: run the given job immediately |
| 2544 | int executeRunCommand(String pkgName, int userId, int jobId, boolean force) { |
| 2545 | if (DEBUG) { |
| 2546 | Slog.v(TAG, "executeRunCommand(): " + pkgName + "/" + userId |
| 2547 | + " " + jobId + " f=" + force); |
| 2548 | } |
| 2549 | |
| 2550 | try { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2551 | final int uid = AppGlobals.getPackageManager().getPackageUid(pkgName, 0, |
| 2552 | userId != UserHandle.USER_ALL ? userId : UserHandle.USER_SYSTEM); |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2553 | if (uid < 0) { |
| 2554 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 2555 | } |
| 2556 | |
| 2557 | synchronized (mLock) { |
| 2558 | final JobStatus js = mJobs.getJobByUidAndJobId(uid, jobId); |
| 2559 | if (js == null) { |
| 2560 | return JobSchedulerShellCommand.CMD_ERR_NO_JOB; |
| 2561 | } |
| 2562 | |
| 2563 | js.overrideState = (force) ? JobStatus.OVERRIDE_FULL : JobStatus.OVERRIDE_SOFT; |
| 2564 | if (!js.isConstraintsSatisfied()) { |
| 2565 | js.overrideState = 0; |
| 2566 | return JobSchedulerShellCommand.CMD_ERR_CONSTRAINTS; |
| 2567 | } |
| 2568 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2569 | queueReadyJobsForExecutionLocked(); |
| 2570 | maybeRunPendingJobsLocked(); |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2571 | } |
| 2572 | } catch (RemoteException e) { |
| 2573 | // can't happen |
| 2574 | } |
| 2575 | return 0; |
| 2576 | } |
| 2577 | |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2578 | // Shell command infrastructure: immediately timeout currently executing jobs |
| 2579 | int executeTimeoutCommand(PrintWriter pw, String pkgName, int userId, |
| 2580 | boolean hasJobId, int jobId) { |
| 2581 | if (DEBUG) { |
| 2582 | Slog.v(TAG, "executeTimeoutCommand(): " + pkgName + "/" + userId + " " + jobId); |
| 2583 | } |
| 2584 | |
| 2585 | synchronized (mLock) { |
| 2586 | boolean foundSome = false; |
| 2587 | for (int i=0; i<mActiveServices.size(); i++) { |
Dianne Hackborn | eb90fa9 | 2017-06-30 14:03:36 -0700 | [diff] [blame] | 2588 | final JobServiceContext jc = mActiveServices.get(i); |
| 2589 | final JobStatus js = jc.getRunningJobLocked(); |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 2590 | if (jc.timeoutIfExecutingLocked(pkgName, userId, hasJobId, jobId, "shell")) { |
Dianne Hackborn | eb90fa9 | 2017-06-30 14:03:36 -0700 | [diff] [blame] | 2591 | foundSome = true; |
| 2592 | pw.print("Timing out: "); |
| 2593 | js.printUniqueId(pw); |
| 2594 | pw.print(" "); |
| 2595 | pw.println(js.getServiceComponent().flattenToShortString()); |
| 2596 | } |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2597 | } |
| 2598 | if (!foundSome) { |
| 2599 | pw.println("No matching executing jobs found."); |
| 2600 | } |
| 2601 | } |
| 2602 | return 0; |
| 2603 | } |
| 2604 | |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 2605 | // Shell command infrastructure: cancel a scheduled job |
| 2606 | int executeCancelCommand(PrintWriter pw, String pkgName, int userId, |
| 2607 | boolean hasJobId, int jobId) { |
| 2608 | if (DEBUG) { |
| 2609 | Slog.v(TAG, "executeCancelCommand(): " + pkgName + "/" + userId + " " + jobId); |
| 2610 | } |
| 2611 | |
| 2612 | int pkgUid = -1; |
| 2613 | try { |
| 2614 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 2615 | pkgUid = pm.getPackageUid(pkgName, 0, userId); |
| 2616 | } catch (RemoteException e) { /* can't happen */ } |
| 2617 | |
| 2618 | if (pkgUid < 0) { |
| 2619 | pw.println("Package " + pkgName + " not found."); |
| 2620 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 2621 | } |
| 2622 | |
| 2623 | if (!hasJobId) { |
| 2624 | pw.println("Canceling all jobs for " + pkgName + " in user " + userId); |
| 2625 | if (!cancelJobsForUid(pkgUid, "cancel shell command for package")) { |
| 2626 | pw.println("No matching jobs found."); |
| 2627 | } |
| 2628 | } else { |
| 2629 | pw.println("Canceling job " + pkgName + "/#" + jobId + " in user " + userId); |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 2630 | if (!cancelJob(pkgUid, jobId, Process.SHELL_UID)) { |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 2631 | pw.println("No matching job found."); |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | return 0; |
| 2636 | } |
| 2637 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 2638 | void setMonitorBattery(boolean enabled) { |
| 2639 | synchronized (mLock) { |
| 2640 | if (mBatteryController != null) { |
| 2641 | mBatteryController.getTracker().setMonitorBatteryLocked(enabled); |
| 2642 | } |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | int getBatterySeq() { |
| 2647 | synchronized (mLock) { |
| 2648 | return mBatteryController != null ? mBatteryController.getTracker().getSeq() : -1; |
| 2649 | } |
| 2650 | } |
| 2651 | |
| 2652 | boolean getBatteryCharging() { |
| 2653 | synchronized (mLock) { |
| 2654 | return mBatteryController != null |
| 2655 | ? mBatteryController.getTracker().isOnStablePower() : false; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | boolean getBatteryNotLow() { |
| 2660 | synchronized (mLock) { |
| 2661 | return mBatteryController != null |
| 2662 | ? mBatteryController.getTracker().isBatteryNotLow() : false; |
| 2663 | } |
| 2664 | } |
| 2665 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 2666 | int getStorageSeq() { |
| 2667 | synchronized (mLock) { |
| 2668 | return mStorageController != null ? mStorageController.getTracker().getSeq() : -1; |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | boolean getStorageNotLow() { |
| 2673 | synchronized (mLock) { |
| 2674 | return mStorageController != null |
| 2675 | ? mStorageController.getTracker().isStorageNotLow() : false; |
| 2676 | } |
| 2677 | } |
| 2678 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2679 | long getCurrentHeartbeat() { |
| 2680 | synchronized (mLock) { |
| 2681 | return mHeartbeat; |
| 2682 | } |
| 2683 | } |
| 2684 | |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2685 | int getJobState(PrintWriter pw, String pkgName, int userId, int jobId) { |
| 2686 | try { |
| 2687 | final int uid = AppGlobals.getPackageManager().getPackageUid(pkgName, 0, |
| 2688 | userId != UserHandle.USER_ALL ? userId : UserHandle.USER_SYSTEM); |
| 2689 | if (uid < 0) { |
| 2690 | pw.print("unknown("); pw.print(pkgName); pw.println(")"); |
| 2691 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 2692 | } |
| 2693 | |
| 2694 | synchronized (mLock) { |
| 2695 | final JobStatus js = mJobs.getJobByUidAndJobId(uid, jobId); |
| 2696 | if (DEBUG) Slog.d(TAG, "get-job-state " + uid + "/" + jobId + ": " + js); |
| 2697 | if (js == null) { |
| 2698 | pw.print("unknown("); UserHandle.formatUid(pw, uid); |
| 2699 | pw.print("/jid"); pw.print(jobId); pw.println(")"); |
| 2700 | return JobSchedulerShellCommand.CMD_ERR_NO_JOB; |
| 2701 | } |
| 2702 | |
| 2703 | boolean printed = false; |
| 2704 | if (mPendingJobs.contains(js)) { |
| 2705 | pw.print("pending"); |
| 2706 | printed = true; |
| 2707 | } |
| 2708 | if (isCurrentlyActiveLocked(js)) { |
| 2709 | if (printed) { |
| 2710 | pw.print(" "); |
| 2711 | } |
| 2712 | printed = true; |
| 2713 | pw.println("active"); |
| 2714 | } |
| 2715 | if (!ArrayUtils.contains(mStartedUsers, js.getUserId())) { |
| 2716 | if (printed) { |
| 2717 | pw.print(" "); |
| 2718 | } |
| 2719 | printed = true; |
| 2720 | pw.println("user-stopped"); |
| 2721 | } |
| 2722 | if (mBackingUpUids.indexOfKey(js.getSourceUid()) >= 0) { |
| 2723 | if (printed) { |
| 2724 | pw.print(" "); |
| 2725 | } |
| 2726 | printed = true; |
| 2727 | pw.println("backing-up"); |
| 2728 | } |
| 2729 | boolean componentPresent = false; |
| 2730 | try { |
| 2731 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 2732 | js.getServiceComponent(), |
| 2733 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 2734 | js.getUserId()) != null); |
| 2735 | } catch (RemoteException e) { |
| 2736 | } |
| 2737 | if (!componentPresent) { |
| 2738 | if (printed) { |
| 2739 | pw.print(" "); |
| 2740 | } |
| 2741 | printed = true; |
| 2742 | pw.println("no-component"); |
| 2743 | } |
| 2744 | if (js.isReady()) { |
| 2745 | if (printed) { |
| 2746 | pw.print(" "); |
| 2747 | } |
| 2748 | printed = true; |
| 2749 | pw.println("ready"); |
| 2750 | } |
| 2751 | if (!printed) { |
| 2752 | pw.print("waiting"); |
| 2753 | } |
| 2754 | pw.println(); |
| 2755 | } |
| 2756 | } catch (RemoteException e) { |
| 2757 | // can't happen |
| 2758 | } |
| 2759 | return 0; |
| 2760 | } |
| 2761 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2762 | private String printContextIdToJobMap(JobStatus[] map, String initial) { |
| 2763 | StringBuilder s = new StringBuilder(initial + ": "); |
| 2764 | for (int i=0; i<map.length; i++) { |
| 2765 | s.append("(") |
| 2766 | .append(map[i] == null? -1: map[i].getJobId()) |
| 2767 | .append(map[i] == null? -1: map[i].getUid()) |
| 2768 | .append(")" ); |
| 2769 | } |
| 2770 | return s.toString(); |
| 2771 | } |
| 2772 | |
| 2773 | private String printPendingQueue() { |
| 2774 | StringBuilder s = new StringBuilder("Pending queue: "); |
| 2775 | Iterator<JobStatus> it = mPendingJobs.iterator(); |
| 2776 | while (it.hasNext()) { |
| 2777 | JobStatus js = it.next(); |
| 2778 | s.append("(") |
| 2779 | .append(js.getJob().getId()) |
| 2780 | .append(", ") |
| 2781 | .append(js.getUid()) |
| 2782 | .append(") "); |
| 2783 | } |
| 2784 | return s.toString(); |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 2785 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2786 | |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2787 | static void dumpHelp(PrintWriter pw) { |
| 2788 | pw.println("Job Scheduler (jobscheduler) dump options:"); |
| 2789 | pw.println(" [-h] [package] ..."); |
| 2790 | pw.println(" -h: print this help"); |
| 2791 | pw.println(" [package] is an optional package name to limit the output to."); |
| 2792 | } |
| 2793 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2794 | /** Sort jobs by caller UID, then by Job ID. */ |
| 2795 | private static void sortJobs(List<JobStatus> jobs) { |
| 2796 | Collections.sort(jobs, new Comparator<JobStatus>() { |
| 2797 | @Override |
| 2798 | public int compare(JobStatus o1, JobStatus o2) { |
| 2799 | int uid1 = o1.getUid(); |
| 2800 | int uid2 = o2.getUid(); |
| 2801 | int id1 = o1.getJobId(); |
| 2802 | int id2 = o2.getJobId(); |
| 2803 | if (uid1 != uid2) { |
| 2804 | return uid1 < uid2 ? -1 : 1; |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2805 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2806 | return id1 < id2 ? -1 : (id1 > id2 ? 1 : 0); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2807 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2808 | }); |
| 2809 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 2810 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2811 | void dumpInternal(final PrintWriter pw, int filterUid) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2812 | final int filterUidFinal = UserHandle.getAppId(filterUid); |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 2813 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
| 2814 | final long nowUptime = sUptimeMillisClock.millis(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 2815 | synchronized (mLock) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2816 | mConstants.dump(pw); |
| 2817 | pw.println(); |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 2818 | pw.println("Started users: " + Arrays.toString(mStartedUsers)); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2819 | pw.print("Registered "); |
| 2820 | pw.print(mJobs.size()); |
| 2821 | pw.println(" jobs:"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2822 | if (mJobs.size() > 0) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2823 | final List<JobStatus> jobs = mJobs.mJobSet.getAllJobs(); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2824 | sortJobs(jobs); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2825 | for (JobStatus job : jobs) { |
| 2826 | pw.print(" JOB #"); job.printUniqueId(pw); pw.print(": "); |
| 2827 | pw.println(job.toShortStringExceptUniqueId()); |
| 2828 | |
| 2829 | // Skip printing details if the caller requested a filter |
| 2830 | if (!job.shouldDump(filterUidFinal)) { |
| 2831 | continue; |
| 2832 | } |
| 2833 | |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2834 | job.dump(pw, " ", true, nowElapsed); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2835 | pw.print(" Ready: "); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2836 | pw.print(isReadyToBeExecutedLocked(job)); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2837 | pw.print(" (job="); |
| 2838 | pw.print(job.isReady()); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2839 | pw.print(" user="); |
| 2840 | pw.print(ArrayUtils.contains(mStartedUsers, job.getUserId())); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 2841 | pw.print(" !pending="); |
| 2842 | pw.print(!mPendingJobs.contains(job)); |
| 2843 | pw.print(" !active="); |
| 2844 | pw.print(!isCurrentlyActiveLocked(job)); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2845 | pw.print(" !backingup="); |
| 2846 | pw.print(!(mBackingUpUids.indexOfKey(job.getSourceUid()) >= 0)); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 2847 | pw.print(" comp="); |
| 2848 | boolean componentPresent = false; |
| 2849 | try { |
| 2850 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 2851 | job.getServiceComponent(), |
| 2852 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 2853 | job.getUserId()) != null); |
| 2854 | } catch (RemoteException e) { |
| 2855 | } |
| 2856 | pw.print(componentPresent); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2857 | pw.println(")"); |
| 2858 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2859 | } else { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 2860 | pw.println(" None."); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2861 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 2862 | for (int i=0; i<mControllers.size(); i++) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2863 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2864 | mControllers.get(i).dumpControllerStateLocked(pw, filterUidFinal); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2865 | } |
| 2866 | pw.println(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2867 | pw.println("Uid priority overrides:"); |
| 2868 | for (int i=0; i< mUidPriorityOverride.size(); i++) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2869 | int uid = mUidPriorityOverride.keyAt(i); |
| 2870 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 2871 | pw.print(" "); pw.print(UserHandle.formatUid(uid)); |
| 2872 | pw.print(": "); pw.println(mUidPriorityOverride.valueAt(i)); |
| 2873 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2874 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2875 | if (mBackingUpUids.size() > 0) { |
| 2876 | pw.println(); |
| 2877 | pw.println("Backing up uids:"); |
| 2878 | boolean first = true; |
| 2879 | for (int i = 0; i < mBackingUpUids.size(); i++) { |
| 2880 | int uid = mBackingUpUids.keyAt(i); |
| 2881 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 2882 | if (first) { |
| 2883 | pw.print(" "); |
| 2884 | first = false; |
| 2885 | } else { |
| 2886 | pw.print(", "); |
| 2887 | } |
| 2888 | pw.print(UserHandle.formatUid(uid)); |
| 2889 | } |
| 2890 | } |
| 2891 | pw.println(); |
| 2892 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2893 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2894 | mJobPackageTracker.dump(pw, "", filterUidFinal); |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 2895 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2896 | if (mJobPackageTracker.dumpHistory(pw, "", filterUidFinal)) { |
| 2897 | pw.println(); |
| 2898 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2899 | pw.println("Pending queue:"); |
| 2900 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 2901 | JobStatus job = mPendingJobs.get(i); |
| 2902 | pw.print(" Pending #"); pw.print(i); pw.print(": "); |
| 2903 | pw.println(job.toShortString()); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2904 | job.dump(pw, " ", false, nowElapsed); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2905 | int priority = evaluateJobPriorityLocked(job); |
| 2906 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 2907 | pw.print(" Evaluated priority: "); pw.println(priority); |
| 2908 | } |
| 2909 | pw.print(" Tag: "); pw.println(job.getTag()); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2910 | pw.print(" Enq: "); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2911 | TimeUtils.formatDuration(job.madePending - nowUptime, pw); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2912 | pw.println(); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2913 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2914 | pw.println(); |
| 2915 | pw.println("Active jobs:"); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 2916 | for (int i=0; i<mActiveServices.size(); i++) { |
| 2917 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2918 | pw.print(" Slot #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2919 | final JobStatus job = jsc.getRunningJobLocked(); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2920 | if (job == null) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 2921 | if (jsc.mStoppedReason != null) { |
| 2922 | pw.print("inactive since "); |
| 2923 | TimeUtils.formatDuration(jsc.mStoppedTime, nowElapsed, pw); |
| 2924 | pw.print(", stopped because: "); |
| 2925 | pw.println(jsc.mStoppedReason); |
| 2926 | } else { |
| 2927 | pw.println("inactive"); |
| 2928 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2929 | continue; |
| 2930 | } else { |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2931 | pw.println(job.toShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2932 | pw.print(" Running for: "); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2933 | TimeUtils.formatDuration(nowElapsed - jsc.getExecutionStartTimeElapsed(), pw); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2934 | pw.print(", timeout at: "); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2935 | TimeUtils.formatDuration(jsc.getTimeoutElapsed() - nowElapsed, pw); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2936 | pw.println(); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2937 | job.dump(pw, " ", false, nowElapsed); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2938 | int priority = evaluateJobPriorityLocked(jsc.getRunningJobLocked()); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2939 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2940 | pw.print(" Evaluated priority: "); pw.println(priority); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2941 | } |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2942 | pw.print(" Active at "); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2943 | TimeUtils.formatDuration(job.madeActive - nowUptime, pw); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2944 | pw.print(", pending for "); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2945 | TimeUtils.formatDuration(job.madeActive - job.madePending, pw); |
| 2946 | pw.println(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2947 | } |
| 2948 | } |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2949 | if (filterUid == -1) { |
| 2950 | pw.println(); |
| 2951 | pw.print("mReadyToRock="); pw.println(mReadyToRock); |
| 2952 | pw.print("mReportedActive="); pw.println(mReportedActive); |
| 2953 | pw.print("mMaxActiveJobs="); pw.println(mMaxActiveJobs); |
| 2954 | } |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 2955 | pw.println(); |
| 2956 | pw.print("PersistStats: "); |
| 2957 | pw.println(mJobs.getPersistStats()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2958 | } |
| 2959 | pw.println(); |
| 2960 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2961 | |
| 2962 | void dumpInternalProto(final FileDescriptor fd, int filterUid) { |
| 2963 | ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 2964 | final int filterUidFinal = UserHandle.getAppId(filterUid); |
| 2965 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
| 2966 | final long nowUptime = sUptimeMillisClock.millis(); |
| 2967 | |
| 2968 | synchronized (mLock) { |
| 2969 | mConstants.dump(proto, JobSchedulerServiceDumpProto.SETTINGS); |
| 2970 | for (int u : mStartedUsers) { |
| 2971 | proto.write(JobSchedulerServiceDumpProto.STARTED_USERS, u); |
| 2972 | } |
| 2973 | if (mJobs.size() > 0) { |
| 2974 | final List<JobStatus> jobs = mJobs.mJobSet.getAllJobs(); |
| 2975 | sortJobs(jobs); |
| 2976 | for (JobStatus job : jobs) { |
| 2977 | final long rjToken = proto.start(JobSchedulerServiceDumpProto.REGISTERED_JOBS); |
| 2978 | job.writeToShortProto(proto, JobSchedulerServiceDumpProto.RegisteredJob.INFO); |
| 2979 | |
| 2980 | // Skip printing details if the caller requested a filter |
| 2981 | if (!job.shouldDump(filterUidFinal)) { |
| 2982 | continue; |
| 2983 | } |
| 2984 | |
| 2985 | job.dump(proto, JobSchedulerServiceDumpProto.RegisteredJob.DUMP, true, nowElapsed); |
| 2986 | |
| 2987 | // isReadyToBeExecuted |
| 2988 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_JOB_READY, |
| 2989 | job.isReady()); |
| 2990 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_USER_STARTED, |
| 2991 | ArrayUtils.contains(mStartedUsers, job.getUserId())); |
| 2992 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_JOB_PENDING, |
| 2993 | mPendingJobs.contains(job)); |
| 2994 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_JOB_CURRENTLY_ACTIVE, |
| 2995 | isCurrentlyActiveLocked(job)); |
| 2996 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_UID_BACKING_UP, |
| 2997 | mBackingUpUids.indexOfKey(job.getSourceUid()) >= 0); |
| 2998 | boolean componentPresent = false; |
| 2999 | try { |
| 3000 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 3001 | job.getServiceComponent(), |
| 3002 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 3003 | job.getUserId()) != null); |
| 3004 | } catch (RemoteException e) { |
| 3005 | } |
| 3006 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_COMPONENT_PRESENT, |
| 3007 | componentPresent); |
| 3008 | |
| 3009 | proto.end(rjToken); |
| 3010 | } |
| 3011 | } |
| 3012 | for (StateController controller : mControllers) { |
| 3013 | controller.dumpControllerStateLocked( |
| 3014 | proto, JobSchedulerServiceDumpProto.CONTROLLERS, filterUidFinal); |
| 3015 | } |
| 3016 | for (int i=0; i< mUidPriorityOverride.size(); i++) { |
| 3017 | int uid = mUidPriorityOverride.keyAt(i); |
| 3018 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 3019 | long pToken = proto.start(JobSchedulerServiceDumpProto.PRIORITY_OVERRIDES); |
| 3020 | proto.write(JobSchedulerServiceDumpProto.PriorityOverride.UID, uid); |
| 3021 | proto.write(JobSchedulerServiceDumpProto.PriorityOverride.OVERRIDE_VALUE, |
| 3022 | mUidPriorityOverride.valueAt(i)); |
| 3023 | proto.end(pToken); |
| 3024 | } |
| 3025 | } |
| 3026 | for (int i = 0; i < mBackingUpUids.size(); i++) { |
| 3027 | int uid = mBackingUpUids.keyAt(i); |
| 3028 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 3029 | proto.write(JobSchedulerServiceDumpProto.BACKING_UP_UIDS, uid); |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | mJobPackageTracker.dump(proto, JobSchedulerServiceDumpProto.PACKAGE_TRACKER, |
| 3034 | filterUidFinal); |
| 3035 | mJobPackageTracker.dumpHistory(proto, JobSchedulerServiceDumpProto.HISTORY, |
| 3036 | filterUidFinal); |
| 3037 | |
| 3038 | for (JobStatus job : mPendingJobs) { |
| 3039 | final long pjToken = proto.start(JobSchedulerServiceDumpProto.PENDING_JOBS); |
| 3040 | |
| 3041 | job.writeToShortProto(proto, PendingJob.INFO); |
| 3042 | job.dump(proto, PendingJob.DUMP, false, nowElapsed); |
| 3043 | int priority = evaluateJobPriorityLocked(job); |
| 3044 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 3045 | proto.write(PendingJob.EVALUATED_PRIORITY, priority); |
| 3046 | } |
| 3047 | proto.write(PendingJob.ENQUEUED_DURATION_MS, nowUptime - job.madePending); |
| 3048 | |
| 3049 | proto.end(pjToken); |
| 3050 | } |
| 3051 | for (JobServiceContext jsc : mActiveServices) { |
| 3052 | final long ajToken = proto.start(JobSchedulerServiceDumpProto.ACTIVE_JOBS); |
| 3053 | final JobStatus job = jsc.getRunningJobLocked(); |
| 3054 | |
| 3055 | if (job == null) { |
| 3056 | final long ijToken = proto.start(ActiveJob.INACTIVE); |
| 3057 | |
| 3058 | proto.write(ActiveJob.InactiveJob.TIME_SINCE_STOPPED_MS, |
| 3059 | nowElapsed - jsc.mStoppedTime); |
| 3060 | if (jsc.mStoppedReason != null) { |
| 3061 | proto.write(ActiveJob.InactiveJob.STOPPED_REASON, |
| 3062 | jsc.mStoppedReason); |
| 3063 | } |
| 3064 | |
| 3065 | proto.end(ijToken); |
| 3066 | } else { |
| 3067 | final long rjToken = proto.start(ActiveJob.RUNNING); |
| 3068 | |
| 3069 | job.writeToShortProto(proto, ActiveJob.RunningJob.INFO); |
| 3070 | |
| 3071 | proto.write(ActiveJob.RunningJob.RUNNING_DURATION_MS, |
| 3072 | nowElapsed - jsc.getExecutionStartTimeElapsed()); |
| 3073 | proto.write(ActiveJob.RunningJob.TIME_UNTIL_TIMEOUT_MS, |
| 3074 | jsc.getTimeoutElapsed() - nowElapsed); |
| 3075 | |
| 3076 | job.dump(proto, ActiveJob.RunningJob.DUMP, false, nowElapsed); |
| 3077 | |
| 3078 | int priority = evaluateJobPriorityLocked(jsc.getRunningJobLocked()); |
| 3079 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 3080 | proto.write(ActiveJob.RunningJob.EVALUATED_PRIORITY, priority); |
| 3081 | } |
| 3082 | |
| 3083 | proto.write(ActiveJob.RunningJob.TIME_SINCE_MADE_ACTIVE_MS, |
| 3084 | nowUptime - job.madeActive); |
| 3085 | proto.write(ActiveJob.RunningJob.PENDING_DURATION_MS, |
| 3086 | job.madeActive - job.madePending); |
| 3087 | |
| 3088 | proto.end(rjToken); |
| 3089 | } |
| 3090 | proto.end(ajToken); |
| 3091 | } |
| 3092 | if (filterUid == -1) { |
| 3093 | proto.write(JobSchedulerServiceDumpProto.IS_READY_TO_ROCK, mReadyToRock); |
| 3094 | proto.write(JobSchedulerServiceDumpProto.REPORTED_ACTIVE, mReportedActive); |
| 3095 | proto.write(JobSchedulerServiceDumpProto.MAX_ACTIVE_JOBS, mMaxActiveJobs); |
| 3096 | } |
| 3097 | } |
| 3098 | |
| 3099 | proto.flush(); |
| 3100 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 3101 | } |