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 test_power_acpi_caps(void)
25 printf("Power management library not supported, skipping test\n");
30 #include <rte_power.h>
32 #define TEST_POWER_LCORE_ID 2U
33 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
34 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
36 #define TEST_POWER_SYSFILE_CUR_FREQ \
37 "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_cur_freq"
39 static uint32_t total_freq_num;
40 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
43 check_cur_freq(unsigned lcore_id, uint32_t idx)
45 #define TEST_POWER_CONVERT_TO_DECIMAL 10
47 char fullpath[PATH_MAX];
52 if (snprintf(fullpath, sizeof(fullpath),
53 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
56 f = fopen(fullpath, "r");
60 if (fgets(buf, sizeof(buf), f) == NULL) {
61 goto fail_get_cur_freq;
63 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
64 ret = (freqs[idx] == cur_freq ? 0 : -1);
72 /* Check rte_power_freqs() */
74 check_power_freqs(void)
79 memset(freqs, 0, sizeof(freqs));
81 /* test with an invalid lcore id */
82 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
83 TEST_POWER_FREQS_NUM_MAX);
85 printf("Unexpectedly get available freqs successfully on "
86 "lcore %u\n", TEST_POWER_LCORE_INVALID);
90 /* test with NULL buffer to save available freqs */
91 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
92 TEST_POWER_FREQS_NUM_MAX);
94 printf("Unexpectedly get available freqs successfully with "
95 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
99 /* test of getting zero number of freqs */
100 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
102 printf("Unexpectedly get available freqs successfully with "
103 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
107 /* test with all valid input parameters */
108 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
109 TEST_POWER_FREQS_NUM_MAX);
110 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
111 printf("Fail to get available freqs on lcore %u\n",
112 TEST_POWER_LCORE_ID);
116 /* Save the total number of available freqs */
117 total_freq_num = ret;
122 /* Check rte_power_get_freq() */
124 check_power_get_freq(void)
129 /* test with an invalid lcore id */
130 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
131 if (count < TEST_POWER_FREQS_NUM_MAX) {
132 printf("Unexpectedly get freq index successfully on "
133 "lcore %u\n", TEST_POWER_LCORE_INVALID);
137 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
138 if (count >= TEST_POWER_FREQS_NUM_MAX) {
139 printf("Fail to get the freq index on lcore %u\n",
140 TEST_POWER_LCORE_ID);
144 /* Check the current frequency */
145 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
152 /* Check rte_power_set_freq() */
154 check_power_set_freq(void)
158 /* test with an invalid lcore id */
159 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
161 printf("Unexpectedly set freq index successfully on "
162 "lcore %u\n", TEST_POWER_LCORE_INVALID);
166 /* test with an invalid freq index */
167 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
168 TEST_POWER_FREQS_NUM_MAX);
170 printf("Unexpectedly set an invalid freq index (%u)"
171 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
172 TEST_POWER_LCORE_ID);
177 * test with an invalid freq index which is right one bigger than
178 * total number of freqs
180 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
182 printf("Unexpectedly set an invalid freq index (%u)"
183 "successfully on lcore %u\n", total_freq_num,
184 TEST_POWER_LCORE_ID);
187 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
189 printf("Fail to set freq index on lcore %u\n",
190 TEST_POWER_LCORE_ID);
194 /* Check the current frequency */
195 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
202 /* Check rte_power_freq_down() */
204 check_power_freq_down(void)
208 /* test with an invalid lcore id */
209 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
211 printf("Unexpectedly scale down successfully the freq on "
212 "lcore %u\n", TEST_POWER_LCORE_INVALID);
216 /* Scale down to min and then scale down one step */
217 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
219 printf("Fail to scale down the freq to min on lcore %u\n",
220 TEST_POWER_LCORE_ID);
223 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
225 printf("Fail to scale down the freq on lcore %u\n",
226 TEST_POWER_LCORE_ID);
230 /* Check the current frequency */
231 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
235 /* Scale up to max and then scale down one step */
236 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
238 printf("Fail to scale up the freq to max on lcore %u\n",
239 TEST_POWER_LCORE_ID);
242 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
244 printf("Fail to scale down the freq on lcore %u\n",
245 TEST_POWER_LCORE_ID);
249 /* Check the current frequency */
250 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
257 /* Check rte_power_freq_up() */
259 check_power_freq_up(void)
263 /* test with an invalid lcore id */
264 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
266 printf("Unexpectedly scale up successfully the freq on %u\n",
267 TEST_POWER_LCORE_INVALID);
271 /* Scale down to min and then scale up one step */
272 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
274 printf("Fail to scale down the freq to min on lcore %u\n",
275 TEST_POWER_LCORE_ID);
278 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
280 printf("Fail to scale up the freq on lcore %u\n",
281 TEST_POWER_LCORE_ID);
285 /* Check the current frequency */
286 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
290 /* Scale up to max and then scale up one step */
291 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
293 printf("Fail to scale up the freq to max on lcore %u\n",
294 TEST_POWER_LCORE_ID);
297 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
299 printf("Fail to scale up the freq on lcore %u\n",
300 TEST_POWER_LCORE_ID);
304 /* Check the current frequency */
305 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
312 /* Check rte_power_freq_max() */
314 check_power_freq_max(void)
318 /* test with an invalid lcore id */
319 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
321 printf("Unexpectedly scale up successfully the freq to max on "
322 "lcore %u\n", TEST_POWER_LCORE_INVALID);
325 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
327 printf("Fail to scale up the freq to max on lcore %u\n",
328 TEST_POWER_LCORE_ID);
332 /* Check the current frequency */
333 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
340 /* Check rte_power_freq_min() */
342 check_power_freq_min(void)
346 /* test with an invalid lcore id */
347 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
349 printf("Unexpectedly scale down successfully the freq to min "
350 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
353 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
355 printf("Fail to scale down the freq to min on lcore %u\n",
356 TEST_POWER_LCORE_ID);
360 /* Check the current frequency */
361 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
369 test_power_acpi_cpufreq(void)
372 enum power_management_env env;
374 ret = rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
376 printf("Failed on setting environment to PM_ENV_ACPI_CPUFREQ, this "
377 "may occur if environment is not configured correctly or "
378 " operating in another valid Power management environment\n");
382 /* Test environment configuration */
383 env = rte_power_get_env();
384 if (env != PM_ENV_ACPI_CPUFREQ) {
385 printf("Unexpectedly got an environment other than ACPI cpufreq\n");
389 /* verify that function pointers are not NULL */
390 if (rte_power_freqs == NULL) {
391 printf("rte_power_freqs should not be NULL, environment has not been "
395 if (rte_power_get_freq == NULL) {
396 printf("rte_power_get_freq should not be NULL, environment has not "
397 "been initialised\n");
400 if (rte_power_set_freq == NULL) {
401 printf("rte_power_set_freq should not be NULL, environment has not "
402 "been initialised\n");
405 if (rte_power_freq_up == NULL) {
406 printf("rte_power_freq_up should not be NULL, environment has not "
407 "been initialised\n");
410 if (rte_power_freq_down == NULL) {
411 printf("rte_power_freq_down should not be NULL, environment has not "
412 "been initialised\n");
415 if (rte_power_freq_max == NULL) {
416 printf("rte_power_freq_max should not be NULL, environment has not "
417 "been initialised\n");
420 if (rte_power_freq_min == NULL) {
421 printf("rte_power_freq_min should not be NULL, environment has not "
422 "been initialised\n");
426 /* test of init power management for an invalid lcore */
427 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
429 printf("Unexpectedly initialise power management successfully "
430 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
431 rte_power_unset_env();
435 /* Test initialisation of a valid lcore */
436 ret = rte_power_init(TEST_POWER_LCORE_ID);
438 printf("Cannot initialise power management for lcore %u, this "
439 "may occur if environment is not configured "
440 "correctly(APCI cpufreq) or operating in another valid "
441 "Power management environment\n", TEST_POWER_LCORE_ID);
442 rte_power_unset_env();
447 * test of initialising power management for the lcore which has
450 ret = rte_power_init(TEST_POWER_LCORE_ID);
452 printf("Unexpectedly init successfully power twice on "
453 "lcore %u\n", TEST_POWER_LCORE_ID);
457 ret = check_power_freqs();
461 if (total_freq_num < 2) {
462 rte_power_exit(TEST_POWER_LCORE_ID);
463 printf("Frequency can not be changed due to CPU itself\n");
464 rte_power_unset_env();
468 ret = check_power_get_freq();
472 ret = check_power_set_freq();
476 ret = check_power_freq_down();
480 ret = check_power_freq_up();
484 ret = check_power_freq_max();
488 ret = check_power_freq_min();
492 ret = rte_power_exit(TEST_POWER_LCORE_ID);
494 printf("Cannot exit power management for lcore %u\n",
495 TEST_POWER_LCORE_ID);
496 rte_power_unset_env();
501 * test of exiting power management for the lcore which has been exited
503 ret = rte_power_exit(TEST_POWER_LCORE_ID);
505 printf("Unexpectedly exit successfully power management twice "
506 "on lcore %u\n", TEST_POWER_LCORE_ID);
507 rte_power_unset_env();
511 /* test of exit power management for an invalid lcore */
512 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
514 printf("Unpectedly exit power management successfully for "
515 "lcore %u\n", TEST_POWER_LCORE_INVALID);
516 rte_power_unset_env();
519 rte_power_unset_env();
523 rte_power_exit(TEST_POWER_LCORE_ID);
524 rte_power_unset_env();
529 test_power_acpi_caps(void)
531 struct rte_power_core_capabilities caps;
534 ret = rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
536 printf("Error setting ACPI environment\n");
540 ret = rte_power_init(TEST_POWER_LCORE_ID);
542 printf("Cannot initialise power management for lcore %u, this "
543 "may occur if environment is not configured "
544 "correctly(APCI cpufreq) or operating in another valid "
545 "Power management environment\n", TEST_POWER_LCORE_ID);
546 rte_power_unset_env();
550 ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
552 printf("ACPI: Error getting capabilities\n");
556 printf("ACPI: Capabilities %lx\n", caps.capabilities);
558 rte_power_unset_env();
564 REGISTER_TEST_COMMAND(power_acpi_cpufreq_autotest, test_power_acpi_cpufreq);
565 REGISTER_TEST_COMMAND(power_acpi_caps_autotest, test_power_acpi_caps);