• 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 #ifndef OS_ATTR_MANAGER_H
17 #define OS_ATTR_MANAGER_H
18 #include <array>
19 #include <cerrno>
20 #include <cstring>
21 #include <fcntl.h>
22 #include "ffrt_inner.h"
23 #ifdef USE_OHOS_QOS
24 #include "qos.h"
25 #else
26 #include "staging_qos/sched/qos.h"
27 #endif
28 #include "dfx/log/ffrt_log_api.h"
29 
30 namespace ffrt {
31 const std::string cpuctlGroupIvePath = "/dev/cpuctl/cam2stage";
32 const std::string cpusetGroupIvePath = "/dev/cpuset/cam2stage";
33 const std::string cpuThreadNode = "/tasks";
34 const std::string cpuSharesNode = "/cpu.shares";
35 const std::string cpuMapNode = "/cpus";
36 #ifdef OHOS_STANDARD_SYSTEM
37 const std::string cpuUclampminNode = "/load.min";
38 const std::string cpuUclampmaxNode = "/load.max";
39 #else
40 const std::string cpuUclampminNode = "/cpu.uclamp.min";
41 const std::string cpuUclampmaxNode = "/cpu.uclamp.max";
42 const std::string cpuLatencyniceNode = "/cpu.latency.nice";
43 const std::string cpuvipprioNode = "/cpu.vip_prio";
44 #endif
45 constexpr int PATH_MAX_LENS = 4096;
46 constexpr int CGROUP_SHARES_MIN = 2;
47 constexpr int CGROUP_SHARES_MAX = 262144;
48 constexpr int CGROUP_LAT_NICE_MIN = -20;
49 constexpr int CGROUP_LAT_NICE_MAX = 19;
50 constexpr int CGROUP_UCLAMP_MIN = 0;
51 constexpr int CGROUP_UCLAMP_MAX = 100;
52 constexpr int CGROUP_VIPPRIO_MIN = 0;
53 constexpr int CGROUP_VIPPRIO_MAX = 10;
54 class OSAttrManager {
55 public:
OSAttrManager()56     OSAttrManager() {}
~OSAttrManager()57     ~OSAttrManager() {}
58 
Instance()59     static inline OSAttrManager* Instance()
60     {
61         static OSAttrManager instance;
62         return &instance;
63     }
64 
65     bool CheckSchedAttrPara(const std::string &name, int min, int max, int paraValue);
66     int UpdateSchedAttr(const QoS& qos, ffrt_os_sched_attr *attr);
67     void SetCGroupCtlPara(const std::string &name, int32_t value);
68     void SetCGroupSetPara(const std::string &name, const std::string &value);
69     void SetTidToCGroup(int32_t pid);
70     void SetTidToCGroupPrivate(const std::string &filename, int32_t pid);
71     template <typename T>
SetCGroupPara(const std::string & filename,T & value)72     void SetCGroupPara(const std::string &filename, T& value)
73     {
74         char filePath[PATH_MAX_LENS] = {0};
75         if (filename.empty()) {
76             FFRT_LOGE("[cgroup_ctrl] invalid para, filename is empty");
77             return;
78         }
79 
80         if ((strlen(filename.c_str()) > PATH_MAX_LENS) || (realpath(filename.c_str(), filePath) == nullptr)) {
81             FFRT_LOGE("[cgroup_ctrl] invalid file path:%s, error:%s\n", filename.c_str(), strerror(errno));
82             return;
83         }
84 
85         int32_t fd = open(filePath, O_RDWR);
86         if (fd < 0) {
87             FFRT_LOGE("[cgroup_ctrl] fail to open filePath:%s", filePath);
88             return;
89         }
90 
91         std::string valueStr;
92         if constexpr (std::is_same<T, int32_t>::value) {
93             valueStr = std::to_string(value);
94         } else if constexpr (std::is_same<T, const std::string>::value) {
95             valueStr = value;
96         } else {
97             FFRT_LOGE("[cgroup_ctrl] invalid value type\n");
98             close(fd);
99             return;
100         }
101 
102         int32_t ret = write(fd, valueStr.c_str(), valueStr.size());
103         if (ret < 0) {
104             FFRT_LOGE("[cgroup_ctrl] fail to write path:%s valueStr:%s to fd:%d, errno:%d",
105                 filePath, valueStr.c_str(), fd, errno);
106             close(fd);
107             return;
108         }
109 
110         const uint32_t bufferLen = 20;
111         std::array<char, bufferLen> buffer {};
112         int32_t count = read(fd, buffer.data(), bufferLen);
113         if (count <= 0) {
114             FFRT_LOGE("[cgroup_ctrl] fail to read value:%s to fd:%d, errno:%d", buffer.data(), fd, errno);
115         } else {
116             FFRT_LOGI("[cgroup_ctrl] success to read %s buffer:%s", filePath, buffer.data());
117         }
118         close(fd);
119     }
120 };
121 } // namespace ffrt
122 
123 #endif /* OS_ATTR_MANAGER_H */