blob: 83d7a828e47eef8a3cf9e865a4db2b1efa797a7e [file] [log] [blame]
Chong Zhang6d58e4b2020-03-31 09:41:10 -07001/*
2 * Copyright (C) 2020 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
Chong Zhangacb33502020-04-20 11:04:48 -070017//#define LOG_NDEBUG 0
Chong Zhang6d58e4b2020-03-31 09:41:10 -070018#define LOG_TAG "TranscodingJobScheduler"
19
20#define VALIDATE_STATE 1
21
22#include <inttypes.h>
23#include <media/TranscodingJobScheduler.h>
24#include <utils/Log.h>
25
26#include <utility>
27
28namespace android {
29
Chong Zhang7ae4e2f2020-04-17 15:24:34 -070030constexpr static uid_t OFFLINE_UID = -1;
Chong Zhang6d58e4b2020-03-31 09:41:10 -070031
32//static
33String8 TranscodingJobScheduler::jobToString(const JobKeyType& jobKey) {
34 return String8::format("{client:%lld, job:%d}", (long long)jobKey.first, jobKey.second);
35}
36
37TranscodingJobScheduler::TranscodingJobScheduler(
38 const std::shared_ptr<TranscoderInterface>& transcoder,
Chong Zhang7ae4e2f2020-04-17 15:24:34 -070039 const std::shared_ptr<UidPolicyInterface>& uidPolicy)
40 : mTranscoder(transcoder), mUidPolicy(uidPolicy), mCurrentJob(nullptr), mResourceLost(false) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -070041 // Only push empty offline queue initially. Realtime queues are added when requests come in.
Chong Zhang7ae4e2f2020-04-17 15:24:34 -070042 mUidSortedList.push_back(OFFLINE_UID);
43 mOfflineUidIterator = mUidSortedList.begin();
44 mJobQueues.emplace(OFFLINE_UID, JobQueueType());
Chong Zhang6d58e4b2020-03-31 09:41:10 -070045}
46
47TranscodingJobScheduler::~TranscodingJobScheduler() {}
48
49TranscodingJobScheduler::Job* TranscodingJobScheduler::getTopJob_l() {
50 if (mJobMap.empty()) {
51 return nullptr;
52 }
Chong Zhang7ae4e2f2020-04-17 15:24:34 -070053 uid_t topUid = *mUidSortedList.begin();
54 JobKeyType topJobKey = *mJobQueues[topUid].begin();
Chong Zhang6d58e4b2020-03-31 09:41:10 -070055 return &mJobMap[topJobKey];
56}
57
58void TranscodingJobScheduler::updateCurrentJob_l() {
59 Job* topJob = getTopJob_l();
60 Job* curJob = mCurrentJob;
61 ALOGV("updateCurrentJob: topJob is %s, curJob is %s",
62 topJob == nullptr ? "null" : jobToString(topJob->key).c_str(),
63 curJob == nullptr ? "null" : jobToString(curJob->key).c_str());
64
65 // If we found a topJob that should be run, and it's not already running,
66 // take some actions to ensure it's running.
67 if (topJob != nullptr && (topJob != curJob || topJob->state != Job::RUNNING)) {
68 // If another job is currently running, pause it first.
69 if (curJob != nullptr && curJob->state == Job::RUNNING) {
70 mTranscoder->pause(curJob->key.first, curJob->key.second);
71 curJob->state = Job::PAUSED;
72 }
73 // If we are not experiencing resource loss, we can start or resume
74 // the topJob now.
75 if (!mResourceLost) {
76 if (topJob->state == Job::NOT_STARTED) {
77 mTranscoder->start(topJob->key.first, topJob->key.second);
78 } else if (topJob->state == Job::PAUSED) {
79 mTranscoder->resume(topJob->key.first, topJob->key.second);
80 }
81 topJob->state = Job::RUNNING;
82 }
83 }
84 mCurrentJob = topJob;
85}
86
87void TranscodingJobScheduler::removeJob_l(const JobKeyType& jobKey) {
88 ALOGV("%s: job %s", __FUNCTION__, jobToString(jobKey).c_str());
89
90 if (mJobMap.count(jobKey) == 0) {
91 ALOGE("job %s doesn't exist", jobToString(jobKey).c_str());
92 return;
93 }
94
Chong Zhang7ae4e2f2020-04-17 15:24:34 -070095 // Remove job from uid's queue.
96 const uid_t uid = mJobMap[jobKey].uid;
97 JobQueueType& jobQueue = mJobQueues[uid];
Chong Zhang6d58e4b2020-03-31 09:41:10 -070098 auto it = std::find(jobQueue.begin(), jobQueue.end(), jobKey);
99 if (it == jobQueue.end()) {
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700100 ALOGE("couldn't find job %s in queue for uid %d", jobToString(jobKey).c_str(), uid);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700101 return;
102 }
103 jobQueue.erase(it);
104
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700105 // If this is the last job in a real-time queue, remove this uid's queue.
106 if (uid != OFFLINE_UID && jobQueue.empty()) {
107 mUidSortedList.remove(uid);
108 mJobQueues.erase(uid);
Chong Zhangacb33502020-04-20 11:04:48 -0700109 mUidPolicy->unregisterMonitorUid(uid);
110
111 std::unordered_set<uid_t> topUids = mUidPolicy->getTopUids();
112 moveUidsToTop_l(topUids, false /*preserveTopUid*/);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700113 }
114
115 // Clear current job.
116 if (mCurrentJob == &mJobMap[jobKey]) {
117 mCurrentJob = nullptr;
118 }
119
120 // Remove job from job map.
121 mJobMap.erase(jobKey);
122}
123
Chong Zhangacb33502020-04-20 11:04:48 -0700124/**
125 * Moves the set of uids to the front of mUidSortedList (which is used to pick
126 * the next job to run).
127 *
128 * This is called when 1) we received a onTopUidsChanged() callbcak from UidPolicy,
129 * or 2) we removed the job queue for a uid because it becomes empty.
130 *
131 * In case of 1), if there are multiple uids in the set, and the current front
132 * uid in mUidSortedList is still in the set, we try to keep that uid at front
133 * so that current job run is not interrupted. (This is not a concern for case 2)
134 * because the queue for a uid was just removed entirely.)
135 */
136void TranscodingJobScheduler::moveUidsToTop_l(const std::unordered_set<uid_t>& uids,
137 bool preserveTopUid) {
138 // If uid set is empty, nothing to do. Do not change the queue status.
139 if (uids.empty()) {
140 return;
141 }
142
143 // Save the current top uid.
144 uid_t curTopUid = *mUidSortedList.begin();
145 bool pushCurTopToFront = false;
146 int32_t numUidsMoved = 0;
147
148 // Go through the sorted uid list once, and move the ones in top set to front.
149 for (auto it = mUidSortedList.begin(); it != mUidSortedList.end();) {
150 uid_t uid = *it;
151
152 if (uid != OFFLINE_UID && uids.count(uid) > 0) {
153 it = mUidSortedList.erase(it);
154
155 // If this is the top we're preserving, don't push it here, push
156 // it after the for-loop.
157 if (uid == curTopUid && preserveTopUid) {
158 pushCurTopToFront = true;
159 } else {
160 mUidSortedList.push_front(uid);
161 }
162
163 // If we found all uids in the set, break out.
164 if (++numUidsMoved == uids.size()) {
165 break;
166 }
167 } else {
168 ++it;
169 }
170 }
171
172 if (pushCurTopToFront) {
173 mUidSortedList.push_front(curTopUid);
174 }
175}
176
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700177bool TranscodingJobScheduler::submit(ClientIdType clientId, int32_t jobId, uid_t uid,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700178 const TranscodingRequestParcel& request,
179 const std::weak_ptr<ITranscodingClientCallback>& callback) {
180 JobKeyType jobKey = std::make_pair(clientId, jobId);
181
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700182 ALOGV("%s: job %s, uid %d, prioirty %d", __FUNCTION__, jobToString(jobKey).c_str(), uid,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700183 (int32_t)request.priority);
184
185 std::scoped_lock lock{mLock};
186
187 if (mJobMap.count(jobKey) > 0) {
188 ALOGE("job %s already exists", jobToString(jobKey).c_str());
189 return false;
190 }
191
192 // TODO(chz): only support offline vs real-time for now. All kUnspecified jobs
193 // go to offline queue.
194 if (request.priority == TranscodingJobPriority::kUnspecified) {
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700195 uid = OFFLINE_UID;
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700196 }
197
198 // Add job to job map.
199 mJobMap[jobKey].key = jobKey;
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700200 mJobMap[jobKey].uid = uid;
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700201 mJobMap[jobKey].state = Job::NOT_STARTED;
202 mJobMap[jobKey].request = request;
203 mJobMap[jobKey].callback = callback;
204
205 // If it's an offline job, the queue was already added in constructor.
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700206 // If it's a real-time jobs, check if a queue is already present for the uid,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700207 // and add a new queue if needed.
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700208 if (uid != OFFLINE_UID) {
209 if (mJobQueues.count(uid) == 0) {
Chong Zhangacb33502020-04-20 11:04:48 -0700210 mUidPolicy->registerMonitorUid(uid);
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700211 if (mUidPolicy->isUidOnTop(uid)) {
212 mUidSortedList.push_front(uid);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700213 } else {
214 // Shouldn't be submitting real-time requests from non-top app,
215 // put it in front of the offline queue.
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700216 mUidSortedList.insert(mOfflineUidIterator, uid);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700217 }
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700218 } else if (uid != *mUidSortedList.begin()) {
219 if (mUidPolicy->isUidOnTop(uid)) {
220 mUidSortedList.remove(uid);
221 mUidSortedList.push_front(uid);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700222 }
223 }
224 }
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700225 // Append this job to the uid's queue.
226 mJobQueues[uid].push_back(jobKey);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700227
228 updateCurrentJob_l();
229
230 validateState_l();
231 return true;
232}
233
234bool TranscodingJobScheduler::cancel(ClientIdType clientId, int32_t jobId) {
235 JobKeyType jobKey = std::make_pair(clientId, jobId);
236
237 ALOGV("%s: job %s", __FUNCTION__, jobToString(jobKey).c_str());
238
239 std::scoped_lock lock{mLock};
240
241 if (mJobMap.count(jobKey) == 0) {
242 ALOGE("job %s doesn't exist", jobToString(jobKey).c_str());
243 return false;
244 }
245 // If the job is running, pause it first.
246 if (mJobMap[jobKey].state == Job::RUNNING) {
247 mTranscoder->pause(clientId, jobId);
248 }
249
250 // Remove the job.
251 removeJob_l(jobKey);
252
253 // Start next job.
254 updateCurrentJob_l();
255
256 validateState_l();
257 return true;
258}
259
260bool TranscodingJobScheduler::getJob(ClientIdType clientId, int32_t jobId,
261 TranscodingRequestParcel* request) {
262 JobKeyType jobKey = std::make_pair(clientId, jobId);
263
264 std::scoped_lock lock{mLock};
265
266 if (mJobMap.count(jobKey) == 0) {
267 ALOGE("job %s doesn't exist", jobToString(jobKey).c_str());
268 return false;
269 }
270
271 *(TranscodingRequest*)request = mJobMap[jobKey].request;
272 return true;
273}
274
275void TranscodingJobScheduler::onFinish(ClientIdType clientId, int32_t jobId) {
276 JobKeyType jobKey = std::make_pair(clientId, jobId);
277
278 ALOGV("%s: job %s", __FUNCTION__, jobToString(jobKey).c_str());
279
280 std::scoped_lock lock{mLock};
281
282 if (mJobMap.count(jobKey) == 0) {
Chong Zhangacb33502020-04-20 11:04:48 -0700283 ALOGW("ignoring finish for non-existent job");
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700284 return;
285 }
286
287 // Only ignore if job was never started. In particular, propagate the status
288 // to client if the job is paused. Transcoder could have posted finish when
289 // we're pausing it, and the finish arrived after we changed current job.
290 if (mJobMap[jobKey].state == Job::NOT_STARTED) {
Chong Zhangacb33502020-04-20 11:04:48 -0700291 ALOGW("ignoring finish for job that was never started");
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700292 return;
293 }
294
295 {
296 auto clientCallback = mJobMap[jobKey].callback.lock();
297 if (clientCallback != nullptr) {
298 clientCallback->onTranscodingFinished(jobId, TranscodingResultParcel({jobId, 0}));
299 }
300 }
301
302 // Remove the job.
303 removeJob_l(jobKey);
304
305 // Start next job.
306 updateCurrentJob_l();
307
308 validateState_l();
309}
310
311void TranscodingJobScheduler::onError(int64_t clientId, int32_t jobId, TranscodingErrorCode err) {
312 JobKeyType jobKey = std::make_pair(clientId, jobId);
313
314 ALOGV("%s: job %s, err %d", __FUNCTION__, jobToString(jobKey).c_str(), (int32_t)err);
315
316 std::scoped_lock lock{mLock};
317
318 if (mJobMap.count(jobKey) == 0) {
Chong Zhangacb33502020-04-20 11:04:48 -0700319 ALOGW("ignoring error for non-existent job");
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700320 return;
321 }
322
323 // Only ignore if job was never started. In particular, propagate the status
324 // to client if the job is paused. Transcoder could have posted finish when
325 // we're pausing it, and the finish arrived after we changed current job.
326 if (mJobMap[jobKey].state == Job::NOT_STARTED) {
Chong Zhangacb33502020-04-20 11:04:48 -0700327 ALOGW("ignoring error for job that was never started");
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700328 return;
329 }
330
331 {
332 auto clientCallback = mJobMap[jobKey].callback.lock();
333 if (clientCallback != nullptr) {
334 clientCallback->onTranscodingFailed(jobId, err);
335 }
336 }
337
338 // Remove the job.
339 removeJob_l(jobKey);
340
341 // Start next job.
342 updateCurrentJob_l();
343
344 validateState_l();
345}
346
Chong Zhangacb33502020-04-20 11:04:48 -0700347void TranscodingJobScheduler::onProgressUpdate(int64_t clientId, int32_t jobId, int32_t progress) {
348 JobKeyType jobKey = std::make_pair(clientId, jobId);
349
350 ALOGV("%s: job %s, progress %d", __FUNCTION__, jobToString(jobKey).c_str(), progress);
351
352 std::scoped_lock lock{mLock};
353
354 if (mJobMap.count(jobKey) == 0) {
355 ALOGW("ignoring progress for non-existent job");
356 return;
357 }
358
359 // Only ignore if job was never started. In particular, propagate the status
360 // to client if the job is paused. Transcoder could have posted finish when
361 // we're pausing it, and the finish arrived after we changed current job.
362 if (mJobMap[jobKey].state == Job::NOT_STARTED) {
363 ALOGW("ignoring progress for job that was never started");
364 return;
365 }
366
367 {
368 auto clientCallback = mJobMap[jobKey].callback.lock();
369 if (clientCallback != nullptr) {
370 clientCallback->onProgressUpdate(jobId, progress);
371 }
372 }
373}
374
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700375void TranscodingJobScheduler::onResourceLost() {
376 ALOGV("%s", __FUNCTION__);
377
378 std::scoped_lock lock{mLock};
379
380 // If we receive a resource loss event, the TranscoderLibrary already paused
381 // the transcoding, so we don't need to call onPaused to notify it to pause.
382 // Only need to update the job state here.
383 if (mCurrentJob != nullptr && mCurrentJob->state == Job::RUNNING) {
384 mCurrentJob->state = Job::PAUSED;
385 }
386 mResourceLost = true;
387
388 validateState_l();
389}
390
Chong Zhangacb33502020-04-20 11:04:48 -0700391void TranscodingJobScheduler::onTopUidsChanged(const std::unordered_set<uid_t>& uids) {
392 if (uids.empty()) {
393 ALOGW("%s: ignoring empty uids", __FUNCTION__);
394 return;
395 }
396
397 std::string uidStr;
398 for (auto it = uids.begin(); it != uids.end(); it++) {
399 if (!uidStr.empty()) {
400 uidStr += ", ";
401 }
402 uidStr += std::to_string(*it);
403 }
404
405 ALOGD("%s: topUids: size %zu, uids: %s", __FUNCTION__, uids.size(), uidStr.c_str());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700406
407 std::scoped_lock lock{mLock};
408
Chong Zhangacb33502020-04-20 11:04:48 -0700409 moveUidsToTop_l(uids, true /*preserveTopUid*/);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700410
411 updateCurrentJob_l();
412
413 validateState_l();
414}
415
416void TranscodingJobScheduler::onResourceAvailable() {
417 ALOGV("%s", __FUNCTION__);
418
419 std::scoped_lock lock{mLock};
420
421 mResourceLost = false;
422 updateCurrentJob_l();
423
424 validateState_l();
425}
426
427void TranscodingJobScheduler::validateState_l() {
428#ifdef VALIDATE_STATE
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700429 LOG_ALWAYS_FATAL_IF(mJobQueues.count(OFFLINE_UID) != 1,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700430 "mJobQueues offline queue number is not 1");
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700431 LOG_ALWAYS_FATAL_IF(*mOfflineUidIterator != OFFLINE_UID,
432 "mOfflineUidIterator not pointing to offline uid");
433 LOG_ALWAYS_FATAL_IF(mUidSortedList.size() != mJobQueues.size(),
434 "mUidList and mJobQueues size mismatch");
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700435
436 int32_t totalJobs = 0;
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700437 for (auto uidIt = mUidSortedList.begin(); uidIt != mUidSortedList.end(); uidIt++) {
438 LOG_ALWAYS_FATAL_IF(mJobQueues.count(*uidIt) != 1, "mJobQueues count for uid %d is not 1",
439 *uidIt);
440 for (auto jobIt = mJobQueues[*uidIt].begin(); jobIt != mJobQueues[*uidIt].end(); jobIt++) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700441 LOG_ALWAYS_FATAL_IF(mJobMap.count(*jobIt) != 1, "mJobs count for job %s is not 1",
442 jobToString(*jobIt).c_str());
443 }
444
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700445 totalJobs += mJobQueues[*uidIt].size();
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700446 }
447 LOG_ALWAYS_FATAL_IF(mJobMap.size() != totalJobs,
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700448 "mJobs size doesn't match total jobs counted from uid queues");
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700449#endif // VALIDATE_STATE
450}
451
452} // namespace android