• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         ops.WorkerLooper = CPUWorker::WorkerLooperStandard;
59 #ifdef OHOS_STANDARD_SYSTEM
60         ops.WaitForNewAction = [pIns] (const WorkerThread* thread) { return pIns->WorkerIdleActionSimplified(thread); };
61         ops.WorkerRetired = [pIns] (WorkerThread* thread) { pIns->WorkerRetiredSimplified(thread); };
62 #endif
63     }
64 
65     return new (std::nothrow) CPUWorker(qos, std::move(ops), pIns);
66 }
67 
CreateCPUMonitor(void * manager)68 CPUMonitor* CPUManagerStrategy::CreateCPUMonitor(void* manager)
69 {
70     constexpr int processNameLen = 32;
71     static std::once_flag flag;
72     static char processName[processNameLen];
73     std::call_once(flag, []() {
74         GetProcessName(processName, processNameLen);
75     });
76     SCPUWorkerManager* pIns = reinterpret_cast<SCPUWorkerManager*>(manager);
77     // default strategy of monitor ops
78     CpuMonitorOps ops {
79         [pIns] (const QoS& qos) { return pIns->IncWorker(qos); },
80         [pIns] (const QoS& qos) { pIns->WakeupWorkers(qos); },
81         [pIns] (const QoS& qos) { return pIns->GetTaskCount(qos); },
82         [pIns] (const QoS& qos) { return pIns->GetWorkerCount(qos); },
83         CPUMonitor::HandleTaskNotifyDefault,
84     };
85 
86 #ifdef OHOS_STANDARD_SYSTEM
87     for (const auto& notifyFunc : NOTIFY_FUNCTION_FACTORY) {
88         if (strstr(processName, notifyFunc.first.c_str())) {
89             ops.HandleTaskNotity = notifyFunc.second;
90             break;
91         }
92     }
93 #endif
94 
95     return new SCPUMonitor(std::move(ops));
96 }
97 }
98