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