4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
44 #include <rte_memcpy.h>
45 #include <rte_atomic.h>
47 #include "rte_power.h"
49 #ifdef RTE_LIBRTE_POWER_DEBUG
50 #define POWER_DEBUG_TRACE(fmt, args...) do { \
51 RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
54 #define POWER_DEBUG_TRACE(fmt, args...)
57 #define FOPEN_OR_ERR_RET(f, retval) do { \
59 RTE_LOG(ERR, POWER, "File not openned\n"); \
64 #define FOPS_OR_NULL_GOTO(ret, label) do { \
65 if ((ret) == NULL) { \
66 RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
71 #define FOPS_OR_ERR_GOTO(ret, label) do { \
73 RTE_LOG(ERR, POWER, "File operations failed\n"); \
79 #define POWER_CONVERT_TO_DECIMAL 10
81 #define POWER_GOVERNOR_USERSPACE "userspace"
82 #define POWER_SYSFILE_GOVERNOR \
83 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
84 #define POWER_SYSFILE_AVAIL_FREQ \
85 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies"
86 #define POWER_SYSFILE_SETSPEED \
87 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed"
97 * Power info per lcore.
99 struct rte_power_info {
100 unsigned lcore_id; /**< Logical core id */
101 uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */
102 uint32_t nb_freqs; /**< number of available freqs */
103 FILE *f; /**< FD of scaling_setspeed */
104 char governor_ori[32]; /**< Original governor name */
105 uint32_t curr_idx; /**< Freq index in freqs array */
106 volatile uint32_t state; /**< Power in use state */
107 } __rte_cache_aligned;
109 static struct rte_power_info lcore_power_info[RTE_MAX_LCORE];
112 * It is to set specific freq for specific logical core, according to the index
113 * of supported frequencies.
116 set_freq_internal(struct rte_power_info *pi, uint32_t idx)
118 if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) {
119 RTE_LOG(ERR, POWER, "Invalid frequency index %u, which "
120 "should be less than %u\n", idx, pi->nb_freqs);
124 /* Check if it is the same as current */
125 if (idx == pi->curr_idx)
128 POWER_DEBUG_TRACE("Freqency[%u] %u to be set for lcore %u\n",
129 idx, pi->freqs[idx], pi->lcore_id);
130 if (fseek(pi->f, 0, SEEK_SET) < 0) {
131 RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 "
132 "for setting frequency for lcore %u\n", pi->lcore_id);
135 if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) {
136 RTE_LOG(ERR, POWER, "Fail to write new frequency for "
137 "lcore %u\n", pi->lcore_id);
147 * It is to check the current scaling governor by reading sys file, and then
148 * set it into 'userspace' if it is not by writing the sys file. The original
149 * governor will be saved for rolling back.
152 power_set_governor_userspace(struct rte_power_info *pi)
157 char fullpath[PATH_MAX];
161 rte_snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
163 f = fopen(fullpath, "rw+");
164 FOPEN_OR_ERR_RET(f, ret);
166 s = fgets(buf, sizeof(buf), f);
167 FOPS_OR_NULL_GOTO(s, out);
169 /* Check if current governor is userspace */
170 if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
171 sizeof(POWER_GOVERNOR_USERSPACE)) == 0) {
173 POWER_DEBUG_TRACE("Power management governor of lcore %u is "
174 "already userspace\n", pi->lcore_id);
177 /* Save the original governor */
178 rte_snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf);
180 /* Write 'userspace' to the governor */
181 val = fseek(f, 0, SEEK_SET);
182 FOPS_OR_ERR_GOTO(val, out);
184 val = fputs(POWER_GOVERNOR_USERSPACE, f);
185 FOPS_OR_ERR_GOTO(val, out);
188 RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been "
189 "set to user space successfully\n", pi->lcore_id);
197 * It is to get the available frequencies of the specific lcore by reading the
201 power_get_available_freqs(struct rte_power_info *pi)
204 int ret = -1, i, count;
207 char fullpath[PATH_MAX];
208 char *freqs[RTE_MAX_LCORE_FREQS];
211 rte_snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
213 f = fopen(fullpath, "r");
214 FOPEN_OR_ERR_RET(f, ret);
216 s = fgets(buf, sizeof(buf), f);
217 FOPS_OR_NULL_GOTO(s, out);
219 /* Strip the line break if there is */
220 p = strchr(buf, '\n');
224 /* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
225 count = rte_strsplit(buf, sizeof(buf), freqs,
226 RTE_MAX_LCORE_FREQS, ' ');
228 RTE_LOG(ERR, POWER, "No available frequency in "
229 ""POWER_SYSFILE_AVAIL_FREQ"\n", pi->lcore_id);
232 if (count >= RTE_MAX_LCORE_FREQS) {
233 RTE_LOG(ERR, POWER, "Too many available frequencies : %d\n",
238 /* Store the available frequncies into power context */
239 for (i = 0, pi->nb_freqs = 0; i < count; i++) {
240 POWER_DEBUG_TRACE("Lcore %u frequency[%d]: %s\n", pi->lcore_id,
242 pi->freqs[pi->nb_freqs++] = strtoul(freqs[i], &p,
243 POWER_CONVERT_TO_DECIMAL);
247 POWER_DEBUG_TRACE("%d frequencie(s) of lcore %u are available\n",
248 count, pi->lcore_id);
256 * It is to fopen the sys file for the future setting the lcore frequency.
259 power_init_for_setting_freq(struct rte_power_info *pi)
262 char fullpath[PATH_MAX];
267 rte_snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
269 f = fopen(fullpath, "rw+");
270 FOPEN_OR_ERR_RET(f, -1);
272 s = fgets(buf, sizeof(buf), f);
273 FOPS_OR_NULL_GOTO(s, out);
275 freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
276 for (i = 0; i < pi->nb_freqs; i++) {
277 if (freq == pi->freqs[i]) {
291 rte_power_init(unsigned lcore_id)
293 struct rte_power_info *pi;
295 if (lcore_id >= RTE_MAX_LCORE) {
296 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
297 lcore_id, RTE_MAX_LCORE - 1U);
301 pi = &lcore_power_info[lcore_id];
302 if (rte_atomic32_cmpset(&(pi->state), POWER_IDLE, POWER_ONGOING)
304 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
305 "in use\n", lcore_id);
309 pi->lcore_id = lcore_id;
310 /* Check and set the governor */
311 if (power_set_governor_userspace(pi) < 0) {
312 RTE_LOG(ERR, POWER, "Cannot set governor of lcore %u to "
313 "userspace\n", lcore_id);
317 /* Get the available frequencies */
318 if (power_get_available_freqs(pi) < 0) {
319 RTE_LOG(ERR, POWER, "Cannot get available frequencies of "
320 "lcore %u\n", lcore_id);
324 /* Init for setting lcore frequency */
325 if (power_init_for_setting_freq(pi) < 0) {
326 RTE_LOG(ERR, POWER, "Cannot init for setting frequency for "
327 "lcore %u\n", lcore_id);
331 /* Set freq to max by default */
332 if (rte_power_freq_max(lcore_id) < 0) {
333 RTE_LOG(ERR, POWER, "Cannot set frequency of lcore %u "
334 "to max\n", lcore_id);
338 RTE_LOG(INFO, POWER, "Initialized successfully for lcore %u "
339 "power manamgement\n", lcore_id);
340 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_USED);
345 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
351 * It is to check the governor and then set the original governor back if
352 * needed by writing the the sys file.
355 power_set_governor_original(struct rte_power_info *pi)
360 char fullpath[PATH_MAX];
364 rte_snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
366 f = fopen(fullpath, "rw+");
367 FOPEN_OR_ERR_RET(f, ret);
369 s = fgets(buf, sizeof(buf), f);
370 FOPS_OR_NULL_GOTO(s, out);
372 /* Check if the governor to be set is the same as current */
373 if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) {
375 POWER_DEBUG_TRACE("Power management governor of lcore %u "
376 "has already been set to %s\n",
377 pi->lcore_id, pi->governor_ori);
381 /* Write back the original governor */
382 val = fseek(f, 0, SEEK_SET);
383 FOPS_OR_ERR_GOTO(val, out);
385 val = fputs(pi->governor_ori, f);
386 FOPS_OR_ERR_GOTO(val, out);
389 RTE_LOG(INFO, POWER, "Power manamgement governor of lcore %u "
390 "has been set back to %s successfully\n",
391 pi->lcore_id, pi->governor_ori);
399 rte_power_exit(unsigned lcore_id)
401 struct rte_power_info *pi;
403 if (lcore_id >= RTE_MAX_LCORE) {
404 RTE_LOG(ERR, POWER, "Lcore id %u can not exceeds %u\n",
405 lcore_id, RTE_MAX_LCORE - 1U);
408 pi = &lcore_power_info[lcore_id];
409 if (rte_atomic32_cmpset(&(pi->state), POWER_USED, POWER_ONGOING)
411 RTE_LOG(INFO, POWER, "Power management of lcore %u is "
412 "not used\n", lcore_id);
416 /* Close FD of setting freq */
420 /* Set the governor back to the original */
421 if (power_set_governor_original(pi) < 0) {
422 RTE_LOG(ERR, POWER, "Cannot set the governor of %u back "
423 "to the original\n", lcore_id);
427 RTE_LOG(INFO, POWER, "Power management of lcore %u has exited from "
428 "'userspace' mode and been set back to the "
429 "original\n", lcore_id);
430 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_IDLE);
435 rte_atomic32_cmpset(&(pi->state), POWER_ONGOING, POWER_UNKNOWN);
441 rte_power_freqs(unsigned lcore_id, uint32_t *freqs, uint32_t num)
443 struct rte_power_info *pi;
445 if (lcore_id >= RTE_MAX_LCORE || !freqs) {
446 RTE_LOG(ERR, POWER, "Invalid input parameter\n");
450 pi = &lcore_power_info[lcore_id];
451 if (num < pi->nb_freqs) {
452 RTE_LOG(ERR, POWER, "Buffer size is not enough\n");
455 rte_memcpy(freqs, pi->freqs, pi->nb_freqs * sizeof(uint32_t));
461 rte_power_get_freq(unsigned lcore_id)
463 if (lcore_id >= RTE_MAX_LCORE) {
464 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
465 return RTE_POWER_INVALID_FREQ_INDEX;
468 return lcore_power_info[lcore_id].curr_idx;
472 rte_power_set_freq(unsigned lcore_id, uint32_t index)
474 if (lcore_id >= RTE_MAX_LCORE) {
475 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
479 return set_freq_internal(&(lcore_power_info[lcore_id]), index);
483 rte_power_freq_down(unsigned lcore_id)
485 struct rte_power_info *pi;
487 if (lcore_id >= RTE_MAX_LCORE) {
488 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
492 pi = &lcore_power_info[lcore_id];
493 if (pi->curr_idx + 1 == pi->nb_freqs)
496 /* Frequencies in the array are from high to low. */
497 return set_freq_internal(pi, pi->curr_idx + 1);
501 rte_power_freq_up(unsigned lcore_id)
503 struct rte_power_info *pi;
505 if (lcore_id >= RTE_MAX_LCORE) {
506 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
510 pi = &lcore_power_info[lcore_id];
511 if (pi->curr_idx == 0)
514 /* Frequencies in the array are from high to low. */
515 return set_freq_internal(pi, pi->curr_idx - 1);
519 rte_power_freq_max(unsigned lcore_id)
521 if (lcore_id >= RTE_MAX_LCORE) {
522 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
526 /* Frequencies in the array are from high to low. */
527 return set_freq_internal(&lcore_power_info[lcore_id], 0);
531 rte_power_freq_min(unsigned lcore_id)
533 struct rte_power_info *pi;
535 if (lcore_id >= RTE_MAX_LCORE) {
536 RTE_LOG(ERR, POWER, "Invalid lcore ID\n");
540 pi = &lcore_power_info[lcore_id];
542 /* Frequencies in the array are from high to low. */
543 return set_freq_internal(pi, pi->nb_freqs - 1);