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