1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
14 #ifndef RTE_LIBRTE_POWER
17 test_power_acpi_cpufreq(void)
19 printf("Power management library not supported, skipping test\n");
24 test_power_acpi_caps(void)
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);
370 test_power_acpi_cpufreq(void)
373 enum power_management_env env;
375 /* Test initialisation of a valid lcore */
376 ret = rte_power_init(TEST_POWER_LCORE_ID);
378 printf("Cannot initialise power management for lcore %u, this "
379 "may occur if environment is not configured "
380 "correctly(APCI cpufreq) or operating in another valid "
381 "Power management environment\n",
382 TEST_POWER_LCORE_ID);
383 rte_power_unset_env();
387 /* Test environment configuration */
388 env = rte_power_get_env();
389 if ((env != PM_ENV_ACPI_CPUFREQ) && (env != PM_ENV_PSTATE_CPUFREQ)) {
390 printf("Unexpectedly got an environment other than ACPI/PSTATE\n");
394 /* verify that function pointers are not NULL */
395 if (rte_power_freqs == NULL) {
396 printf("rte_power_freqs should not be NULL, environment has not been "
400 if (rte_power_get_freq == NULL) {
401 printf("rte_power_get_freq should not be NULL, environment has not "
402 "been initialised\n");
405 if (rte_power_set_freq == NULL) {
406 printf("rte_power_set_freq should not be NULL, environment has not "
407 "been initialised\n");
410 if (rte_power_freq_up == NULL) {
411 printf("rte_power_freq_up should not be NULL, environment has not "
412 "been initialised\n");
415 if (rte_power_freq_down == NULL) {
416 printf("rte_power_freq_down should not be NULL, environment has not "
417 "been initialised\n");
420 if (rte_power_freq_max == NULL) {
421 printf("rte_power_freq_max should not be NULL, environment has not "
422 "been initialised\n");
425 if (rte_power_freq_min == NULL) {
426 printf("rte_power_freq_min should not be NULL, environment has not "
427 "been initialised\n");
431 ret = rte_power_exit(TEST_POWER_LCORE_ID);
433 printf("Cannot exit power management for lcore %u\n",
434 TEST_POWER_LCORE_ID);
435 rte_power_unset_env();
439 /* test of init power management for an invalid lcore */
440 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
442 printf("Unexpectedly initialise power management successfully "
443 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
444 rte_power_unset_env();
448 /* Test initialisation of a valid lcore */
449 ret = rte_power_init(TEST_POWER_LCORE_ID);
451 printf("Cannot initialise power management for lcore %u, this "
452 "may occur if environment is not configured "
453 "correctly(APCI cpufreq) or operating in another valid "
454 "Power management environment\n", TEST_POWER_LCORE_ID);
455 rte_power_unset_env();
460 * test of initialising power management for the lcore which has
463 ret = rte_power_init(TEST_POWER_LCORE_ID);
465 printf("Unexpectedly init successfully power twice on "
466 "lcore %u\n", TEST_POWER_LCORE_ID);
470 ret = check_power_freqs();
474 if (total_freq_num < 2) {
475 rte_power_exit(TEST_POWER_LCORE_ID);
476 printf("Frequency can not be changed due to CPU itself\n");
477 rte_power_unset_env();
481 ret = check_power_get_freq();
485 ret = check_power_set_freq();
489 ret = check_power_freq_down();
493 ret = check_power_freq_up();
497 ret = check_power_freq_max();
501 ret = check_power_freq_min();
505 ret = rte_power_exit(TEST_POWER_LCORE_ID);
507 printf("Cannot exit power management for lcore %u\n",
508 TEST_POWER_LCORE_ID);
509 rte_power_unset_env();
514 * test of exiting power management for the lcore which has been exited
516 ret = rte_power_exit(TEST_POWER_LCORE_ID);
518 printf("Unexpectedly exit successfully power management twice "
519 "on lcore %u\n", TEST_POWER_LCORE_ID);
520 rte_power_unset_env();
524 /* test of exit power management for an invalid lcore */
525 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
527 printf("Unpectedly exit power management successfully for "
528 "lcore %u\n", TEST_POWER_LCORE_INVALID);
529 rte_power_unset_env();
532 rte_power_unset_env();
536 rte_power_exit(TEST_POWER_LCORE_ID);
537 rte_power_unset_env();
542 test_power_acpi_caps(void)
544 struct rte_power_core_capabilities caps;
547 ret = rte_power_init(TEST_POWER_LCORE_ID);
549 printf("Cannot initialise power management for lcore %u, this "
550 "may occur if environment is not configured "
551 "correctly(APCI cpufreq) or operating in another valid "
552 "Power management environment\n", TEST_POWER_LCORE_ID);
553 rte_power_unset_env();
557 ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
559 printf("POWER: Error getting capabilities\n");
563 printf("POWER: Capabilities %"PRIx64"\n", caps.capabilities);
565 rte_power_unset_env();
571 REGISTER_TEST_COMMAND(power_acpi_cpufreq_autotest, test_power_acpi_cpufreq);
572 REGISTER_TEST_COMMAND(power_acpi_caps_autotest, test_power_acpi_caps);