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