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