cpu-boosting: Check whether cpu stall handler is turned on
[platform/core/system/resourced.git] / src / common / conf / cpu-common.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "macro.h"
20 #include "cpu-common.h"
21 #include "trace.h"
22 #include "resourced.h"
23
24 static struct cpu_sched_conf *cpu_sched_conf = NULL;
25 static struct cpu_affinity_conf *cpu_affinity_conf = NULL;
26 static struct cpu_throttling_conf *cpu_throttling_conf = NULL;
27 static struct cpu_boosting_conf *cpu_boosting_conf[CPU_BOOSTING_LEVEL_END] = {NULL, };
28 static bool cpu_boosting_cpu_stall_handler_enabled = false;
29
30 void cpu_boosting_enable_cpu_stall_handler(void)
31 {
32         cpu_boosting_cpu_stall_handler_enabled = true;
33 }
34
35 bool cpu_boosting_is_enabled_of_cpu_stall_handler(void)
36 {
37         return cpu_boosting_cpu_stall_handler_enabled;
38 }
39
40 struct cpu_sched_conf *get_cpu_sched_conf(void)
41 {
42         if (!cpu_sched_conf) {
43                 cpu_sched_conf = (struct cpu_sched_conf *)calloc(1, sizeof (struct cpu_sched_conf));
44                 if (!cpu_sched_conf) {
45                         _E("Failed to alloc memory for cpu scheduler configuration");
46                         return NULL;
47                 }
48                 else {
49                         cpu_sched_conf->cpu_sched_flag = CPU_SCHED_UNINITIALIZED;
50                         cpu_sched_conf->cpu_cgroup_info.rt_runtime_us = 0;
51                         cpu_sched_conf->cpu_cgroup_info.rt_period_us = 0;
52                 }
53         }
54
55         return cpu_sched_conf;
56 }
57
58 void free_cpu_sched_conf(void)
59 {
60         if (cpu_sched_conf)
61                 free(cpu_sched_conf);
62 }
63
64 char *get_cpu_affinity_conf_name(void)
65 {
66         if (cpu_affinity_conf)
67                 return cpu_affinity_conf->cpuset_cgroup_info.name;
68         else
69                 return NULL;
70 }
71
72 char *get_cpu_affinity_conf_value(void)
73 {
74         if (cpu_affinity_conf)
75                 return cpu_affinity_conf->cpuset_cgroup_info.value;
76         else
77                 return NULL;
78 }
79
80 int set_cpu_affinity_conf(const char *name, const char *value)
81 {
82         if (!cpu_affinity_conf) {
83                 cpu_affinity_conf = (struct cpu_affinity_conf *)calloc(1, sizeof (struct cpu_affinity_conf));
84                 if (!cpu_affinity_conf) {
85                         _E("Failed to alloc memory for cpu affinity configuration");
86                         return RESOURCED_ERROR_OUT_OF_MEMORY;
87                 }
88         }
89
90         if (strlen(name) + 1 > sizeof(cpu_affinity_conf->cpuset_cgroup_info.name)) {
91                 _E("Size of cpu configuration for name is not enough");
92                 return RESOURCED_ERROR_OUT_OF_MEMORY;
93         }
94         strncpy(cpu_affinity_conf->cpuset_cgroup_info.name, name, sizeof(cpu_affinity_conf->cpuset_cgroup_info.name) - 1);
95
96         if (strlen(value) + 1 > sizeof(cpu_affinity_conf->cpuset_cgroup_info.value)) {
97                 _E("Size of cpu configuration for value is not enough");
98                 return RESOURCED_ERROR_OUT_OF_MEMORY;
99         }
100         strncpy(cpu_affinity_conf->cpuset_cgroup_info.value, value, sizeof(cpu_affinity_conf->cpuset_cgroup_info.value) - 1);
101
102         return RESOURCED_ERROR_NONE;
103 }
104
105 void free_cpu_affinity_conf(void)
106 {
107         if (cpu_affinity_conf)
108                 free(cpu_affinity_conf);
109 }
110
111 struct cpu_throttling_conf *get_cpu_throttling_conf(void)
112 {
113         if (!cpu_throttling_conf) {
114                 cpu_throttling_conf = (struct cpu_throttling_conf *)calloc(1, sizeof (struct cpu_throttling_conf));
115                 if (!cpu_throttling_conf) {
116                         _E("Failed to alloc memory for cpu throttling configuration");
117                         return NULL;
118                 }
119                 else {
120                         cpu_throttling_conf->enable = false;
121                         cpu_throttling_conf->cpu_sched_info.cpu_sched_type = CPU_SCHED_NONE;
122                         cpu_throttling_conf->cpu_sched_info.cpu_nice = CPU_INIT_NICE;
123                         cpu_throttling_conf->cpu_cgroup_info.cfs_runtime_us = 0;
124                         cpu_throttling_conf->cpu_cgroup_info.cfs_period_us = 0;
125                         cpu_throttling_conf->cpu_cgroup_info.cpu_share = 0;
126                 }
127         }
128
129         return cpu_throttling_conf;
130 }
131
132 void free_cpu_throttling_conf(void)
133 {
134         if (cpu_throttling_conf)
135                 free(cpu_throttling_conf);
136 }
137
138 struct cpu_boosting_conf *get_cpu_boosting_conf(cpu_boosting_level_e level)
139 {
140         if (!cpu_boosting_conf[level]) {
141                 cpu_boosting_conf[level] = (struct cpu_boosting_conf *)
142                         calloc(1, sizeof (struct cpu_boosting_conf));
143
144                 if (!cpu_boosting_conf[level]) {
145                         _E("Failed to alloc memory for cpu boosting (level = %d) configuration", level);
146                         return NULL;
147                 }
148                 else {
149                         cpu_boosting_conf[level]->enable = false;
150                         cpu_boosting_conf[level]->cpu_sched_info.cpu_sched_type = CPU_SCHED_NONE;
151                         cpu_boosting_conf[level]->cpu_sched_info.cpu_nice = CPU_INIT_NICE;
152                         cpu_boosting_conf[level]->cpu_sched_info.cpu_rt_priority = CPU_INIT_PRIO;
153                 }
154         }
155
156         return cpu_boosting_conf[level];
157 }
158
159 void free_cpu_boosting_conf(cpu_boosting_level_e level)
160 {
161         if (cpu_boosting_conf[level])
162                 free(cpu_boosting_conf[level]);
163 }