1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #ifndef RTE_LIBRTE_POWER
16 test_power_acpi_cpufreq(void)
18 printf("Power management library not supported, skipping test\n");
23 #include <rte_power.h>
25 #define TEST_POWER_LCORE_ID 2U
26 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
27 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
29 #define TEST_POWER_SYSFILE_CUR_FREQ \
30 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
32 static uint32_t total_freq_num;
33 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
36 check_cur_freq(unsigned lcore_id, uint32_t idx)
38 #define TEST_POWER_CONVERT_TO_DECIMAL 10
40 char fullpath[PATH_MAX];
45 if (snprintf(fullpath, sizeof(fullpath),
46 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
49 f = fopen(fullpath, "r");
53 if (fgets(buf, sizeof(buf), f) == NULL) {
54 goto fail_get_cur_freq;
56 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
57 ret = (freqs[idx] == cur_freq ? 0 : -1);
65 /* Check rte_power_freqs() */
67 check_power_freqs(void)
72 memset(freqs, 0, sizeof(freqs));
74 /* test with an invalid lcore id */
75 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
76 TEST_POWER_FREQS_NUM_MAX);
78 printf("Unexpectedly get available freqs successfully on "
79 "lcore %u\n", TEST_POWER_LCORE_INVALID);
83 /* test with NULL buffer to save available freqs */
84 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
85 TEST_POWER_FREQS_NUM_MAX);
87 printf("Unexpectedly get available freqs successfully with "
88 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
92 /* test of getting zero number of freqs */
93 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
95 printf("Unexpectedly get available freqs successfully with "
96 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
100 /* test with all valid input parameters */
101 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
102 TEST_POWER_FREQS_NUM_MAX);
103 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
104 printf("Fail to get available freqs on lcore %u\n",
105 TEST_POWER_LCORE_ID);
109 /* Save the total number of available freqs */
110 total_freq_num = ret;
115 /* Check rte_power_get_freq() */
117 check_power_get_freq(void)
122 /* test with an invalid lcore id */
123 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
124 if (count < TEST_POWER_FREQS_NUM_MAX) {
125 printf("Unexpectedly get freq index successfully on "
126 "lcore %u\n", TEST_POWER_LCORE_INVALID);
130 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
131 if (count >= TEST_POWER_FREQS_NUM_MAX) {
132 printf("Fail to get the freq index on lcore %u\n",
133 TEST_POWER_LCORE_ID);
137 /* Check the current frequency */
138 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
145 /* Check rte_power_set_freq() */
147 check_power_set_freq(void)
151 /* test with an invalid lcore id */
152 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
154 printf("Unexpectedly set freq index successfully on "
155 "lcore %u\n", TEST_POWER_LCORE_INVALID);
159 /* test with an invalid freq index */
160 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
161 TEST_POWER_FREQS_NUM_MAX);
163 printf("Unexpectedly set an invalid freq index (%u)"
164 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
165 TEST_POWER_LCORE_ID);
170 * test with an invalid freq index which is right one bigger than
171 * total number of freqs
173 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
175 printf("Unexpectedly set an invalid freq index (%u)"
176 "successfully on lcore %u\n", total_freq_num,
177 TEST_POWER_LCORE_ID);
180 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
182 printf("Fail to set freq index on lcore %u\n",
183 TEST_POWER_LCORE_ID);
187 /* Check the current frequency */
188 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
195 /* Check rte_power_freq_down() */
197 check_power_freq_down(void)
201 /* test with an invalid lcore id */
202 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
204 printf("Unexpectedly scale down successfully the freq on "
205 "lcore %u\n", TEST_POWER_LCORE_INVALID);
209 /* Scale down to min and then scale down one step */
210 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
212 printf("Fail to scale down the freq to min on lcore %u\n",
213 TEST_POWER_LCORE_ID);
216 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
218 printf("Fail to scale down the freq on lcore %u\n",
219 TEST_POWER_LCORE_ID);
223 /* Check the current frequency */
224 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
228 /* Scale up to max and then scale down one step */
229 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
231 printf("Fail to scale up the freq to max on lcore %u\n",
232 TEST_POWER_LCORE_ID);
235 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
237 printf("Fail to scale down the freq on lcore %u\n",
238 TEST_POWER_LCORE_ID);
242 /* Check the current frequency */
243 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
250 /* Check rte_power_freq_up() */
252 check_power_freq_up(void)
256 /* test with an invalid lcore id */
257 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
259 printf("Unexpectedly scale up successfully the freq on %u\n",
260 TEST_POWER_LCORE_INVALID);
264 /* Scale down to min and then scale up one step */
265 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
267 printf("Fail to scale down the freq to min on lcore %u\n",
268 TEST_POWER_LCORE_ID);
271 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
273 printf("Fail to scale up the freq on lcore %u\n",
274 TEST_POWER_LCORE_ID);
278 /* Check the current frequency */
279 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
283 /* Scale up to max and then scale up one step */
284 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
286 printf("Fail to scale up the freq to max on lcore %u\n",
287 TEST_POWER_LCORE_ID);
290 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
292 printf("Fail to scale up the freq on lcore %u\n",
293 TEST_POWER_LCORE_ID);
297 /* Check the current frequency */
298 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
305 /* Check rte_power_freq_max() */
307 check_power_freq_max(void)
311 /* test with an invalid lcore id */
312 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
314 printf("Unexpectedly scale up successfully the freq to max on "
315 "lcore %u\n", TEST_POWER_LCORE_INVALID);
318 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
320 printf("Fail to scale up the freq to max on lcore %u\n",
321 TEST_POWER_LCORE_ID);
325 /* Check the current frequency */
326 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
333 /* Check rte_power_freq_min() */
335 check_power_freq_min(void)
339 /* test with an invalid lcore id */
340 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
342 printf("Unexpectedly scale down successfully the freq to min "
343 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
346 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
348 printf("Fail to scale down the freq to min on lcore %u\n",
349 TEST_POWER_LCORE_ID);
353 /* Check the current frequency */
354 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
362 test_power_acpi_cpufreq(void)
365 enum power_management_env env;
367 ret = rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
369 printf("Failed on setting environment to PM_ENV_ACPI_CPUFREQ, this "
370 "may occur if environment is not configured correctly or "
371 " operating in another valid Power management environment\n");
375 /* Test environment configuration */
376 env = rte_power_get_env();
377 if (env != PM_ENV_ACPI_CPUFREQ) {
378 printf("Unexpectedly got an environment other than ACPI cpufreq\n");
382 /* verify that function pointers are not NULL */
383 if (rte_power_freqs == NULL) {
384 printf("rte_power_freqs should not be NULL, environment has not been "
388 if (rte_power_get_freq == NULL) {
389 printf("rte_power_get_freq should not be NULL, environment has not "
390 "been initialised\n");
393 if (rte_power_set_freq == NULL) {
394 printf("rte_power_set_freq should not be NULL, environment has not "
395 "been initialised\n");
398 if (rte_power_freq_up == NULL) {
399 printf("rte_power_freq_up should not be NULL, environment has not "
400 "been initialised\n");
403 if (rte_power_freq_down == NULL) {
404 printf("rte_power_freq_down should not be NULL, environment has not "
405 "been initialised\n");
408 if (rte_power_freq_max == NULL) {
409 printf("rte_power_freq_max should not be NULL, environment has not "
410 "been initialised\n");
413 if (rte_power_freq_min == NULL) {
414 printf("rte_power_freq_min should not be NULL, environment has not "
415 "been initialised\n");
419 /* test of init power management for an invalid lcore */
420 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
422 printf("Unexpectedly initialise power management successfully "
423 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
424 rte_power_unset_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", TEST_POWER_LCORE_ID);
435 rte_power_unset_env();
440 * test of initialising power management for the lcore which has
443 ret = rte_power_init(TEST_POWER_LCORE_ID);
445 printf("Unexpectedly init successfully power twice on "
446 "lcore %u\n", TEST_POWER_LCORE_ID);
450 ret = check_power_freqs();
454 if (total_freq_num < 2) {
455 rte_power_exit(TEST_POWER_LCORE_ID);
456 printf("Frequency can not be changed due to CPU itself\n");
457 rte_power_unset_env();
461 ret = check_power_get_freq();
465 ret = check_power_set_freq();
469 ret = check_power_freq_down();
473 ret = check_power_freq_up();
477 ret = check_power_freq_max();
481 ret = check_power_freq_min();
485 ret = rte_power_exit(TEST_POWER_LCORE_ID);
487 printf("Cannot exit power management for lcore %u\n",
488 TEST_POWER_LCORE_ID);
489 rte_power_unset_env();
494 * test of exiting power management for the lcore which has been exited
496 ret = rte_power_exit(TEST_POWER_LCORE_ID);
498 printf("Unexpectedly exit successfully power management twice "
499 "on lcore %u\n", TEST_POWER_LCORE_ID);
500 rte_power_unset_env();
504 /* test of exit power management for an invalid lcore */
505 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
507 printf("Unpectedly exit power management successfully for "
508 "lcore %u\n", TEST_POWER_LCORE_INVALID);
509 rte_power_unset_env();
512 rte_power_unset_env();
516 rte_power_exit(TEST_POWER_LCORE_ID);
517 rte_power_unset_env();
522 REGISTER_TEST_COMMAND(power_acpi_cpufreq_autotest, test_power_acpi_cpufreq);