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; |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [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 | 2f0b031 | 2018-02-08 02:30:27 +0000 | [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 | 2f0b031 | 2018-02-08 02:30:27 +0000 | [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 | /** |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 790 | * Return whether an UID is in the foreground or not. |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 791 | */ |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 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; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 803 | private final Predicate<Integer> mIsUidInForegroundPredicate = this::isUidInForeground; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 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. |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 829 | toCancel.maybeAddForegroundExemption(mIsUidInForegroundPredicate); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 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. |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 841 | jobStatus.maybeAddForegroundExemption(mIsUidInForegroundPredicate); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 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, |
Bookatz | 9086762 | 2018-01-31 15:05:57 -0800 | [diff] [blame] | 865 | uId, null, jobStatus.getBatteryName(), |
| 866 | StatsLog.SCHEDULED_JOB_STATE_CHANGED__STATE__SCHEDULED); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 867 | |
| 868 | // If the job is immediately ready to run, then we can just immediately |
| 869 | // put it in the pending list and try to schedule it. This is especially |
| 870 | // important for jobs with a 0 deadline constraint, since they will happen a fair |
| 871 | // amount, we want to handle them as quickly as possible, and semantically we want to |
| 872 | // make sure we have started holding the wake lock for the job before returning to |
| 873 | // the caller. |
| 874 | // If the job is not yet ready to run, there is nothing more to do -- we are |
| 875 | // now just waiting for one of its controllers to change state and schedule |
| 876 | // the job appropriately. |
| 877 | if (isReadyToBeExecutedLocked(jobStatus)) { |
| 878 | // This is a new job, we can just immediately put it on the pending |
| 879 | // list and try to run it. |
| 880 | mJobPackageTracker.notePending(jobStatus); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 881 | addOrderedItem(mPendingJobs, jobStatus, mEnqueueTimeComparator); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 882 | maybeRunPendingJobsLocked(); |
| 883 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 884 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 885 | return JobScheduler.RESULT_SUCCESS; |
| 886 | } |
| 887 | |
| 888 | public List<JobInfo> getPendingJobs(int uid) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 889 | synchronized (mLock) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 890 | List<JobStatus> jobs = mJobs.getJobsByUid(uid); |
| 891 | ArrayList<JobInfo> outList = new ArrayList<JobInfo>(jobs.size()); |
| 892 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 893 | JobStatus job = jobs.get(i); |
| 894 | outList.add(job.getJob()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 895 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 896 | return outList; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 897 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 900 | public JobInfo getPendingJob(int uid, int jobId) { |
| 901 | synchronized (mLock) { |
| 902 | List<JobStatus> jobs = mJobs.getJobsByUid(uid); |
| 903 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 904 | JobStatus job = jobs.get(i); |
| 905 | if (job.getJobId() == jobId) { |
| 906 | return job.getJob(); |
| 907 | } |
| 908 | } |
| 909 | return null; |
| 910 | } |
| 911 | } |
| 912 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 913 | void cancelJobsForUser(int userHandle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 914 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 915 | final List<JobStatus> jobsForUser = mJobs.getJobsByUser(userHandle); |
| 916 | for (int i=0; i<jobsForUser.size(); i++) { |
| 917 | JobStatus toRemove = jobsForUser.get(i); |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 918 | cancelJobImplLocked(toRemove, null, "user removed"); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 919 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
Michael Wachenschwanz | 3f2b655 | 2017-05-15 13:53:09 -0700 | [diff] [blame] | 923 | private void cancelJobsForNonExistentUsers() { |
| 924 | UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class); |
| 925 | synchronized (mLock) { |
| 926 | mJobs.removeJobsOfNonUsers(umi.getUserIds()); |
| 927 | } |
| 928 | } |
| 929 | |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 930 | void cancelJobsForPackageAndUid(String pkgName, int uid, String reason) { |
| 931 | if ("android".equals(pkgName)) { |
| 932 | Slog.wtfStack(TAG, "Can't cancel all jobs for system package"); |
| 933 | return; |
| 934 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 935 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 936 | final List<JobStatus> jobsForUid = mJobs.getJobsByUid(uid); |
| 937 | for (int i = jobsForUid.size() - 1; i >= 0; i--) { |
| 938 | final JobStatus job = jobsForUid.get(i); |
| 939 | if (job.getSourcePackageName().equals(pkgName)) { |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 940 | cancelJobImplLocked(job, null, reason); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 941 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 946 | /** |
| 947 | * Entry point from client to cancel all jobs originating from their uid. |
| 948 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 949 | * execution or being executed. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 950 | * @param uid Uid to check against for removal of a job. |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 951 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 952 | */ |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 953 | public boolean cancelJobsForUid(int uid, String reason) { |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 954 | if (uid == Process.SYSTEM_UID) { |
Makoto Onuki | e7b9618 | 2017-08-30 14:53:16 -0700 | [diff] [blame] | 955 | Slog.wtfStack(TAG, "Can't cancel all jobs for system uid"); |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 956 | return false; |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 957 | } |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 958 | |
| 959 | boolean jobsCanceled = false; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 960 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 961 | final List<JobStatus> jobsForUid = mJobs.getJobsByUid(uid); |
| 962 | for (int i=0; i<jobsForUid.size(); i++) { |
| 963 | JobStatus toRemove = jobsForUid.get(i); |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 964 | cancelJobImplLocked(toRemove, null, reason); |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 965 | jobsCanceled = true; |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 966 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 967 | } |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 968 | return jobsCanceled; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /** |
| 972 | * Entry point from client to cancel the job corresponding to the jobId provided. |
| 973 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 974 | * execution or being executed. |
| 975 | * @param uid Uid of the calling client. |
| 976 | * @param jobId Id of the job, provided at schedule-time. |
| 977 | */ |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 978 | public boolean cancelJob(int uid, int jobId, int callingUid) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 979 | JobStatus toCancel; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 980 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 981 | toCancel = mJobs.getJobByUidAndJobId(uid, jobId); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 982 | if (toCancel != null) { |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 983 | cancelJobImplLocked(toCancel, null, |
| 984 | "cancel() called by app, callingUid=" + callingUid |
| 985 | + " uid=" + uid + " jobId=" + jobId); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 986 | } |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 987 | return (toCancel != null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 991 | private void cancelJobImplLocked(JobStatus cancelled, JobStatus incomingJob, String reason) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 992 | if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString()); |
| 993 | cancelled.unprepareLocked(ActivityManager.getService()); |
| 994 | stopTrackingJobLocked(cancelled, incomingJob, true /* writeBack */); |
| 995 | // Remove from pending queue. |
| 996 | if (mPendingJobs.remove(cancelled)) { |
| 997 | mJobPackageTracker.noteNonpending(cancelled); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 998 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 999 | // Cancel if running. |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1000 | stopJobOnServiceContextLocked(cancelled, JobParameters.REASON_CANCELED, reason); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1001 | reportActiveLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1004 | void updateUidState(int uid, int procState) { |
| 1005 | synchronized (mLock) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1006 | if (procState == ActivityManager.PROCESS_STATE_TOP) { |
| 1007 | // Only use this if we are exactly the top app. All others can live |
| 1008 | // with just the foreground priority. This means that persistent processes |
| 1009 | // can never be the top app priority... that is fine. |
| 1010 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_TOP_APP); |
Dianne Hackborn | 10fc4fd | 2017-12-19 17:23:13 -0800 | [diff] [blame] | 1011 | } else if (procState <= ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1012 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_FOREGROUND_APP); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1013 | } else { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1014 | mUidPriorityOverride.delete(uid); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1019 | @Override |
| 1020 | public void onDeviceIdleStateChanged(boolean deviceIdle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1021 | synchronized (mLock) { |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1022 | if (deviceIdle) { |
Jeff Sharkey | 34618b5 | 2016-06-01 15:51:19 -0600 | [diff] [blame] | 1023 | // When becoming idle, make sure no jobs are actively running, |
| 1024 | // except those using the idle exemption flag. |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1025 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1026 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1027 | final JobStatus executing = jsc.getRunningJobLocked(); |
Jeff Sharkey | 34618b5 | 2016-06-01 15:51:19 -0600 | [diff] [blame] | 1028 | if (executing != null |
| 1029 | && (executing.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) == 0) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1030 | jsc.cancelExecutingJobLocked(JobParameters.REASON_DEVICE_IDLE, |
| 1031 | "cancelled due to doze"); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1032 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1033 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1034 | } else { |
| 1035 | // When coming out of idle, allow thing to start back up. |
| 1036 | if (mReadyToRock) { |
| 1037 | if (mLocalDeviceIdleController != null) { |
| 1038 | if (!mReportedActive) { |
| 1039 | mReportedActive = true; |
| 1040 | mLocalDeviceIdleController.setJobsActive(true); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1041 | } |
| 1042 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1043 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1049 | void reportActiveLocked() { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1050 | // active is true if pending queue contains jobs OR some job is running. |
| 1051 | boolean active = mPendingJobs.size() > 0; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1052 | if (mPendingJobs.size() <= 0) { |
| 1053 | for (int i=0; i<mActiveServices.size(); i++) { |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 1054 | final JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1055 | final JobStatus job = jsc.getRunningJobLocked(); |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 1056 | if (job != null |
| 1057 | && (job.getJob().getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) == 0 |
| 1058 | && !job.dozeWhitelisted) { |
| 1059 | // We will report active if we have a job running and it is not an exception |
| 1060 | // due to being in the foreground or whitelisted. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1061 | active = true; |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (mReportedActive != active) { |
| 1068 | mReportedActive = active; |
| 1069 | if (mLocalDeviceIdleController != null) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1070 | mLocalDeviceIdleController.setJobsActive(active); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 1075 | void reportAppUsage(String packageName, int userId) { |
| 1076 | // This app just transitioned into interactive use or near equivalent, so we should |
| 1077 | // take a look at its job state for feedback purposes. |
| 1078 | } |
| 1079 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1080 | /** |
| 1081 | * Initializes the system service. |
| 1082 | * <p> |
| 1083 | * Subclasses must define a single argument constructor that accepts the context |
| 1084 | * and passes it to super. |
| 1085 | * </p> |
| 1086 | * |
| 1087 | * @param context The system server context. |
| 1088 | */ |
| 1089 | public JobSchedulerService(Context context) { |
| 1090 | super(context); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1091 | |
| 1092 | mLocalPM = LocalServices.getService(PackageManagerInternal.class); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1093 | mActivityManagerInternal = Preconditions.checkNotNull( |
| 1094 | LocalServices.getService(ActivityManagerInternal.class)); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1095 | |
Dianne Hackborn | 970e3f4 | 2016-06-01 10:55:13 -0700 | [diff] [blame] | 1096 | mHandler = new JobHandler(context.getMainLooper()); |
| 1097 | mConstants = new Constants(mHandler); |
| 1098 | mJobSchedulerStub = new JobSchedulerStub(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1099 | |
| 1100 | // Set up the app standby bucketing tracker |
| 1101 | UsageStatsManagerInternal usageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
| 1102 | mStandbyTracker = new StandbyTracker(usageStats); |
| 1103 | usageStats.addAppIdleStateChangeListener(mStandbyTracker); |
| 1104 | |
| 1105 | // The job store needs to call back |
| 1106 | publishLocalService(JobSchedulerInternal.class, new LocalService()); |
| 1107 | |
| 1108 | // Initialize the job store and set up any persisted jobs |
Dianne Hackborn | 970e3f4 | 2016-06-01 10:55:13 -0700 | [diff] [blame] | 1109 | mJobs = JobStore.initAndGet(this); |
| 1110 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1111 | // Create the controllers. |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1112 | mControllers = new ArrayList<StateController>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1113 | mControllers.add(ConnectivityController.get(this)); |
| 1114 | mControllers.add(TimeController.get(this)); |
| 1115 | mControllers.add(IdleController.get(this)); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 1116 | mBatteryController = BatteryController.get(this); |
| 1117 | mControllers.add(mBatteryController); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 1118 | mStorageController = StorageController.get(this); |
| 1119 | mControllers.add(mStorageController); |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 1120 | mControllers.add(BackgroundJobsController.get(this)); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 1121 | mControllers.add(AppIdleController.get(this)); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1122 | mControllers.add(ContentObserverController.get(this)); |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 1123 | mDeviceIdleJobsController = DeviceIdleJobsController.get(this); |
| 1124 | mControllers.add(mDeviceIdleJobsController); |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 1125 | |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 1126 | mForceAppStandbyTracker = ForceAppStandbyTracker.getInstance(context); |
| 1127 | |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 1128 | // If the job store determined that it can't yet reschedule persisted jobs, |
| 1129 | // we need to start watching the clock. |
| 1130 | if (!mJobs.jobTimesInflatedValid()) { |
| 1131 | Slog.w(TAG, "!!! RTC not yet good; tracking time updates for job scheduling"); |
| 1132 | context.registerReceiver(mTimeSetReceiver, new IntentFilter(Intent.ACTION_TIME_CHANGED)); |
| 1133 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 1136 | private final BroadcastReceiver mTimeSetReceiver = new BroadcastReceiver() { |
| 1137 | @Override |
| 1138 | public void onReceive(Context context, Intent intent) { |
| 1139 | if (Intent.ACTION_TIME_CHANGED.equals(intent.getAction())) { |
| 1140 | // When we reach clock sanity, recalculate the temporal windows |
| 1141 | // of all affected jobs. |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1142 | if (mJobs.clockNowValidToInflate(sSystemClock.millis())) { |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 1143 | Slog.i(TAG, "RTC now valid; recalculating persisted job windows"); |
| 1144 | |
| 1145 | // We've done our job now, so stop watching the time. |
| 1146 | context.unregisterReceiver(this); |
| 1147 | |
| 1148 | // And kick off the work to update the affected jobs, using a secondary |
| 1149 | // thread instead of chugging away here on the main looper thread. |
| 1150 | FgThread.getHandler().post(mJobTimeUpdater); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | }; |
| 1155 | |
| 1156 | private final Runnable mJobTimeUpdater = () -> { |
| 1157 | final ArrayList<JobStatus> toRemove = new ArrayList<>(); |
| 1158 | final ArrayList<JobStatus> toAdd = new ArrayList<>(); |
| 1159 | synchronized (mLock) { |
| 1160 | // Note: we intentionally both look up the existing affected jobs and replace them |
| 1161 | // with recalculated ones inside the same lock lifetime. |
| 1162 | getJobStore().getRtcCorrectedJobsLocked(toAdd, toRemove); |
| 1163 | |
| 1164 | // Now, at each position [i], we have both the existing JobStatus |
| 1165 | // and the one that replaces it. |
| 1166 | final int N = toAdd.size(); |
| 1167 | for (int i = 0; i < N; i++) { |
| 1168 | final JobStatus oldJob = toRemove.get(i); |
| 1169 | final JobStatus newJob = toAdd.get(i); |
| 1170 | if (DEBUG) { |
| 1171 | Slog.v(TAG, " replacing " + oldJob + " with " + newJob); |
| 1172 | } |
| 1173 | cancelJobImplLocked(oldJob, newJob, "deferred rtc calculation"); |
| 1174 | } |
| 1175 | } |
| 1176 | }; |
| 1177 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1178 | @Override |
| 1179 | public void onStart() { |
| 1180 | publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub); |
| 1181 | } |
| 1182 | |
| 1183 | @Override |
| 1184 | public void onBootPhase(int phase) { |
| 1185 | if (PHASE_SYSTEM_SERVICES_READY == phase) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1186 | mConstants.start(getContext().getContentResolver()); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1187 | |
Makoto Onuki | 2f0b031 | 2018-02-08 02:30:27 +0000 | [diff] [blame] | 1188 | mForceAppStandbyTracker.start(); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1189 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1190 | // Register br for package removals and user removals. |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 1191 | final IntentFilter filter = new IntentFilter(); |
| 1192 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1193 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 1194 | filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1195 | filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1196 | filter.addDataScheme("package"); |
| 1197 | getContext().registerReceiverAsUser( |
| 1198 | mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
| 1199 | final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED); |
| 1200 | getContext().registerReceiverAsUser( |
| 1201 | mBroadcastReceiver, UserHandle.ALL, userFilter, null, null); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1202 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1203 | ActivityManager.getService().registerUidObserver(mUidObserver, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1204 | ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1205 | | ActivityManager.UID_OBSERVER_IDLE | ActivityManager.UID_OBSERVER_ACTIVE, |
| 1206 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1207 | } catch (RemoteException e) { |
| 1208 | // ignored; both services live in system_server |
| 1209 | } |
Michael Wachenschwanz | 3f2b655 | 2017-05-15 13:53:09 -0700 | [diff] [blame] | 1210 | // Remove any jobs that are not associated with any of the current users. |
| 1211 | cancelJobsForNonExistentUsers(); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1212 | } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1213 | synchronized (mLock) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1214 | // Let's go! |
| 1215 | mReadyToRock = true; |
| 1216 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 1217 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1218 | mLocalDeviceIdleController |
| 1219 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1220 | // Create the "runners". |
| 1221 | for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) { |
| 1222 | mActiveServices.add( |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1223 | new JobServiceContext(this, mBatteryStats, mJobPackageTracker, |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1224 | getContext().getMainLooper())); |
| 1225 | } |
| 1226 | // Attach jobs to their controllers. |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1227 | mJobs.forEachJob(new JobStatusFunctor() { |
| 1228 | @Override |
| 1229 | public void process(JobStatus job) { |
| 1230 | for (int controller = 0; controller < mControllers.size(); controller++) { |
| 1231 | final StateController sc = mControllers.get(controller); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1232 | sc.maybeStartTrackingJobLocked(job, null); |
| 1233 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1234 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1235 | }); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1236 | // GO GO GO! |
| 1237 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 1238 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Called when we have a job status object that we need to insert in our |
| 1244 | * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know |
| 1245 | * about. |
| 1246 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1247 | private void startTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) { |
| 1248 | if (!jobStatus.isPreparedLocked()) { |
| 1249 | Slog.wtf(TAG, "Not yet prepared when started tracking: " + jobStatus); |
| 1250 | } |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1251 | jobStatus.enqueueTime = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1252 | final boolean update = mJobs.add(jobStatus); |
| 1253 | if (mReadyToRock) { |
| 1254 | for (int i = 0; i < mControllers.size(); i++) { |
| 1255 | StateController controller = mControllers.get(i); |
| 1256 | if (update) { |
| 1257 | controller.maybeStopTrackingJobLocked(jobStatus, null, true); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1258 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1259 | controller.maybeStartTrackingJobLocked(jobStatus, lastJob); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1260 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * Called when we want to remove a JobStatus object that we've finished executing. Returns the |
| 1266 | * object removed. |
| 1267 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1268 | private boolean stopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 1269 | boolean writeBack) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1270 | // Deal with any remaining work items in the old job. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 1271 | jobStatus.stopTrackingJobLocked(ActivityManager.getService(), incomingJob); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1272 | |
| 1273 | // Remove from store as well as controllers. |
| 1274 | final boolean removed = mJobs.remove(jobStatus, writeBack); |
| 1275 | if (removed && mReadyToRock) { |
| 1276 | for (int i=0; i<mControllers.size(); i++) { |
| 1277 | StateController controller = mControllers.get(i); |
| 1278 | controller.maybeStopTrackingJobLocked(jobStatus, incomingJob, false); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1279 | } |
| 1280 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1281 | return removed; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1284 | private boolean stopJobOnServiceContextLocked(JobStatus job, int reason, String debugReason) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1285 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1286 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1287 | final JobStatus executing = jsc.getRunningJobLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1288 | if (executing != null && executing.matches(job.getUid(), job.getJobId())) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1289 | jsc.cancelExecutingJobLocked(reason, debugReason); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1290 | return true; |
| 1291 | } |
| 1292 | } |
| 1293 | return false; |
| 1294 | } |
| 1295 | |
| 1296 | /** |
| 1297 | * @param job JobStatus we are querying against. |
| 1298 | * @return Whether or not the job represented by the status object is currently being run or |
| 1299 | * is pending. |
| 1300 | */ |
| 1301 | private boolean isCurrentlyActiveLocked(JobStatus job) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1302 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1303 | JobServiceContext serviceContext = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1304 | final JobStatus running = serviceContext.getRunningJobLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1305 | if (running != null && running.matches(job.getUid(), job.getJobId())) { |
| 1306 | return true; |
| 1307 | } |
| 1308 | } |
| 1309 | return false; |
| 1310 | } |
| 1311 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1312 | void noteJobsPending(List<JobStatus> jobs) { |
| 1313 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 1314 | JobStatus job = jobs.get(i); |
| 1315 | mJobPackageTracker.notePending(job); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | void noteJobsNonpending(List<JobStatus> jobs) { |
| 1320 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 1321 | JobStatus job = jobs.get(i); |
| 1322 | mJobPackageTracker.noteNonpending(job); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1326 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1327 | * Reschedules the given job based on the job's backoff policy. It doesn't make sense to |
| 1328 | * specify an override deadline on a failed job (the failed job will run even though it's not |
| 1329 | * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any |
| 1330 | * ready job with {@link JobStatus#numFailures} > 0 will be executed. |
| 1331 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1332 | * @param failureToReschedule Provided job status that we will reschedule. |
| 1333 | * @return A newly instantiated JobStatus with the same constraints as the last job except |
| 1334 | * with adjusted timing constraints. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1335 | * |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1336 | * @see #maybeQueueReadyJobsForExecutionLocked |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1337 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1338 | private JobStatus getRescheduleJobForFailureLocked(JobStatus failureToReschedule) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1339 | final long elapsedNowMillis = sElapsedRealtimeClock.millis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1340 | final JobInfo job = failureToReschedule.getJob(); |
| 1341 | |
| 1342 | final long initialBackoffMillis = job.getInitialBackoffMillis(); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1343 | final int backoffAttempts = failureToReschedule.getNumFailures() + 1; |
| 1344 | long delayMillis; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1345 | |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1346 | if (failureToReschedule.hasWorkLocked()) { |
| 1347 | if (backoffAttempts > mConstants.MAX_WORK_RESCHEDULE_COUNT) { |
| 1348 | Slog.w(TAG, "Not rescheduling " + failureToReschedule + ": attempt #" |
| 1349 | + backoffAttempts + " > work limit " |
| 1350 | + mConstants.MAX_STANDARD_RESCHEDULE_COUNT); |
| 1351 | return null; |
| 1352 | } |
| 1353 | } else if (backoffAttempts > mConstants.MAX_STANDARD_RESCHEDULE_COUNT) { |
| 1354 | Slog.w(TAG, "Not rescheduling " + failureToReschedule + ": attempt #" |
| 1355 | + backoffAttempts + " > std limit " + mConstants.MAX_STANDARD_RESCHEDULE_COUNT); |
| 1356 | return null; |
| 1357 | } |
| 1358 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1359 | switch (job.getBackoffPolicy()) { |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1360 | case JobInfo.BACKOFF_POLICY_LINEAR: { |
| 1361 | long backoff = initialBackoffMillis; |
| 1362 | if (backoff < mConstants.MIN_LINEAR_BACKOFF_TIME) { |
| 1363 | backoff = mConstants.MIN_LINEAR_BACKOFF_TIME; |
| 1364 | } |
| 1365 | delayMillis = backoff * backoffAttempts; |
| 1366 | } break; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1367 | default: |
| 1368 | if (DEBUG) { |
| 1369 | Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential."); |
| 1370 | } |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1371 | case JobInfo.BACKOFF_POLICY_EXPONENTIAL: { |
| 1372 | long backoff = initialBackoffMillis; |
| 1373 | if (backoff < mConstants.MIN_EXP_BACKOFF_TIME) { |
| 1374 | backoff = mConstants.MIN_EXP_BACKOFF_TIME; |
| 1375 | } |
| 1376 | delayMillis = (long) Math.scalb(backoff, backoffAttempts - 1); |
| 1377 | } break; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1378 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1379 | delayMillis = |
| 1380 | Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1381 | JobStatus newJob = new JobStatus(failureToReschedule, getCurrentHeartbeat(), |
| 1382 | elapsedNowMillis + delayMillis, |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 1383 | JobStatus.NO_LATEST_RUNTIME, backoffAttempts, |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1384 | failureToReschedule.getLastSuccessfulRunTime(), sSystemClock.millis()); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1385 | for (int ic=0; ic<mControllers.size(); ic++) { |
| 1386 | StateController controller = mControllers.get(ic); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1387 | controller.rescheduleForFailureLocked(newJob, failureToReschedule); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1388 | } |
| 1389 | return newJob; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1393 | * Called after a periodic has executed so we can reschedule it. We take the last execution |
| 1394 | * time of the job to be the time of completion (i.e. the time at which this function is |
| 1395 | * called). |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1396 | * <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] | 1397 | * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead |
| 1398 | * to underscheduling at least, rather than if we had taken the last execution time to be the |
| 1399 | * start of the execution. |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1400 | * <p>Unlike a reschedule prior to execution, in this case we advance the next-heartbeat |
| 1401 | * tracking as though the job were newly-scheduled. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1402 | * @return A new job representing the execution criteria for this instantiation of the |
| 1403 | * recurring job. |
| 1404 | */ |
| 1405 | private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1406 | final long elapsedNow = sElapsedRealtimeClock.millis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1407 | // Compute how much of the period is remaining. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1408 | long runEarly = 0L; |
| 1409 | |
| 1410 | // If this periodic was rescheduled it won't have a deadline. |
| 1411 | if (periodicToReschedule.hasDeadlineConstraint()) { |
| 1412 | runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L); |
| 1413 | } |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 1414 | long flex = periodicToReschedule.getJob().getFlexMillis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1415 | long period = periodicToReschedule.getJob().getIntervalMillis(); |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 1416 | long newLatestRuntimeElapsed = elapsedNow + runEarly + period; |
| 1417 | long newEarliestRunTimeElapsed = newLatestRuntimeElapsed - flex; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1418 | |
| 1419 | if (DEBUG) { |
| 1420 | Slog.v(TAG, "Rescheduling executed periodic. New execution window [" + |
| 1421 | newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s"); |
| 1422 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1423 | return new JobStatus(periodicToReschedule, getCurrentHeartbeat(), |
| 1424 | newEarliestRunTimeElapsed, newLatestRuntimeElapsed, |
| 1425 | 0 /* backoffAttempt */, |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1426 | sSystemClock.millis() /* lastSuccessfulRunTime */, |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 1427 | periodicToReschedule.getLastFailedRunTime()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | // JobCompletedListener implementations. |
| 1431 | |
| 1432 | /** |
| 1433 | * A job just finished executing. We fetch the |
| 1434 | * {@link com.android.server.job.controllers.JobStatus} from the store and depending on |
| 1435 | * whether we want to reschedule we readd it to the controllers. |
| 1436 | * @param jobStatus Completed job. |
| 1437 | * @param needsReschedule Whether the implementing class should reschedule this job. |
| 1438 | */ |
| 1439 | @Override |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1440 | public void onJobCompletedLocked(JobStatus jobStatus, boolean needsReschedule) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1441 | if (DEBUG) { |
| 1442 | Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule); |
| 1443 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1444 | |
| 1445 | // If the job wants to be rescheduled, we first need to make the next upcoming |
| 1446 | // job so we can transfer any appropriate state over from the previous job when |
| 1447 | // we stop it. |
| 1448 | final JobStatus rescheduledJob = needsReschedule |
| 1449 | ? getRescheduleJobForFailureLocked(jobStatus) : null; |
| 1450 | |
Shreyas Basarge | 73f1025 | 2016-02-11 17:06:13 +0000 | [diff] [blame] | 1451 | // Do not write back immediately if this is a periodic job. The job may get lost if system |
| 1452 | // shuts down before it is added back. |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1453 | if (!stopTrackingJobLocked(jobStatus, rescheduledJob, !jobStatus.getJob().isPeriodic())) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1454 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 1455 | 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] | 1456 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1457 | // We still want to check for jobs to execute, because this job may have |
| 1458 | // scheduled a new job under the same job id, and now we can run it. |
| 1459 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1460 | return; |
| 1461 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1462 | |
| 1463 | if (rescheduledJob != null) { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1464 | try { |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1465 | rescheduledJob.prepareLocked(ActivityManager.getService()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1466 | } catch (SecurityException e) { |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1467 | Slog.w(TAG, "Unable to regrant job permissions for " + rescheduledJob); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1468 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1469 | startTrackingJobLocked(rescheduledJob, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1470 | } else if (jobStatus.getJob().isPeriodic()) { |
| 1471 | JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1472 | try { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1473 | rescheduledPeriodic.prepareLocked(ActivityManager.getService()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1474 | } catch (SecurityException e) { |
| 1475 | Slog.w(TAG, "Unable to regrant job permissions for " + rescheduledPeriodic); |
| 1476 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1477 | startTrackingJobLocked(rescheduledPeriodic, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1478 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1479 | jobStatus.unprepareLocked(ActivityManager.getService()); |
| 1480 | reportActiveLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1481 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | // StateChangedListener implementations. |
| 1485 | |
| 1486 | /** |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1487 | * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that |
| 1488 | * some controller's state has changed, so as to run through the list of jobs and start/stop |
| 1489 | * any that are eligible. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1490 | */ |
| 1491 | @Override |
| 1492 | public void onControllerStateChanged() { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1493 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | @Override |
| 1497 | public void onRunJobNow(JobStatus jobStatus) { |
| 1498 | mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget(); |
| 1499 | } |
| 1500 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1501 | final private class JobHandler extends Handler { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1502 | |
| 1503 | public JobHandler(Looper looper) { |
| 1504 | super(looper); |
| 1505 | } |
| 1506 | |
| 1507 | @Override |
| 1508 | public void handleMessage(Message message) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1509 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1510 | if (!mReadyToRock) { |
| 1511 | return; |
| 1512 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1513 | switch (message.what) { |
| 1514 | case MSG_JOB_EXPIRED: { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1515 | JobStatus runNow = (JobStatus) message.obj; |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 1516 | // runNow can be null, which is a controller's way of indicating that its |
| 1517 | // state is such that all ready jobs should be run immediately. |
Christopher Tate | b1d6448 | 2017-03-15 12:01:22 -0700 | [diff] [blame] | 1518 | if (runNow != null && isReadyToBeExecutedLocked(runNow)) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1519 | mJobPackageTracker.notePending(runNow); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1520 | addOrderedItem(mPendingJobs, runNow, mEnqueueTimeComparator); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1521 | } else { |
| 1522 | queueReadyJobsForExecutionLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1523 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1524 | } break; |
| 1525 | case MSG_CHECK_JOB: |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1526 | if (mReportedActive) { |
| 1527 | // if jobs are currently being run, queue all ready jobs for execution. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1528 | queueReadyJobsForExecutionLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1529 | } else { |
| 1530 | // 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] | 1531 | maybeQueueReadyJobsForExecutionLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1532 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1533 | break; |
| 1534 | case MSG_CHECK_JOB_GREEDY: |
| 1535 | queueReadyJobsForExecutionLocked(); |
| 1536 | break; |
| 1537 | case MSG_STOP_JOB: |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1538 | cancelJobImplLocked((JobStatus) message.obj, null, |
| 1539 | "app no longer allowed to run"); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1540 | break; |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1541 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1542 | maybeRunPendingJobsLocked(); |
| 1543 | // Don't remove JOB_EXPIRED in case one came along while processing the queue. |
| 1544 | removeMessages(MSG_CHECK_JOB); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1545 | } |
| 1546 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1547 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1548 | |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 1549 | private void stopNonReadyActiveJobsLocked() { |
| 1550 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1551 | JobServiceContext serviceContext = mActiveServices.get(i); |
| 1552 | final JobStatus running = serviceContext.getRunningJobLocked(); |
| 1553 | if (running != null && !running.isReady()) { |
| 1554 | serviceContext.cancelExecutingJobLocked( |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 1555 | JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED, |
| 1556 | "cancelled due to unsatisfied constraints"); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1561 | /** |
| 1562 | * Run through list of jobs and execute all possible - at least one is expired so we do |
| 1563 | * as many as we can. |
| 1564 | */ |
| 1565 | private void queueReadyJobsForExecutionLocked() { |
| 1566 | if (DEBUG) { |
| 1567 | Slog.d(TAG, "queuing all ready jobs for execution:"); |
| 1568 | } |
| 1569 | noteJobsNonpending(mPendingJobs); |
| 1570 | mPendingJobs.clear(); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 1571 | stopNonReadyActiveJobsLocked(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1572 | boolean updated = updateStandbyHeartbeatLocked(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1573 | mJobs.forEachJob(mReadyQueueFunctor); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1574 | if (updated) updateNextStandbyHeartbeatsLocked(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1575 | mReadyQueueFunctor.postProcess(); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1576 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1577 | if (DEBUG) { |
| 1578 | final int queuedJobs = mPendingJobs.size(); |
| 1579 | if (queuedJobs == 0) { |
| 1580 | Slog.d(TAG, "No jobs pending."); |
| 1581 | } else { |
| 1582 | Slog.d(TAG, queuedJobs + " jobs queued."); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1583 | } |
| 1584 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1585 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1586 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1587 | final class ReadyJobQueueFunctor implements JobStatusFunctor { |
| 1588 | ArrayList<JobStatus> newReadyJobs; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1589 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1590 | @Override |
| 1591 | public void process(JobStatus job) { |
| 1592 | if (isReadyToBeExecutedLocked(job)) { |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1593 | if (DEBUG) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1594 | Slog.d(TAG, " queued " + job.toShortString()); |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1595 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1596 | if (newReadyJobs == null) { |
| 1597 | newReadyJobs = new ArrayList<JobStatus>(); |
| 1598 | } |
| 1599 | newReadyJobs.add(job); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1600 | } |
| 1601 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1602 | |
| 1603 | public void postProcess() { |
| 1604 | if (newReadyJobs != null) { |
| 1605 | noteJobsPending(newReadyJobs); |
| 1606 | mPendingJobs.addAll(newReadyJobs); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1607 | if (mPendingJobs.size() > 1) { |
| 1608 | mPendingJobs.sort(mEnqueueTimeComparator); |
| 1609 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1610 | } |
| 1611 | newReadyJobs = null; |
| 1612 | } |
| 1613 | } |
| 1614 | private final ReadyJobQueueFunctor mReadyQueueFunctor = new ReadyJobQueueFunctor(); |
| 1615 | |
| 1616 | /** |
| 1617 | * The state of at least one job has changed. Here is where we could enforce various |
| 1618 | * policies on when we want to execute jobs. |
| 1619 | * Right now the policy is such: |
| 1620 | * If >1 of the ready jobs is idle mode we send all of them off |
| 1621 | * if more than 2 network connectivity jobs are ready we send them all off. |
| 1622 | * If more than 4 jobs total are ready we send them all off. |
| 1623 | * TODO: It would be nice to consolidate these sort of high-level policies somewhere. |
| 1624 | */ |
| 1625 | final class MaybeReadyJobQueueFunctor implements JobStatusFunctor { |
| 1626 | int chargingCount; |
| 1627 | int batteryNotLowCount; |
| 1628 | int storageNotLowCount; |
| 1629 | int idleCount; |
| 1630 | int backoffCount; |
| 1631 | int connectivityCount; |
| 1632 | int contentCount; |
| 1633 | List<JobStatus> runnableJobs; |
| 1634 | |
| 1635 | public MaybeReadyJobQueueFunctor() { |
| 1636 | reset(); |
| 1637 | } |
| 1638 | |
| 1639 | // Functor method invoked for each job via JobStore.forEachJob() |
| 1640 | @Override |
| 1641 | public void process(JobStatus job) { |
| 1642 | if (isReadyToBeExecutedLocked(job)) { |
| 1643 | try { |
| 1644 | if (ActivityManager.getService().isAppStartModeDisabled(job.getUid(), |
| 1645 | job.getJob().getService().getPackageName())) { |
| 1646 | Slog.w(TAG, "Aborting job " + job.getUid() + ":" |
| 1647 | + job.getJob().toString() + " -- package not allowed to start"); |
| 1648 | mHandler.obtainMessage(MSG_STOP_JOB, job).sendToTarget(); |
| 1649 | return; |
| 1650 | } |
| 1651 | } catch (RemoteException e) { |
| 1652 | } |
| 1653 | if (job.getNumFailures() > 0) { |
| 1654 | backoffCount++; |
| 1655 | } |
| 1656 | if (job.hasIdleConstraint()) { |
| 1657 | idleCount++; |
| 1658 | } |
| 1659 | if (job.hasConnectivityConstraint()) { |
| 1660 | connectivityCount++; |
| 1661 | } |
| 1662 | if (job.hasChargingConstraint()) { |
| 1663 | chargingCount++; |
| 1664 | } |
| 1665 | if (job.hasBatteryNotLowConstraint()) { |
| 1666 | batteryNotLowCount++; |
| 1667 | } |
| 1668 | if (job.hasStorageNotLowConstraint()) { |
| 1669 | storageNotLowCount++; |
| 1670 | } |
| 1671 | if (job.hasContentTriggerConstraint()) { |
| 1672 | contentCount++; |
| 1673 | } |
| 1674 | if (runnableJobs == null) { |
| 1675 | runnableJobs = new ArrayList<>(); |
| 1676 | } |
| 1677 | runnableJobs.add(job); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | public void postProcess() { |
| 1682 | if (backoffCount > 0 || |
| 1683 | idleCount >= mConstants.MIN_IDLE_COUNT || |
| 1684 | connectivityCount >= mConstants.MIN_CONNECTIVITY_COUNT || |
| 1685 | chargingCount >= mConstants.MIN_CHARGING_COUNT || |
| 1686 | batteryNotLowCount >= mConstants.MIN_BATTERY_NOT_LOW_COUNT || |
| 1687 | storageNotLowCount >= mConstants.MIN_STORAGE_NOT_LOW_COUNT || |
| 1688 | contentCount >= mConstants.MIN_CONTENT_COUNT || |
| 1689 | (runnableJobs != null |
| 1690 | && runnableJobs.size() >= mConstants.MIN_READY_JOBS_COUNT)) { |
| 1691 | if (DEBUG) { |
| 1692 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLocked: Running jobs."); |
| 1693 | } |
| 1694 | noteJobsPending(runnableJobs); |
| 1695 | mPendingJobs.addAll(runnableJobs); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1696 | if (mPendingJobs.size() > 1) { |
| 1697 | mPendingJobs.sort(mEnqueueTimeComparator); |
| 1698 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1699 | } else { |
| 1700 | if (DEBUG) { |
| 1701 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLocked: Not running anything."); |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | // Be ready for next time |
| 1706 | reset(); |
| 1707 | } |
| 1708 | |
| 1709 | private void reset() { |
| 1710 | chargingCount = 0; |
| 1711 | idleCount = 0; |
| 1712 | backoffCount = 0; |
| 1713 | connectivityCount = 0; |
| 1714 | batteryNotLowCount = 0; |
| 1715 | storageNotLowCount = 0; |
| 1716 | contentCount = 0; |
| 1717 | runnableJobs = null; |
| 1718 | } |
| 1719 | } |
| 1720 | private final MaybeReadyJobQueueFunctor mMaybeQueueFunctor = new MaybeReadyJobQueueFunctor(); |
| 1721 | |
| 1722 | private void maybeQueueReadyJobsForExecutionLocked() { |
| 1723 | if (DEBUG) Slog.d(TAG, "Maybe queuing ready jobs..."); |
| 1724 | |
| 1725 | noteJobsNonpending(mPendingJobs); |
| 1726 | mPendingJobs.clear(); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 1727 | stopNonReadyActiveJobsLocked(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1728 | boolean updated = updateStandbyHeartbeatLocked(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1729 | mJobs.forEachJob(mMaybeQueueFunctor); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1730 | if (updated) updateNextStandbyHeartbeatsLocked(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1731 | mMaybeQueueFunctor.postProcess(); |
| 1732 | } |
| 1733 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1734 | private boolean updateStandbyHeartbeatLocked() { |
| 1735 | final long sinceLast = sElapsedRealtimeClock.millis() - mLastHeartbeatTime; |
| 1736 | final long beatsElapsed = sinceLast / mConstants.STANDBY_HEARTBEAT_TIME; |
| 1737 | if (beatsElapsed > 0) { |
| 1738 | mHeartbeat += beatsElapsed; |
| 1739 | mLastHeartbeatTime += beatsElapsed * mConstants.STANDBY_HEARTBEAT_TIME; |
| 1740 | if (DEBUG_STANDBY) { |
| 1741 | Slog.v(TAG, "Advancing standby heartbeat by " + beatsElapsed + " to " + mHeartbeat); |
| 1742 | } |
| 1743 | return true; |
| 1744 | } |
| 1745 | return false; |
| 1746 | } |
| 1747 | |
| 1748 | private void updateNextStandbyHeartbeatsLocked() { |
| 1749 | // don't update ACTIVE or NEVER bucket milestones |
| 1750 | for (int i = 1; i < mNextBucketHeartbeat.length - 1; i++) { |
| 1751 | while (mHeartbeat >= mNextBucketHeartbeat[i]) { |
| 1752 | mNextBucketHeartbeat[i] += mConstants.STANDBY_BEATS[i]; |
| 1753 | } |
| 1754 | if (DEBUG_STANDBY) { |
| 1755 | Slog.v(TAG, " Bucket " + i + " next heartbeat " + mNextBucketHeartbeat[i]); |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1760 | /** |
| 1761 | * Criteria for moving a job into the pending queue: |
| 1762 | * - It's ready. |
| 1763 | * - It's not pending. |
| 1764 | * - It's not already running on a JSC. |
| 1765 | * - The user that requested the job is running. |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1766 | * - The job's standby bucket has come due to be runnable. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1767 | * - The component is enabled and runnable. |
| 1768 | */ |
| 1769 | private boolean isReadyToBeExecutedLocked(JobStatus job) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1770 | final boolean jobReady = job.isReady(); |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 1771 | |
| 1772 | if (DEBUG) { |
| 1773 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1774 | + " ready=" + jobReady); |
| 1775 | } |
| 1776 | |
| 1777 | // This is a condition that is very likely to be false (most jobs that are |
| 1778 | // scheduled are sitting there, not ready yet) and very cheap to check (just |
| 1779 | // a few conditions on data in JobStatus). |
| 1780 | if (!jobReady) { |
| 1781 | return false; |
| 1782 | } |
| 1783 | |
| 1784 | final boolean jobExists = mJobs.containsJob(job); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1785 | |
| 1786 | final int userId = job.getUserId(); |
| 1787 | final boolean userStarted = ArrayUtils.contains(mStartedUsers, userId); |
| 1788 | |
| 1789 | if (DEBUG) { |
| 1790 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 1791 | + " exists=" + jobExists + " userStarted=" + userStarted); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 1794 | // These are also fairly cheap to check, though they typically will not |
| 1795 | // be conditions we fail. |
| 1796 | if (!jobExists || !userStarted) { |
| 1797 | return false; |
| 1798 | } |
| 1799 | |
| 1800 | final boolean jobPending = mPendingJobs.contains(job); |
| 1801 | final boolean jobActive = isCurrentlyActiveLocked(job); |
| 1802 | |
| 1803 | if (DEBUG) { |
| 1804 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1805 | + " pending=" + jobPending + " active=" + jobActive); |
| 1806 | } |
| 1807 | |
| 1808 | // These can be a little more expensive (especially jobActive, since we need to |
| 1809 | // go through the array of all potentially active jobs), so we are doing them |
| 1810 | // later... but still before checking with the package manager! |
| 1811 | if (jobPending || jobActive) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1812 | return false; |
| 1813 | } |
| 1814 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1815 | // 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] | 1816 | // an appropriate amount of time since the last invocation. During device- |
| 1817 | // wide parole, standby bucketing is ignored. |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 1818 | // |
| 1819 | // But if a job has FLAG_EXEMPT_FROM_APP_STANDBY, don't check it. |
| 1820 | if (!mInParole && !job.getJob().isExemptedFromAppStandby()) { |
Christopher Tate | a5a85bd | 2018-01-03 17:20:36 -0800 | [diff] [blame] | 1821 | final int bucket = job.getStandbyBucket(); |
| 1822 | if (mHeartbeat < mNextBucketHeartbeat[bucket]) { |
| 1823 | // Only skip this job if it's still waiting for the end of its (initial) nominal |
| 1824 | // bucket interval. Once it's waited that long, we let it go ahead and clear. |
| 1825 | // The final (NEVER) bucket is special; we never age those apps' jobs into |
| 1826 | // runnability. |
| 1827 | if (bucket >= mConstants.STANDBY_BEATS.length |
| 1828 | || (mHeartbeat < job.getBaseHeartbeat() + mConstants.STANDBY_BEATS[bucket])) { |
| 1829 | // TODO: log/trace that we're deferring the job due to bucketing if we hit this |
| 1830 | if (job.getWhenStandbyDeferred() == 0) { |
| 1831 | if (DEBUG_STANDBY) { |
| 1832 | Slog.v(TAG, "Bucket deferral: " + mHeartbeat + " < " |
| 1833 | + mNextBucketHeartbeat[job.getStandbyBucket()] + " for " + job); |
| 1834 | } |
| 1835 | job.setWhenStandbyDeferred(sElapsedRealtimeClock.millis()); |
Christopher Tate | 0c4d768 | 2017-12-06 15:10:22 -0800 | [diff] [blame] | 1836 | } |
Christopher Tate | a5a85bd | 2018-01-03 17:20:36 -0800 | [diff] [blame] | 1837 | return false; |
| 1838 | } else { |
| 1839 | if (DEBUG_STANDBY) { |
| 1840 | Slog.v(TAG, "Bucket deferred job aged into runnability at " |
| 1841 | + mHeartbeat + " : " + job); |
| 1842 | } |
Christopher Tate | 0c4d768 | 2017-12-06 15:10:22 -0800 | [diff] [blame] | 1843 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1844 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | // The expensive check last: validate that the defined package+service is |
| 1848 | // still present & viable. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1849 | final boolean componentPresent; |
| 1850 | try { |
| 1851 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 1852 | job.getServiceComponent(), PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 1853 | userId) != null); |
| 1854 | } catch (RemoteException e) { |
| 1855 | throw e.rethrowAsRuntimeException(); |
| 1856 | } |
| 1857 | |
| 1858 | if (DEBUG) { |
| 1859 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1860 | + " componentPresent=" + componentPresent); |
| 1861 | } |
| 1862 | |
| 1863 | // Everything else checked out so far, so this is the final yes/no check |
| 1864 | return componentPresent; |
| 1865 | } |
| 1866 | |
| 1867 | /** |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1868 | * Reconcile jobs in the pending queue against available execution contexts. |
| 1869 | * A controller can force a job into the pending queue even if it's already running, but |
| 1870 | * here is where we decide whether to actually execute it. |
| 1871 | */ |
| 1872 | private void maybeRunPendingJobsLocked() { |
| 1873 | if (DEBUG) { |
| 1874 | Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs."); |
| 1875 | } |
| 1876 | assignJobsToContextsLocked(); |
| 1877 | reportActiveLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1880 | private int adjustJobPriority(int curPriority, JobStatus job) { |
| 1881 | if (curPriority < JobInfo.PRIORITY_TOP_APP) { |
| 1882 | float factor = mJobPackageTracker.getLoadFactor(job); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1883 | if (factor >= mConstants.HEAVY_USE_FACTOR) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1884 | curPriority += JobInfo.PRIORITY_ADJ_ALWAYS_RUNNING; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1885 | } else if (factor >= mConstants.MODERATE_USE_FACTOR) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1886 | curPriority += JobInfo.PRIORITY_ADJ_OFTEN_RUNNING; |
| 1887 | } |
| 1888 | } |
| 1889 | return curPriority; |
| 1890 | } |
| 1891 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1892 | private int evaluateJobPriorityLocked(JobStatus job) { |
| 1893 | int priority = job.getPriority(); |
| 1894 | if (priority >= JobInfo.PRIORITY_FOREGROUND_APP) { |
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 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1897 | int override = mUidPriorityOverride.get(job.getSourceUid(), 0); |
| 1898 | if (override != 0) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1899 | return adjustJobPriority(override, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1900 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1901 | return adjustJobPriority(priority, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1902 | } |
| 1903 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1904 | /** |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1905 | * Takes jobs from pending queue and runs them on available contexts. |
| 1906 | * If no contexts are available, preempts lower priority jobs to |
| 1907 | * run higher priority ones. |
| 1908 | * Lock on mJobs before calling this function. |
| 1909 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1910 | private void assignJobsToContextsLocked() { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1911 | if (DEBUG) { |
| 1912 | Slog.d(TAG, printPendingQueue()); |
| 1913 | } |
| 1914 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1915 | int memLevel; |
| 1916 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1917 | memLevel = ActivityManager.getService().getMemoryTrimLevel(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1918 | } catch (RemoteException e) { |
| 1919 | memLevel = ProcessStats.ADJ_MEM_FACTOR_NORMAL; |
| 1920 | } |
| 1921 | switch (memLevel) { |
| 1922 | case ProcessStats.ADJ_MEM_FACTOR_MODERATE: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1923 | mMaxActiveJobs = mConstants.BG_MODERATE_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1924 | break; |
| 1925 | case ProcessStats.ADJ_MEM_FACTOR_LOW: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1926 | mMaxActiveJobs = mConstants.BG_LOW_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1927 | break; |
| 1928 | case ProcessStats.ADJ_MEM_FACTOR_CRITICAL: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1929 | mMaxActiveJobs = mConstants.BG_CRITICAL_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1930 | break; |
| 1931 | default: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1932 | mMaxActiveJobs = mConstants.BG_NORMAL_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1933 | break; |
| 1934 | } |
| 1935 | |
| 1936 | JobStatus[] contextIdToJobMap = mTmpAssignContextIdToJobMap; |
| 1937 | boolean[] act = mTmpAssignAct; |
| 1938 | int[] preferredUidForContext = mTmpAssignPreferredUidForContext; |
| 1939 | int numActive = 0; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1940 | int numForeground = 0; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1941 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
| 1942 | final JobServiceContext js = mActiveServices.get(i); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1943 | final JobStatus status = js.getRunningJobLocked(); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1944 | if ((contextIdToJobMap[i] = status) != null) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1945 | numActive++; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1946 | if (status.lastEvaluatedPriority >= JobInfo.PRIORITY_TOP_APP) { |
| 1947 | numForeground++; |
| 1948 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1949 | } |
| 1950 | act[i] = false; |
| 1951 | preferredUidForContext[i] = js.getPreferredUid(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1952 | } |
| 1953 | if (DEBUG) { |
| 1954 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs initial")); |
| 1955 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1956 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 1957 | JobStatus nextPending = mPendingJobs.get(i); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1958 | |
| 1959 | // If job is already running, go to next job. |
| 1960 | int jobRunningContext = findJobContextIdFromMap(nextPending, contextIdToJobMap); |
| 1961 | if (jobRunningContext != -1) { |
| 1962 | continue; |
| 1963 | } |
| 1964 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1965 | final int priority = evaluateJobPriorityLocked(nextPending); |
| 1966 | nextPending.lastEvaluatedPriority = priority; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1967 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1968 | // Find a context for nextPending. The context should be available OR |
| 1969 | // it should have lowest priority among all running jobs |
| 1970 | // (sharing the same Uid as nextPending) |
| 1971 | int minPriority = Integer.MAX_VALUE; |
| 1972 | int minPriorityContextId = -1; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1973 | for (int j=0; j<MAX_JOB_CONTEXTS_COUNT; j++) { |
| 1974 | JobStatus job = contextIdToJobMap[j]; |
| 1975 | int preferredUid = preferredUidForContext[j]; |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1976 | if (job == null) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1977 | if ((numActive < mMaxActiveJobs || |
| 1978 | (priority >= JobInfo.PRIORITY_TOP_APP && |
| 1979 | numForeground < mConstants.FG_JOB_COUNT)) && |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1980 | (preferredUid == nextPending.getUid() || |
| 1981 | preferredUid == JobServiceContext.NO_PREFERRED_UID)) { |
| 1982 | // This slot is free, and we haven't yet hit the limit on |
| 1983 | // concurrent jobs... we can just throw the job in to here. |
| 1984 | minPriorityContextId = j; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1985 | break; |
| 1986 | } |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1987 | // No job on this context, but nextPending can't run here because |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1988 | // the context has a preferred Uid or we have reached the limit on |
| 1989 | // concurrent jobs. |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1990 | continue; |
| 1991 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1992 | if (job.getUid() != nextPending.getUid()) { |
| 1993 | continue; |
| 1994 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1995 | if (evaluateJobPriorityLocked(job) >= nextPending.lastEvaluatedPriority) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1996 | continue; |
| 1997 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1998 | if (minPriority > nextPending.lastEvaluatedPriority) { |
| 1999 | minPriority = nextPending.lastEvaluatedPriority; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2000 | minPriorityContextId = j; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | if (minPriorityContextId != -1) { |
| 2004 | contextIdToJobMap[minPriorityContextId] = nextPending; |
| 2005 | act[minPriorityContextId] = true; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2006 | numActive++; |
| 2007 | if (priority >= JobInfo.PRIORITY_TOP_APP) { |
| 2008 | numForeground++; |
| 2009 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | if (DEBUG) { |
| 2013 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs final")); |
| 2014 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2015 | mJobPackageTracker.noteConcurrency(numActive, numForeground); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2016 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2017 | boolean preservePreferredUid = false; |
| 2018 | if (act[i]) { |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2019 | JobStatus js = mActiveServices.get(i).getRunningJobLocked(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2020 | if (js != null) { |
| 2021 | if (DEBUG) { |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2022 | Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJobLocked()); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2023 | } |
| 2024 | // preferredUid will be set to uid of currently running job. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 2025 | mActiveServices.get(i).preemptExecutingJobLocked(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2026 | preservePreferredUid = true; |
| 2027 | } else { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2028 | final JobStatus pendingJob = contextIdToJobMap[i]; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2029 | if (DEBUG) { |
| 2030 | Slog.d(TAG, "About to run job on context " |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2031 | + String.valueOf(i) + ", job: " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2032 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 2033 | for (int ic=0; ic<mControllers.size(); ic++) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2034 | mControllers.get(ic).prepareForExecutionLocked(pendingJob); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 2035 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2036 | if (!mActiveServices.get(i).executeRunnableJob(pendingJob)) { |
| 2037 | Slog.d(TAG, "Error executing " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2038 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 2039 | if (mPendingJobs.remove(pendingJob)) { |
| 2040 | mJobPackageTracker.noteNonpending(pendingJob); |
| 2041 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | if (!preservePreferredUid) { |
| 2045 | mActiveServices.get(i).clearPreferredUid(); |
| 2046 | } |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | int findJobContextIdFromMap(JobStatus jobStatus, JobStatus[] map) { |
| 2051 | for (int i=0; i<map.length; i++) { |
| 2052 | if (map[i] != null && map[i].matches(jobStatus.getUid(), jobStatus.getJobId())) { |
| 2053 | return i; |
| 2054 | } |
| 2055 | } |
| 2056 | return -1; |
| 2057 | } |
| 2058 | |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2059 | final class LocalService implements JobSchedulerInternal { |
| 2060 | |
| 2061 | /** |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2062 | * The current bucket heartbeat ordinal |
| 2063 | */ |
| 2064 | public long currentHeartbeat() { |
| 2065 | return getCurrentHeartbeat(); |
| 2066 | } |
| 2067 | |
| 2068 | /** |
| 2069 | * Heartbeat ordinal at which the given standby bucket's jobs next become runnable |
| 2070 | */ |
| 2071 | public long nextHeartbeatForBucket(int bucket) { |
| 2072 | synchronized (mLock) { |
| 2073 | return mNextBucketHeartbeat[bucket]; |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | /** |
Christopher Tate | 435c2f4 | 2018-01-18 12:59:15 -0800 | [diff] [blame] | 2078 | * Heartbeat ordinal for the given app. This is typically the heartbeat at which |
| 2079 | * the app last ran jobs, so that a newly-scheduled job in an app that hasn't run |
| 2080 | * jobs in a long time is immediately runnable even if the app is bucketed into |
| 2081 | * an infrequent time allocation. |
| 2082 | */ |
| 2083 | public long baseHeartbeatForApp(String packageName, @UserIdInt int userId, |
| 2084 | final int appStandbyBucket) { |
| 2085 | if (appStandbyBucket == 0 || |
| 2086 | appStandbyBucket >= mConstants.STANDBY_BEATS.length) { |
| 2087 | // ACTIVE => everything can be run right away |
| 2088 | // NEVER => we won't run them anyway, so let them go in the future |
| 2089 | // as soon as the app enters normal use |
| 2090 | return 0; |
| 2091 | } |
| 2092 | |
| 2093 | final long timeSinceLastJob = mStandbyTracker.getTimeSinceLastJobRun( |
| 2094 | packageName, userId); |
| 2095 | final long bucketLength = mConstants.STANDBY_BEATS[appStandbyBucket]; |
| 2096 | final long bucketsAgo = timeSinceLastJob / bucketLength; |
| 2097 | |
| 2098 | // If we haven't run any jobs for more than the app's current bucket period, just |
| 2099 | // consider anything new to be immediately runnable. Otherwise, base it on the |
| 2100 | // bucket at which we last ran jobs. |
| 2101 | return (bucketsAgo > bucketLength) ? 0 : (getCurrentHeartbeat() - bucketsAgo); |
| 2102 | } |
| 2103 | |
| 2104 | /** |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2105 | * Returns a list of all pending jobs. A running job is not considered pending. Periodic |
| 2106 | * jobs are always considered pending. |
| 2107 | */ |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 2108 | @Override |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2109 | public List<JobInfo> getSystemScheduledPendingJobs() { |
| 2110 | synchronized (mLock) { |
| 2111 | final List<JobInfo> pendingJobs = new ArrayList<JobInfo>(); |
| 2112 | mJobs.forEachJob(Process.SYSTEM_UID, new JobStatusFunctor() { |
| 2113 | @Override |
| 2114 | public void process(JobStatus job) { |
| 2115 | if (job.getJob().isPeriodic() || !isCurrentlyActiveLocked(job)) { |
| 2116 | pendingJobs.add(job.getJob()); |
| 2117 | } |
| 2118 | } |
| 2119 | }); |
| 2120 | return pendingJobs; |
| 2121 | } |
| 2122 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2123 | |
| 2124 | @Override |
Christopher Tate | 1d99c39 | 2017-12-07 16:54:04 -0800 | [diff] [blame] | 2125 | public void cancelJobsForUid(int uid, String reason) { |
| 2126 | JobSchedulerService.this.cancelJobsForUid(uid, reason); |
| 2127 | } |
| 2128 | |
| 2129 | @Override |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2130 | public void addBackingUpUid(int uid) { |
| 2131 | synchronized (mLock) { |
| 2132 | // No need to actually do anything here, since for a full backup the |
| 2133 | // activity manager will kill the process which will kill the job (and |
| 2134 | // cause it to restart, but now it can't run). |
| 2135 | mBackingUpUids.put(uid, uid); |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | @Override |
| 2140 | public void removeBackingUpUid(int uid) { |
| 2141 | synchronized (mLock) { |
| 2142 | mBackingUpUids.delete(uid); |
| 2143 | // If there are any jobs for this uid, we need to rebuild the pending list |
| 2144 | // in case they are now ready to run. |
| 2145 | if (mJobs.countJobsForUid(uid) > 0) { |
| 2146 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 2147 | } |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | @Override |
| 2152 | public void clearAllBackingUpUids() { |
| 2153 | synchronized (mLock) { |
| 2154 | if (mBackingUpUids.size() > 0) { |
| 2155 | mBackingUpUids.clear(); |
| 2156 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 2157 | } |
| 2158 | } |
| 2159 | } |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 2160 | |
| 2161 | @Override |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 2162 | public void reportAppUsage(String packageName, int userId) { |
| 2163 | JobSchedulerService.this.reportAppUsage(packageName, userId); |
| 2164 | } |
| 2165 | |
| 2166 | @Override |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 2167 | public JobStorePersistStats getPersistStats() { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 2168 | synchronized (mLock) { |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 2169 | return new JobStorePersistStats(mJobs.getPersistStats()); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 2170 | } |
| 2171 | } |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2174 | /** |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2175 | * Tracking of app assignments to standby buckets |
| 2176 | */ |
| 2177 | final class StandbyTracker extends AppIdleStateChangeListener { |
| 2178 | final UsageStatsManagerInternal mUsageStats; |
| 2179 | |
| 2180 | StandbyTracker(UsageStatsManagerInternal usageStats) { |
| 2181 | mUsageStats = usageStats; |
| 2182 | } |
| 2183 | |
Christopher Tate | 435c2f4 | 2018-01-18 12:59:15 -0800 | [diff] [blame] | 2184 | public long getTimeSinceLastJobRun(String packageName, final @UserIdInt int userId) { |
| 2185 | return mUsageStats.getTimeSinceLastJobRun(packageName, userId); |
| 2186 | } |
| 2187 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2188 | // AppIdleStateChangeListener interface for live updates |
| 2189 | |
| 2190 | @Override |
Christopher Tate | 435c2f4 | 2018-01-18 12:59:15 -0800 | [diff] [blame] | 2191 | public void onAppIdleStateChanged(final String packageName, final @UserIdInt int userId, |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2192 | boolean idle, int bucket) { |
| 2193 | final int uid = mLocalPM.getPackageUid(packageName, |
| 2194 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 2195 | if (uid < 0) { |
| 2196 | if (DEBUG_STANDBY) { |
| 2197 | Slog.i(TAG, "App idle state change for unknown app " |
| 2198 | + packageName + "/" + userId); |
| 2199 | } |
| 2200 | return; |
| 2201 | } |
| 2202 | |
| 2203 | final int bucketIndex = standbyBucketToBucketIndex(bucket); |
| 2204 | // update job bookkeeping out of band |
| 2205 | BackgroundThread.getHandler().post(() -> { |
| 2206 | if (DEBUG_STANDBY) { |
| 2207 | Slog.i(TAG, "Moving uid " + uid + " to bucketIndex " + bucketIndex); |
| 2208 | } |
| 2209 | synchronized (mLock) { |
Suprabh Shukla | 75edab1 | 2018-01-29 14:09:06 -0800 | [diff] [blame] | 2210 | mJobs.forEachJobForSourceUid(uid, job -> job.setStandbyBucket(bucketIndex)); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2211 | onControllerStateChanged(); |
| 2212 | } |
| 2213 | }); |
| 2214 | } |
| 2215 | |
| 2216 | @Override |
| 2217 | public void onParoleStateChanged(boolean isParoleOn) { |
Christopher Tate | a5a85bd | 2018-01-03 17:20:36 -0800 | [diff] [blame] | 2218 | if (DEBUG_STANDBY) { |
| 2219 | Slog.i(TAG, "Global parole state now " + (isParoleOn ? "ON" : "OFF")); |
| 2220 | } |
| 2221 | mInParole = isParoleOn; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2222 | } |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 2223 | |
| 2224 | @Override |
| 2225 | public void onUserInteractionStarted(String packageName, int userId) { |
| 2226 | final int uid = mLocalPM.getPackageUid(packageName, |
| 2227 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 2228 | if (uid < 0) { |
| 2229 | // Quietly ignore; the case is already logged elsewhere |
| 2230 | return; |
| 2231 | } |
| 2232 | |
| 2233 | final long sinceLast = sElapsedRealtimeClock.millis() - |
| 2234 | mUsageStats.getTimeSinceLastJobRun(packageName, userId); |
| 2235 | final DeferredJobCounter counter = new DeferredJobCounter(); |
| 2236 | synchronized (mLock) { |
| 2237 | mJobs.forEachJobForSourceUid(uid, counter); |
| 2238 | } |
| 2239 | |
| 2240 | mUsageStats.reportAppJobState(packageName, userId, counter.numDeferred(), sinceLast); |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | static class DeferredJobCounter implements JobStatusFunctor { |
| 2245 | private int mDeferred = 0; |
| 2246 | |
| 2247 | public int numDeferred() { |
| 2248 | return mDeferred; |
| 2249 | } |
| 2250 | |
| 2251 | @Override |
| 2252 | public void process(JobStatus job) { |
| 2253 | if (job.getWhenStandbyDeferred() > 0) { |
| 2254 | mDeferred++; |
| 2255 | } |
| 2256 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | public static int standbyBucketToBucketIndex(int bucket) { |
| 2260 | // Normalize AppStandby constants to indices into our bookkeeping |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 2261 | if (bucket == UsageStatsManager.STANDBY_BUCKET_NEVER) return 4; |
| 2262 | else if (bucket >= UsageStatsManager.STANDBY_BUCKET_RARE) return 3; |
| 2263 | else if (bucket >= UsageStatsManager.STANDBY_BUCKET_FREQUENT) return 2; |
| 2264 | else if (bucket >= UsageStatsManager.STANDBY_BUCKET_WORKING_SET) return 1; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2265 | else return 0; |
| 2266 | } |
| 2267 | |
| 2268 | public static int standbyBucketForPackage(String packageName, int userId, long elapsedNow) { |
| 2269 | UsageStatsManagerInternal usageStats = LocalServices.getService( |
| 2270 | UsageStatsManagerInternal.class); |
| 2271 | int bucket = usageStats != null |
| 2272 | ? usageStats.getAppStandbyBucket(packageName, userId, elapsedNow) |
| 2273 | : 0; |
| 2274 | |
| 2275 | bucket = standbyBucketToBucketIndex(bucket); |
| 2276 | |
| 2277 | if (DEBUG_STANDBY) { |
| 2278 | Slog.v(TAG, packageName + "/" + userId + " standby bucket index: " + bucket); |
| 2279 | } |
| 2280 | return bucket; |
| 2281 | } |
| 2282 | |
| 2283 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2284 | * Binder stub trampoline implementation |
| 2285 | */ |
| 2286 | final class JobSchedulerStub extends IJobScheduler.Stub { |
| 2287 | /** Cache determination of whether a given app can persist jobs |
| 2288 | * key is uid of the calling app; value is undetermined/true/false |
| 2289 | */ |
| 2290 | private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>(); |
| 2291 | |
| 2292 | // Enforce that only the app itself (or shared uid participant) can schedule a |
| 2293 | // job that runs one of the app's services, as well as verifying that the |
| 2294 | // named service properly requires the BIND_JOB_SERVICE permission |
| 2295 | private void enforceValidJobRequest(int uid, JobInfo job) { |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 2296 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2297 | final ComponentName service = job.getService(); |
| 2298 | try { |
Jeff Sharkey | c7bacab | 2016-02-09 15:56:11 -0700 | [diff] [blame] | 2299 | ServiceInfo si = pm.getServiceInfo(service, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2300 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 2301 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Jeff Sharkey | 12c0da4 | 2016-02-25 17:10:50 -0700 | [diff] [blame] | 2302 | UserHandle.getUserId(uid)); |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 2303 | if (si == null) { |
| 2304 | throw new IllegalArgumentException("No such service " + service); |
| 2305 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2306 | if (si.applicationInfo.uid != uid) { |
| 2307 | throw new IllegalArgumentException("uid " + uid + |
| 2308 | " cannot schedule job in " + service.getPackageName()); |
| 2309 | } |
| 2310 | if (!JobService.PERMISSION_BIND.equals(si.permission)) { |
| 2311 | throw new IllegalArgumentException("Scheduled service " + service |
| 2312 | + " does not require android.permission.BIND_JOB_SERVICE permission"); |
| 2313 | } |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 2314 | } catch (RemoteException e) { |
| 2315 | // Can't happen; the Package Manager is in this same process |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | private boolean canPersistJobs(int pid, int uid) { |
| 2320 | // If we get this far we're good to go; all we need to do now is check |
| 2321 | // whether the app is allowed to persist its scheduled work. |
| 2322 | final boolean canPersist; |
| 2323 | synchronized (mPersistCache) { |
| 2324 | Boolean cached = mPersistCache.get(uid); |
| 2325 | if (cached != null) { |
| 2326 | canPersist = cached.booleanValue(); |
| 2327 | } else { |
| 2328 | // Persisting jobs is tantamount to running at boot, so we permit |
| 2329 | // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED |
| 2330 | // permission |
| 2331 | int result = getContext().checkPermission( |
| 2332 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid); |
| 2333 | canPersist = (result == PackageManager.PERMISSION_GRANTED); |
| 2334 | mPersistCache.put(uid, canPersist); |
| 2335 | } |
| 2336 | } |
| 2337 | return canPersist; |
| 2338 | } |
| 2339 | |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2340 | private void validateJobFlags(JobInfo job, int callingUid) { |
| 2341 | if ((job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0) { |
| 2342 | getContext().enforceCallingOrSelfPermission( |
| 2343 | android.Manifest.permission.CONNECTIVITY_INTERNAL, TAG); |
| 2344 | } |
| 2345 | if ((job.getFlags() & JobInfo.FLAG_EXEMPT_FROM_APP_STANDBY) != 0) { |
| 2346 | if (callingUid != Process.SYSTEM_UID) { |
| 2347 | throw new SecurityException("Job has invalid flags"); |
| 2348 | } |
Makoto Onuki | 2b5811a | 2018-02-08 11:09:42 -0800 | [diff] [blame^] | 2349 | if (job.isPeriodic()) { |
| 2350 | Slog.wtf(TAG, "Periodic jobs mustn't have" |
| 2351 | + " FLAG_EXEMPT_FROM_APP_STANDBY. Job=" + job); |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | } |
| 2355 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2356 | // IJobScheduler implementation |
| 2357 | @Override |
| 2358 | public int schedule(JobInfo job) throws RemoteException { |
| 2359 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 2360 | Slog.d(TAG, "Scheduling job: " + job.toString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2361 | } |
| 2362 | final int pid = Binder.getCallingPid(); |
| 2363 | final int uid = Binder.getCallingUid(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2364 | final int userId = UserHandle.getUserId(uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2365 | |
| 2366 | enforceValidJobRequest(uid, job); |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 2367 | if (job.isPersisted()) { |
| 2368 | if (!canPersistJobs(pid, uid)) { |
| 2369 | throw new IllegalArgumentException("Error: requested job be persisted without" |
| 2370 | + " holding RECEIVE_BOOT_COMPLETED permission."); |
| 2371 | } |
| 2372 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2373 | |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2374 | validateJobFlags(job, uid); |
Jeff Sharkey | 785f494 | 2016-07-14 10:31:15 -0600 | [diff] [blame] | 2375 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2376 | long ident = Binder.clearCallingIdentity(); |
| 2377 | try { |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2378 | return JobSchedulerService.this.scheduleAsPackage(job, null, uid, null, userId, |
| 2379 | null); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2380 | } finally { |
| 2381 | Binder.restoreCallingIdentity(ident); |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | // IJobScheduler implementation |
| 2386 | @Override |
| 2387 | public int enqueue(JobInfo job, JobWorkItem work) throws RemoteException { |
| 2388 | if (DEBUG) { |
| 2389 | Slog.d(TAG, "Enqueueing job: " + job.toString() + " work: " + work); |
| 2390 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2391 | final int uid = Binder.getCallingUid(); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2392 | final int userId = UserHandle.getUserId(uid); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2393 | |
| 2394 | enforceValidJobRequest(uid, job); |
| 2395 | if (job.isPersisted()) { |
| 2396 | throw new IllegalArgumentException("Can't enqueue work for persisted jobs"); |
| 2397 | } |
| 2398 | if (work == null) { |
| 2399 | throw new NullPointerException("work is null"); |
| 2400 | } |
| 2401 | |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2402 | validateJobFlags(job, uid); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2403 | |
| 2404 | long ident = Binder.clearCallingIdentity(); |
| 2405 | try { |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2406 | return JobSchedulerService.this.scheduleAsPackage(job, work, uid, null, userId, |
| 2407 | null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2408 | } finally { |
| 2409 | Binder.restoreCallingIdentity(ident); |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | @Override |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2414 | public int scheduleAsPackage(JobInfo job, String packageName, int userId, String tag) |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2415 | throws RemoteException { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2416 | final int callerUid = Binder.getCallingUid(); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2417 | if (DEBUG) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2418 | Slog.d(TAG, "Caller uid " + callerUid + " scheduling job: " + job.toString() |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2419 | + " on behalf of " + packageName + "/"); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2420 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2421 | |
| 2422 | if (packageName == null) { |
| 2423 | throw new NullPointerException("Must specify a package for scheduleAsPackage()"); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2424 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2425 | |
| 2426 | int mayScheduleForOthers = getContext().checkCallingOrSelfPermission( |
| 2427 | android.Manifest.permission.UPDATE_DEVICE_STATS); |
| 2428 | if (mayScheduleForOthers != PackageManager.PERMISSION_GRANTED) { |
| 2429 | throw new SecurityException("Caller uid " + callerUid |
| 2430 | + " not permitted to schedule jobs for other apps"); |
| 2431 | } |
| 2432 | |
Makoto Onuki | 959acb5 | 2018-01-26 14:10:03 -0800 | [diff] [blame] | 2433 | validateJobFlags(job, callerUid); |
Jeff Sharkey | 4f10040 | 2016-05-03 17:44:23 -0600 | [diff] [blame] | 2434 | |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2435 | long ident = Binder.clearCallingIdentity(); |
| 2436 | try { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 2437 | return JobSchedulerService.this.scheduleAsPackage(job, null, callerUid, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2438 | packageName, userId, tag); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 2439 | } finally { |
| 2440 | Binder.restoreCallingIdentity(ident); |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2445 | public List<JobInfo> getAllPendingJobs() throws RemoteException { |
| 2446 | final int uid = Binder.getCallingUid(); |
| 2447 | |
| 2448 | long ident = Binder.clearCallingIdentity(); |
| 2449 | try { |
| 2450 | return JobSchedulerService.this.getPendingJobs(uid); |
| 2451 | } finally { |
| 2452 | Binder.restoreCallingIdentity(ident); |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | @Override |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 2457 | public JobInfo getPendingJob(int jobId) throws RemoteException { |
| 2458 | final int uid = Binder.getCallingUid(); |
| 2459 | |
| 2460 | long ident = Binder.clearCallingIdentity(); |
| 2461 | try { |
| 2462 | return JobSchedulerService.this.getPendingJob(uid, jobId); |
| 2463 | } finally { |
| 2464 | Binder.restoreCallingIdentity(ident); |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2469 | public void cancelAll() throws RemoteException { |
| 2470 | final int uid = Binder.getCallingUid(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2471 | long ident = Binder.clearCallingIdentity(); |
| 2472 | try { |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 2473 | JobSchedulerService.this.cancelJobsForUid(uid, |
| 2474 | "cancelAll() called by app, callingUid=" + uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2475 | } finally { |
| 2476 | Binder.restoreCallingIdentity(ident); |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | @Override |
| 2481 | public void cancel(int jobId) throws RemoteException { |
| 2482 | final int uid = Binder.getCallingUid(); |
| 2483 | |
| 2484 | long ident = Binder.clearCallingIdentity(); |
| 2485 | try { |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 2486 | JobSchedulerService.this.cancelJob(uid, jobId, uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2487 | } finally { |
| 2488 | Binder.restoreCallingIdentity(ident); |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * "dumpsys" infrastructure |
| 2494 | */ |
| 2495 | @Override |
| 2496 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2497 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2498 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2499 | int filterUid = -1; |
| 2500 | boolean proto = false; |
| 2501 | if (!ArrayUtils.isEmpty(args)) { |
| 2502 | int opti = 0; |
| 2503 | while (opti < args.length) { |
| 2504 | String arg = args[opti]; |
| 2505 | if ("-h".equals(arg)) { |
| 2506 | dumpHelp(pw); |
| 2507 | return; |
| 2508 | } else if ("-a".equals(arg)) { |
| 2509 | // Ignore, we always dump all. |
| 2510 | } else if ("--proto".equals(arg)) { |
| 2511 | proto = true; |
| 2512 | } else if (arg.length() > 0 && arg.charAt(0) == '-') { |
| 2513 | pw.println("Unknown option: " + arg); |
| 2514 | return; |
| 2515 | } else { |
| 2516 | break; |
| 2517 | } |
| 2518 | opti++; |
| 2519 | } |
| 2520 | if (opti < args.length) { |
| 2521 | String pkg = args[opti]; |
| 2522 | try { |
| 2523 | filterUid = getContext().getPackageManager().getPackageUid(pkg, |
| 2524 | PackageManager.MATCH_ANY_USER); |
| 2525 | } catch (NameNotFoundException ignored) { |
| 2526 | pw.println("Invalid package: " + pkg); |
| 2527 | return; |
| 2528 | } |
| 2529 | } |
| 2530 | } |
| 2531 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2532 | long identityToken = Binder.clearCallingIdentity(); |
| 2533 | try { |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2534 | if (proto) { |
| 2535 | JobSchedulerService.this.dumpInternalProto(fd, filterUid); |
| 2536 | } else { |
| 2537 | JobSchedulerService.this.dumpInternal(pw, filterUid); |
| 2538 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2539 | } finally { |
| 2540 | Binder.restoreCallingIdentity(identityToken); |
| 2541 | } |
| 2542 | } |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2543 | |
| 2544 | @Override |
| 2545 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2546 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2547 | (new JobSchedulerShellCommand(JobSchedulerService.this)).exec( |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2548 | this, in, out, err, args, callback, resultReceiver); |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2549 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2550 | }; |
| 2551 | |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2552 | // Shell command infrastructure: run the given job immediately |
| 2553 | int executeRunCommand(String pkgName, int userId, int jobId, boolean force) { |
| 2554 | if (DEBUG) { |
| 2555 | Slog.v(TAG, "executeRunCommand(): " + pkgName + "/" + userId |
| 2556 | + " " + jobId + " f=" + force); |
| 2557 | } |
| 2558 | |
| 2559 | try { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2560 | final int uid = AppGlobals.getPackageManager().getPackageUid(pkgName, 0, |
| 2561 | userId != UserHandle.USER_ALL ? userId : UserHandle.USER_SYSTEM); |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2562 | if (uid < 0) { |
| 2563 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 2564 | } |
| 2565 | |
| 2566 | synchronized (mLock) { |
| 2567 | final JobStatus js = mJobs.getJobByUidAndJobId(uid, jobId); |
| 2568 | if (js == null) { |
| 2569 | return JobSchedulerShellCommand.CMD_ERR_NO_JOB; |
| 2570 | } |
| 2571 | |
| 2572 | js.overrideState = (force) ? JobStatus.OVERRIDE_FULL : JobStatus.OVERRIDE_SOFT; |
| 2573 | if (!js.isConstraintsSatisfied()) { |
| 2574 | js.overrideState = 0; |
| 2575 | return JobSchedulerShellCommand.CMD_ERR_CONSTRAINTS; |
| 2576 | } |
| 2577 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2578 | queueReadyJobsForExecutionLocked(); |
| 2579 | maybeRunPendingJobsLocked(); |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 2580 | } |
| 2581 | } catch (RemoteException e) { |
| 2582 | // can't happen |
| 2583 | } |
| 2584 | return 0; |
| 2585 | } |
| 2586 | |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2587 | // Shell command infrastructure: immediately timeout currently executing jobs |
| 2588 | int executeTimeoutCommand(PrintWriter pw, String pkgName, int userId, |
| 2589 | boolean hasJobId, int jobId) { |
| 2590 | if (DEBUG) { |
| 2591 | Slog.v(TAG, "executeTimeoutCommand(): " + pkgName + "/" + userId + " " + jobId); |
| 2592 | } |
| 2593 | |
| 2594 | synchronized (mLock) { |
| 2595 | boolean foundSome = false; |
| 2596 | for (int i=0; i<mActiveServices.size(); i++) { |
Dianne Hackborn | eb90fa9 | 2017-06-30 14:03:36 -0700 | [diff] [blame] | 2597 | final JobServiceContext jc = mActiveServices.get(i); |
| 2598 | final JobStatus js = jc.getRunningJobLocked(); |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 2599 | if (jc.timeoutIfExecutingLocked(pkgName, userId, hasJobId, jobId, "shell")) { |
Dianne Hackborn | eb90fa9 | 2017-06-30 14:03:36 -0700 | [diff] [blame] | 2600 | foundSome = true; |
| 2601 | pw.print("Timing out: "); |
| 2602 | js.printUniqueId(pw); |
| 2603 | pw.print(" "); |
| 2604 | pw.println(js.getServiceComponent().flattenToShortString()); |
| 2605 | } |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2606 | } |
| 2607 | if (!foundSome) { |
| 2608 | pw.println("No matching executing jobs found."); |
| 2609 | } |
| 2610 | } |
| 2611 | return 0; |
| 2612 | } |
| 2613 | |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 2614 | // Shell command infrastructure: cancel a scheduled job |
| 2615 | int executeCancelCommand(PrintWriter pw, String pkgName, int userId, |
| 2616 | boolean hasJobId, int jobId) { |
| 2617 | if (DEBUG) { |
| 2618 | Slog.v(TAG, "executeCancelCommand(): " + pkgName + "/" + userId + " " + jobId); |
| 2619 | } |
| 2620 | |
| 2621 | int pkgUid = -1; |
| 2622 | try { |
| 2623 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 2624 | pkgUid = pm.getPackageUid(pkgName, 0, userId); |
| 2625 | } catch (RemoteException e) { /* can't happen */ } |
| 2626 | |
| 2627 | if (pkgUid < 0) { |
| 2628 | pw.println("Package " + pkgName + " not found."); |
| 2629 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 2630 | } |
| 2631 | |
| 2632 | if (!hasJobId) { |
| 2633 | pw.println("Canceling all jobs for " + pkgName + " in user " + userId); |
| 2634 | if (!cancelJobsForUid(pkgUid, "cancel shell command for package")) { |
| 2635 | pw.println("No matching jobs found."); |
| 2636 | } |
| 2637 | } else { |
| 2638 | pw.println("Canceling job " + pkgName + "/#" + jobId + " in user " + userId); |
Makoto Onuki | d2bfec6 | 2018-01-12 13:58:01 -0800 | [diff] [blame] | 2639 | if (!cancelJob(pkgUid, jobId, Process.SHELL_UID)) { |
Christopher Tate | 8c67d12 | 2017-09-29 16:54:26 -0700 | [diff] [blame] | 2640 | pw.println("No matching job found."); |
| 2641 | } |
| 2642 | } |
| 2643 | |
| 2644 | return 0; |
| 2645 | } |
| 2646 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 2647 | void setMonitorBattery(boolean enabled) { |
| 2648 | synchronized (mLock) { |
| 2649 | if (mBatteryController != null) { |
| 2650 | mBatteryController.getTracker().setMonitorBatteryLocked(enabled); |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | int getBatterySeq() { |
| 2656 | synchronized (mLock) { |
| 2657 | return mBatteryController != null ? mBatteryController.getTracker().getSeq() : -1; |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | boolean getBatteryCharging() { |
| 2662 | synchronized (mLock) { |
| 2663 | return mBatteryController != null |
| 2664 | ? mBatteryController.getTracker().isOnStablePower() : false; |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | boolean getBatteryNotLow() { |
| 2669 | synchronized (mLock) { |
| 2670 | return mBatteryController != null |
| 2671 | ? mBatteryController.getTracker().isBatteryNotLow() : false; |
| 2672 | } |
| 2673 | } |
| 2674 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 2675 | int getStorageSeq() { |
| 2676 | synchronized (mLock) { |
| 2677 | return mStorageController != null ? mStorageController.getTracker().getSeq() : -1; |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | boolean getStorageNotLow() { |
| 2682 | synchronized (mLock) { |
| 2683 | return mStorageController != null |
| 2684 | ? mStorageController.getTracker().isStorageNotLow() : false; |
| 2685 | } |
| 2686 | } |
| 2687 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 2688 | long getCurrentHeartbeat() { |
| 2689 | synchronized (mLock) { |
| 2690 | return mHeartbeat; |
| 2691 | } |
| 2692 | } |
| 2693 | |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2694 | int getJobState(PrintWriter pw, String pkgName, int userId, int jobId) { |
| 2695 | try { |
| 2696 | final int uid = AppGlobals.getPackageManager().getPackageUid(pkgName, 0, |
| 2697 | userId != UserHandle.USER_ALL ? userId : UserHandle.USER_SYSTEM); |
| 2698 | if (uid < 0) { |
| 2699 | pw.print("unknown("); pw.print(pkgName); pw.println(")"); |
| 2700 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 2701 | } |
| 2702 | |
| 2703 | synchronized (mLock) { |
| 2704 | final JobStatus js = mJobs.getJobByUidAndJobId(uid, jobId); |
| 2705 | if (DEBUG) Slog.d(TAG, "get-job-state " + uid + "/" + jobId + ": " + js); |
| 2706 | if (js == null) { |
| 2707 | pw.print("unknown("); UserHandle.formatUid(pw, uid); |
| 2708 | pw.print("/jid"); pw.print(jobId); pw.println(")"); |
| 2709 | return JobSchedulerShellCommand.CMD_ERR_NO_JOB; |
| 2710 | } |
| 2711 | |
| 2712 | boolean printed = false; |
| 2713 | if (mPendingJobs.contains(js)) { |
| 2714 | pw.print("pending"); |
| 2715 | printed = true; |
| 2716 | } |
| 2717 | if (isCurrentlyActiveLocked(js)) { |
| 2718 | if (printed) { |
| 2719 | pw.print(" "); |
| 2720 | } |
| 2721 | printed = true; |
| 2722 | pw.println("active"); |
| 2723 | } |
| 2724 | if (!ArrayUtils.contains(mStartedUsers, js.getUserId())) { |
| 2725 | if (printed) { |
| 2726 | pw.print(" "); |
| 2727 | } |
| 2728 | printed = true; |
| 2729 | pw.println("user-stopped"); |
| 2730 | } |
| 2731 | if (mBackingUpUids.indexOfKey(js.getSourceUid()) >= 0) { |
| 2732 | if (printed) { |
| 2733 | pw.print(" "); |
| 2734 | } |
| 2735 | printed = true; |
| 2736 | pw.println("backing-up"); |
| 2737 | } |
| 2738 | boolean componentPresent = false; |
| 2739 | try { |
| 2740 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 2741 | js.getServiceComponent(), |
| 2742 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 2743 | js.getUserId()) != null); |
| 2744 | } catch (RemoteException e) { |
| 2745 | } |
| 2746 | if (!componentPresent) { |
| 2747 | if (printed) { |
| 2748 | pw.print(" "); |
| 2749 | } |
| 2750 | printed = true; |
| 2751 | pw.println("no-component"); |
| 2752 | } |
| 2753 | if (js.isReady()) { |
| 2754 | if (printed) { |
| 2755 | pw.print(" "); |
| 2756 | } |
| 2757 | printed = true; |
| 2758 | pw.println("ready"); |
| 2759 | } |
| 2760 | if (!printed) { |
| 2761 | pw.print("waiting"); |
| 2762 | } |
| 2763 | pw.println(); |
| 2764 | } |
| 2765 | } catch (RemoteException e) { |
| 2766 | // can't happen |
| 2767 | } |
| 2768 | return 0; |
| 2769 | } |
| 2770 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 2771 | private String printContextIdToJobMap(JobStatus[] map, String initial) { |
| 2772 | StringBuilder s = new StringBuilder(initial + ": "); |
| 2773 | for (int i=0; i<map.length; i++) { |
| 2774 | s.append("(") |
| 2775 | .append(map[i] == null? -1: map[i].getJobId()) |
| 2776 | .append(map[i] == null? -1: map[i].getUid()) |
| 2777 | .append(")" ); |
| 2778 | } |
| 2779 | return s.toString(); |
| 2780 | } |
| 2781 | |
| 2782 | private String printPendingQueue() { |
| 2783 | StringBuilder s = new StringBuilder("Pending queue: "); |
| 2784 | Iterator<JobStatus> it = mPendingJobs.iterator(); |
| 2785 | while (it.hasNext()) { |
| 2786 | JobStatus js = it.next(); |
| 2787 | s.append("(") |
| 2788 | .append(js.getJob().getId()) |
| 2789 | .append(", ") |
| 2790 | .append(js.getUid()) |
| 2791 | .append(") "); |
| 2792 | } |
| 2793 | return s.toString(); |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 2794 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2795 | |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2796 | static void dumpHelp(PrintWriter pw) { |
| 2797 | pw.println("Job Scheduler (jobscheduler) dump options:"); |
| 2798 | pw.println(" [-h] [package] ..."); |
| 2799 | pw.println(" -h: print this help"); |
| 2800 | pw.println(" [package] is an optional package name to limit the output to."); |
| 2801 | } |
| 2802 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2803 | /** Sort jobs by caller UID, then by Job ID. */ |
| 2804 | private static void sortJobs(List<JobStatus> jobs) { |
| 2805 | Collections.sort(jobs, new Comparator<JobStatus>() { |
| 2806 | @Override |
| 2807 | public int compare(JobStatus o1, JobStatus o2) { |
| 2808 | int uid1 = o1.getUid(); |
| 2809 | int uid2 = o2.getUid(); |
| 2810 | int id1 = o1.getJobId(); |
| 2811 | int id2 = o2.getJobId(); |
| 2812 | if (uid1 != uid2) { |
| 2813 | return uid1 < uid2 ? -1 : 1; |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2814 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2815 | return id1 < id2 ? -1 : (id1 > id2 ? 1 : 0); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2816 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2817 | }); |
| 2818 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 2819 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2820 | void dumpInternal(final PrintWriter pw, int filterUid) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2821 | final int filterUidFinal = UserHandle.getAppId(filterUid); |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 2822 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
| 2823 | final long nowUptime = sUptimeMillisClock.millis(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 2824 | synchronized (mLock) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2825 | mConstants.dump(pw); |
| 2826 | pw.println(); |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 2827 | pw.println("Started users: " + Arrays.toString(mStartedUsers)); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2828 | pw.print("Registered "); |
| 2829 | pw.print(mJobs.size()); |
| 2830 | pw.println(" jobs:"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2831 | if (mJobs.size() > 0) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2832 | final List<JobStatus> jobs = mJobs.mJobSet.getAllJobs(); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2833 | sortJobs(jobs); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2834 | for (JobStatus job : jobs) { |
| 2835 | pw.print(" JOB #"); job.printUniqueId(pw); pw.print(": "); |
| 2836 | pw.println(job.toShortStringExceptUniqueId()); |
| 2837 | |
| 2838 | // Skip printing details if the caller requested a filter |
| 2839 | if (!job.shouldDump(filterUidFinal)) { |
| 2840 | continue; |
| 2841 | } |
| 2842 | |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2843 | job.dump(pw, " ", true, nowElapsed); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2844 | pw.print(" Ready: "); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2845 | pw.print(isReadyToBeExecutedLocked(job)); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2846 | pw.print(" (job="); |
| 2847 | pw.print(job.isReady()); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2848 | pw.print(" user="); |
| 2849 | pw.print(ArrayUtils.contains(mStartedUsers, job.getUserId())); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 2850 | pw.print(" !pending="); |
| 2851 | pw.print(!mPendingJobs.contains(job)); |
| 2852 | pw.print(" !active="); |
| 2853 | pw.print(!isCurrentlyActiveLocked(job)); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2854 | pw.print(" !backingup="); |
| 2855 | pw.print(!(mBackingUpUids.indexOfKey(job.getSourceUid()) >= 0)); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 2856 | pw.print(" comp="); |
| 2857 | boolean componentPresent = false; |
| 2858 | try { |
| 2859 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 2860 | job.getServiceComponent(), |
| 2861 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 2862 | job.getUserId()) != null); |
| 2863 | } catch (RemoteException e) { |
| 2864 | } |
| 2865 | pw.print(componentPresent); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2866 | pw.println(")"); |
| 2867 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2868 | } else { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 2869 | pw.println(" None."); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2870 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 2871 | for (int i=0; i<mControllers.size(); i++) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2872 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2873 | mControllers.get(i).dumpControllerStateLocked(pw, filterUidFinal); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2874 | } |
| 2875 | pw.println(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2876 | pw.println("Uid priority overrides:"); |
| 2877 | for (int i=0; i< mUidPriorityOverride.size(); i++) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2878 | int uid = mUidPriorityOverride.keyAt(i); |
| 2879 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 2880 | pw.print(" "); pw.print(UserHandle.formatUid(uid)); |
| 2881 | pw.print(": "); pw.println(mUidPriorityOverride.valueAt(i)); |
| 2882 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2883 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 2884 | if (mBackingUpUids.size() > 0) { |
| 2885 | pw.println(); |
| 2886 | pw.println("Backing up uids:"); |
| 2887 | boolean first = true; |
| 2888 | for (int i = 0; i < mBackingUpUids.size(); i++) { |
| 2889 | int uid = mBackingUpUids.keyAt(i); |
| 2890 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 2891 | if (first) { |
| 2892 | pw.print(" "); |
| 2893 | first = false; |
| 2894 | } else { |
| 2895 | pw.print(", "); |
| 2896 | } |
| 2897 | pw.print(UserHandle.formatUid(uid)); |
| 2898 | } |
| 2899 | } |
| 2900 | pw.println(); |
| 2901 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2902 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2903 | mJobPackageTracker.dump(pw, "", filterUidFinal); |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 2904 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2905 | if (mJobPackageTracker.dumpHistory(pw, "", filterUidFinal)) { |
| 2906 | pw.println(); |
| 2907 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2908 | pw.println("Pending queue:"); |
| 2909 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 2910 | JobStatus job = mPendingJobs.get(i); |
| 2911 | pw.print(" Pending #"); pw.print(i); pw.print(": "); |
| 2912 | pw.println(job.toShortString()); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2913 | job.dump(pw, " ", false, nowElapsed); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2914 | int priority = evaluateJobPriorityLocked(job); |
| 2915 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 2916 | pw.print(" Evaluated priority: "); pw.println(priority); |
| 2917 | } |
| 2918 | pw.print(" Tag: "); pw.println(job.getTag()); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2919 | pw.print(" Enq: "); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2920 | TimeUtils.formatDuration(job.madePending - nowUptime, pw); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2921 | pw.println(); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2922 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2923 | pw.println(); |
| 2924 | pw.println("Active jobs:"); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 2925 | for (int i=0; i<mActiveServices.size(); i++) { |
| 2926 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2927 | pw.print(" Slot #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2928 | final JobStatus job = jsc.getRunningJobLocked(); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2929 | if (job == null) { |
Dianne Hackborn | 729a328 | 2017-06-09 16:06:01 -0700 | [diff] [blame] | 2930 | if (jsc.mStoppedReason != null) { |
| 2931 | pw.print("inactive since "); |
| 2932 | TimeUtils.formatDuration(jsc.mStoppedTime, nowElapsed, pw); |
| 2933 | pw.print(", stopped because: "); |
| 2934 | pw.println(jsc.mStoppedReason); |
| 2935 | } else { |
| 2936 | pw.println("inactive"); |
| 2937 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2938 | continue; |
| 2939 | } else { |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2940 | pw.println(job.toShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2941 | pw.print(" Running for: "); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2942 | TimeUtils.formatDuration(nowElapsed - jsc.getExecutionStartTimeElapsed(), pw); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2943 | pw.print(", timeout at: "); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2944 | TimeUtils.formatDuration(jsc.getTimeoutElapsed() - nowElapsed, pw); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2945 | pw.println(); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2946 | job.dump(pw, " ", false, nowElapsed); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2947 | int priority = evaluateJobPriorityLocked(jsc.getRunningJobLocked()); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2948 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2949 | pw.print(" Evaluated priority: "); pw.println(priority); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2950 | } |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2951 | pw.print(" Active at "); |
Dianne Hackborn | 6d06826 | 2017-05-16 13:14:37 -0700 | [diff] [blame] | 2952 | TimeUtils.formatDuration(job.madeActive - nowUptime, pw); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 2953 | pw.print(", pending for "); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2954 | TimeUtils.formatDuration(job.madeActive - job.madePending, pw); |
| 2955 | pw.println(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2956 | } |
| 2957 | } |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2958 | if (filterUid == -1) { |
| 2959 | pw.println(); |
| 2960 | pw.print("mReadyToRock="); pw.println(mReadyToRock); |
| 2961 | pw.print("mReportedActive="); pw.println(mReportedActive); |
| 2962 | pw.print("mMaxActiveJobs="); pw.println(mMaxActiveJobs); |
| 2963 | } |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 2964 | pw.println(); |
| 2965 | pw.print("PersistStats: "); |
| 2966 | pw.println(mJobs.getPersistStats()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2967 | } |
| 2968 | pw.println(); |
| 2969 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 2970 | |
| 2971 | void dumpInternalProto(final FileDescriptor fd, int filterUid) { |
| 2972 | ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 2973 | final int filterUidFinal = UserHandle.getAppId(filterUid); |
| 2974 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
| 2975 | final long nowUptime = sUptimeMillisClock.millis(); |
| 2976 | |
| 2977 | synchronized (mLock) { |
| 2978 | mConstants.dump(proto, JobSchedulerServiceDumpProto.SETTINGS); |
| 2979 | for (int u : mStartedUsers) { |
| 2980 | proto.write(JobSchedulerServiceDumpProto.STARTED_USERS, u); |
| 2981 | } |
| 2982 | if (mJobs.size() > 0) { |
| 2983 | final List<JobStatus> jobs = mJobs.mJobSet.getAllJobs(); |
| 2984 | sortJobs(jobs); |
| 2985 | for (JobStatus job : jobs) { |
| 2986 | final long rjToken = proto.start(JobSchedulerServiceDumpProto.REGISTERED_JOBS); |
| 2987 | job.writeToShortProto(proto, JobSchedulerServiceDumpProto.RegisteredJob.INFO); |
| 2988 | |
| 2989 | // Skip printing details if the caller requested a filter |
| 2990 | if (!job.shouldDump(filterUidFinal)) { |
| 2991 | continue; |
| 2992 | } |
| 2993 | |
| 2994 | job.dump(proto, JobSchedulerServiceDumpProto.RegisteredJob.DUMP, true, nowElapsed); |
| 2995 | |
| 2996 | // isReadyToBeExecuted |
| 2997 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_JOB_READY, |
| 2998 | job.isReady()); |
| 2999 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_USER_STARTED, |
| 3000 | ArrayUtils.contains(mStartedUsers, job.getUserId())); |
| 3001 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_JOB_PENDING, |
| 3002 | mPendingJobs.contains(job)); |
| 3003 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_JOB_CURRENTLY_ACTIVE, |
| 3004 | isCurrentlyActiveLocked(job)); |
| 3005 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_UID_BACKING_UP, |
| 3006 | mBackingUpUids.indexOfKey(job.getSourceUid()) >= 0); |
| 3007 | boolean componentPresent = false; |
| 3008 | try { |
| 3009 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 3010 | job.getServiceComponent(), |
| 3011 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 3012 | job.getUserId()) != null); |
| 3013 | } catch (RemoteException e) { |
| 3014 | } |
| 3015 | proto.write(JobSchedulerServiceDumpProto.RegisteredJob.IS_COMPONENT_PRESENT, |
| 3016 | componentPresent); |
| 3017 | |
| 3018 | proto.end(rjToken); |
| 3019 | } |
| 3020 | } |
| 3021 | for (StateController controller : mControllers) { |
| 3022 | controller.dumpControllerStateLocked( |
| 3023 | proto, JobSchedulerServiceDumpProto.CONTROLLERS, filterUidFinal); |
| 3024 | } |
| 3025 | for (int i=0; i< mUidPriorityOverride.size(); i++) { |
| 3026 | int uid = mUidPriorityOverride.keyAt(i); |
| 3027 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 3028 | long pToken = proto.start(JobSchedulerServiceDumpProto.PRIORITY_OVERRIDES); |
| 3029 | proto.write(JobSchedulerServiceDumpProto.PriorityOverride.UID, uid); |
| 3030 | proto.write(JobSchedulerServiceDumpProto.PriorityOverride.OVERRIDE_VALUE, |
| 3031 | mUidPriorityOverride.valueAt(i)); |
| 3032 | proto.end(pToken); |
| 3033 | } |
| 3034 | } |
| 3035 | for (int i = 0; i < mBackingUpUids.size(); i++) { |
| 3036 | int uid = mBackingUpUids.keyAt(i); |
| 3037 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 3038 | proto.write(JobSchedulerServiceDumpProto.BACKING_UP_UIDS, uid); |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | mJobPackageTracker.dump(proto, JobSchedulerServiceDumpProto.PACKAGE_TRACKER, |
| 3043 | filterUidFinal); |
| 3044 | mJobPackageTracker.dumpHistory(proto, JobSchedulerServiceDumpProto.HISTORY, |
| 3045 | filterUidFinal); |
| 3046 | |
| 3047 | for (JobStatus job : mPendingJobs) { |
| 3048 | final long pjToken = proto.start(JobSchedulerServiceDumpProto.PENDING_JOBS); |
| 3049 | |
| 3050 | job.writeToShortProto(proto, PendingJob.INFO); |
| 3051 | job.dump(proto, PendingJob.DUMP, false, nowElapsed); |
| 3052 | int priority = evaluateJobPriorityLocked(job); |
| 3053 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 3054 | proto.write(PendingJob.EVALUATED_PRIORITY, priority); |
| 3055 | } |
| 3056 | proto.write(PendingJob.ENQUEUED_DURATION_MS, nowUptime - job.madePending); |
| 3057 | |
| 3058 | proto.end(pjToken); |
| 3059 | } |
| 3060 | for (JobServiceContext jsc : mActiveServices) { |
| 3061 | final long ajToken = proto.start(JobSchedulerServiceDumpProto.ACTIVE_JOBS); |
| 3062 | final JobStatus job = jsc.getRunningJobLocked(); |
| 3063 | |
| 3064 | if (job == null) { |
| 3065 | final long ijToken = proto.start(ActiveJob.INACTIVE); |
| 3066 | |
| 3067 | proto.write(ActiveJob.InactiveJob.TIME_SINCE_STOPPED_MS, |
| 3068 | nowElapsed - jsc.mStoppedTime); |
| 3069 | if (jsc.mStoppedReason != null) { |
| 3070 | proto.write(ActiveJob.InactiveJob.STOPPED_REASON, |
| 3071 | jsc.mStoppedReason); |
| 3072 | } |
| 3073 | |
| 3074 | proto.end(ijToken); |
| 3075 | } else { |
| 3076 | final long rjToken = proto.start(ActiveJob.RUNNING); |
| 3077 | |
| 3078 | job.writeToShortProto(proto, ActiveJob.RunningJob.INFO); |
| 3079 | |
| 3080 | proto.write(ActiveJob.RunningJob.RUNNING_DURATION_MS, |
| 3081 | nowElapsed - jsc.getExecutionStartTimeElapsed()); |
| 3082 | proto.write(ActiveJob.RunningJob.TIME_UNTIL_TIMEOUT_MS, |
| 3083 | jsc.getTimeoutElapsed() - nowElapsed); |
| 3084 | |
| 3085 | job.dump(proto, ActiveJob.RunningJob.DUMP, false, nowElapsed); |
| 3086 | |
| 3087 | int priority = evaluateJobPriorityLocked(jsc.getRunningJobLocked()); |
| 3088 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 3089 | proto.write(ActiveJob.RunningJob.EVALUATED_PRIORITY, priority); |
| 3090 | } |
| 3091 | |
| 3092 | proto.write(ActiveJob.RunningJob.TIME_SINCE_MADE_ACTIVE_MS, |
| 3093 | nowUptime - job.madeActive); |
| 3094 | proto.write(ActiveJob.RunningJob.PENDING_DURATION_MS, |
| 3095 | job.madeActive - job.madePending); |
| 3096 | |
| 3097 | proto.end(rjToken); |
| 3098 | } |
| 3099 | proto.end(ajToken); |
| 3100 | } |
| 3101 | if (filterUid == -1) { |
| 3102 | proto.write(JobSchedulerServiceDumpProto.IS_READY_TO_ROCK, mReadyToRock); |
| 3103 | proto.write(JobSchedulerServiceDumpProto.REPORTED_ACTIVE, mReportedActive); |
| 3104 | proto.write(JobSchedulerServiceDumpProto.MAX_ACTIVE_JOBS, mMaxActiveJobs); |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | proto.flush(); |
| 3109 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 3110 | } |