• 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 #include "util/ffrt_facade.h"
22 #include <cstring>
23 
24 namespace ffrt {
25 std::array<sched_mode_type, QoS::MaxNum()> CPUManagerStrategy::schedMode {};
26 
CreateCPUWorker(const QoS & qos,void * manager)27 WorkerThread* CPUManagerStrategy::CreateCPUWorker(const QoS& qos, void* manager)
28 {
29     CPUWorkerManager* pIns = reinterpret_cast<CPUWorkerManager*>(manager);
30     // default strategy of worker ops
31     CpuWorkerOps ops {
32         [pIns] (const WorkerThread* thread) { pIns->NotifyTaskPicked(thread); },
33         [pIns] (const WorkerThread* thread) { return pIns->WorkerIdleAction(thread); },
34         [pIns] (WorkerThread* thread) { pIns->WorkerRetired(thread); },
35         [pIns] (WorkerThread* thread) { pIns->WorkerPrepare(thread); },
36         [pIns] (const WorkerThread* thread, int timeout) { return pIns->TryPoll(thread, timeout); },
37         [pIns] (WorkerThread* thread) { return pIns->StealTaskBatch(thread); },
38         [pIns] (WorkerThread* thread) { return pIns->PickUpTaskBatch(thread); },
39         [pIns] (const QoS& qos) { return pIns->GetTaskCount(qos); },
40 #ifdef FFRT_WORKERS_DYNAMIC_SCALING
41         [pIns] (const WorkerThread* thread) { return pIns->IsExceedRunningThreshold(thread); },
42         [pIns] () { return pIns->IsBlockAwareInit(); },
43 #endif
44     };
45     if (strstr(GetCurrentProcessName(), "CameraDaemon")) {
46         // CameraDaemon customized strategy
47 #ifdef OHOS_STANDARD_SYSTEM
48         ops.WorkerRetired = [pIns] (WorkerThread* thread) { pIns->WorkerRetiredSimplified(thread); };
49 #endif
50     }
51 
52     return new (std::nothrow) CPUWorker(qos, std::move(ops), pIns);
53 }
54 
CreateCPUMonitor(void * manager)55 CPUMonitor* CPUManagerStrategy::CreateCPUMonitor(void* manager)
56 {
57     SCPUWorkerManager* pIns = reinterpret_cast<SCPUWorkerManager*>(manager);
58     // default strategy of monitor ops
59     CpuMonitorOps ops {
60         [pIns] (const QoS& qos) { return pIns->IncWorker(qos); },
61         [pIns] (const QoS& qos) { pIns->WakeupWorkers(qos); },
62         [pIns] (const QoS& qos) { return pIns->GetTaskCount(qos); },
63         [pIns] (const QoS& qos) { return pIns->GetWorkerCount(qos); },
64         SCPUMonitor::HandleTaskNotifyDefault,
65     };
66     return new SCPUMonitor(std::move(ops));
67 }
68 }