• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <algorithm>
16 #include <unistd.h>
17 #include <sys/syscall.h>
18 #include <sys/resource.h>
19 #include "eu/cpu_worker.h"
20 #include "eu/osattr_manager.h"
21 #include "eu/qos_interface.h"
22 #include "util/name_manager.h"
23 #include "util/ffrt_facade.h"
24 #include "internal_inc/osal.h"
25 #include "util/white_list.h"
26 
27 namespace {
28 constexpr int CFS_PRIO_MIN = 1;
29 constexpr int CFS_PRIO_DEFAULT = 20;
30 constexpr int CFS_PRIO_MAX = 40;
31 constexpr int VIP_PRIO_MIN = 41;
32 constexpr int VIP_PRIO_MAX = 50;
33 constexpr int RT_PRIO_MIN = 51;
34 constexpr int RT_PRIO_MAX = 139;
35 constexpr int THREAD_INDEX_MAX = 99999;
36 }
37 
38 namespace ffrt {
WorkerSetup()39 void CPUWorker::WorkerSetup()
40 {
41     static std::atomic<uint64_t> threadIndex[QoS::MaxNum()] = {0};
42     std::string qosStr = std::to_string(qos());
43     uint64_t index = threadIndex[qos()].fetch_add(1, std::memory_order_seq_cst);
44     if (index > THREAD_INDEX_MAX) {
45         index %= THREAD_INDEX_MAX;
46     }
47     std::string threadName = std::string(WORKER_THREAD_NAME_PREFIX) + qosStr +
48         std::string(WORKER_THREAD_SYMBOL) + std::to_string(index);
49     if (qosStr == "") {
50         FFRT_SYSEVENT_LOGE("ffrt threadName qos[%d] index[%d]", qos(), index);
51     }
52     pthread_setname_np(pthread_self(), threadName.c_str());
53 }
54 
SetCpuAffinity(unsigned long affinity,int tid)55 int SetCpuAffinity(unsigned long affinity, int tid)
56 {
57     cpu_set_t mask;
58     CPU_ZERO(&mask);
59     for (unsigned long i = 0; i < sizeof(affinity) * 8; i++) {
60         if ((affinity & (static_cast<unsigned long>(1) << i)) != 0) {
61             CPU_SET(i, &mask);
62         }
63     }
64     int ret = syscall(__NR_sched_setaffinity, tid, sizeof(mask), &mask);
65     if (ret < 0) {
66         FFRT_SYSEVENT_LOGW("set qos affinity failed for tid %d\n", tid);
67     }
68     return ret;
69 }
70 
SetDefaultThreadAttr(CPUWorker * thread,const QoS & qos)71 void SetDefaultThreadAttr(CPUWorker* thread, const QoS& qos)
72 {
73     if (qos() <= qos_max) {
74         FFRTQosApplyForOther(qos(), thread->Id());
75         FFRT_LOGD("qos apply tid[%d] level[%d]\n", thread->Id(), qos());
76     }
77 }
78 
SetThreadAttr(const QoS & newQos)79 void CPUWorker::SetThreadAttr(const QoS& newQos)
80 {
81     SetDefaultThreadAttr(this, newQos);
82 }
83 }