1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <rte_power.h>
15 #define TEST_POWER_LCORE_ID 2U
16 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
17 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
19 #define TEST_POWER_SYSFILE_CUR_FREQ \
20 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
22 static uint32_t total_freq_num;
23 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
26 check_cur_freq(unsigned lcore_id, uint32_t idx)
28 #define TEST_POWER_CONVERT_TO_DECIMAL 10
30 char fullpath[PATH_MAX];
35 if (snprintf(fullpath, sizeof(fullpath),
36 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
39 f = fopen(fullpath, "r");
43 if (fgets(buf, sizeof(buf), f) == NULL) {
44 goto fail_get_cur_freq;
46 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
47 ret = (freqs[idx] == cur_freq ? 0 : -1);
55 /* Check rte_power_freqs() */
57 check_power_freqs(void)
62 memset(freqs, 0, sizeof(freqs));
64 /* test with an invalid lcore id */
65 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
66 TEST_POWER_FREQS_NUM_MAX);
68 printf("Unexpectedly get available freqs successfully on "
69 "lcore %u\n", TEST_POWER_LCORE_INVALID);
73 /* test with NULL buffer to save available freqs */
74 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
75 TEST_POWER_FREQS_NUM_MAX);
77 printf("Unexpectedly get available freqs successfully with "
78 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
82 /* test of getting zero number of freqs */
83 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
85 printf("Unexpectedly get available freqs successfully with "
86 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
90 /* test with all valid input parameters */
91 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
92 TEST_POWER_FREQS_NUM_MAX);
93 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
94 printf("Fail to get available freqs on lcore %u\n",
99 /* Save the total number of available freqs */
100 total_freq_num = ret;
105 /* Check rte_power_get_freq() */
107 check_power_get_freq(void)
112 /* test with an invalid lcore id */
113 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
114 if (count < TEST_POWER_FREQS_NUM_MAX) {
115 printf("Unexpectedly get freq index successfully on "
116 "lcore %u\n", TEST_POWER_LCORE_INVALID);
120 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
121 if (count >= TEST_POWER_FREQS_NUM_MAX) {
122 printf("Fail to get the freq index on lcore %u\n",
123 TEST_POWER_LCORE_ID);
127 /* Check the current frequency */
128 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
135 /* Check rte_power_set_freq() */
137 check_power_set_freq(void)
141 /* test with an invalid lcore id */
142 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
144 printf("Unexpectedly set freq index successfully on "
145 "lcore %u\n", TEST_POWER_LCORE_INVALID);
149 /* test with an invalid freq index */
150 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
151 TEST_POWER_FREQS_NUM_MAX);
153 printf("Unexpectedly set an invalid freq index (%u)"
154 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
155 TEST_POWER_LCORE_ID);
160 * test with an invalid freq index which is right one bigger than
161 * total number of freqs
163 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
165 printf("Unexpectedly set an invalid freq index (%u)"
166 "successfully on lcore %u\n", total_freq_num,
167 TEST_POWER_LCORE_ID);
170 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
172 printf("Fail to set freq index on lcore %u\n",
173 TEST_POWER_LCORE_ID);
177 /* Check the current frequency */
178 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
185 /* Check rte_power_freq_down() */
187 check_power_freq_down(void)
191 /* test with an invalid lcore id */
192 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
194 printf("Unexpectedly scale down successfully the freq on "
195 "lcore %u\n", TEST_POWER_LCORE_INVALID);
199 /* Scale down to min and then scale down one step */
200 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
202 printf("Fail to scale down the freq to min on lcore %u\n",
203 TEST_POWER_LCORE_ID);
206 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
208 printf("Fail to scale down the freq on lcore %u\n",
209 TEST_POWER_LCORE_ID);
213 /* Check the current frequency */
214 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
218 /* Scale up to max and then scale down one step */
219 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
221 printf("Fail to scale up the freq to max on lcore %u\n",
222 TEST_POWER_LCORE_ID);
225 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
227 printf("Fail to scale down the freq on lcore %u\n",
228 TEST_POWER_LCORE_ID);
232 /* Check the current frequency */
233 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
240 /* Check rte_power_freq_up() */
242 check_power_freq_up(void)
246 /* test with an invalid lcore id */
247 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
249 printf("Unexpectedly scale up successfully the freq on %u\n",
250 TEST_POWER_LCORE_INVALID);
254 /* Scale down to min and then scale up one step */
255 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
257 printf("Fail to scale down the freq to min on lcore %u\n",
258 TEST_POWER_LCORE_ID);
261 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
263 printf("Fail to scale up the freq on lcore %u\n",
264 TEST_POWER_LCORE_ID);
268 /* Check the current frequency */
269 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
273 /* Scale up to max and then scale up one step */
274 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
276 printf("Fail to scale up the freq to max on lcore %u\n",
277 TEST_POWER_LCORE_ID);
280 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
282 printf("Fail to scale up the freq on lcore %u\n",
283 TEST_POWER_LCORE_ID);
287 /* Check the current frequency */
288 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
295 /* Check rte_power_freq_max() */
297 check_power_freq_max(void)
301 /* test with an invalid lcore id */
302 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
304 printf("Unexpectedly scale up successfully the freq to max on "
305 "lcore %u\n", TEST_POWER_LCORE_INVALID);
308 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
310 printf("Fail to scale up the freq to max on lcore %u\n",
311 TEST_POWER_LCORE_ID);
315 /* Check the current frequency */
316 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
323 /* Check rte_power_freq_min() */
325 check_power_freq_min(void)
329 /* test with an invalid lcore id */
330 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
332 printf("Unexpectedly scale down successfully the freq to min "
333 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
336 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
338 printf("Fail to scale down the freq to min on lcore %u\n",
339 TEST_POWER_LCORE_ID);
343 /* Check the current frequency */
344 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
352 test_power_acpi_cpufreq(void)
355 enum power_management_env env;
357 ret = rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
359 printf("Failed on setting environment to PM_ENV_ACPI_CPUFREQ, this "
360 "may occur if environment is not configured correctly or "
361 " operating in another valid Power management environment\n");
365 /* Test environment configuration */
366 env = rte_power_get_env();
367 if (env != PM_ENV_ACPI_CPUFREQ) {
368 printf("Unexpectedly got an environment other than ACPI cpufreq\n");
372 /* verify that function pointers are not NULL */
373 if (rte_power_freqs == NULL) {
374 printf("rte_power_freqs should not be NULL, environment has not been "
378 if (rte_power_get_freq == NULL) {
379 printf("rte_power_get_freq should not be NULL, environment has not "
380 "been initialised\n");
383 if (rte_power_set_freq == NULL) {
384 printf("rte_power_set_freq should not be NULL, environment has not "
385 "been initialised\n");
388 if (rte_power_freq_up == NULL) {
389 printf("rte_power_freq_up should not be NULL, environment has not "
390 "been initialised\n");
393 if (rte_power_freq_down == NULL) {
394 printf("rte_power_freq_down should not be NULL, environment has not "
395 "been initialised\n");
398 if (rte_power_freq_max == NULL) {
399 printf("rte_power_freq_max should not be NULL, environment has not "
400 "been initialised\n");
403 if (rte_power_freq_min == NULL) {
404 printf("rte_power_freq_min should not be NULL, environment has not "
405 "been initialised\n");
409 /* test of init power management for an invalid lcore */
410 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
412 printf("Unexpectedly initialise power management successfully "
413 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
414 rte_power_unset_env();
418 /* Test initialisation of a valid lcore */
419 ret = rte_power_init(TEST_POWER_LCORE_ID);
421 printf("Cannot initialise power management for lcore %u, this "
422 "may occur if environment is not configured "
423 "correctly(APCI cpufreq) or operating in another valid "
424 "Power management environment\n", TEST_POWER_LCORE_ID);
425 rte_power_unset_env();
430 * test of initialising power management for the lcore which has
433 ret = rte_power_init(TEST_POWER_LCORE_ID);
435 printf("Unexpectedly init successfully power twice on "
436 "lcore %u\n", TEST_POWER_LCORE_ID);
440 ret = check_power_freqs();
444 if (total_freq_num < 2) {
445 rte_power_exit(TEST_POWER_LCORE_ID);
446 printf("Frequency can not be changed due to CPU itself\n");
447 rte_power_unset_env();
451 ret = check_power_get_freq();
455 ret = check_power_set_freq();
459 ret = check_power_freq_down();
463 ret = check_power_freq_up();
467 ret = check_power_freq_max();
471 ret = check_power_freq_min();
475 ret = rte_power_exit(TEST_POWER_LCORE_ID);
477 printf("Cannot exit power management for lcore %u\n",
478 TEST_POWER_LCORE_ID);
479 rte_power_unset_env();
484 * test of exiting power management for the lcore which has been exited
486 ret = rte_power_exit(TEST_POWER_LCORE_ID);
488 printf("Unexpectedly exit successfully power management twice "
489 "on lcore %u\n", TEST_POWER_LCORE_ID);
490 rte_power_unset_env();
494 /* test of exit power management for an invalid lcore */
495 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
497 printf("Unpectedly exit power management successfully for "
498 "lcore %u\n", TEST_POWER_LCORE_INVALID);
499 rte_power_unset_env();
502 rte_power_unset_env();
506 rte_power_exit(TEST_POWER_LCORE_ID);
507 rte_power_unset_env();
511 REGISTER_TEST_COMMAND(power_acpi_cpufreq_autotest, test_power_acpi_cpufreq);