• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <climits>
17 #include <cstring>
18 #include <sys/stat.h>
19 #include "eu/scpu_monitor.h"
20 #include "eu/cpu_manager_interface.h"
21 #include "eu/qos_interface.h"
22 #include "sched/scheduler.h"
23 #include "sched/workgroup_internal.h"
24 #include "eu/co_routine_factory.h"
25 #include "eu/scpuworker_manager.h"
26 
27 namespace ffrt {
28 constexpr int MANAGER_DESTRUCT_TIMESOUT = 1000000;
SCPUWorkerManager()29 SCPUWorkerManager::SCPUWorkerManager()
30 {
31     monitor = new SCPUMonitor({
32         std::bind(&SCPUWorkerManager::IncWorker, this, std::placeholders::_1),
33         std::bind(&SCPUWorkerManager::WakeupWorkers, this, std::placeholders::_1),
34         std::bind(&SCPUWorkerManager::GetTaskCount, this, std::placeholders::_1),
35         std::bind(&SCPUWorkerManager::GetWorkerCount, this, std::placeholders::_1)});
36 }
37 
~SCPUWorkerManager()38 SCPUWorkerManager::~SCPUWorkerManager()
39 {
40     tearDown = true;
41     for (auto qos = QoS::Min(); qos < QoS::Max(); ++qos) {
42         int try_cnt = MANAGER_DESTRUCT_TIMESOUT;
43         while (try_cnt-- > 0) {
44 #ifdef FFRT_IO_TASK_SCHEDULER
45             pollersMtx[qos].unlock();
46             PollerProxy::Instance()->GetPoller(qos).WakeUp();
47 #endif
48             sleepCtl[qos].cv.notify_all();
49             {
50                 usleep(1);
51                 std::shared_lock<std::shared_mutex> lck(groupCtl[qos].tgMutex);
52                 if (groupCtl[qos].threads.empty()) {
53                     break;
54                 }
55             }
56         }
57 
58         if (try_cnt <= 0) {
59             FFRT_LOGE("erase qos[%d] threads failed", qos);
60         }
61     }
62     delete monitor;
63 }
64 
WorkerIdleAction(const WorkerThread * thread)65 WorkerAction SCPUWorkerManager::WorkerIdleAction(const WorkerThread* thread)
66 {
67     if (tearDown) {
68         return WorkerAction::RETIRE;
69     }
70 
71     auto& ctl = sleepCtl[thread->GetQos()];
72     std::unique_lock lk(ctl.mutex);
73     (void)monitor->IntoSleep(thread->GetQos());
74 #if !defined(IDLE_WORKER_DESTRUCT)
75     constexpr int waiting_seconds = 10;
76 #else
77     constexpr int waiting_seconds = 5;
78 #endif
79 #ifdef FFRT_IO_TASK_SCHEDULER
80     if (ctl.cv.wait_for(lk, std::chrono::seconds(waiting_seconds), [this, thread] {
81         bool taskExistence = GetTaskCount(thread->GetQos()) ||
82             reinterpret_cast<const CPUWorker*>(thread)->priority_task ||
83             reinterpret_cast<const CPUWorker*>(thread)->localFifo.GetLength();
84         bool needPoll = !PollerProxy::Instance()->GetPoller(thread->GetQos()).DetermineEmptyMap() && !polling_;
85         return tearDown || taskExistence || needPoll;
86         })) {
87 #else
88     if (ctl.cv.wait_for(lk, std::chrono::seconds(waiting_seconds),
89         [this, thread] {return tearDown || GetTaskCount(thread->GetQos());})) {
90 #endif
91         monitor->WakeupCount(thread->GetQos());
92         FFRT_LOGD("worker awake");
93         return WorkerAction::RETRY;
94     } else {
95 #if !defined(IDLE_WORKER_DESTRUCT)
96         monitor->IntoDeepSleep(thread->GetQos());
97         CoStackFree();
98         if (monitor->IsExceedDeepSleepThreshold()) {
99             ffrt::CoRoutineReleaseMem();
100         }
101 #ifdef FFRT_IO_TASK_SCHEDULER
102         ctl.cv.wait(lk, [this, thread] {
103             return tearDown || GetTaskCount(thread->GetQos()) ||
104             reinterpret_cast<const CPUWorker*>(thread)->priority_task ||
105             reinterpret_cast<const CPUWorker*>(thread)->localFifo.GetLength();
106             });
107 #else
108         ctl.cv.wait(lk, [this, thread] {return tearDown || GetTaskCount(thread->GetQos());});
109 #endif
110         monitor->OutOfDeepSleep(thread->GetQos());
111         FFRT_LOGD("worker awake");
112         return WorkerAction::RETRY;
113 #else
114         monitor->TimeoutCount(thread->GetQos());
115         FFRT_LOGD("worker exit");
116         return WorkerAction::RETIRE;
117 #endif
118     }
119 }
120 
121 void SCPUWorkerManager::WorkerPrepare(WorkerThread* thread)
122 {
123 }
124 } // namespace ffrt
125