1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_cycles.h>
18 test_power_cpufreq(void)
20 printf("Power management library not supported, skipping test\n");
27 printf("Power management library not supported, skipping test\n");
32 #include <rte_power.h>
34 #define TEST_POWER_LCORE_ID 2U
35 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
36 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
38 /* macros used for rounding frequency to nearest 100000 */
39 #define TEST_FREQ_ROUNDING_DELTA 50000
40 #define TEST_ROUND_FREQ_TO_N_100000 100000
42 #define TEST_POWER_SYSFILE_CPUINFO_FREQ \
43 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_cur_freq"
44 #define TEST_POWER_SYSFILE_SCALING_FREQ \
45 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
47 static uint32_t total_freq_num;
48 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
51 check_cur_freq(unsigned int lcore_id, uint32_t idx, bool turbo)
53 #define TEST_POWER_CONVERT_TO_DECIMAL 10
56 char fullpath[PATH_MAX];
58 enum power_management_env env;
64 if (snprintf(fullpath, sizeof(fullpath),
65 TEST_POWER_SYSFILE_CPUINFO_FREQ, lcore_id) < 0) {
68 f = fopen(fullpath, "r");
70 if (snprintf(fullpath, sizeof(fullpath),
71 TEST_POWER_SYSFILE_SCALING_FREQ, lcore_id) < 0) {
74 f = fopen(fullpath, "r");
79 for (i = 0; i < MAX_LOOP; i++) {
81 if (fgets(buf, sizeof(buf), f) == NULL)
84 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
87 env = rte_power_get_env();
88 if (env == PM_ENV_CPPC_CPUFREQ || env == PM_ENV_PSTATE_CPUFREQ) {
89 /* convert the frequency to nearest 100000 value
90 * Ex: if cur_freq=1396789 then freq_conv=1400000
91 * Ex: if cur_freq=800030 then freq_conv=800000
93 freq_conv = (cur_freq + TEST_FREQ_ROUNDING_DELTA)
94 / TEST_ROUND_FREQ_TO_N_100000;
95 freq_conv = freq_conv * TEST_ROUND_FREQ_TO_N_100000;
99 ret = (freqs[idx] <= freq_conv ? 0 : -1);
101 ret = (freqs[idx] == freq_conv ? 0 : -1);
106 if (fseek(f, 0, SEEK_SET) < 0) {
107 printf("Fail to set file position indicator to 0\n");
111 /* wait for the value to be updated */
121 /* Check rte_power_freqs() */
123 check_power_freqs(void)
128 memset(freqs, 0, sizeof(freqs));
130 /* test with an invalid lcore id */
131 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
132 TEST_POWER_FREQS_NUM_MAX);
134 printf("Unexpectedly get available freqs successfully on "
135 "lcore %u\n", TEST_POWER_LCORE_INVALID);
139 /* test with NULL buffer to save available freqs */
140 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
141 TEST_POWER_FREQS_NUM_MAX);
143 printf("Unexpectedly get available freqs successfully with "
144 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
148 /* test of getting zero number of freqs */
149 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
151 printf("Unexpectedly get available freqs successfully with "
152 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
156 /* test with all valid input parameters */
157 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
158 TEST_POWER_FREQS_NUM_MAX);
159 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
160 printf("Fail to get available freqs on lcore %u\n",
161 TEST_POWER_LCORE_ID);
165 /* Save the total number of available freqs */
166 total_freq_num = ret;
171 /* Check rte_power_get_freq() */
173 check_power_get_freq(void)
178 /* test with an invalid lcore id */
179 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
180 if (count < TEST_POWER_FREQS_NUM_MAX) {
181 printf("Unexpectedly get freq index successfully on "
182 "lcore %u\n", TEST_POWER_LCORE_INVALID);
186 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
187 if (count >= TEST_POWER_FREQS_NUM_MAX) {
188 printf("Fail to get the freq index on lcore %u\n",
189 TEST_POWER_LCORE_ID);
193 /* Check the current frequency */
194 ret = check_cur_freq(TEST_POWER_LCORE_ID, count, false);
201 /* Check rte_power_set_freq() */
203 check_power_set_freq(void)
207 /* test with an invalid lcore id */
208 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
210 printf("Unexpectedly set freq index successfully on "
211 "lcore %u\n", TEST_POWER_LCORE_INVALID);
215 /* test with an invalid freq index */
216 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
217 TEST_POWER_FREQS_NUM_MAX);
219 printf("Unexpectedly set an invalid freq index (%u)"
220 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
221 TEST_POWER_LCORE_ID);
226 * test with an invalid freq index which is right one bigger than
227 * total number of freqs
229 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
231 printf("Unexpectedly set an invalid freq index (%u)"
232 "successfully on lcore %u\n", total_freq_num,
233 TEST_POWER_LCORE_ID);
236 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
238 printf("Fail to set freq index on lcore %u\n",
239 TEST_POWER_LCORE_ID);
243 /* Check the current frequency */
244 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
251 /* Check rte_power_freq_down() */
253 check_power_freq_down(void)
257 rte_power_freq_enable_turbo(TEST_POWER_LCORE_ID);
259 /* test with an invalid lcore id */
260 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
262 printf("Unexpectedly scale down successfully the freq on "
263 "lcore %u\n", TEST_POWER_LCORE_INVALID);
267 /* Scale down to min and then scale down one step */
268 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
270 printf("Fail to scale down the freq to min on lcore %u\n",
271 TEST_POWER_LCORE_ID);
274 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
276 printf("Fail to scale down the freq on lcore %u\n",
277 TEST_POWER_LCORE_ID);
281 /* Check the current frequency */
282 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
286 /* Scale up to max and then scale down one step */
287 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
289 printf("Fail to scale up the freq to max on lcore %u\n",
290 TEST_POWER_LCORE_ID);
293 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
295 printf("Fail to scale down the freq on lcore %u\n",
296 TEST_POWER_LCORE_ID);
300 /* Check the current frequency */
301 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1, false);
308 /* Check rte_power_freq_up() */
310 check_power_freq_up(void)
314 /* test with an invalid lcore id */
315 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
317 printf("Unexpectedly scale up successfully the freq on %u\n",
318 TEST_POWER_LCORE_INVALID);
322 /* Scale down to min and then scale up one step */
323 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
325 printf("Fail to scale down the freq to min on lcore %u\n",
326 TEST_POWER_LCORE_ID);
329 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
331 printf("Fail to scale up the freq on lcore %u\n",
332 TEST_POWER_LCORE_ID);
336 /* Check the current frequency */
337 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2, false);
341 /* Scale up to max and then scale up one step */
342 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
344 printf("Fail to scale up the freq to max on lcore %u\n",
345 TEST_POWER_LCORE_ID);
348 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
350 printf("Fail to scale up the freq on lcore %u\n",
351 TEST_POWER_LCORE_ID);
355 /* Check the current frequency */
356 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
363 /* Check rte_power_freq_max() */
365 check_power_freq_max(void)
369 /* test with an invalid lcore id */
370 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
372 printf("Unexpectedly scale up successfully the freq to max on "
373 "lcore %u\n", TEST_POWER_LCORE_INVALID);
376 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
378 printf("Fail to scale up the freq to max on lcore %u\n",
379 TEST_POWER_LCORE_ID);
383 /* Check the current frequency */
384 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
391 /* Check rte_power_freq_min() */
393 check_power_freq_min(void)
397 /* test with an invalid lcore id */
398 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
400 printf("Unexpectedly scale down successfully the freq to min "
401 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
404 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
406 printf("Fail to scale down the freq to min on lcore %u\n",
407 TEST_POWER_LCORE_ID);
411 /* Check the current frequency */
412 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
419 /* Check rte_power_turbo() */
421 check_power_turbo(void)
425 if (rte_power_turbo_status(TEST_POWER_LCORE_ID) == 0) {
426 printf("Turbo not available on lcore %u, skipping test\n",
427 TEST_POWER_LCORE_ID);
431 /* test with an invalid lcore id */
432 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_INVALID);
434 printf("Unexpectedly enable turbo successfully on lcore %u\n",
435 TEST_POWER_LCORE_INVALID);
438 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_ID);
440 printf("Fail to enable turbo on lcore %u\n",
441 TEST_POWER_LCORE_ID);
444 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
446 printf("Fail to scale up the freq to max on lcore %u\n",
447 TEST_POWER_LCORE_ID);
451 /* Check the current frequency */
452 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
456 /* test with an invalid lcore id */
457 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_INVALID);
459 printf("Unexpectedly disable turbo successfully on lcore %u\n",
460 TEST_POWER_LCORE_INVALID);
463 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_ID);
465 printf("Fail to disable turbo on lcore %u\n",
466 TEST_POWER_LCORE_ID);
469 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
471 printf("Fail to scale up the freq to max on lcore %u\n",
472 TEST_POWER_LCORE_ID);
476 /* Check the current frequency */
477 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1, false);
485 test_power_cpufreq(void)
488 enum power_management_env env;
490 /* Test initialisation of a valid lcore */
491 ret = rte_power_init(TEST_POWER_LCORE_ID);
493 printf("Cannot initialise power management for lcore %u, this "
494 "may occur if environment is not configured "
495 "correctly(APCI cpufreq) or operating in another valid "
496 "Power management environment\n",
497 TEST_POWER_LCORE_ID);
498 rte_power_unset_env();
502 /* Test environment configuration */
503 env = rte_power_get_env();
504 if ((env != PM_ENV_ACPI_CPUFREQ) && (env != PM_ENV_PSTATE_CPUFREQ) &&
505 (env != PM_ENV_CPPC_CPUFREQ)) {
506 printf("Unexpectedly got an environment other than ACPI/PSTATE\n");
510 /* verify that function pointers are not NULL */
511 if (rte_power_freqs == NULL) {
512 printf("rte_power_freqs should not be NULL, environment has not been "
516 if (rte_power_get_freq == NULL) {
517 printf("rte_power_get_freq should not be NULL, environment has not "
518 "been initialised\n");
521 if (rte_power_set_freq == NULL) {
522 printf("rte_power_set_freq should not be NULL, environment has not "
523 "been initialised\n");
526 if (rte_power_freq_up == NULL) {
527 printf("rte_power_freq_up should not be NULL, environment has not "
528 "been initialised\n");
531 if (rte_power_freq_down == NULL) {
532 printf("rte_power_freq_down should not be NULL, environment has not "
533 "been initialised\n");
536 if (rte_power_freq_max == NULL) {
537 printf("rte_power_freq_max should not be NULL, environment has not "
538 "been initialised\n");
541 if (rte_power_freq_min == NULL) {
542 printf("rte_power_freq_min should not be NULL, environment has not "
543 "been initialised\n");
546 if (rte_power_turbo_status == NULL) {
547 printf("rte_power_turbo_status should not be NULL, environment has not "
548 "been initialised\n");
551 if (rte_power_freq_enable_turbo == NULL) {
552 printf("rte_power_freq_enable_turbo should not be NULL, environment has not "
553 "been initialised\n");
556 if (rte_power_freq_disable_turbo == NULL) {
557 printf("rte_power_freq_disable_turbo should not be NULL, environment has not "
558 "been initialised\n");
562 ret = rte_power_exit(TEST_POWER_LCORE_ID);
564 printf("Cannot exit power management for lcore %u\n",
565 TEST_POWER_LCORE_ID);
566 rte_power_unset_env();
570 /* test of init power management for an invalid lcore */
571 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
573 printf("Unexpectedly initialise power management successfully "
574 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
575 rte_power_unset_env();
579 /* Test initialisation of a valid lcore */
580 ret = rte_power_init(TEST_POWER_LCORE_ID);
582 printf("Cannot initialise power management for lcore %u, this "
583 "may occur if environment is not configured "
584 "correctly(APCI cpufreq) or operating in another valid "
585 "Power management environment\n", TEST_POWER_LCORE_ID);
586 rte_power_unset_env();
591 * test of initialising power management for the lcore which has
594 ret = rte_power_init(TEST_POWER_LCORE_ID);
596 printf("Unexpectedly init successfully power twice on "
597 "lcore %u\n", TEST_POWER_LCORE_ID);
601 ret = check_power_freqs();
605 if (total_freq_num < 2) {
606 rte_power_exit(TEST_POWER_LCORE_ID);
607 printf("Frequency can not be changed due to CPU itself\n");
608 rte_power_unset_env();
612 ret = check_power_get_freq();
616 ret = check_power_set_freq();
620 ret = check_power_freq_down();
624 ret = check_power_freq_up();
628 ret = check_power_freq_max();
632 ret = check_power_freq_min();
636 ret = check_power_turbo();
640 ret = rte_power_exit(TEST_POWER_LCORE_ID);
642 printf("Cannot exit power management for lcore %u\n",
643 TEST_POWER_LCORE_ID);
644 rte_power_unset_env();
649 * test of exiting power management for the lcore which has been exited
651 ret = rte_power_exit(TEST_POWER_LCORE_ID);
653 printf("Unexpectedly exit successfully power management twice "
654 "on lcore %u\n", TEST_POWER_LCORE_ID);
655 rte_power_unset_env();
659 /* test of exit power management for an invalid lcore */
660 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
662 printf("Unpectedly exit power management successfully for "
663 "lcore %u\n", TEST_POWER_LCORE_INVALID);
664 rte_power_unset_env();
667 rte_power_unset_env();
671 rte_power_exit(TEST_POWER_LCORE_ID);
672 rte_power_unset_env();
677 test_power_caps(void)
679 struct rte_power_core_capabilities caps;
682 ret = rte_power_init(TEST_POWER_LCORE_ID);
684 printf("Cannot initialise power management for lcore %u, this "
685 "may occur if environment is not configured "
686 "correctly(APCI cpufreq) or operating in another valid "
687 "Power management environment\n", TEST_POWER_LCORE_ID);
688 rte_power_unset_env();
692 ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
694 printf("POWER: Error getting capabilities\n");
698 printf("POWER: Capabilities %"PRIx64"\n", caps.capabilities);
700 rte_power_unset_env();
706 REGISTER_TEST_COMMAND(power_cpufreq_autotest, test_power_cpufreq);
707 REGISTER_TEST_COMMAND(power_caps_autotest, test_power_caps);