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 #define TEST_POWER_SYSFILE_CUR_FREQ \
39 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_cur_freq"
41 static uint32_t total_freq_num;
42 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
45 check_cur_freq(unsigned lcore_id, uint32_t idx)
47 #define TEST_POWER_CONVERT_TO_DECIMAL 10
50 char fullpath[PATH_MAX];
56 if (snprintf(fullpath, sizeof(fullpath),
57 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
60 f = fopen(fullpath, "r");
64 for (i = 0; i < MAX_LOOP; i++) {
66 if (fgets(buf, sizeof(buf), f) == NULL)
69 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
70 ret = (freqs[idx] == cur_freq ? 0 : -1);
75 if (fseek(f, 0, SEEK_SET) < 0) {
76 printf("Fail to set file position indicator to 0\n");
80 /* wait for the value to be updated */
90 /* Check rte_power_freqs() */
92 check_power_freqs(void)
97 memset(freqs, 0, sizeof(freqs));
99 /* test with an invalid lcore id */
100 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
101 TEST_POWER_FREQS_NUM_MAX);
103 printf("Unexpectedly get available freqs successfully on "
104 "lcore %u\n", TEST_POWER_LCORE_INVALID);
108 /* test with NULL buffer to save available freqs */
109 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
110 TEST_POWER_FREQS_NUM_MAX);
112 printf("Unexpectedly get available freqs successfully with "
113 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
117 /* test of getting zero number of freqs */
118 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
120 printf("Unexpectedly get available freqs successfully with "
121 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
125 /* test with all valid input parameters */
126 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
127 TEST_POWER_FREQS_NUM_MAX);
128 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
129 printf("Fail to get available freqs on lcore %u\n",
130 TEST_POWER_LCORE_ID);
134 /* Save the total number of available freqs */
135 total_freq_num = ret;
140 /* Check rte_power_get_freq() */
142 check_power_get_freq(void)
147 /* test with an invalid lcore id */
148 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
149 if (count < TEST_POWER_FREQS_NUM_MAX) {
150 printf("Unexpectedly get freq index successfully on "
151 "lcore %u\n", TEST_POWER_LCORE_INVALID);
155 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
156 if (count >= TEST_POWER_FREQS_NUM_MAX) {
157 printf("Fail to get the freq index on lcore %u\n",
158 TEST_POWER_LCORE_ID);
162 /* Check the current frequency */
163 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
170 /* Check rte_power_set_freq() */
172 check_power_set_freq(void)
176 /* test with an invalid lcore id */
177 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
179 printf("Unexpectedly set freq index successfully on "
180 "lcore %u\n", TEST_POWER_LCORE_INVALID);
184 /* test with an invalid freq index */
185 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
186 TEST_POWER_FREQS_NUM_MAX);
188 printf("Unexpectedly set an invalid freq index (%u)"
189 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
190 TEST_POWER_LCORE_ID);
195 * test with an invalid freq index which is right one bigger than
196 * total number of freqs
198 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
200 printf("Unexpectedly set an invalid freq index (%u)"
201 "successfully on lcore %u\n", total_freq_num,
202 TEST_POWER_LCORE_ID);
205 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
207 printf("Fail to set freq index on lcore %u\n",
208 TEST_POWER_LCORE_ID);
212 /* Check the current frequency */
213 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
220 /* Check rte_power_freq_down() */
222 check_power_freq_down(void)
226 /* test with an invalid lcore id */
227 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
229 printf("Unexpectedly scale down successfully the freq on "
230 "lcore %u\n", TEST_POWER_LCORE_INVALID);
234 /* Scale down to min and then scale down one step */
235 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
237 printf("Fail to scale down the freq to min on lcore %u\n",
238 TEST_POWER_LCORE_ID);
241 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
243 printf("Fail to scale down the freq on lcore %u\n",
244 TEST_POWER_LCORE_ID);
248 /* Check the current frequency */
249 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
253 /* Scale up to max and then scale down one step */
254 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
256 printf("Fail to scale up the freq to max on lcore %u\n",
257 TEST_POWER_LCORE_ID);
260 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
262 printf("Fail to scale down the freq on lcore %u\n",
263 TEST_POWER_LCORE_ID);
267 /* Check the current frequency */
268 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
275 /* Check rte_power_freq_up() */
277 check_power_freq_up(void)
281 /* test with an invalid lcore id */
282 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
284 printf("Unexpectedly scale up successfully the freq on %u\n",
285 TEST_POWER_LCORE_INVALID);
289 /* Scale down to min and then scale up one step */
290 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
292 printf("Fail to scale down the freq to min on lcore %u\n",
293 TEST_POWER_LCORE_ID);
296 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
298 printf("Fail to scale up the freq on lcore %u\n",
299 TEST_POWER_LCORE_ID);
303 /* Check the current frequency */
304 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
308 /* Scale up to max and then scale up one step */
309 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
311 printf("Fail to scale up the freq to max on lcore %u\n",
312 TEST_POWER_LCORE_ID);
315 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
317 printf("Fail to scale up the freq on lcore %u\n",
318 TEST_POWER_LCORE_ID);
322 /* Check the current frequency */
323 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
330 /* Check rte_power_freq_max() */
332 check_power_freq_max(void)
336 /* test with an invalid lcore id */
337 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
339 printf("Unexpectedly scale up successfully the freq to max on "
340 "lcore %u\n", TEST_POWER_LCORE_INVALID);
343 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
345 printf("Fail to scale up the freq to max on lcore %u\n",
346 TEST_POWER_LCORE_ID);
350 /* Check the current frequency */
351 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
358 /* Check rte_power_freq_min() */
360 check_power_freq_min(void)
364 /* test with an invalid lcore id */
365 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
367 printf("Unexpectedly scale down successfully the freq to min "
368 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
371 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
373 printf("Fail to scale down the freq to min on lcore %u\n",
374 TEST_POWER_LCORE_ID);
378 /* Check the current frequency */
379 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
386 /* Check rte_power_turbo() */
388 check_power_turbo(void)
392 if (rte_power_turbo_status(TEST_POWER_LCORE_ID) == 0) {
393 printf("Turbo not available on lcore %u, skipping test\n",
394 TEST_POWER_LCORE_ID);
398 /* test with an invalid lcore id */
399 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_INVALID);
401 printf("Unexpectedly enable turbo successfully on lcore %u\n",
402 TEST_POWER_LCORE_INVALID);
405 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_ID);
407 printf("Fail to enable turbo on lcore %u\n",
408 TEST_POWER_LCORE_ID);
412 /* Check the current frequency */
413 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
417 /* test with an invalid lcore id */
418 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_INVALID);
420 printf("Unexpectedly disable turbo successfully on lcore %u\n",
421 TEST_POWER_LCORE_INVALID);
424 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_ID);
426 printf("Fail to disable turbo on lcore %u\n",
427 TEST_POWER_LCORE_ID);
431 /* Check the current frequency */
432 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
440 test_power_cpufreq(void)
443 enum power_management_env env;
445 /* Test initialisation of a valid lcore */
446 ret = rte_power_init(TEST_POWER_LCORE_ID);
448 printf("Cannot initialise power management for lcore %u, this "
449 "may occur if environment is not configured "
450 "correctly(APCI cpufreq) or operating in another valid "
451 "Power management environment\n",
452 TEST_POWER_LCORE_ID);
453 rte_power_unset_env();
457 /* Test environment configuration */
458 env = rte_power_get_env();
459 if ((env != PM_ENV_ACPI_CPUFREQ) && (env != PM_ENV_PSTATE_CPUFREQ)) {
460 printf("Unexpectedly got an environment other than ACPI/PSTATE\n");
464 /* verify that function pointers are not NULL */
465 if (rte_power_freqs == NULL) {
466 printf("rte_power_freqs should not be NULL, environment has not been "
470 if (rte_power_get_freq == NULL) {
471 printf("rte_power_get_freq should not be NULL, environment has not "
472 "been initialised\n");
475 if (rte_power_set_freq == NULL) {
476 printf("rte_power_set_freq should not be NULL, environment has not "
477 "been initialised\n");
480 if (rte_power_freq_up == NULL) {
481 printf("rte_power_freq_up should not be NULL, environment has not "
482 "been initialised\n");
485 if (rte_power_freq_down == NULL) {
486 printf("rte_power_freq_down should not be NULL, environment has not "
487 "been initialised\n");
490 if (rte_power_freq_max == NULL) {
491 printf("rte_power_freq_max should not be NULL, environment has not "
492 "been initialised\n");
495 if (rte_power_freq_min == NULL) {
496 printf("rte_power_freq_min should not be NULL, environment has not "
497 "been initialised\n");
500 if (rte_power_turbo_status == NULL) {
501 printf("rte_power_turbo_status should not be NULL, environment has not "
502 "been initialised\n");
505 if (rte_power_freq_enable_turbo == NULL) {
506 printf("rte_power_freq_enable_turbo should not be NULL, environment has not "
507 "been initialised\n");
510 if (rte_power_freq_disable_turbo == NULL) {
511 printf("rte_power_freq_disable_turbo should not be NULL, environment has not "
512 "been initialised\n");
516 ret = rte_power_exit(TEST_POWER_LCORE_ID);
518 printf("Cannot exit power management for lcore %u\n",
519 TEST_POWER_LCORE_ID);
520 rte_power_unset_env();
524 /* test of init power management for an invalid lcore */
525 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
527 printf("Unexpectedly initialise power management successfully "
528 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
529 rte_power_unset_env();
533 /* Test initialisation of a valid lcore */
534 ret = rte_power_init(TEST_POWER_LCORE_ID);
536 printf("Cannot initialise power management for lcore %u, this "
537 "may occur if environment is not configured "
538 "correctly(APCI cpufreq) or operating in another valid "
539 "Power management environment\n", TEST_POWER_LCORE_ID);
540 rte_power_unset_env();
545 * test of initialising power management for the lcore which has
548 ret = rte_power_init(TEST_POWER_LCORE_ID);
550 printf("Unexpectedly init successfully power twice on "
551 "lcore %u\n", TEST_POWER_LCORE_ID);
555 ret = check_power_freqs();
559 if (total_freq_num < 2) {
560 rte_power_exit(TEST_POWER_LCORE_ID);
561 printf("Frequency can not be changed due to CPU itself\n");
562 rte_power_unset_env();
566 ret = check_power_get_freq();
570 ret = check_power_set_freq();
574 ret = check_power_freq_down();
578 ret = check_power_freq_up();
582 ret = check_power_freq_max();
586 ret = check_power_freq_min();
590 ret = check_power_turbo();
594 ret = rte_power_exit(TEST_POWER_LCORE_ID);
596 printf("Cannot exit power management for lcore %u\n",
597 TEST_POWER_LCORE_ID);
598 rte_power_unset_env();
603 * test of exiting power management for the lcore which has been exited
605 ret = rte_power_exit(TEST_POWER_LCORE_ID);
607 printf("Unexpectedly exit successfully power management twice "
608 "on lcore %u\n", TEST_POWER_LCORE_ID);
609 rte_power_unset_env();
613 /* test of exit power management for an invalid lcore */
614 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
616 printf("Unpectedly exit power management successfully for "
617 "lcore %u\n", TEST_POWER_LCORE_INVALID);
618 rte_power_unset_env();
621 rte_power_unset_env();
625 rte_power_exit(TEST_POWER_LCORE_ID);
626 rte_power_unset_env();
631 test_power_caps(void)
633 struct rte_power_core_capabilities caps;
636 ret = rte_power_init(TEST_POWER_LCORE_ID);
638 printf("Cannot initialise power management for lcore %u, this "
639 "may occur if environment is not configured "
640 "correctly(APCI cpufreq) or operating in another valid "
641 "Power management environment\n", TEST_POWER_LCORE_ID);
642 rte_power_unset_env();
646 ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
648 printf("POWER: Error getting capabilities\n");
652 printf("POWER: Capabilities %"PRIx64"\n", caps.capabilities);
654 rte_power_unset_env();
660 REGISTER_TEST_COMMAND(power_cpufreq_autotest, test_power_cpufreq);
661 REGISTER_TEST_COMMAND(power_caps_autotest, test_power_caps);