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 #ifndef FFRT_CPU_WORKER_HPP 17 #define FFRT_CPU_WORKER_HPP 18 19 #include "eu/worker_thread.h" 20 #include "eu/cpu_manager_strategy.h" 21 #include "c/executor_task.h" 22 #include "sync/poller.h" 23 #include "util/spmc_queue.h" 24 #include "tm/task_base.h" 25 26 namespace ffrt { 27 const unsigned int LOCAL_QUEUE_SIZE = 128; 28 const unsigned int STEAL_BUFFER_SIZE = LOCAL_QUEUE_SIZE / 2; 29 30 class CPUWorker : public WorkerThread { 31 public: CPUWorker(const QoS & qos,CpuWorkerOps && ops,void * worker_mgr)32 CPUWorker(const QoS& qos, CpuWorkerOps&& ops, void* worker_mgr) : WorkerThread(qos), ops(ops) 33 { 34 #ifdef FFRT_SEND_EVENT 35 uint64_t freq = 1000000; 36 #if defined(__aarch64__) 37 asm volatile("mrs %0, cntfrq_el0" : "=r"(freq)); 38 #endif 39 this->cacheFreq = freq; 40 this->cacheQos = static_cast<int>(qos); 41 #endif 42 this->worker_mgr = worker_mgr; 43 localFifo.Init(LOCAL_QUEUE_SIZE); 44 #ifdef FFRT_PTHREAD_ENABLE 45 Start(CPUWorker::WrapDispatch, this); 46 #else 47 Start(CPUWorker::Dispatch, this); 48 #endif 49 } 50 51 CpuWorkerOps ops; 52 SpmcQueue localFifo; 53 void* priority_task = nullptr; 54 unsigned int tick = 0; 55 unsigned int global_interval = 60; 56 unsigned int budget = 10; 57 58 public: 59 static void WorkerLooperDefault(CPUWorker* worker); 60 61 private: 62 static void* WrapDispatch(void* worker); 63 static void Dispatch(CPUWorker* worker); 64 static void RunTask(TaskBase* task, CPUWorker* worker); 65 static void RunTaskLifo(TaskBase* task, CPUWorker* worker); 66 static void* GetTask(CPUWorker* worker); 67 static PollerRet TryPoll(CPUWorker* worker, int timeout); 68 static bool LocalEmpty(CPUWorker* worker); 69 #ifdef FFRT_SEND_EVENT 70 int cacheQos; // cache int qos 71 std::string cacheLabel; // cache string label 72 uint64_t cacheFreq = 1000000; // cache cpu freq 73 #endif 74 }; 75 } // namespace ffrt 76 #endif 77