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];
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;
94 ret = (freqs[idx] <= freq_conv ? 0 : -1);
96 ret = (freqs[idx] == freq_conv ? 0 : -1);
101 if (fseek(f, 0, SEEK_SET) < 0) {
102 printf("Fail to set file position indicator to 0\n");
106 /* wait for the value to be updated */
116 /* Check rte_power_freqs() */
118 check_power_freqs(void)
123 memset(freqs, 0, sizeof(freqs));
125 /* test with an invalid lcore id */
126 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
127 TEST_POWER_FREQS_NUM_MAX);
129 printf("Unexpectedly get available freqs successfully on "
130 "lcore %u\n", TEST_POWER_LCORE_INVALID);
134 /* test with NULL buffer to save available freqs */
135 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
136 TEST_POWER_FREQS_NUM_MAX);
138 printf("Unexpectedly get available freqs successfully with "
139 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
143 /* test of getting zero number of freqs */
144 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
146 printf("Unexpectedly get available freqs successfully with "
147 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
151 /* test with all valid input parameters */
152 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
153 TEST_POWER_FREQS_NUM_MAX);
154 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
155 printf("Fail to get available freqs on lcore %u\n",
156 TEST_POWER_LCORE_ID);
160 /* Save the total number of available freqs */
161 total_freq_num = ret;
166 /* Check rte_power_get_freq() */
168 check_power_get_freq(void)
173 /* test with an invalid lcore id */
174 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
175 if (count < TEST_POWER_FREQS_NUM_MAX) {
176 printf("Unexpectedly get freq index successfully on "
177 "lcore %u\n", TEST_POWER_LCORE_INVALID);
181 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
182 if (count >= TEST_POWER_FREQS_NUM_MAX) {
183 printf("Fail to get the freq index on lcore %u\n",
184 TEST_POWER_LCORE_ID);
188 /* Check the current frequency */
189 ret = check_cur_freq(TEST_POWER_LCORE_ID, count, false);
196 /* Check rte_power_set_freq() */
198 check_power_set_freq(void)
202 /* test with an invalid lcore id */
203 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
205 printf("Unexpectedly set freq index successfully on "
206 "lcore %u\n", TEST_POWER_LCORE_INVALID);
210 /* test with an invalid freq index */
211 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
212 TEST_POWER_FREQS_NUM_MAX);
214 printf("Unexpectedly set an invalid freq index (%u)"
215 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
216 TEST_POWER_LCORE_ID);
221 * test with an invalid freq index which is right one bigger than
222 * total number of freqs
224 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
226 printf("Unexpectedly set an invalid freq index (%u)"
227 "successfully on lcore %u\n", total_freq_num,
228 TEST_POWER_LCORE_ID);
231 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
233 printf("Fail to set freq index on lcore %u\n",
234 TEST_POWER_LCORE_ID);
238 /* Check the current frequency */
239 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
246 /* Check rte_power_freq_down() */
248 check_power_freq_down(void)
252 /* test with an invalid lcore id */
253 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
255 printf("Unexpectedly scale down successfully the freq on "
256 "lcore %u\n", TEST_POWER_LCORE_INVALID);
260 /* Scale down to min and then scale down one step */
261 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
263 printf("Fail to scale down the freq to min on lcore %u\n",
264 TEST_POWER_LCORE_ID);
267 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
269 printf("Fail to scale down the freq on lcore %u\n",
270 TEST_POWER_LCORE_ID);
274 /* Check the current frequency */
275 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
279 /* Scale up to max and then scale down one step */
280 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
282 printf("Fail to scale up the freq to max on lcore %u\n",
283 TEST_POWER_LCORE_ID);
286 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
288 printf("Fail to scale down the freq on lcore %u\n",
289 TEST_POWER_LCORE_ID);
293 /* Check the current frequency */
294 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1, false);
301 /* Check rte_power_freq_up() */
303 check_power_freq_up(void)
307 /* test with an invalid lcore id */
308 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
310 printf("Unexpectedly scale up successfully the freq on %u\n",
311 TEST_POWER_LCORE_INVALID);
315 /* Scale down to min and then scale up one step */
316 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
318 printf("Fail to scale down the freq to min on lcore %u\n",
319 TEST_POWER_LCORE_ID);
322 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
324 printf("Fail to scale up the freq on lcore %u\n",
325 TEST_POWER_LCORE_ID);
329 /* Check the current frequency */
330 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2, false);
334 /* Scale up to max and then scale up one step */
335 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
337 printf("Fail to scale up the freq to max on lcore %u\n",
338 TEST_POWER_LCORE_ID);
341 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
343 printf("Fail to scale up the freq on lcore %u\n",
344 TEST_POWER_LCORE_ID);
348 /* Check the current frequency */
349 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
356 /* Check rte_power_freq_max() */
358 check_power_freq_max(void)
362 /* test with an invalid lcore id */
363 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
365 printf("Unexpectedly scale up successfully the freq to max on "
366 "lcore %u\n", TEST_POWER_LCORE_INVALID);
369 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
371 printf("Fail to scale up the freq to max on lcore %u\n",
372 TEST_POWER_LCORE_ID);
376 /* Check the current frequency */
377 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
384 /* Check rte_power_freq_min() */
386 check_power_freq_min(void)
390 /* test with an invalid lcore id */
391 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
393 printf("Unexpectedly scale down successfully the freq to min "
394 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
397 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
399 printf("Fail to scale down the freq to min on lcore %u\n",
400 TEST_POWER_LCORE_ID);
404 /* Check the current frequency */
405 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1, false);
412 /* Check rte_power_turbo() */
414 check_power_turbo(void)
418 if (rte_power_turbo_status(TEST_POWER_LCORE_ID) == 0) {
419 printf("Turbo not available on lcore %u, skipping test\n",
420 TEST_POWER_LCORE_ID);
424 /* test with an invalid lcore id */
425 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_INVALID);
427 printf("Unexpectedly enable turbo successfully on lcore %u\n",
428 TEST_POWER_LCORE_INVALID);
431 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_ID);
433 printf("Fail to enable turbo on lcore %u\n",
434 TEST_POWER_LCORE_ID);
438 /* Check the current frequency */
439 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0, true);
443 /* test with an invalid lcore id */
444 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_INVALID);
446 printf("Unexpectedly disable turbo successfully on lcore %u\n",
447 TEST_POWER_LCORE_INVALID);
450 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_ID);
452 printf("Fail to disable turbo on lcore %u\n",
453 TEST_POWER_LCORE_ID);
457 /* Check the current frequency */
458 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1, false);
466 test_power_cpufreq(void)
469 enum power_management_env env;
471 /* Test initialisation of a valid lcore */
472 ret = rte_power_init(TEST_POWER_LCORE_ID);
474 printf("Cannot initialise power management for lcore %u, this "
475 "may occur if environment is not configured "
476 "correctly(APCI cpufreq) or operating in another valid "
477 "Power management environment\n",
478 TEST_POWER_LCORE_ID);
479 rte_power_unset_env();
483 /* Test environment configuration */
484 env = rte_power_get_env();
485 if ((env != PM_ENV_ACPI_CPUFREQ) && (env != PM_ENV_PSTATE_CPUFREQ)) {
486 printf("Unexpectedly got an environment other than ACPI/PSTATE\n");
490 /* verify that function pointers are not NULL */
491 if (rte_power_freqs == NULL) {
492 printf("rte_power_freqs should not be NULL, environment has not been "
496 if (rte_power_get_freq == NULL) {
497 printf("rte_power_get_freq should not be NULL, environment has not "
498 "been initialised\n");
501 if (rte_power_set_freq == NULL) {
502 printf("rte_power_set_freq should not be NULL, environment has not "
503 "been initialised\n");
506 if (rte_power_freq_up == NULL) {
507 printf("rte_power_freq_up should not be NULL, environment has not "
508 "been initialised\n");
511 if (rte_power_freq_down == NULL) {
512 printf("rte_power_freq_down should not be NULL, environment has not "
513 "been initialised\n");
516 if (rte_power_freq_max == NULL) {
517 printf("rte_power_freq_max should not be NULL, environment has not "
518 "been initialised\n");
521 if (rte_power_freq_min == NULL) {
522 printf("rte_power_freq_min should not be NULL, environment has not "
523 "been initialised\n");
526 if (rte_power_turbo_status == NULL) {
527 printf("rte_power_turbo_status should not be NULL, environment has not "
528 "been initialised\n");
531 if (rte_power_freq_enable_turbo == NULL) {
532 printf("rte_power_freq_enable_turbo should not be NULL, environment has not "
533 "been initialised\n");
536 if (rte_power_freq_disable_turbo == NULL) {
537 printf("rte_power_freq_disable_turbo should not be NULL, environment has not "
538 "been initialised\n");
542 ret = rte_power_exit(TEST_POWER_LCORE_ID);
544 printf("Cannot exit power management for lcore %u\n",
545 TEST_POWER_LCORE_ID);
546 rte_power_unset_env();
550 /* test of init power management for an invalid lcore */
551 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
553 printf("Unexpectedly initialise power management successfully "
554 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
555 rte_power_unset_env();
559 /* Test initialisation of a valid lcore */
560 ret = rte_power_init(TEST_POWER_LCORE_ID);
562 printf("Cannot initialise power management for lcore %u, this "
563 "may occur if environment is not configured "
564 "correctly(APCI cpufreq) or operating in another valid "
565 "Power management environment\n", TEST_POWER_LCORE_ID);
566 rte_power_unset_env();
571 * test of initialising power management for the lcore which has
574 ret = rte_power_init(TEST_POWER_LCORE_ID);
576 printf("Unexpectedly init successfully power twice on "
577 "lcore %u\n", TEST_POWER_LCORE_ID);
581 ret = check_power_freqs();
585 if (total_freq_num < 2) {
586 rte_power_exit(TEST_POWER_LCORE_ID);
587 printf("Frequency can not be changed due to CPU itself\n");
588 rte_power_unset_env();
592 ret = check_power_get_freq();
596 ret = check_power_set_freq();
600 ret = check_power_freq_down();
604 ret = check_power_freq_up();
608 ret = check_power_freq_max();
612 ret = check_power_freq_min();
616 ret = check_power_turbo();
620 ret = rte_power_exit(TEST_POWER_LCORE_ID);
622 printf("Cannot exit power management for lcore %u\n",
623 TEST_POWER_LCORE_ID);
624 rte_power_unset_env();
629 * test of exiting power management for the lcore which has been exited
631 ret = rte_power_exit(TEST_POWER_LCORE_ID);
633 printf("Unexpectedly exit successfully power management twice "
634 "on lcore %u\n", TEST_POWER_LCORE_ID);
635 rte_power_unset_env();
639 /* test of exit power management for an invalid lcore */
640 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
642 printf("Unpectedly exit power management successfully for "
643 "lcore %u\n", TEST_POWER_LCORE_INVALID);
644 rte_power_unset_env();
647 rte_power_unset_env();
651 rte_power_exit(TEST_POWER_LCORE_ID);
652 rte_power_unset_env();
657 test_power_caps(void)
659 struct rte_power_core_capabilities caps;
662 ret = rte_power_init(TEST_POWER_LCORE_ID);
664 printf("Cannot initialise power management for lcore %u, this "
665 "may occur if environment is not configured "
666 "correctly(APCI cpufreq) or operating in another valid "
667 "Power management environment\n", TEST_POWER_LCORE_ID);
668 rte_power_unset_env();
672 ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
674 printf("POWER: Error getting capabilities\n");
678 printf("POWER: Capabilities %"PRIx64"\n", caps.capabilities);
680 rte_power_unset_env();
686 REGISTER_TEST_COMMAND(power_cpufreq_autotest, test_power_cpufreq);
687 REGISTER_TEST_COMMAND(power_caps_autotest, test_power_caps);