blob: 9dd070cc942779d74e3451a8752c8ddb2b3fa617 [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
17// #define LOG_NDEBUG 0
18#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
30constexpr static pid_t OFFLINE_PID = -1;
31
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,
39 const std::shared_ptr<ProcessInfoInterface>& procInfo)
40 : mTranscoder(transcoder), mProcInfo(procInfo), mCurrentJob(nullptr), mResourceLost(false) {
41 // Only push empty offline queue initially. Realtime queues are added when requests come in.
42 mPidSortedList.push_back(OFFLINE_PID);
43 mOfflinePidIterator = mPidSortedList.begin();
44 mJobQueues.emplace(OFFLINE_PID, JobQueueType());
45}
46
47TranscodingJobScheduler::~TranscodingJobScheduler() {}
48
49TranscodingJobScheduler::Job* TranscodingJobScheduler::getTopJob_l() {
50 if (mJobMap.empty()) {
51 return nullptr;
52 }
53 pid_t topPid = *mPidSortedList.begin();
54 JobKeyType topJobKey = *mJobQueues[topPid].begin();
55 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
95 // Remove job from pid's queue.
96 const pid_t pid = mJobMap[jobKey].pid;
97 JobQueueType& jobQueue = mJobQueues[pid];
98 auto it = std::find(jobQueue.begin(), jobQueue.end(), jobKey);
99 if (it == jobQueue.end()) {
100 ALOGE("couldn't find job %s in queue for pid %d", jobToString(jobKey).c_str(), pid);
101 return;
102 }
103 jobQueue.erase(it);
104
105 // If this is the last job in a real-time queue, remove this pid's queue.
106 if (pid != OFFLINE_PID && jobQueue.empty()) {
107 mPidSortedList.remove(pid);
108 mJobQueues.erase(pid);
109 }
110
111 // Clear current job.
112 if (mCurrentJob == &mJobMap[jobKey]) {
113 mCurrentJob = nullptr;
114 }
115
116 // Remove job from job map.
117 mJobMap.erase(jobKey);
118}
119
120bool TranscodingJobScheduler::submit(ClientIdType clientId, int32_t jobId, pid_t pid,
121 const TranscodingRequestParcel& request,
122 const std::weak_ptr<ITranscodingClientCallback>& callback) {
123 JobKeyType jobKey = std::make_pair(clientId, jobId);
124
125 ALOGV("%s: job %s, pid %d, prioirty %d", __FUNCTION__, jobToString(jobKey).c_str(), pid,
126 (int32_t)request.priority);
127
128 std::scoped_lock lock{mLock};
129
130 if (mJobMap.count(jobKey) > 0) {
131 ALOGE("job %s already exists", jobToString(jobKey).c_str());
132 return false;
133 }
134
135 // TODO(chz): only support offline vs real-time for now. All kUnspecified jobs
136 // go to offline queue.
137 if (request.priority == TranscodingJobPriority::kUnspecified) {
138 pid = OFFLINE_PID;
139 }
140
141 // Add job to job map.
142 mJobMap[jobKey].key = jobKey;
143 mJobMap[jobKey].pid = pid;
144 mJobMap[jobKey].state = Job::NOT_STARTED;
145 mJobMap[jobKey].request = request;
146 mJobMap[jobKey].callback = callback;
147
148 // If it's an offline job, the queue was already added in constructor.
149 // If it's a real-time jobs, check if a queue is already present for the pid,
150 // and add a new queue if needed.
151 if (pid != OFFLINE_PID) {
152 if (mJobQueues.count(pid) == 0) {
153 if (mProcInfo->isProcessOnTop(pid)) {
154 mPidSortedList.push_front(pid);
155 } else {
156 // Shouldn't be submitting real-time requests from non-top app,
157 // put it in front of the offline queue.
158 mPidSortedList.insert(mOfflinePidIterator, pid);
159 }
160 } else if (pid != *mPidSortedList.begin()) {
161 if (mProcInfo->isProcessOnTop(pid)) {
162 mPidSortedList.remove(pid);
163 mPidSortedList.push_front(pid);
164 }
165 }
166 }
167 // Append this job to the pid's queue.
168 mJobQueues[pid].push_back(jobKey);
169
170 updateCurrentJob_l();
171
172 validateState_l();
173 return true;
174}
175
176bool TranscodingJobScheduler::cancel(ClientIdType clientId, int32_t jobId) {
177 JobKeyType jobKey = std::make_pair(clientId, jobId);
178
179 ALOGV("%s: job %s", __FUNCTION__, jobToString(jobKey).c_str());
180
181 std::scoped_lock lock{mLock};
182
183 if (mJobMap.count(jobKey) == 0) {
184 ALOGE("job %s doesn't exist", jobToString(jobKey).c_str());
185 return false;
186 }
187 // If the job is running, pause it first.
188 if (mJobMap[jobKey].state == Job::RUNNING) {
189 mTranscoder->pause(clientId, jobId);
190 }
191
192 // Remove the job.
193 removeJob_l(jobKey);
194
195 // Start next job.
196 updateCurrentJob_l();
197
198 validateState_l();
199 return true;
200}
201
202bool TranscodingJobScheduler::getJob(ClientIdType clientId, int32_t jobId,
203 TranscodingRequestParcel* request) {
204 JobKeyType jobKey = std::make_pair(clientId, jobId);
205
206 std::scoped_lock lock{mLock};
207
208 if (mJobMap.count(jobKey) == 0) {
209 ALOGE("job %s doesn't exist", jobToString(jobKey).c_str());
210 return false;
211 }
212
213 *(TranscodingRequest*)request = mJobMap[jobKey].request;
214 return true;
215}
216
217void TranscodingJobScheduler::onFinish(ClientIdType clientId, int32_t jobId) {
218 JobKeyType jobKey = std::make_pair(clientId, jobId);
219
220 ALOGV("%s: job %s", __FUNCTION__, jobToString(jobKey).c_str());
221
222 std::scoped_lock lock{mLock};
223
224 if (mJobMap.count(jobKey) == 0) {
225 ALOGW("ignoring abort for non-existent job");
226 return;
227 }
228
229 // Only ignore if job was never started. In particular, propagate the status
230 // to client if the job is paused. Transcoder could have posted finish when
231 // we're pausing it, and the finish arrived after we changed current job.
232 if (mJobMap[jobKey].state == Job::NOT_STARTED) {
233 ALOGW("ignoring abort for job that was never started");
234 return;
235 }
236
237 {
238 auto clientCallback = mJobMap[jobKey].callback.lock();
239 if (clientCallback != nullptr) {
240 clientCallback->onTranscodingFinished(jobId, TranscodingResultParcel({jobId, 0}));
241 }
242 }
243
244 // Remove the job.
245 removeJob_l(jobKey);
246
247 // Start next job.
248 updateCurrentJob_l();
249
250 validateState_l();
251}
252
253void TranscodingJobScheduler::onError(int64_t clientId, int32_t jobId, TranscodingErrorCode err) {
254 JobKeyType jobKey = std::make_pair(clientId, jobId);
255
256 ALOGV("%s: job %s, err %d", __FUNCTION__, jobToString(jobKey).c_str(), (int32_t)err);
257
258 std::scoped_lock lock{mLock};
259
260 if (mJobMap.count(jobKey) == 0) {
261 ALOGW("ignoring abort for non-existent job");
262 return;
263 }
264
265 // Only ignore if job was never started. In particular, propagate the status
266 // to client if the job is paused. Transcoder could have posted finish when
267 // we're pausing it, and the finish arrived after we changed current job.
268 if (mJobMap[jobKey].state == Job::NOT_STARTED) {
269 ALOGW("ignoring abort for job that was never started");
270 return;
271 }
272
273 {
274 auto clientCallback = mJobMap[jobKey].callback.lock();
275 if (clientCallback != nullptr) {
276 clientCallback->onTranscodingFailed(jobId, err);
277 }
278 }
279
280 // Remove the job.
281 removeJob_l(jobKey);
282
283 // Start next job.
284 updateCurrentJob_l();
285
286 validateState_l();
287}
288
289void TranscodingJobScheduler::onResourceLost() {
290 ALOGV("%s", __FUNCTION__);
291
292 std::scoped_lock lock{mLock};
293
294 // If we receive a resource loss event, the TranscoderLibrary already paused
295 // the transcoding, so we don't need to call onPaused to notify it to pause.
296 // Only need to update the job state here.
297 if (mCurrentJob != nullptr && mCurrentJob->state == Job::RUNNING) {
298 mCurrentJob->state = Job::PAUSED;
299 }
300 mResourceLost = true;
301
302 validateState_l();
303}
304
305void TranscodingJobScheduler::onTopProcessChanged(pid_t pid) {
306 ALOGV("%s: pid %d", __FUNCTION__, pid);
307
308 std::scoped_lock lock{mLock};
309
310 if (pid < 0) {
311 ALOGW("bringProcessToTop: ignoring invalid pid %d", pid);
312 return;
313 }
314 // If this pid doesn't have any jobs, we don't care about it.
315 if (mJobQueues.count(pid) == 0) {
316 ALOGW("bringProcessToTop: ignoring pid %d without any jobs", pid);
317 return;
318 }
319 // If this pid is already top, don't do anything.
320 if (pid == *mPidSortedList.begin()) {
321 ALOGW("pid %d is already top", pid);
322 return;
323 }
324
325 mPidSortedList.remove(pid);
326 mPidSortedList.push_front(pid);
327
328 updateCurrentJob_l();
329
330 validateState_l();
331}
332
333void TranscodingJobScheduler::onResourceAvailable() {
334 ALOGV("%s", __FUNCTION__);
335
336 std::scoped_lock lock{mLock};
337
338 mResourceLost = false;
339 updateCurrentJob_l();
340
341 validateState_l();
342}
343
344void TranscodingJobScheduler::validateState_l() {
345#ifdef VALIDATE_STATE
346 LOG_ALWAYS_FATAL_IF(mJobQueues.count(OFFLINE_PID) != 1,
347 "mJobQueues offline queue number is not 1");
348 LOG_ALWAYS_FATAL_IF(*mOfflinePidIterator != OFFLINE_PID,
349 "mOfflinePidIterator not pointing to offline pid");
350 LOG_ALWAYS_FATAL_IF(mPidSortedList.size() != mJobQueues.size(),
351 "mPidList and mJobQueues size mismatch");
352
353 int32_t totalJobs = 0;
354 for (auto pidIt = mPidSortedList.begin(); pidIt != mPidSortedList.end(); pidIt++) {
355 LOG_ALWAYS_FATAL_IF(mJobQueues.count(*pidIt) != 1, "mJobQueues count for pid %d is not 1",
356 *pidIt);
357 for (auto jobIt = mJobQueues[*pidIt].begin(); jobIt != mJobQueues[*pidIt].end(); jobIt++) {
358 LOG_ALWAYS_FATAL_IF(mJobMap.count(*jobIt) != 1, "mJobs count for job %s is not 1",
359 jobToString(*jobIt).c_str());
360 }
361
362 totalJobs += mJobQueues[*pidIt].size();
363 }
364 LOG_ALWAYS_FATAL_IF(mJobMap.size() != totalJobs,
365 "mJobs size doesn't match total jobs counted from pid queues");
366#endif // VALIDATE_STATE
367}
368
369} // namespace android