4 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "cpu-common.h"
22 #include "resourced.h"
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;
30 void cpu_boosting_enable_cpu_stall_handler(void)
32 cpu_boosting_cpu_stall_handler_enabled = true;
35 bool cpu_boosting_is_enabled_of_cpu_stall_handler(void)
37 return cpu_boosting_cpu_stall_handler_enabled;
40 struct cpu_sched_conf *get_cpu_sched_conf(void)
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");
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;
55 return cpu_sched_conf;
58 void free_cpu_sched_conf(void)
64 char *get_cpu_affinity_conf_name(void)
66 if (cpu_affinity_conf)
67 return cpu_affinity_conf->cpuset_cgroup_info.name;
72 char *get_cpu_affinity_conf_value(void)
74 if (cpu_affinity_conf)
75 return cpu_affinity_conf->cpuset_cgroup_info.value;
80 int set_cpu_affinity_conf(const char *name, const char *value)
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;
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;
94 strncpy(cpu_affinity_conf->cpuset_cgroup_info.name, name, sizeof(cpu_affinity_conf->cpuset_cgroup_info.name) - 1);
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;
100 strncpy(cpu_affinity_conf->cpuset_cgroup_info.value, value, sizeof(cpu_affinity_conf->cpuset_cgroup_info.value) - 1);
102 return RESOURCED_ERROR_NONE;
105 void free_cpu_affinity_conf(void)
107 if (cpu_affinity_conf)
108 free(cpu_affinity_conf);
111 struct cpu_throttling_conf *get_cpu_throttling_conf(void)
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");
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;
129 return cpu_throttling_conf;
132 void free_cpu_throttling_conf(void)
134 if (cpu_throttling_conf)
135 free(cpu_throttling_conf);
138 struct cpu_boosting_conf *get_cpu_boosting_conf(cpu_boosting_level_e level)
140 if (!cpu_boosting_conf[level]) {
141 cpu_boosting_conf[level] = (struct cpu_boosting_conf *)
142 calloc(1, sizeof (struct cpu_boosting_conf));
144 if (!cpu_boosting_conf[level]) {
145 _E("Failed to alloc memory for cpu boosting (level = %d) configuration", level);
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;
156 return cpu_boosting_conf[level];
159 void free_cpu_boosting_conf(cpu_boosting_level_e level)
161 if (cpu_boosting_conf[level])
162 free(cpu_boosting_conf[level]);