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);
385 /* test of init power management for an invalid lcore */
386 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
388 printf("Unexpectedly initialise power management successfully "
389 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
393 ret = rte_power_init(TEST_POWER_LCORE_ID);
395 printf("Cannot initialise power management for lcore %u\n",
396 TEST_POWER_LCORE_ID);
401 * test of initialising power management for the lcore which has
404 ret = rte_power_init(TEST_POWER_LCORE_ID);
406 printf("Unexpectedly init successfully power twice on "
407 "lcore %u\n", TEST_POWER_LCORE_ID);
411 ret = check_power_freqs();
415 if (total_freq_num < 2) {
416 rte_power_exit(TEST_POWER_LCORE_ID);
417 printf("Frequency can not be changed due to CPU itself\n");
421 ret = check_power_get_freq();
425 ret = check_power_set_freq();
429 ret = check_power_freq_down();
433 ret = check_power_freq_up();
437 ret = check_power_freq_max();
441 ret = check_power_freq_min();
445 ret = rte_power_exit(TEST_POWER_LCORE_ID);
447 printf("Cannot exit power management for lcore %u\n",
448 TEST_POWER_LCORE_ID);
453 * test of exiting power management for the lcore which has been exited
455 ret = rte_power_exit(TEST_POWER_LCORE_ID);
457 printf("Unexpectedly exit successfully power management twice "
458 "on lcore %u\n", TEST_POWER_LCORE_ID);
462 /* test of exit power management for an invalid lcore */
463 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
465 printf("Unpectedly exit power management successfully for "
466 "lcore %u\n", TEST_POWER_LCORE_INVALID);
473 rte_power_exit(TEST_POWER_LCORE_ID);
478 static struct test_command power_cmd = {
479 .command = "power_autotest",
480 .callback = test_power,
482 REGISTER_TEST_COMMAND(power_cmd);