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