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 lcore_id, uint32_t idx)
53 #define TEST_POWER_CONVERT_TO_DECIMAL 10
56 char fullpath[PATH_MAX];
62 if (snprintf(fullpath, sizeof(fullpath),
63 TEST_POWER_SYSFILE_SCALING_FREQ, lcore_id) < 0) {
66 f = fopen(fullpath, "r");
68 if (snprintf(fullpath, sizeof(fullpath),
69 TEST_POWER_SYSFILE_CPUINFO_FREQ, lcore_id) < 0) {
72 f = fopen(fullpath, "r");
77 for (i = 0; i < MAX_LOOP; i++) {
79 if (fgets(buf, sizeof(buf), f) == NULL)
82 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
84 /* convert the frequency to nearest 100000 value
85 * Ex: if cur_freq=1396789 then freq_conv=1400000
86 * Ex: if cur_freq=800030 then freq_conv=800000
88 unsigned int freq_conv = 0;
89 freq_conv = (cur_freq + TEST_FREQ_ROUNDING_DELTA)
90 / TEST_ROUND_FREQ_TO_N_100000;
91 freq_conv = freq_conv * TEST_ROUND_FREQ_TO_N_100000;
93 ret = (freqs[idx] == freq_conv ? 0 : -1);
98 if (fseek(f, 0, SEEK_SET) < 0) {
99 printf("Fail to set file position indicator to 0\n");
103 /* wait for the value to be updated */
113 /* Check rte_power_freqs() */
115 check_power_freqs(void)
120 memset(freqs, 0, sizeof(freqs));
122 /* test with an invalid lcore id */
123 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
124 TEST_POWER_FREQS_NUM_MAX);
126 printf("Unexpectedly get available freqs successfully on "
127 "lcore %u\n", TEST_POWER_LCORE_INVALID);
131 /* test with NULL buffer to save available freqs */
132 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
133 TEST_POWER_FREQS_NUM_MAX);
135 printf("Unexpectedly get available freqs successfully with "
136 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
140 /* test of getting zero number of freqs */
141 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
143 printf("Unexpectedly get available freqs successfully with "
144 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
148 /* test with all valid input parameters */
149 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
150 TEST_POWER_FREQS_NUM_MAX);
151 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
152 printf("Fail to get available freqs on lcore %u\n",
153 TEST_POWER_LCORE_ID);
157 /* Save the total number of available freqs */
158 total_freq_num = ret;
163 /* Check rte_power_get_freq() */
165 check_power_get_freq(void)
170 /* test with an invalid lcore id */
171 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
172 if (count < TEST_POWER_FREQS_NUM_MAX) {
173 printf("Unexpectedly get freq index successfully on "
174 "lcore %u\n", TEST_POWER_LCORE_INVALID);
178 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
179 if (count >= TEST_POWER_FREQS_NUM_MAX) {
180 printf("Fail to get the freq index on lcore %u\n",
181 TEST_POWER_LCORE_ID);
185 /* Check the current frequency */
186 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
193 /* Check rte_power_set_freq() */
195 check_power_set_freq(void)
199 /* test with an invalid lcore id */
200 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
202 printf("Unexpectedly set freq index successfully on "
203 "lcore %u\n", TEST_POWER_LCORE_INVALID);
207 /* test with an invalid freq index */
208 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
209 TEST_POWER_FREQS_NUM_MAX);
211 printf("Unexpectedly set an invalid freq index (%u)"
212 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
213 TEST_POWER_LCORE_ID);
218 * test with an invalid freq index which is right one bigger than
219 * total number of freqs
221 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
223 printf("Unexpectedly set an invalid freq index (%u)"
224 "successfully on lcore %u\n", total_freq_num,
225 TEST_POWER_LCORE_ID);
228 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
230 printf("Fail to set freq index on lcore %u\n",
231 TEST_POWER_LCORE_ID);
235 /* Check the current frequency */
236 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
243 /* Check rte_power_freq_down() */
245 check_power_freq_down(void)
249 /* test with an invalid lcore id */
250 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
252 printf("Unexpectedly scale down successfully the freq on "
253 "lcore %u\n", TEST_POWER_LCORE_INVALID);
257 /* Scale down to min and then scale down one step */
258 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
260 printf("Fail to scale down the freq to min on lcore %u\n",
261 TEST_POWER_LCORE_ID);
264 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
266 printf("Fail to scale down the freq on lcore %u\n",
267 TEST_POWER_LCORE_ID);
271 /* Check the current frequency */
272 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
276 /* Scale up to max and then scale down one step */
277 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
279 printf("Fail to scale up the freq to max on lcore %u\n",
280 TEST_POWER_LCORE_ID);
283 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
285 printf("Fail to scale down the freq on lcore %u\n",
286 TEST_POWER_LCORE_ID);
290 /* Check the current frequency */
291 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
298 /* Check rte_power_freq_up() */
300 check_power_freq_up(void)
304 /* test with an invalid lcore id */
305 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
307 printf("Unexpectedly scale up successfully the freq on %u\n",
308 TEST_POWER_LCORE_INVALID);
312 /* Scale down to min and then scale up one step */
313 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
315 printf("Fail to scale down the freq to min on lcore %u\n",
316 TEST_POWER_LCORE_ID);
319 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
321 printf("Fail to scale up the freq on lcore %u\n",
322 TEST_POWER_LCORE_ID);
326 /* Check the current frequency */
327 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
331 /* Scale up to max and then scale up one step */
332 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
334 printf("Fail to scale up the freq to max on lcore %u\n",
335 TEST_POWER_LCORE_ID);
338 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
340 printf("Fail to scale up the freq on lcore %u\n",
341 TEST_POWER_LCORE_ID);
345 /* Check the current frequency */
346 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
353 /* Check rte_power_freq_max() */
355 check_power_freq_max(void)
359 /* test with an invalid lcore id */
360 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
362 printf("Unexpectedly scale up successfully the freq to max on "
363 "lcore %u\n", TEST_POWER_LCORE_INVALID);
366 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
368 printf("Fail to scale up the freq to max on lcore %u\n",
369 TEST_POWER_LCORE_ID);
373 /* Check the current frequency */
374 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
381 /* Check rte_power_freq_min() */
383 check_power_freq_min(void)
387 /* test with an invalid lcore id */
388 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
390 printf("Unexpectedly scale down successfully the freq to min "
391 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
394 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
396 printf("Fail to scale down the freq to min on lcore %u\n",
397 TEST_POWER_LCORE_ID);
401 /* Check the current frequency */
402 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
409 /* Check rte_power_turbo() */
411 check_power_turbo(void)
415 if (rte_power_turbo_status(TEST_POWER_LCORE_ID) == 0) {
416 printf("Turbo not available on lcore %u, skipping test\n",
417 TEST_POWER_LCORE_ID);
421 /* test with an invalid lcore id */
422 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_INVALID);
424 printf("Unexpectedly enable turbo successfully on lcore %u\n",
425 TEST_POWER_LCORE_INVALID);
428 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_ID);
430 printf("Fail to enable turbo on lcore %u\n",
431 TEST_POWER_LCORE_ID);
435 /* Check the current frequency */
436 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
440 /* test with an invalid lcore id */
441 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_INVALID);
443 printf("Unexpectedly disable turbo successfully on lcore %u\n",
444 TEST_POWER_LCORE_INVALID);
447 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_ID);
449 printf("Fail to disable turbo on lcore %u\n",
450 TEST_POWER_LCORE_ID);
454 /* Check the current frequency */
455 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
463 test_power_cpufreq(void)
466 enum power_management_env env;
468 /* Test initialisation of a valid lcore */
469 ret = rte_power_init(TEST_POWER_LCORE_ID);
471 printf("Cannot initialise power management for lcore %u, this "
472 "may occur if environment is not configured "
473 "correctly(APCI cpufreq) or operating in another valid "
474 "Power management environment\n",
475 TEST_POWER_LCORE_ID);
476 rte_power_unset_env();
480 /* Test environment configuration */
481 env = rte_power_get_env();
482 if ((env != PM_ENV_ACPI_CPUFREQ) && (env != PM_ENV_PSTATE_CPUFREQ)) {
483 printf("Unexpectedly got an environment other than ACPI/PSTATE\n");
487 /* verify that function pointers are not NULL */
488 if (rte_power_freqs == NULL) {
489 printf("rte_power_freqs should not be NULL, environment has not been "
493 if (rte_power_get_freq == NULL) {
494 printf("rte_power_get_freq should not be NULL, environment has not "
495 "been initialised\n");
498 if (rte_power_set_freq == NULL) {
499 printf("rte_power_set_freq should not be NULL, environment has not "
500 "been initialised\n");
503 if (rte_power_freq_up == NULL) {
504 printf("rte_power_freq_up should not be NULL, environment has not "
505 "been initialised\n");
508 if (rte_power_freq_down == NULL) {
509 printf("rte_power_freq_down should not be NULL, environment has not "
510 "been initialised\n");
513 if (rte_power_freq_max == NULL) {
514 printf("rte_power_freq_max should not be NULL, environment has not "
515 "been initialised\n");
518 if (rte_power_freq_min == NULL) {
519 printf("rte_power_freq_min should not be NULL, environment has not "
520 "been initialised\n");
523 if (rte_power_turbo_status == NULL) {
524 printf("rte_power_turbo_status should not be NULL, environment has not "
525 "been initialised\n");
528 if (rte_power_freq_enable_turbo == NULL) {
529 printf("rte_power_freq_enable_turbo should not be NULL, environment has not "
530 "been initialised\n");
533 if (rte_power_freq_disable_turbo == NULL) {
534 printf("rte_power_freq_disable_turbo should not be NULL, environment has not "
535 "been initialised\n");
539 ret = rte_power_exit(TEST_POWER_LCORE_ID);
541 printf("Cannot exit power management for lcore %u\n",
542 TEST_POWER_LCORE_ID);
543 rte_power_unset_env();
547 /* test of init power management for an invalid lcore */
548 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
550 printf("Unexpectedly initialise power management successfully "
551 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
552 rte_power_unset_env();
556 /* Test initialisation of a valid lcore */
557 ret = rte_power_init(TEST_POWER_LCORE_ID);
559 printf("Cannot initialise power management for lcore %u, this "
560 "may occur if environment is not configured "
561 "correctly(APCI cpufreq) or operating in another valid "
562 "Power management environment\n", TEST_POWER_LCORE_ID);
563 rte_power_unset_env();
568 * test of initialising power management for the lcore which has
571 ret = rte_power_init(TEST_POWER_LCORE_ID);
573 printf("Unexpectedly init successfully power twice on "
574 "lcore %u\n", TEST_POWER_LCORE_ID);
578 ret = check_power_freqs();
582 if (total_freq_num < 2) {
583 rte_power_exit(TEST_POWER_LCORE_ID);
584 printf("Frequency can not be changed due to CPU itself\n");
585 rte_power_unset_env();
589 ret = check_power_get_freq();
593 ret = check_power_set_freq();
597 ret = check_power_freq_down();
601 ret = check_power_freq_up();
605 ret = check_power_freq_max();
609 ret = check_power_freq_min();
613 ret = check_power_turbo();
617 ret = rte_power_exit(TEST_POWER_LCORE_ID);
619 printf("Cannot exit power management for lcore %u\n",
620 TEST_POWER_LCORE_ID);
621 rte_power_unset_env();
626 * test of exiting power management for the lcore which has been exited
628 ret = rte_power_exit(TEST_POWER_LCORE_ID);
630 printf("Unexpectedly exit successfully power management twice "
631 "on lcore %u\n", TEST_POWER_LCORE_ID);
632 rte_power_unset_env();
636 /* test of exit power management for an invalid lcore */
637 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
639 printf("Unpectedly exit power management successfully for "
640 "lcore %u\n", TEST_POWER_LCORE_INVALID);
641 rte_power_unset_env();
644 rte_power_unset_env();
648 rte_power_exit(TEST_POWER_LCORE_ID);
649 rte_power_unset_env();
654 test_power_caps(void)
656 struct rte_power_core_capabilities caps;
659 ret = rte_power_init(TEST_POWER_LCORE_ID);
661 printf("Cannot initialise power management for lcore %u, this "
662 "may occur if environment is not configured "
663 "correctly(APCI cpufreq) or operating in another valid "
664 "Power management environment\n", TEST_POWER_LCORE_ID);
665 rte_power_unset_env();
669 ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
671 printf("POWER: Error getting capabilities\n");
675 printf("POWER: Capabilities %"PRIx64"\n", caps.capabilities);
677 rte_power_unset_env();
683 REGISTER_TEST_COMMAND(power_cpufreq_autotest, test_power_cpufreq);
684 REGISTER_TEST_COMMAND(power_caps_autotest, test_power_caps);