• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unistd.h>
17 #include <sys/syscall.h>
18 #include <sys/resource.h>
19 #include "dfx/log/ffrt_log_api.h"
20 #include "eu/osattr_manager.h"
21 #include "qos_interface.h"
22 #include "qos_config.h"
23 #include "qos_policy.h"
24 
25 namespace ffrt {
SetQosPolicy(struct QosPolicyDatas * policyDatas)26 int SetQosPolicy(struct QosPolicyDatas *policyDatas)
27 {
28     return QosPolicy(policyDatas);
29 }
30 
QosPolicyInit()31 static __attribute__((constructor)) void QosPolicyInit()
32 {
33     int ret;
34 
35     ret = SetQosPolicy(&QosConfig::Instance().getPolicySystem());
36     if (ret) {
37         FFRT_LOGE("uid %d set g_systemServerQosPolicy failed", getuid());
38     }
39 
40     setFuncAffinity(SetAffinity);
41     setFuncPriority(SetPriority);
42     FFRT_LOGI("set qos policy finish");
43 }
44 
SetAffinity(unsigned long affinity,int tid)45 int SetAffinity(unsigned long affinity, int tid)
46 {
47     cpu_set_t mask;
48     CPU_ZERO(&mask);
49     for (unsigned long i = 0; i < sizeof(affinity) * 8; i++) {
50         if ((affinity & (static_cast<unsigned long>(1) << i)) != 0) {
51             CPU_SET(i, &mask);
52         }
53     }
54     int ret = syscall(__NR_sched_setaffinity, tid, sizeof(mask), &mask);
55     if (ret < 0) {
56         FFRT_LOGE("set qos affinity failed for tid %d\n", tid);
57     }
58     return ret;
59 }
60 
SetPriority(unsigned char priority,WorkerThread * thread)61 void SetPriority(unsigned char priority, WorkerThread* thread)
62 {
63     int ret;
64     if (priority < MAX_RT_PRIO) {
65         struct sched_param param;
66         param.sched_priority = MAX_RT_PRIO - priority;
67         ret = pthread_setschedparam(thread->GetThread().native_handle(), SCHED_RR, &param);
68         if (ret != 0) {
69             FFRT_LOGE("[%d] set priority failed ret[%d] errno[%d]\n", thread->Id(), ret, errno);
70         }
71     } else if (priority < MAX_VIP_PRIO) {
72         pid_t pid = getpid();
73         const std::string path = "/proc/" + std::to_string(pid) + "/task/" + std::to_string(thread->Id()) + "/vip_prio";
74         int vip_prio = MAX_VIP_PRIO - priority;
75         OSAttrManager::Instance()->SetCGroupPara(path, vip_prio);
76     } else {
77         struct sched_param param;
78         param.sched_priority = 0;
79         ret = pthread_setschedparam(thread->GetThread().native_handle(), SCHED_OTHER, &param);
80         if (ret != 0) {
81             FFRT_LOGE("[%d] set priority sched_normal failed ret[%d] errno[%d]\n", thread->Id(), ret, errno);
82         }
83         ret = setpriority(PRIO_PROCESS, thread->Id(), priority - DEFAULT_PRIO);
84         if (ret != 0) {
85             FFRT_LOGE("[%d] set priority failed ret[%d] errno[%d]\n", thread->Id(), ret, errno);
86         }
87     }
88 }
89 }
90