1 /*
2 * Copyright (c) 2024 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 "cpu_manager_strategy.h"
17 #include "internal_inc/osal.h"
18 #include "eu/cpuworker_manager.h"
19 #include "eu/scpuworker_manager.h"
20 #include "eu/scpu_monitor.h"
21
22 #include <cstring>
23
24 namespace ffrt {
25 const std::map<std::string, void(*)(const ffrt::QoS&, void*, ffrt::TaskNotifyType)> NOTIFY_FUNCTION_FACTORY = {
26 { "CameraDaemon", ffrt::CPUMonitor::HandleTaskNotifyConservative },
27 { "bluetooth", ffrt::CPUMonitor::HandleTaskNotifyUltraConservative },
28 };
29
CreateCPUWorker(const QoS & qos,void * manager)30 WorkerThread* CPUManagerStrategy::CreateCPUWorker(const QoS& qos, void* manager)
31 {
32 constexpr int processNameLen = 32;
33 static std::once_flag flag;
34 static char processName[processNameLen];
35 std::call_once(flag, []() {
36 GetProcessName(processName, processNameLen);
37 });
38 CPUWorkerManager* pIns = reinterpret_cast<CPUWorkerManager*>(manager);
39 // default strategy of worker ops
40 CpuWorkerOps ops {
41 CPUWorker::WorkerLooperDefault,
42 [pIns] (WorkerThread* thread) { return pIns->PickUpTaskFromGlobalQueue(thread); },
43 [pIns] (const WorkerThread* thread) { pIns->NotifyTaskPicked(thread); },
44 [pIns] (const WorkerThread* thread) { return pIns->WorkerIdleAction(thread); },
45 [pIns] (WorkerThread* thread) { pIns->WorkerRetired(thread); },
46 [pIns] (WorkerThread* thread) { pIns->WorkerPrepare(thread); },
47 [pIns] (const WorkerThread* thread, int timeout) { return pIns->TryPoll(thread, timeout); },
48 [pIns] (WorkerThread* thread) { return pIns->StealTaskBatch(thread); },
49 [pIns] (WorkerThread* thread) { return pIns->PickUpTaskBatch(thread); },
50 #ifdef FFRT_WORKERS_DYNAMIC_SCALING
51 [pIns] (const WorkerThread* thread) { return pIns->IsExceedRunningThreshold(thread); },
52 [pIns] () { return pIns->IsBlockAwareInit(); },
53 #endif
54 };
55
56 if (strstr(processName, "CameraDaemon")) {
57 // CameraDaemon customized strategy
58 #ifdef OHOS_STANDARD_SYSTEM
59 ops.WorkerRetired = [pIns] (WorkerThread* thread) { pIns->WorkerRetiredSimplified(thread); };
60 #endif
61 }
62
63 return new (std::nothrow) CPUWorker(qos, std::move(ops), pIns);
64 }
65
CreateCPUMonitor(void * manager)66 CPUMonitor* CPUManagerStrategy::CreateCPUMonitor(void* manager)
67 {
68 constexpr int processNameLen = 32;
69 static std::once_flag flag;
70 static char processName[processNameLen];
71 std::call_once(flag, []() {
72 GetProcessName(processName, processNameLen);
73 });
74 SCPUWorkerManager* pIns = reinterpret_cast<SCPUWorkerManager*>(manager);
75 // default strategy of monitor ops
76 CpuMonitorOps ops {
77 [pIns] (const QoS& qos) { return pIns->IncWorker(qos); },
78 [pIns] (const QoS& qos) { pIns->WakeupWorkers(qos); },
79 [pIns] (const QoS& qos) { return pIns->GetTaskCount(qos); },
80 [pIns] (const QoS& qos) { return pIns->GetWorkerCount(qos); },
81 CPUMonitor::HandleTaskNotifyDefault,
82 };
83
84 #ifdef OHOS_STANDARD_SYSTEM
85 for (const auto& notifyFunc : NOTIFY_FUNCTION_FACTORY) {
86 if (strstr(processName, notifyFunc.first.c_str())) {
87 ops.HandleTaskNotity = notifyFunc.second;
88 break;
89 }
90 }
91 #endif
92
93 return new SCPUMonitor(std::move(ops));
94 }
95 }
96