4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <rte_power.h>
44 #define TEST_POWER_LCORE_ID 2U
45 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
46 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
48 #define TEST_POWER_SYSFILE_CUR_FREQ \
49 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
51 static uint32_t total_freq_num;
52 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
55 check_cur_freq(unsigned lcore_id, uint32_t idx)
57 #define TEST_POWER_CONVERT_TO_DECIMAL 10
59 char fullpath[PATH_MAX];
64 if (snprintf(fullpath, sizeof(fullpath),
65 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
68 f = fopen(fullpath, "r");
72 if (fgets(buf, sizeof(buf), f) == NULL) {
73 goto fail_get_cur_freq;
75 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
76 ret = (freqs[idx] == cur_freq ? 0 : -1);
84 /* Check rte_power_freqs() */
86 check_power_freqs(void)
91 memset(freqs, 0, sizeof(freqs));
93 /* test with an invalid lcore id */
94 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
95 TEST_POWER_FREQS_NUM_MAX);
97 printf("Unexpectedly get available freqs successfully on "
98 "lcore %u\n", TEST_POWER_LCORE_INVALID);
102 /* test with NULL buffer to save available freqs */
103 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
104 TEST_POWER_FREQS_NUM_MAX);
106 printf("Unexpectedly get available freqs successfully with "
107 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
111 /* test of getting zero number of freqs */
112 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
114 printf("Unexpectedly get available freqs successfully with "
115 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
119 /* test with all valid input parameters */
120 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
121 TEST_POWER_FREQS_NUM_MAX);
122 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
123 printf("Fail to get available freqs on lcore %u\n",
124 TEST_POWER_LCORE_ID);
128 /* Save the total number of available freqs */
129 total_freq_num = ret;
134 /* Check rte_power_get_freq() */
136 check_power_get_freq(void)
141 /* test with an invalid lcore id */
142 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
143 if (count < TEST_POWER_FREQS_NUM_MAX) {
144 printf("Unexpectedly get freq index successfully on "
145 "lcore %u\n", TEST_POWER_LCORE_INVALID);
149 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
150 if (count >= TEST_POWER_FREQS_NUM_MAX) {
151 printf("Fail to get the freq index on lcore %u\n",
152 TEST_POWER_LCORE_ID);
156 /* Check the current frequency */
157 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
164 /* Check rte_power_set_freq() */
166 check_power_set_freq(void)
170 /* test with an invalid lcore id */
171 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
173 printf("Unexpectedly set freq index successfully on "
174 "lcore %u\n", TEST_POWER_LCORE_INVALID);
178 /* test with an invalid freq index */
179 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
180 TEST_POWER_FREQS_NUM_MAX);
182 printf("Unexpectedly set an invalid freq index (%u)"
183 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
184 TEST_POWER_LCORE_ID);
189 * test with an invalid freq index which is right one bigger than
190 * total number of freqs
192 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
194 printf("Unexpectedly set an invalid freq index (%u)"
195 "successfully on lcore %u\n", total_freq_num,
196 TEST_POWER_LCORE_ID);
199 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
201 printf("Fail to set freq index on lcore %u\n",
202 TEST_POWER_LCORE_ID);
206 /* Check the current frequency */
207 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
214 /* Check rte_power_freq_down() */
216 check_power_freq_down(void)
220 /* test with an invalid lcore id */
221 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
223 printf("Unexpectedly scale down successfully the freq on "
224 "lcore %u\n", TEST_POWER_LCORE_INVALID);
228 /* Scale down to min and then scale down one step */
229 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
231 printf("Fail to scale down the freq to min 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, total_freq_num - 1);
247 /* Scale up to max and then scale down one step */
248 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
250 printf("Fail to scale up the freq to max on lcore %u\n",
251 TEST_POWER_LCORE_ID);
254 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
256 printf("Fail to scale down the freq on lcore %u\n",
257 TEST_POWER_LCORE_ID);
261 /* Check the current frequency */
262 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
269 /* Check rte_power_freq_up() */
271 check_power_freq_up(void)
275 /* test with an invalid lcore id */
276 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
278 printf("Unexpectedly scale up successfully the freq on %u\n",
279 TEST_POWER_LCORE_INVALID);
283 /* Scale down to min and then scale up one step */
284 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
286 printf("Fail to scale down the freq to min 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, total_freq_num - 2);
302 /* Scale up to max and then scale up one step */
303 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
305 printf("Fail to scale up the freq to max on lcore %u\n",
306 TEST_POWER_LCORE_ID);
309 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
311 printf("Fail to scale up the freq on lcore %u\n",
312 TEST_POWER_LCORE_ID);
316 /* Check the current frequency */
317 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
324 /* Check rte_power_freq_max() */
326 check_power_freq_max(void)
330 /* test with an invalid lcore id */
331 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
333 printf("Unexpectedly scale up successfully the freq to max on "
334 "lcore %u\n", TEST_POWER_LCORE_INVALID);
337 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
339 printf("Fail to scale up the freq to max on lcore %u\n",
340 TEST_POWER_LCORE_ID);
344 /* Check the current frequency */
345 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
352 /* Check rte_power_freq_min() */
354 check_power_freq_min(void)
358 /* test with an invalid lcore id */
359 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
361 printf("Unexpectedly scale down successfully the freq to min "
362 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
365 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
367 printf("Fail to scale down the freq to min on lcore %u\n",
368 TEST_POWER_LCORE_ID);
372 /* Check the current frequency */
373 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
381 test_power_acpi_cpufreq(void)
384 enum power_management_env env;
386 ret = rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
388 printf("Failed on setting environment to PM_ENV_ACPI_CPUFREQ, this "
389 "may occur if environment is not configured correctly or "
390 " operating in another valid Power management environment\n");
394 /* Test environment configuration */
395 env = rte_power_get_env();
396 if (env != PM_ENV_ACPI_CPUFREQ) {
397 printf("Unexpectedly got an environment other than ACPI cpufreq\n");
401 /* verify that function pointers are not NULL */
402 if (rte_power_freqs == NULL) {
403 printf("rte_power_freqs should not be NULL, environment has not been "
407 if (rte_power_get_freq == NULL) {
408 printf("rte_power_get_freq should not be NULL, environment has not "
409 "been initialised\n");
412 if (rte_power_set_freq == NULL) {
413 printf("rte_power_set_freq should not be NULL, environment has not "
414 "been initialised\n");
417 if (rte_power_freq_up == NULL) {
418 printf("rte_power_freq_up should not be NULL, environment has not "
419 "been initialised\n");
422 if (rte_power_freq_down == NULL) {
423 printf("rte_power_freq_down should not be NULL, environment has not "
424 "been initialised\n");
427 if (rte_power_freq_max == NULL) {
428 printf("rte_power_freq_max should not be NULL, environment has not "
429 "been initialised\n");
432 if (rte_power_freq_min == NULL) {
433 printf("rte_power_freq_min should not be NULL, environment has not "
434 "been initialised\n");
438 /* test of init power management for an invalid lcore */
439 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
441 printf("Unexpectedly initialise power management successfully "
442 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
443 rte_power_unset_env();
447 /* Test initialisation of a valid lcore */
448 ret = rte_power_init(TEST_POWER_LCORE_ID);
450 printf("Cannot initialise power management for lcore %u, this "
451 "may occur if environment is not configured "
452 "correctly(APCI cpufreq) or operating in another valid "
453 "Power management environment\n", TEST_POWER_LCORE_ID);
454 rte_power_unset_env();
459 * test of initialising power management for the lcore which has
462 ret = rte_power_init(TEST_POWER_LCORE_ID);
464 printf("Unexpectedly init successfully power twice on "
465 "lcore %u\n", TEST_POWER_LCORE_ID);
469 ret = check_power_freqs();
473 if (total_freq_num < 2) {
474 rte_power_exit(TEST_POWER_LCORE_ID);
475 printf("Frequency can not be changed due to CPU itself\n");
476 rte_power_unset_env();
480 ret = check_power_get_freq();
484 ret = check_power_set_freq();
488 ret = check_power_freq_down();
492 ret = check_power_freq_up();
496 ret = check_power_freq_max();
500 ret = check_power_freq_min();
504 ret = rte_power_exit(TEST_POWER_LCORE_ID);
506 printf("Cannot exit power management for lcore %u\n",
507 TEST_POWER_LCORE_ID);
508 rte_power_unset_env();
513 * test of exiting power management for the lcore which has been exited
515 ret = rte_power_exit(TEST_POWER_LCORE_ID);
517 printf("Unexpectedly exit successfully power management twice "
518 "on lcore %u\n", TEST_POWER_LCORE_ID);
519 rte_power_unset_env();
523 /* test of exit power management for an invalid lcore */
524 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
526 printf("Unpectedly exit power management successfully for "
527 "lcore %u\n", TEST_POWER_LCORE_INVALID);
528 rte_power_unset_env();
531 rte_power_unset_env();
535 rte_power_exit(TEST_POWER_LCORE_ID);
536 rte_power_unset_env();
540 static struct test_command power_acpi_cpufreq_cmd = {
541 .command = "power_acpi_cpufreq_autotest",
542 .callback = test_power_acpi_cpufreq,
544 REGISTER_TEST_COMMAND(power_acpi_cpufreq_cmd);