1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "queue_handler.h"
16 #include <sys/syscall.h>
17 #include <sstream>
18 #include "dfx/log/ffrt_log_api.h"
19 #include "dfx/trace_record/ffrt_trace_record.h"
20 #include "util/event_handler_adapter.h"
21 #include "util/ffrt_facade.h"
22 #include "util/slab.h"
23 #include "tm/queue_task.h"
24 #include "concurrent_queue.h"
25 #include "eventhandler_adapter_queue.h"
26 #include "sched/scheduler.h"
27
28 namespace {
29 constexpr int PROCESS_NAME_BUFFER_LENGTH = 1024;
30 constexpr uint32_t STRING_SIZE_MAX = 128;
31 constexpr uint32_t TASK_DONE_WAIT_UNIT = 10;
32 constexpr uint64_t SCHED_TIME_ACC_ERROR_US = 5000; // 5ms
33 constexpr uint32_t CONGESTION_CNT = 5;
34 constexpr uint32_t CONGESTION_TIMEOUT_US = 300000000; // 5min
35 }
36
37 namespace ffrt {
QueueHandler(const char * name,const ffrt_queue_attr_t * attr,const int type)38 QueueHandler::QueueHandler(const char* name, const ffrt_queue_attr_t* attr, const int type)
39 {
40 // parse queue attribute
41 if (attr) {
42 qos_ = (ffrt_queue_attr_get_qos(attr) >= ffrt_qos_background) ? ffrt_queue_attr_get_qos(attr) : qos_;
43 timeout_ = ffrt_queue_attr_get_timeout(attr);
44 timeoutCb_ = ffrt_queue_attr_get_callback(attr);
45 }
46
47 // callback reference counting is to ensure life cycle
48 if (timeout_ > 0 && timeoutCb_ != nullptr) {
49 QueueTask* cbTask = GetQueueTaskByFuncStorageOffset(timeoutCb_);
50 cbTask->IncDeleteRef();
51 }
52
53 queue_ = CreateQueue(type, attr);
54 FFRT_COND_DO_ERR((queue_ == nullptr), return, "[queueId=%u] constructed failed", GetQueueId());
55
56 if (name != nullptr && std::string(name).size() <= STRING_SIZE_MAX) {
57 name_ = "sq_" + std::string(name) + "_" + std::to_string(GetQueueId());
58 } else {
59 name_ += "sq_unnamed_" + std::to_string(GetQueueId());
60 FFRT_LOGW("failed to set [queueId=%u] name due to invalid name or length.", GetQueueId());
61 }
62
63 FFRTFacade::GetQMInstance().RegisterQueueId(GetQueueId(), this);
64 FFRT_LOGI("construct %s succ, qos[%d]", name_.c_str(), qos_);
65 }
66
~QueueHandler()67 QueueHandler::~QueueHandler()
68 {
69 FFRT_LOGI("destruct %s enter", name_.c_str());
70 // clear tasks in queue
71 CancelAndWait();
72 FFRTFacade::GetQMInstance().ResetQueueStruct(GetQueueId());
73
74 // release callback resource
75 if (timeout_ > 0) {
76 // wait for all delayedWorker to complete.
77 while (delayedCbCnt_.load() > 0) {
78 this_task::sleep_for(std::chrono::microseconds(timeout_));
79 }
80
81 if (timeoutCb_ != nullptr) {
82 QueueTask* cbTask = GetQueueTaskByFuncStorageOffset(timeoutCb_);
83 cbTask->DecDeleteRef();
84 }
85 }
86
87 if (we_ != nullptr) {
88 DelayedRemove(we_->tp, we_);
89 SimpleAllocator<WaitUntilEntry>::FreeMem(we_);
90 }
91 FFRT_LOGI("destruct %s leave", name_.c_str());
92 }
93
SetLoop(Loop * loop)94 bool QueueHandler::SetLoop(Loop* loop)
95 {
96 FFRT_COND_DO_ERR((queue_ == nullptr), return false, "[queueId=%u] constructed failed", GetQueueId());
97 if (queue_->GetQueueType() == ffrt_queue_eventhandler_interactive) {
98 return true;
99 }
100 FFRT_COND_DO_ERR((queue_->GetQueueType() != ffrt_queue_concurrent),
101 return false, "[queueId=%u] type invalid", GetQueueId());
102 return reinterpret_cast<ConcurrentQueue*>(queue_.get())->SetLoop(loop);
103 }
104
ClearLoop()105 bool QueueHandler::ClearLoop()
106 {
107 FFRT_COND_DO_ERR((queue_ == nullptr), return false, "[queueId=%u] constructed failed", GetQueueId());
108 FFRT_COND_DO_ERR((queue_->GetQueueType() != ffrt_queue_concurrent),
109 return false, "[queueId=%u] type invalid", GetQueueId());
110 return reinterpret_cast<ConcurrentQueue*>(queue_.get())->ClearLoop();
111 }
112
PickUpTask()113 QueueTask* QueueHandler::PickUpTask()
114 {
115 FFRT_COND_DO_ERR((queue_ == nullptr), return nullptr, "[queueId=%u] constructed failed", GetQueueId());
116 return queue_->Pull();
117 }
118
Submit(QueueTask * task)119 void QueueHandler::Submit(QueueTask* task)
120 {
121 FFRT_COND_DO_ERR((queue_ == nullptr), return, "cannot submit, [queueId=%u] constructed failed", GetQueueId());
122 FFRT_COND_DO_ERR((task == nullptr), return, "input invalid, serial task is nullptr");
123
124 // if qos not specified, qos of the queue is inherited by task
125 if (task->GetQos() == qos_inherit || task->GetQos() == qos_default) {
126 task->SetQos(qos_);
127 }
128
129 uint64_t gid = task->gid;
130 FFRT_SERIAL_QUEUE_TASK_SUBMIT_MARKER(GetQueueId(), gid);
131 FFRTTraceRecord::TaskSubmit(&(task->createTime), &(task->fromTid));
132 #if (FFRT_TRACE_RECORD_LEVEL < FFRT_TRACE_RECORD_LEVEL_1)
133 if (queue_->GetQueueType() == ffrt_queue_eventhandler_adapter) {
134 task->fromTid = ExecuteCtx::Cur()->tid;
135 }
136 #endif
137
138 // work after that schedule timeout is set for queue
139 if (task->GetSchedTimeout() > 0) {
140 AddSchedDeadline(task);
141 }
142 if (we_ != nullptr) {
143 CheckOverload();
144 }
145
146 int ret = queue_->Push(task);
147 if (ret == SUCC) {
148 FFRT_LOGD("submit task[%lu] into %s", gid, name_.c_str());
149 return;
150 }
151 if (ret == FAILED) {
152 return;
153 }
154
155 if (!isUsed_.load()) {
156 isUsed_.store(true);
157 }
158
159 // activate queue
160 if (task->GetDelay() == 0) {
161 FFRT_LOGD("task [%llu] activate %s", gid, name_.c_str());
162 TransferTask(task);
163 } else {
164 FFRT_LOGD("task [%llu] with delay [%llu] activate %s", gid, task->GetDelay(), name_.c_str());
165 if (ret == INACTIVE) {
166 queue_->Push(task);
167 }
168 TransferInitTask();
169 }
170 }
171
Cancel()172 void QueueHandler::Cancel()
173 {
174 FFRT_COND_DO_ERR((queue_ == nullptr), return, "cannot cancel, [queueId=%u] constructed failed", GetQueueId());
175 queue_->Remove();
176 }
177
CancelAndWait()178 void QueueHandler::CancelAndWait()
179 {
180 FFRT_COND_DO_ERR((queue_ == nullptr), return, "cannot cancelAndWait, [queueId=%u] constructed failed",
181 GetQueueId());
182 queue_->Stop();
183 while (FFRTFacade::GetQMInstance().QueryQueueStatus(GetQueueId()) || queue_->GetActiveStatus() || isUsed_.load()) {
184 std::this_thread::sleep_for(std::chrono::microseconds(TASK_DONE_WAIT_UNIT));
185 }
186 }
187
Cancel(const char * name)188 int QueueHandler::Cancel(const char* name)
189 {
190 FFRT_COND_DO_ERR((queue_ == nullptr), return INACTIVE,
191 "cannot cancel, [queueId=%u] constructed failed", GetQueueId());
192 int ret = queue_->Remove(name);
193 if (ret != SUCC) {
194 FFRT_LOGD("cancel task %s failed, task may have been executed", name);
195 }
196
197 return ret;
198 }
199
Cancel(QueueTask * task)200 int QueueHandler::Cancel(QueueTask* task)
201 {
202 FFRT_COND_DO_ERR((queue_ == nullptr), return INACTIVE,
203 "cannot cancel, [queueId=%u] constructed failed", GetQueueId());
204 FFRT_COND_DO_ERR((task == nullptr), return INACTIVE, "input invalid, serial task is nullptr");
205
206 if (task->GetSchedTimeout() > 0) {
207 RemoveSchedDeadline(task);
208 }
209
210 int ret = queue_->Remove(task);
211 if (ret == SUCC) {
212 FFRT_LOGD("cancel task[%llu] %s succ", task->gid, task->label.c_str());
213 task->Notify();
214 task->Destroy();
215 } else {
216 FFRT_LOGD("cancel task[%llu] %s failed, task may have been executed", task->gid, task->label.c_str());
217 }
218 return ret;
219 }
220
Dispatch(QueueTask * inTask)221 void QueueHandler::Dispatch(QueueTask* inTask)
222 {
223 QueueTask* nextTask = nullptr;
224 for (QueueTask* task = inTask; task != nullptr; task = nextTask) {
225 // dfx watchdog
226 SetTimeoutMonitor(task);
227 FFRTFacade::GetQMInstance().UpdateQueueInfo(GetQueueId(), task->gid);
228 execTaskId_.store(task->gid);
229
230 // run user task
231 FFRT_LOGD("run task [gid=%llu], queueId=%u", task->gid, GetQueueId());
232 auto f = reinterpret_cast<ffrt_function_header_t*>(task->func_storage);
233 FFRT_SERIAL_QUEUE_TASK_EXECUTE_MARKER(task->gid);
234 FFRTTraceRecord::TaskExecute(&(task->executeTime));
235 if (task->GetSchedTimeout() > 0) {
236 RemoveSchedDeadline(task);
237 }
238
239 uint64_t triggerTime{0};
240 if (queue_->GetQueueType() == ffrt_queue_eventhandler_adapter) {
241 triggerTime = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(
242 std::chrono::steady_clock::now().time_since_epoch()).count());
243 }
244
245 f->exec(f);
246 FFRTTraceRecord::TaskDone<ffrt_queue_task>(task->GetQos(), task);
247 if (queue_->GetQueueType() == ffrt_queue_eventhandler_adapter) {
248 uint64_t completeTime = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(
249 std::chrono::steady_clock::now().time_since_epoch()).count());
250 reinterpret_cast<EventHandlerAdapterQueue*>(queue_.get())->PushHistoryTask(task, triggerTime, completeTime);
251 }
252
253 f->destroy(f);
254 task->Notify();
255
256 // run task batch
257 nextTask = task->GetNextTask();
258 if (nextTask == nullptr) {
259 FFRTFacade::GetQMInstance().ResetQueueInfo(GetQueueId());
260 execTaskId_.store(0);
261 if (!queue_->IsOnLoop()) {
262 Deliver();
263 }
264 }
265 task->DecDeleteRef();
266 }
267 }
268
Deliver()269 void QueueHandler::Deliver()
270 {
271 QueueTask* task = queue_->Pull();
272 if (task != nullptr) {
273 TransferTask(task);
274 } else if (!queue_->GetActiveStatus()) {
275 isUsed_.store(false);
276 }
277 }
278
TransferTask(QueueTask * task)279 void QueueHandler::TransferTask(QueueTask* task)
280 {
281 auto entry = &task->fq_we;
282 if (queue_->GetQueueType() == ffrt_queue_eventhandler_adapter) {
283 reinterpret_cast<EventHandlerAdapterQueue*>(queue_.get())->SetCurrentRunningTask(task);
284 }
285 FFRTScheduler* sch = FFRTFacade::GetSchedInstance();
286 FFRT_READY_MARKER(task->gid); // ffrt queue task ready to enque
287 if (!sch->InsertNode(&entry->node, task->GetQos())) {
288 FFRT_LOGE("failed to insert task [%llu] into %s", task->gid, GetQueueId(), name_.c_str());
289 return;
290 }
291 }
292
TransferInitTask()293 void QueueHandler::TransferInitTask()
294 {
295 std::function<void()> initFunc = [] {};
296 auto f = create_function_wrapper(initFunc, ffrt_function_kind_queue);
297 QueueTask* initTask = GetQueueTaskByFuncStorageOffset(f);
298 new (initTask)ffrt::QueueTask(this);
299 initTask->SetQos(qos_);
300 TransferTask(initTask);
301 }
302
SetTimeoutMonitor(QueueTask * task)303 void QueueHandler::SetTimeoutMonitor(QueueTask* task)
304 {
305 if (timeout_ <= 0) {
306 return;
307 }
308
309 task->IncDeleteRef();
310 WaitUntilEntry* we = new (SimpleAllocator<WaitUntilEntry>::AllocMem()) WaitUntilEntry();
311 // set delayed worker callback
312 we->cb = ([this, task](WaitEntry* we) {
313 if (!task->GetFinishStatus()) {
314 RunTimeOutCallback(task);
315 }
316 delayedCbCnt_.fetch_sub(1);
317 task->DecDeleteRef();
318 SimpleAllocator<WaitUntilEntry>::FreeMem(static_cast<WaitUntilEntry*>(we));
319 });
320
321 // set delayed worker wakeup time
322 std::chrono::microseconds timeout(timeout_);
323 auto now = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
324 we->tp = std::chrono::time_point_cast<std::chrono::steady_clock::duration>(now + timeout);
325
326 if (!DelayedWakeup(we->tp, we, we->cb)) {
327 task->DecDeleteRef();
328 SimpleAllocator<WaitUntilEntry>::FreeMem(we);
329 FFRT_LOGW("failed to set watchdog for task gid=%llu in %s with timeout [%llu us] ", task->gid,
330 name_.c_str(), timeout_);
331 return;
332 }
333
334 delayedCbCnt_.fetch_add(1);
335 FFRT_LOGD("set watchdog of task gid=%llu of %s succ", task->gid, name_.c_str());
336 }
337
RunTimeOutCallback(QueueTask * task)338 void QueueHandler::RunTimeOutCallback(QueueTask* task)
339 {
340 std::stringstream ss;
341 static std::once_flag flag;
342 static char processName[PROCESS_NAME_BUFFER_LENGTH];
343 std::call_once(flag, []() {
344 GetProcessName(processName, PROCESS_NAME_BUFFER_LENGTH);
345 });
346 std::string processNameStr = std::string(processName);
347 ss << "[Serial_Queue_Timeout_Callback] process name:[" << processNameStr << "], serial queue:[" <<
348 name_ << "], queueId:[" << GetQueueId() << "], serial task gid:[" << task->gid << "], task name:["
349 << task->label << "], execution time exceeds[" << timeout_ << "] us";
350 FFRT_LOGE("%s", ss.str().c_str());
351 if (timeoutCb_ != nullptr) {
352 timeoutCb_->exec(timeoutCb_);
353 }
354 }
355
GetDfxInfo() const356 std::string QueueHandler::GetDfxInfo() const
357 {
358 std::stringstream ss;
359 ss << " queue name [" << name_ << "]";
360 if (queue_ != nullptr) {
361 ss << ", remaining tasks count=" << queue_->GetMapSize();
362 }
363 return ss.str();
364 }
365
IsIdle()366 bool QueueHandler::IsIdle()
367 {
368 FFRT_COND_DO_ERR((queue_ == nullptr), return false, "[queueId=%u] constructed failed", GetQueueId());
369 FFRT_COND_DO_ERR((queue_->GetQueueType() != ffrt_queue_eventhandler_adapter),
370 return false, "[queueId=%u] type invalid", GetQueueId());
371
372 return reinterpret_cast<EventHandlerAdapterQueue*>(queue_.get())->IsIdle();
373 }
374
SetEventHandler(void * eventHandler)375 void QueueHandler::SetEventHandler(void* eventHandler)
376 {
377 FFRT_COND_DO_ERR((queue_ == nullptr), return, "[queueId=%u] constructed failed", GetQueueId());
378
379 bool typeInvalid = (queue_->GetQueueType() != ffrt_queue_eventhandler_interactive) &&
380 (queue_->GetQueueType() != ffrt_queue_eventhandler_adapter);
381 FFRT_COND_DO_ERR(typeInvalid, return, "[queueId=%u] type invalid", GetQueueId());
382
383 reinterpret_cast<EventHandlerInteractiveQueue*>(queue_.get())->SetEventHandler(eventHandler);
384 }
385
GetEventHandler()386 void* QueueHandler::GetEventHandler()
387 {
388 FFRT_COND_DO_ERR((queue_ == nullptr), return nullptr, "[queueId=%u] constructed failed", GetQueueId());
389
390 bool typeInvalid = (queue_->GetQueueType() != ffrt_queue_eventhandler_interactive) &&
391 (queue_->GetQueueType() != ffrt_queue_eventhandler_adapter);
392 FFRT_COND_DO_ERR(typeInvalid, return nullptr, "[queueId=%u] type invalid", GetQueueId());
393
394 return reinterpret_cast<EventHandlerInteractiveQueue*>(queue_.get())->GetEventHandler();
395 }
396
Dump(const char * tag,char * buf,uint32_t len,bool historyInfo)397 int QueueHandler::Dump(const char* tag, char* buf, uint32_t len, bool historyInfo)
398 {
399 FFRT_COND_DO_ERR((queue_ == nullptr), return -1, "[queueId=%u] constructed failed", GetQueueId());
400 FFRT_COND_DO_ERR((queue_->GetQueueType() != ffrt_queue_eventhandler_adapter),
401 return -1, "[queueId=%u] type invalid", GetQueueId());
402 return reinterpret_cast<EventHandlerAdapterQueue*>(queue_.get())->Dump(tag, buf, len, historyInfo);
403 }
404
DumpSize(ffrt_inner_queue_priority_t priority)405 int QueueHandler::DumpSize(ffrt_inner_queue_priority_t priority)
406 {
407 FFRT_COND_DO_ERR((queue_ == nullptr), return -1, "[queueId=%u] constructed failed", GetQueueId());
408 FFRT_COND_DO_ERR((queue_->GetQueueType() != ffrt_queue_eventhandler_adapter),
409 return -1, "[queueId=%u] type invalid", GetQueueId());
410 return reinterpret_cast<EventHandlerAdapterQueue*>(queue_.get())->DumpSize(priority);
411 }
412
SendSchedTimer(TimePoint delay)413 void QueueHandler::SendSchedTimer(TimePoint delay)
414 {
415 we_->tp = delay;
416 bool result = DelayedWakeup(we_->tp, we_, we_->cb);
417 while (!result) {
418 FFRT_LOGW("failed to set delayedworker, retry");
419 we_->tp = std::chrono::steady_clock::now() + std::chrono::microseconds(SCHED_TIME_ACC_ERROR_US);
420 result = DelayedWakeup(we_->tp, we_, we_->cb);
421 }
422 }
423
CheckSchedDeadline()424 void QueueHandler::CheckSchedDeadline()
425 {
426 std::vector<uint64_t> timeoutTaskId;
427 // Collecting Timeout Tasks
428 {
429 std::unique_lock lock(mutex_);
430 uint64_t threshold = std::chrono::duration_cast<std::chrono::microseconds>(
431 std::chrono::steady_clock::now().time_since_epoch()).count() + SCHED_TIME_ACC_ERROR_US;
432
433 auto it = schedDeadline_.begin();
434 uint64_t nextDeadline = UINT64_MAX;
435 while (it != schedDeadline_.end()) {
436 if (it->second < threshold) {
437 timeoutTaskId.push_back(it->first->gid);
438 it = schedDeadline_.erase(it);
439 } else {
440 nextDeadline = std::min(nextDeadline, it->second);
441 ++it;
442 }
443 }
444
445 if (schedDeadline_.empty()) {
446 initSchedTimer_ = false;
447 } else {
448 std::chrono::microseconds timeout(nextDeadline);
449 TimePoint tp = std::chrono::time_point_cast<std::chrono::steady_clock::duration>(
450 std::chrono::steady_clock::time_point() + timeout);
451 FFRT_LOGI("queueId=%u set sched timer", GetQueueId());
452 SendSchedTimer(tp);
453 }
454 }
455
456 // Reporting Timeout Information
457 if (!timeoutTaskId.empty()) {
458 ReportTimeout(timeoutTaskId);
459 }
460 }
461
AddSchedDeadline(QueueTask * task)462 void QueueHandler::AddSchedDeadline(QueueTask* task)
463 {
464 // sched timeout only support serial queues, other queue types will be supported based on service requirements.
465 if (queue_->GetQueueType() != ffrt_queue_serial) {
466 return;
467 }
468
469 std::unique_lock lock(mutex_);
470 schedDeadline_.insert({task, task->GetSchedTimeout() + task->GetUptime()});
471
472 if (!initSchedTimer_) {
473 if (we_ == nullptr) {
474 we_ = new (SimpleAllocator<WaitUntilEntry>::AllocMem()) WaitUntilEntry();
475 we_->cb = ([this](WaitEntry* we_) { CheckSchedDeadline(); });
476 }
477 std::chrono::microseconds timeout(schedDeadline_[task]);
478 TimePoint tp = std::chrono::time_point_cast<std::chrono::steady_clock::duration>(
479 std::chrono::steady_clock::time_point() + timeout);
480 SendSchedTimer(tp);
481 initSchedTimer_ = true;
482 }
483 }
484
RemoveSchedDeadline(QueueTask * task)485 void QueueHandler::RemoveSchedDeadline(QueueTask* task)
486 {
487 std::unique_lock lock(mutex_);
488 schedDeadline_.erase(task);
489 }
490
CheckOverload()491 void QueueHandler::CheckOverload()
492 {
493 if (queue_->GetMapSize() <= CONGESTION_CNT) {
494 return;
495 }
496
497 uint64_t expect = queue_->GetHeadUptime();
498 uint64_t now = std::chrono::duration_cast<std::chrono::microseconds>(
499 std::chrono::steady_clock::now().time_since_epoch()).count();
500 if (now > expect && now - expect > CONGESTION_TIMEOUT_US * overloadTimes_.load()) {
501 overloadTimes_.fetch_add(1);
502 std::vector<uint64_t> timeoutVec = {};
503 ReportTimeout(timeoutVec);
504 }
505 }
506
ReportTimeout(const std::vector<uint64_t> & timeoutTaskId)507 void QueueHandler::ReportTimeout(const std::vector<uint64_t>& timeoutTaskId)
508 {
509 std::stringstream ss;
510 ss << "Queue_Schedule_Timeout, queueId=" << GetQueueId() << ", timeout task gid: ";
511 for (auto& id : timeoutTaskId) {
512 ss << id << " ";
513 }
514
515 FFRT_LOGE("%s", ss.str().c_str());
516 ffrt_task_timeout_cb func = ffrt_task_timeout_get_cb();
517 if (func) {
518 func(GetQueueId(), ss.str().c_str(), ss.str().size());
519 }
520 }
521
522 } // namespace ffrt
523