1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
14 #ifndef RTE_LIBRTE_POWER
17 test_power_cpufreq(void)
19 printf("Power management library not supported, skipping test\n");
26 printf("Power management library not supported, skipping test\n");
31 #include <rte_power.h>
33 #define TEST_POWER_LCORE_ID 2U
34 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
35 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
37 #define TEST_POWER_SYSFILE_CUR_FREQ \
38 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_cur_freq"
40 static uint32_t total_freq_num;
41 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
44 check_cur_freq(unsigned lcore_id, uint32_t idx)
46 #define TEST_POWER_CONVERT_TO_DECIMAL 10
48 char fullpath[PATH_MAX];
53 if (snprintf(fullpath, sizeof(fullpath),
54 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
57 f = fopen(fullpath, "r");
61 if (fgets(buf, sizeof(buf), f) == NULL) {
62 goto fail_get_cur_freq;
64 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
65 ret = (freqs[idx] == cur_freq ? 0 : -1);
73 /* Check rte_power_freqs() */
75 check_power_freqs(void)
80 memset(freqs, 0, sizeof(freqs));
82 /* test with an invalid lcore id */
83 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
84 TEST_POWER_FREQS_NUM_MAX);
86 printf("Unexpectedly get available freqs successfully on "
87 "lcore %u\n", TEST_POWER_LCORE_INVALID);
91 /* test with NULL buffer to save available freqs */
92 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
93 TEST_POWER_FREQS_NUM_MAX);
95 printf("Unexpectedly get available freqs successfully with "
96 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
100 /* test of getting zero number of freqs */
101 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
103 printf("Unexpectedly get available freqs successfully with "
104 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
108 /* test with all valid input parameters */
109 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
110 TEST_POWER_FREQS_NUM_MAX);
111 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
112 printf("Fail to get available freqs on lcore %u\n",
113 TEST_POWER_LCORE_ID);
117 /* Save the total number of available freqs */
118 total_freq_num = ret;
123 /* Check rte_power_get_freq() */
125 check_power_get_freq(void)
130 /* test with an invalid lcore id */
131 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
132 if (count < TEST_POWER_FREQS_NUM_MAX) {
133 printf("Unexpectedly get freq index successfully on "
134 "lcore %u\n", TEST_POWER_LCORE_INVALID);
138 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
139 if (count >= TEST_POWER_FREQS_NUM_MAX) {
140 printf("Fail to get the freq index on lcore %u\n",
141 TEST_POWER_LCORE_ID);
145 /* Check the current frequency */
146 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
153 /* Check rte_power_set_freq() */
155 check_power_set_freq(void)
159 /* test with an invalid lcore id */
160 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
162 printf("Unexpectedly set freq index successfully on "
163 "lcore %u\n", TEST_POWER_LCORE_INVALID);
167 /* test with an invalid freq index */
168 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
169 TEST_POWER_FREQS_NUM_MAX);
171 printf("Unexpectedly set an invalid freq index (%u)"
172 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
173 TEST_POWER_LCORE_ID);
178 * test with an invalid freq index which is right one bigger than
179 * total number of freqs
181 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
183 printf("Unexpectedly set an invalid freq index (%u)"
184 "successfully on lcore %u\n", total_freq_num,
185 TEST_POWER_LCORE_ID);
188 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
190 printf("Fail to set freq index on lcore %u\n",
191 TEST_POWER_LCORE_ID);
195 /* Check the current frequency */
196 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
203 /* Check rte_power_freq_down() */
205 check_power_freq_down(void)
209 /* test with an invalid lcore id */
210 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
212 printf("Unexpectedly scale down successfully the freq on "
213 "lcore %u\n", TEST_POWER_LCORE_INVALID);
217 /* Scale down to min and then scale down one step */
218 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
220 printf("Fail to scale down the freq to min on lcore %u\n",
221 TEST_POWER_LCORE_ID);
224 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
226 printf("Fail to scale down the freq on lcore %u\n",
227 TEST_POWER_LCORE_ID);
231 /* Check the current frequency */
232 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
236 /* Scale up to max and then scale down one step */
237 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
239 printf("Fail to scale up the freq to max on lcore %u\n",
240 TEST_POWER_LCORE_ID);
243 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
245 printf("Fail to scale down the freq on lcore %u\n",
246 TEST_POWER_LCORE_ID);
250 /* Check the current frequency */
251 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
258 /* Check rte_power_freq_up() */
260 check_power_freq_up(void)
264 /* test with an invalid lcore id */
265 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
267 printf("Unexpectedly scale up successfully the freq on %u\n",
268 TEST_POWER_LCORE_INVALID);
272 /* Scale down to min and then scale up one step */
273 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
275 printf("Fail to scale down the freq to min on lcore %u\n",
276 TEST_POWER_LCORE_ID);
279 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
281 printf("Fail to scale up the freq on lcore %u\n",
282 TEST_POWER_LCORE_ID);
286 /* Check the current frequency */
287 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
291 /* Scale up to max and then scale up one step */
292 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
294 printf("Fail to scale up the freq to max on lcore %u\n",
295 TEST_POWER_LCORE_ID);
298 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
300 printf("Fail to scale up the freq on lcore %u\n",
301 TEST_POWER_LCORE_ID);
305 /* Check the current frequency */
306 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
313 /* Check rte_power_freq_max() */
315 check_power_freq_max(void)
319 /* test with an invalid lcore id */
320 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
322 printf("Unexpectedly scale up successfully the freq to max on "
323 "lcore %u\n", TEST_POWER_LCORE_INVALID);
326 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
328 printf("Fail to scale up the freq to max on lcore %u\n",
329 TEST_POWER_LCORE_ID);
333 /* Check the current frequency */
334 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
341 /* Check rte_power_freq_min() */
343 check_power_freq_min(void)
347 /* test with an invalid lcore id */
348 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
350 printf("Unexpectedly scale down successfully the freq to min "
351 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
354 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
356 printf("Fail to scale down the freq to min on lcore %u\n",
357 TEST_POWER_LCORE_ID);
361 /* Check the current frequency */
362 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
369 /* Check rte_power_turbo() */
371 check_power_turbo(void)
375 if (rte_power_turbo_status(TEST_POWER_LCORE_ID) == 0) {
376 printf("Turbo not available on lcore %u, skipping test\n",
377 TEST_POWER_LCORE_ID);
381 /* test with an invalid lcore id */
382 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_INVALID);
384 printf("Unexpectedly enable turbo successfully on lcore %u\n",
385 TEST_POWER_LCORE_INVALID);
388 ret = rte_power_freq_enable_turbo(TEST_POWER_LCORE_ID);
390 printf("Fail to enable turbo on lcore %u\n",
391 TEST_POWER_LCORE_ID);
395 /* Check the current frequency */
396 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
400 /* test with an invalid lcore id */
401 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_INVALID);
403 printf("Unexpectedly disable turbo successfully on lcore %u\n",
404 TEST_POWER_LCORE_INVALID);
407 ret = rte_power_freq_disable_turbo(TEST_POWER_LCORE_ID);
409 printf("Fail to disable turbo on lcore %u\n",
410 TEST_POWER_LCORE_ID);
414 /* Check the current frequency */
415 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
423 test_power_cpufreq(void)
426 enum power_management_env env;
428 /* Test initialisation of a valid lcore */
429 ret = rte_power_init(TEST_POWER_LCORE_ID);
431 printf("Cannot initialise power management for lcore %u, this "
432 "may occur if environment is not configured "
433 "correctly(APCI cpufreq) or operating in another valid "
434 "Power management environment\n",
435 TEST_POWER_LCORE_ID);
436 rte_power_unset_env();
440 /* Test environment configuration */
441 env = rte_power_get_env();
442 if ((env != PM_ENV_ACPI_CPUFREQ) && (env != PM_ENV_PSTATE_CPUFREQ)) {
443 printf("Unexpectedly got an environment other than ACPI/PSTATE\n");
447 /* verify that function pointers are not NULL */
448 if (rte_power_freqs == NULL) {
449 printf("rte_power_freqs should not be NULL, environment has not been "
453 if (rte_power_get_freq == NULL) {
454 printf("rte_power_get_freq should not be NULL, environment has not "
455 "been initialised\n");
458 if (rte_power_set_freq == NULL) {
459 printf("rte_power_set_freq should not be NULL, environment has not "
460 "been initialised\n");
463 if (rte_power_freq_up == NULL) {
464 printf("rte_power_freq_up should not be NULL, environment has not "
465 "been initialised\n");
468 if (rte_power_freq_down == NULL) {
469 printf("rte_power_freq_down should not be NULL, environment has not "
470 "been initialised\n");
473 if (rte_power_freq_max == NULL) {
474 printf("rte_power_freq_max should not be NULL, environment has not "
475 "been initialised\n");
478 if (rte_power_freq_min == NULL) {
479 printf("rte_power_freq_min should not be NULL, environment has not "
480 "been initialised\n");
483 if (rte_power_turbo_status == NULL) {
484 printf("rte_power_turbo_status should not be NULL, environment has not "
485 "been initialised\n");
488 if (rte_power_freq_enable_turbo == NULL) {
489 printf("rte_power_freq_enable_turbo should not be NULL, environment has not "
490 "been initialised\n");
493 if (rte_power_freq_disable_turbo == NULL) {
494 printf("rte_power_freq_disable_turbo should not be NULL, environment has not "
495 "been initialised\n");
499 ret = rte_power_exit(TEST_POWER_LCORE_ID);
501 printf("Cannot exit power management for lcore %u\n",
502 TEST_POWER_LCORE_ID);
503 rte_power_unset_env();
507 /* test of init power management for an invalid lcore */
508 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
510 printf("Unexpectedly initialise power management successfully "
511 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
512 rte_power_unset_env();
516 /* Test initialisation of a valid lcore */
517 ret = rte_power_init(TEST_POWER_LCORE_ID);
519 printf("Cannot initialise power management for lcore %u, this "
520 "may occur if environment is not configured "
521 "correctly(APCI cpufreq) or operating in another valid "
522 "Power management environment\n", TEST_POWER_LCORE_ID);
523 rte_power_unset_env();
528 * test of initialising power management for the lcore which has
531 ret = rte_power_init(TEST_POWER_LCORE_ID);
533 printf("Unexpectedly init successfully power twice on "
534 "lcore %u\n", TEST_POWER_LCORE_ID);
538 ret = check_power_freqs();
542 if (total_freq_num < 2) {
543 rte_power_exit(TEST_POWER_LCORE_ID);
544 printf("Frequency can not be changed due to CPU itself\n");
545 rte_power_unset_env();
549 ret = check_power_get_freq();
553 ret = check_power_set_freq();
557 ret = check_power_freq_down();
561 ret = check_power_freq_up();
565 ret = check_power_freq_max();
569 ret = check_power_freq_min();
573 ret = check_power_turbo();
577 ret = rte_power_exit(TEST_POWER_LCORE_ID);
579 printf("Cannot exit power management for lcore %u\n",
580 TEST_POWER_LCORE_ID);
581 rte_power_unset_env();
586 * test of exiting power management for the lcore which has been exited
588 ret = rte_power_exit(TEST_POWER_LCORE_ID);
590 printf("Unexpectedly exit successfully power management twice "
591 "on lcore %u\n", TEST_POWER_LCORE_ID);
592 rte_power_unset_env();
596 /* test of exit power management for an invalid lcore */
597 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
599 printf("Unpectedly exit power management successfully for "
600 "lcore %u\n", TEST_POWER_LCORE_INVALID);
601 rte_power_unset_env();
604 rte_power_unset_env();
608 rte_power_exit(TEST_POWER_LCORE_ID);
609 rte_power_unset_env();
614 test_power_caps(void)
616 struct rte_power_core_capabilities caps;
619 ret = rte_power_init(TEST_POWER_LCORE_ID);
621 printf("Cannot initialise power management for lcore %u, this "
622 "may occur if environment is not configured "
623 "correctly(APCI cpufreq) or operating in another valid "
624 "Power management environment\n", TEST_POWER_LCORE_ID);
625 rte_power_unset_env();
629 ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
631 printf("POWER: Error getting capabilities\n");
635 printf("POWER: Capabilities %"PRIx64"\n", caps.capabilities);
637 rte_power_unset_env();
643 REGISTER_TEST_COMMAND(power_cpufreq_autotest, test_power_cpufreq);
644 REGISTER_TEST_COMMAND(power_caps_autotest, test_power_caps);