1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <sys/sysinfo.h>
16 #include <sys/types.h>
19 #include <rte_power.h>
20 #include <rte_spinlock.h>
22 #include "channel_manager.h"
23 #include "power_manager.h"
24 #include "oob_monitor.h"
26 #define POWER_SCALE_CORE(DIRECTION, core_num , ret) do { \
27 if (core_num >= ci.core_count) \
29 if (!(ci.cd[core_num].global_enabled_cpus)) \
31 rte_spinlock_lock(&global_core_freq_info[core_num].power_sl); \
32 ret = rte_power_freq_##DIRECTION(core_num); \
33 rte_spinlock_unlock(&global_core_freq_info[core_num].power_sl); \
37 rte_spinlock_t power_sl;
38 uint32_t freqs[RTE_MAX_LCORE_FREQS];
40 } __rte_cache_aligned;
42 static struct freq_info global_core_freq_info[RTE_MAX_LCORE];
46 #define SYSFS_CPU_PATH "/sys/devices/system/cpu/cpu%u/topology/core_id"
62 ci->core_count = get_nprocs_conf();
63 ci->branch_ratio_threshold = BRANCH_RATIO_THRESHOLD;
64 ci->cd = malloc(ci->core_count * sizeof(struct core_details));
66 RTE_LOG(ERR, POWER_MANAGER, "Failed to allocate memory for core info.");
69 for (i = 0; i < ci->core_count; i++) {
70 ci->cd[i].global_enabled_cpus = 1;
71 ci->cd[i].oob_enabled = 0;
74 printf("%d cores in system\n", ci->core_count);
79 power_manager_init(void)
81 unsigned int i, num_cpus = 0, num_freqs = 0;
84 unsigned int max_core_num;
86 rte_power_set_env(PM_ENV_NOT_SET);
90 RTE_LOG(ERR, POWER_MANAGER,
91 "Failed to get core info!\n");
95 if (ci->core_count > RTE_MAX_LCORE)
96 max_core_num = RTE_MAX_LCORE;
98 max_core_num = ci->core_count;
100 for (i = 0; i < max_core_num; i++) {
101 if (ci->cd[i].global_enabled_cpus) {
102 if (rte_power_init(i) < 0)
103 RTE_LOG(ERR, POWER_MANAGER,
104 "Unable to initialize power manager "
107 num_freqs = rte_power_freqs(i,
108 global_core_freq_info[i].freqs,
109 RTE_MAX_LCORE_FREQS);
110 if (num_freqs == 0) {
111 RTE_LOG(ERR, POWER_MANAGER,
112 "Unable to get frequency list for core %u\n",
114 ci->cd[i].oob_enabled = 0;
117 global_core_freq_info[i].num_freqs = num_freqs;
119 rte_spinlock_init(&global_core_freq_info[i].power_sl);
121 if (ci->cd[i].oob_enabled)
122 add_core_to_monitor(i);
124 RTE_LOG(INFO, POWER_MANAGER, "Managing %u cores out of %u available host cores\n",
125 num_cpus, ci->core_count);
131 power_manager_get_current_frequency(unsigned core_num)
133 uint32_t freq, index;
135 if (core_num >= RTE_MAX_LCORE) {
136 RTE_LOG(ERR, POWER_MANAGER, "Core(%u) is out of range 0...%d\n",
137 core_num, RTE_MAX_LCORE-1);
140 if (!(ci.cd[core_num].global_enabled_cpus))
143 rte_spinlock_lock(&global_core_freq_info[core_num].power_sl);
144 index = rte_power_get_freq(core_num);
145 rte_spinlock_unlock(&global_core_freq_info[core_num].power_sl);
146 if (index >= RTE_MAX_LCORE_FREQS)
149 freq = global_core_freq_info[core_num].freqs[index];
155 power_manager_exit(void)
159 struct core_info *ci;
160 unsigned int max_core_num;
162 ci = get_core_info();
164 RTE_LOG(ERR, POWER_MANAGER,
165 "Failed to get core info!\n");
169 if (ci->core_count > RTE_MAX_LCORE)
170 max_core_num = RTE_MAX_LCORE;
172 max_core_num = ci->core_count;
174 for (i = 0; i < max_core_num; i++) {
175 if (ci->cd[i].global_enabled_cpus) {
176 if (rte_power_exit(i) < 0) {
177 RTE_LOG(ERR, POWER_MANAGER, "Unable to shutdown power manager "
181 ci->cd[i].global_enabled_cpus = 0;
183 remove_core_from_monitor(i);
189 power_manager_scale_core_up(unsigned core_num)
193 POWER_SCALE_CORE(up, core_num, ret);
198 power_manager_scale_core_down(unsigned core_num)
202 POWER_SCALE_CORE(down, core_num, ret);
207 power_manager_scale_core_min(unsigned core_num)
211 POWER_SCALE_CORE(min, core_num, ret);
216 power_manager_scale_core_max(unsigned core_num)
220 POWER_SCALE_CORE(max, core_num, ret);
225 power_manager_enable_turbo_core(unsigned int core_num)
229 POWER_SCALE_CORE(enable_turbo, core_num, ret);
234 power_manager_disable_turbo_core(unsigned int core_num)
238 POWER_SCALE_CORE(disable_turbo, core_num, ret);
243 power_manager_scale_core_med(unsigned int core_num)
246 struct core_info *ci;
248 ci = get_core_info();
249 if (core_num >= RTE_MAX_LCORE)
251 if (!(ci->cd[core_num].global_enabled_cpus))
253 rte_spinlock_lock(&global_core_freq_info[core_num].power_sl);
254 ret = rte_power_set_freq(core_num,
255 global_core_freq_info[core_num].num_freqs / 2);
256 rte_spinlock_unlock(&global_core_freq_info[core_num].power_sl);