Matthew Williams | 6de79e2 | 2014-05-01 10:47: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 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 17 | package com.android.server.job.controllers; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 18 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 19 | import static com.android.server.job.JobSchedulerService.sElapsedRealtimeClock; |
| 20 | |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 22 | import android.app.AlarmManager; |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 23 | import android.app.AlarmManager.OnAlarmListener; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 25 | import android.os.Process; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
| 27 | import android.os.WorkSource; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 28 | import android.util.Slog; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 29 | import android.util.TimeUtils; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 30 | import android.util.proto.ProtoOutputStream; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 31 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 32 | import com.android.server.job.JobSchedulerService; |
| 33 | import com.android.server.job.StateChangedListener; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 34 | import com.android.server.job.StateControllerProto; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 35 | |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 36 | import java.io.PrintWriter; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 37 | import java.util.Iterator; |
| 38 | import java.util.LinkedList; |
| 39 | import java.util.List; |
| 40 | import java.util.ListIterator; |
| 41 | |
| 42 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 43 | * This class sets an alarm for the next expiring job, and determines whether a job's minimum |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 44 | * delay has been satisfied. |
| 45 | */ |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 46 | public final class TimeController extends StateController { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 47 | private static final String TAG = "JobScheduler.Time"; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 48 | |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 49 | /** Deadline alarm tag for logging purposes */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 50 | private final String DEADLINE_TAG = "*job.deadline*"; |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 51 | /** Delay alarm tag for logging purposes */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 52 | private final String DELAY_TAG = "*job.delay*"; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 53 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 54 | private long mNextJobExpiredElapsedMillis; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 55 | private long mNextDelayExpiredElapsedMillis; |
| 56 | |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 57 | private final boolean mChainedAttributionEnabled; |
| 58 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 59 | private AlarmManager mAlarmService = null; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 60 | /** List of tracked jobs, sorted asc. by deadline */ |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 61 | private final List<JobStatus> mTrackedJobs = new LinkedList<>(); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 62 | /** Singleton. */ |
| 63 | private static TimeController mSingleton; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 64 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 65 | public static synchronized TimeController get(JobSchedulerService jms) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 66 | if (mSingleton == null) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 67 | mSingleton = new TimeController(jms, jms.getContext(), jms.getLock()); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 68 | } |
| 69 | return mSingleton; |
| 70 | } |
| 71 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 72 | private TimeController(StateChangedListener stateChangedListener, Context context, |
| 73 | Object lock) { |
| 74 | super(stateChangedListener, context, lock); |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 75 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 76 | mNextJobExpiredElapsedMillis = Long.MAX_VALUE; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 77 | mNextDelayExpiredElapsedMillis = Long.MAX_VALUE; |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 78 | mChainedAttributionEnabled = WorkSource.isChainedBatteryAttributionEnabled(context); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 82 | * Check if the job has a timing constraint, and if so determine where to insert it in our |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 83 | * list. |
| 84 | */ |
| 85 | @Override |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 86 | public void maybeStartTrackingJobLocked(JobStatus job, JobStatus lastJob) { |
| 87 | if (job.hasTimingDelayConstraint() || job.hasDeadlineConstraint()) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 88 | maybeStopTrackingJobLocked(job, null, false); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 89 | |
| 90 | // First: check the constraints now, because if they are already satisfied |
| 91 | // then there is no need to track it. This gives us a fast path for a common |
| 92 | // pattern of having a job with a 0 deadline constraint ("run immediately"). |
| 93 | // Unlike most controllers, once one of our constraints has been satisfied, it |
| 94 | // will never be unsatisfied (our time base can not go backwards). |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 95 | final long nowElapsedMillis = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 96 | if (job.hasDeadlineConstraint() && evaluateDeadlineConstraint(job, nowElapsedMillis)) { |
| 97 | return; |
| 98 | } else if (job.hasTimingDelayConstraint() && evaluateTimingDelayConstraint(job, |
| 99 | nowElapsedMillis)) { |
| 100 | return; |
| 101 | } |
| 102 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 103 | boolean isInsert = false; |
| 104 | ListIterator<JobStatus> it = mTrackedJobs.listIterator(mTrackedJobs.size()); |
| 105 | while (it.hasPrevious()) { |
| 106 | JobStatus ts = it.previous(); |
| 107 | if (ts.getLatestRunTimeElapsed() < job.getLatestRunTimeElapsed()) { |
| 108 | // Insert |
| 109 | isInsert = true; |
| 110 | break; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 113 | if (isInsert) { |
| 114 | it.next(); |
| 115 | } |
| 116 | it.add(job); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 117 | job.setTrackingController(JobStatus.TRACKING_TIME); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 118 | maybeUpdateAlarmsLocked( |
| 119 | job.hasTimingDelayConstraint() ? job.getEarliestRunTime() : Long.MAX_VALUE, |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 120 | job.hasDeadlineConstraint() ? job.getLatestRunTimeElapsed() : Long.MAX_VALUE, |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 121 | deriveWorkSource(job.getSourceUid(), job.getSourcePackageName())); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 126 | * When we stop tracking a job, we only need to update our alarms if the job we're no longer |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 127 | * tracking was the one our alarms were based off of. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 128 | */ |
| 129 | @Override |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 130 | public void maybeStopTrackingJobLocked(JobStatus job, JobStatus incomingJob, |
| 131 | boolean forUpdate) { |
| 132 | if (job.clearTrackingController(JobStatus.TRACKING_TIME)) { |
| 133 | if (mTrackedJobs.remove(job)) { |
| 134 | checkExpiredDelaysAndResetAlarm(); |
| 135 | checkExpiredDeadlinesAndResetAlarm(); |
| 136 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 141 | * Determines whether this controller can stop tracking the given job. |
| 142 | * The controller is no longer interested in a job once its time constraint is satisfied, and |
| 143 | * the job's deadline is fulfilled - unlike other controllers a time constraint can't toggle |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 144 | * back and forth. |
| 145 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 146 | private boolean canStopTrackingJobLocked(JobStatus job) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 147 | return (!job.hasTimingDelayConstraint() || |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 148 | (job.satisfiedConstraints&JobStatus.CONSTRAINT_TIMING_DELAY) != 0) && |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 149 | (!job.hasDeadlineConstraint() || |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 150 | (job.satisfiedConstraints&JobStatus.CONSTRAINT_DEADLINE) != 0); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 153 | private void ensureAlarmServiceLocked() { |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 154 | if (mAlarmService == null) { |
| 155 | mAlarmService = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 160 | * Checks list of jobs for ones that have an expired deadline, sending them to the JobScheduler |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 161 | * if so, removing them from this list, and updating the alarm for the next expiry time. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 162 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 163 | private void checkExpiredDeadlinesAndResetAlarm() { |
| 164 | synchronized (mLock) { |
| 165 | long nextExpiryTime = Long.MAX_VALUE; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 166 | int nextExpiryUid = 0; |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 167 | String nextExpiryPackageName = null; |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 168 | final long nowElapsedMillis = sElapsedRealtimeClock.millis(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 169 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 170 | Iterator<JobStatus> it = mTrackedJobs.iterator(); |
| 171 | while (it.hasNext()) { |
| 172 | JobStatus job = it.next(); |
| 173 | if (!job.hasDeadlineConstraint()) { |
| 174 | continue; |
| 175 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 176 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 177 | if (evaluateDeadlineConstraint(job, nowElapsedMillis)) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 178 | mStateChangedListener.onRunJobNow(job); |
| 179 | it.remove(); |
| 180 | } else { // Sorted by expiry time, so take the next one and stop. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 181 | nextExpiryTime = job.getLatestRunTimeElapsed(); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 182 | nextExpiryUid = job.getSourceUid(); |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 183 | nextExpiryPackageName = job.getSourcePackageName(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 184 | break; |
| 185 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 186 | } |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 187 | setDeadlineExpiredAlarmLocked(nextExpiryTime, |
| 188 | deriveWorkSource(nextExpiryUid, nextExpiryPackageName)); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 189 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 192 | private boolean evaluateDeadlineConstraint(JobStatus job, long nowElapsedMillis) { |
| 193 | final long jobDeadline = job.getLatestRunTimeElapsed(); |
| 194 | |
| 195 | if (jobDeadline <= nowElapsedMillis) { |
| 196 | if (job.hasTimingDelayConstraint()) { |
| 197 | job.setTimingDelayConstraintSatisfied(true); |
| 198 | } |
| 199 | job.setDeadlineConstraintSatisfied(true); |
| 200 | return true; |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 205 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 206 | * Handles alarm that notifies us that a job's delay has expired. Iterates through the list of |
| 207 | * tracked jobs and marks them as ready as appropriate. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 208 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 209 | private void checkExpiredDelaysAndResetAlarm() { |
| 210 | synchronized (mLock) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 211 | final long nowElapsedMillis = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 212 | long nextDelayTime = Long.MAX_VALUE; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 213 | int nextDelayUid = 0; |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 214 | String nextDelayPackageName = null; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 215 | boolean ready = false; |
| 216 | Iterator<JobStatus> it = mTrackedJobs.iterator(); |
| 217 | while (it.hasNext()) { |
| 218 | final JobStatus job = it.next(); |
| 219 | if (!job.hasTimingDelayConstraint()) { |
| 220 | continue; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 221 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 222 | if (evaluateTimingDelayConstraint(job, nowElapsedMillis)) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 223 | if (canStopTrackingJobLocked(job)) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 224 | it.remove(); |
| 225 | } |
| 226 | if (job.isReady()) { |
| 227 | ready = true; |
| 228 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 229 | } else if (!job.isConstraintSatisfied(JobStatus.CONSTRAINT_TIMING_DELAY)) { |
| 230 | // If this job still doesn't have its delay constraint satisfied, |
| 231 | // then see if it is the next upcoming delay time for the alarm. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 232 | final long jobDelayTime = job.getEarliestRunTime(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 233 | if (nextDelayTime > jobDelayTime) { |
| 234 | nextDelayTime = jobDelayTime; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 235 | nextDelayUid = job.getSourceUid(); |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 236 | nextDelayPackageName = job.getSourcePackageName(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 237 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 240 | if (ready) { |
| 241 | mStateChangedListener.onControllerStateChanged(); |
| 242 | } |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 243 | setDelayExpiredAlarmLocked(nextDelayTime, |
| 244 | deriveWorkSource(nextDelayUid, nextDelayPackageName)); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | private WorkSource deriveWorkSource(int uid, @Nullable String packageName) { |
| 249 | if (mChainedAttributionEnabled) { |
| 250 | WorkSource ws = new WorkSource(); |
| 251 | ws.createWorkChain() |
| 252 | .addNode(uid, packageName) |
| 253 | .addNode(Process.SYSTEM_UID, "JobScheduler"); |
| 254 | return ws; |
| 255 | } else { |
| 256 | return packageName == null ? new WorkSource(uid) : new WorkSource(uid, packageName); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 257 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 260 | private boolean evaluateTimingDelayConstraint(JobStatus job, long nowElapsedMillis) { |
| 261 | final long jobDelayTime = job.getEarliestRunTime(); |
| 262 | if (jobDelayTime <= nowElapsedMillis) { |
| 263 | job.setTimingDelayConstraintSatisfied(true); |
| 264 | return true; |
| 265 | } |
| 266 | return false; |
| 267 | } |
| 268 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 269 | private void maybeUpdateAlarmsLocked(long delayExpiredElapsed, long deadlineExpiredElapsed, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 270 | WorkSource ws) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 271 | if (delayExpiredElapsed < mNextDelayExpiredElapsedMillis) { |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 272 | setDelayExpiredAlarmLocked(delayExpiredElapsed, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 273 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 274 | if (deadlineExpiredElapsed < mNextJobExpiredElapsedMillis) { |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 275 | setDeadlineExpiredAlarmLocked(deadlineExpiredElapsed, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 280 | * Set an alarm with the {@link android.app.AlarmManager} for the next time at which a job's |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 281 | * delay will expire. |
Shreyas Basarge | 4b7e339c | 2016-01-20 16:25:07 +0000 | [diff] [blame] | 282 | * This alarm <b>will</b> wake up the phone. |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 283 | */ |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 284 | private void setDelayExpiredAlarmLocked(long alarmTimeElapsedMillis, WorkSource ws) { |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 285 | alarmTimeElapsedMillis = maybeAdjustAlarmTime(alarmTimeElapsedMillis); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 286 | mNextDelayExpiredElapsedMillis = alarmTimeElapsedMillis; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 287 | updateAlarmWithListenerLocked(DELAY_TAG, mNextDelayExpiredListener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 288 | mNextDelayExpiredElapsedMillis, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 292 | * Set an alarm with the {@link android.app.AlarmManager} for the next time at which a job's |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 293 | * deadline will expire. |
| 294 | * This alarm <b>will</b> wake up the phone. |
| 295 | */ |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 296 | private void setDeadlineExpiredAlarmLocked(long alarmTimeElapsedMillis, WorkSource ws) { |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 297 | alarmTimeElapsedMillis = maybeAdjustAlarmTime(alarmTimeElapsedMillis); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 298 | mNextJobExpiredElapsedMillis = alarmTimeElapsedMillis; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 299 | updateAlarmWithListenerLocked(DEADLINE_TAG, mDeadlineExpiredListener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 300 | mNextJobExpiredElapsedMillis, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 303 | private long maybeAdjustAlarmTime(long proposedAlarmTimeElapsedMillis) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 304 | final long earliestWakeupTimeElapsed = sElapsedRealtimeClock.millis(); |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 305 | if (proposedAlarmTimeElapsedMillis < earliestWakeupTimeElapsed) { |
| 306 | return earliestWakeupTimeElapsed; |
| 307 | } |
| 308 | return proposedAlarmTimeElapsedMillis; |
| 309 | } |
| 310 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 311 | private void updateAlarmWithListenerLocked(String tag, OnAlarmListener listener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 312 | long alarmTimeElapsed, WorkSource ws) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 313 | ensureAlarmServiceLocked(); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 314 | if (alarmTimeElapsed == Long.MAX_VALUE) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 315 | mAlarmService.cancel(listener); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 316 | } else { |
| 317 | if (DEBUG) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 318 | Slog.d(TAG, "Setting " + tag + " for: " + alarmTimeElapsed); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 319 | } |
Shreyas Basarge | 4b7e339c | 2016-01-20 16:25:07 +0000 | [diff] [blame] | 320 | mAlarmService.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTimeElapsed, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 321 | AlarmManager.WINDOW_HEURISTIC, 0, tag, listener, null, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 322 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 325 | // Job/delay expiration alarm handling |
| 326 | |
| 327 | private final OnAlarmListener mDeadlineExpiredListener = new OnAlarmListener() { |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 328 | @Override |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 329 | public void onAlarm() { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 330 | if (DEBUG) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 331 | Slog.d(TAG, "Deadline-expired alarm fired"); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 332 | } |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 333 | checkExpiredDeadlinesAndResetAlarm(); |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | private final OnAlarmListener mNextDelayExpiredListener = new OnAlarmListener() { |
| 338 | @Override |
| 339 | public void onAlarm() { |
| 340 | if (DEBUG) { |
| 341 | Slog.d(TAG, "Delay-expired alarm fired"); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 342 | } |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 343 | checkExpiredDelaysAndResetAlarm(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 344 | } |
| 345 | }; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 346 | |
| 347 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 348 | public void dumpControllerStateLocked(PrintWriter pw, int filterUid) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 349 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 350 | pw.print("Alarms: now="); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 351 | pw.print(nowElapsed); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 352 | pw.println(); |
| 353 | pw.print("Next delay alarm in "); |
| 354 | TimeUtils.formatDuration(mNextDelayExpiredElapsedMillis, nowElapsed, pw); |
| 355 | pw.println(); |
| 356 | pw.print("Next deadline alarm in "); |
| 357 | TimeUtils.formatDuration(mNextJobExpiredElapsedMillis, nowElapsed, pw); |
| 358 | pw.println(); |
| 359 | pw.print("Tracking "); |
| 360 | pw.print(mTrackedJobs.size()); |
| 361 | pw.println(":"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 362 | for (JobStatus ts : mTrackedJobs) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 363 | if (!ts.shouldDump(filterUid)) { |
| 364 | continue; |
| 365 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 366 | pw.print(" #"); |
| 367 | ts.printUniqueId(pw); |
| 368 | pw.print(" from "); |
| 369 | UserHandle.formatUid(pw, ts.getSourceUid()); |
| 370 | pw.print(": Delay="); |
| 371 | if (ts.hasTimingDelayConstraint()) { |
| 372 | TimeUtils.formatDuration(ts.getEarliestRunTime(), nowElapsed, pw); |
| 373 | } else { |
| 374 | pw.print("N/A"); |
| 375 | } |
| 376 | pw.print(", Deadline="); |
| 377 | if (ts.hasDeadlineConstraint()) { |
| 378 | TimeUtils.formatDuration(ts.getLatestRunTimeElapsed(), nowElapsed, pw); |
| 379 | } else { |
| 380 | pw.print("N/A"); |
| 381 | } |
| 382 | pw.println(); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 385 | |
| 386 | @Override |
| 387 | public void dumpControllerStateLocked(ProtoOutputStream proto, long fieldId, int filterUid) { |
| 388 | final long token = proto.start(fieldId); |
| 389 | final long mToken = proto.start(StateControllerProto.TIME); |
| 390 | |
| 391 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
| 392 | proto.write(StateControllerProto.TimeController.NOW_ELAPSED_REALTIME, nowElapsed); |
| 393 | proto.write(StateControllerProto.TimeController.TIME_UNTIL_NEXT_DELAY_ALARM_MS, |
| 394 | mNextDelayExpiredElapsedMillis - nowElapsed); |
| 395 | proto.write(StateControllerProto.TimeController.TIME_UNTIL_NEXT_DEADLINE_ALARM_MS, |
| 396 | mNextJobExpiredElapsedMillis - nowElapsed); |
| 397 | |
| 398 | for (JobStatus ts : mTrackedJobs) { |
| 399 | if (!ts.shouldDump(filterUid)) { |
| 400 | continue; |
| 401 | } |
| 402 | final long tsToken = proto.start(StateControllerProto.TimeController.TRACKED_JOBS); |
| 403 | ts.writeToShortProto(proto, StateControllerProto.TimeController.TrackedJob.INFO); |
| 404 | |
| 405 | proto.write(StateControllerProto.TimeController.TrackedJob.HAS_TIMING_DELAY_CONSTRAINT, |
| 406 | ts.hasTimingDelayConstraint()); |
| 407 | proto.write(StateControllerProto.TimeController.TrackedJob.DELAY_TIME_REMAINING_MS, |
| 408 | ts.getEarliestRunTime() - nowElapsed); |
| 409 | |
| 410 | proto.write(StateControllerProto.TimeController.TrackedJob.HAS_DEADLINE_CONSTRAINT, |
| 411 | ts.hasDeadlineConstraint()); |
| 412 | proto.write(StateControllerProto.TimeController.TrackedJob.TIME_REMAINING_UNTIL_DEADLINE_MS, |
| 413 | ts.getLatestRunTimeElapsed() - nowElapsed); |
| 414 | |
| 415 | proto.end(tsToken); |
| 416 | } |
| 417 | |
| 418 | proto.end(mToken); |
| 419 | proto.end(token); |
| 420 | } |
| 421 | } |