4 * Copyright(c) 2010-2013 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.
40 #include <cmdline_parse.h>
44 #ifdef RTE_LIBRTE_POWER
46 #include <rte_power.h>
48 #define TEST_POWER_LCORE_ID 2U
49 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
50 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
52 #define TEST_POWER_SYSFILE_CUR_FREQ \
53 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
55 static uint32_t total_freq_num;
56 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
59 check_cur_freq(unsigned lcore_id, uint32_t idx)
61 #define TEST_POWER_CONVERT_TO_DECIMAL 10
63 char fullpath[PATH_MAX];
68 if (rte_snprintf(fullpath, sizeof(fullpath),
69 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
72 f = fopen(fullpath, "r");
76 if (fgets(buf, sizeof(buf), f) == NULL) {
77 goto fail_get_cur_freq;
79 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
80 ret = (freqs[idx] == cur_freq ? 0 : -1);
88 /* Check rte_power_freqs() */
90 check_power_freqs(void)
95 memset(freqs, 0, sizeof(freqs));
97 /* test with an invalid lcore id */
98 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
99 TEST_POWER_FREQS_NUM_MAX);
101 printf("Unexpectedly get available freqs successfully on "
102 "lcore %u\n", TEST_POWER_LCORE_INVALID);
106 /* test with NULL buffer to save available freqs */
107 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
108 TEST_POWER_FREQS_NUM_MAX);
110 printf("Unexpectedly get available freqs successfully with "
111 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
115 /* test of getting zero number of freqs */
116 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
118 printf("Unexpectedly get available freqs successfully with "
119 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
123 /* test with all valid input parameters */
124 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
125 TEST_POWER_FREQS_NUM_MAX);
126 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
127 printf("Fail to get available freqs on lcore %u\n",
128 TEST_POWER_LCORE_ID);
132 /* Save the total number of available freqs */
133 total_freq_num = ret;
138 /* Check rte_power_get_freq() */
140 check_power_get_freq(void)
145 /* test with an invalid lcore id */
146 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
147 if (count < TEST_POWER_FREQS_NUM_MAX) {
148 printf("Unexpectedly get freq index successfully on "
149 "lcore %u\n", TEST_POWER_LCORE_INVALID);
153 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
154 if (count >= TEST_POWER_FREQS_NUM_MAX) {
155 printf("Fail to get the freq index on lcore %u\n",
156 TEST_POWER_LCORE_ID);
160 /* Check the current frequency */
161 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
168 /* Check rte_power_set_freq() */
170 check_power_set_freq(void)
174 /* test with an invalid lcore id */
175 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
177 printf("Unexpectedly set freq index successfully on "
178 "lcore %u\n", TEST_POWER_LCORE_INVALID);
182 /* test with an invalid freq index */
183 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
184 TEST_POWER_FREQS_NUM_MAX);
186 printf("Unexpectedly set an invalid freq index (%u)"
187 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
188 TEST_POWER_LCORE_ID);
193 * test with an invalid freq index which is right one bigger than
194 * total number of freqs
196 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
198 printf("Unexpectedly set an invalid freq index (%u)"
199 "successfully on lcore %u\n", total_freq_num,
200 TEST_POWER_LCORE_ID);
203 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, 3);
205 printf("Fail to set freq index on lcore %u\n",
206 TEST_POWER_LCORE_ID);
210 /* Check the current frequency */
211 ret = check_cur_freq(TEST_POWER_LCORE_ID, 3);
218 /* Check rte_power_freq_down() */
220 check_power_freq_down(void)
224 /* test with an invalid lcore id */
225 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
227 printf("Unexpectedly scale down successfully the freq on "
228 "lcore %u\n", TEST_POWER_LCORE_INVALID);
232 /* Scale down to min and then scale down one step */
233 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
235 printf("Fail to scale down the freq to min on lcore %u\n",
236 TEST_POWER_LCORE_ID);
239 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
241 printf("Fail to scale down the freq on lcore %u\n",
242 TEST_POWER_LCORE_ID);
246 /* Check the current frequency */
247 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
251 /* Scale up to max and then scale down one step */
252 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
254 printf("Fail to scale up the freq to max on lcore %u\n",
255 TEST_POWER_LCORE_ID);
258 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
260 printf ("Fail to scale down the freq on lcore %u\n",
261 TEST_POWER_LCORE_ID);
265 /* Check the current frequency */
266 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
273 /* Check rte_power_freq_up() */
275 check_power_freq_up(void)
279 /* test with an invalid lcore id */
280 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
282 printf("Unexpectedly scale up successfully the freq on %u\n",
283 TEST_POWER_LCORE_INVALID);
287 /* Scale down to min and then scale up one step */
288 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
290 printf("Fail to scale down the freq to min on lcore %u\n",
291 TEST_POWER_LCORE_ID);
294 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
296 printf("Fail to scale up the freq on lcore %u\n",
297 TEST_POWER_LCORE_ID);
301 /* Check the current frequency */
302 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
306 /* Scale up to max and then scale up one step */
307 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
309 printf("Fail to scale up the freq to max on lcore %u\n",
310 TEST_POWER_LCORE_ID);
313 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
315 printf("Fail to scale up the freq on lcore %u\n",
316 TEST_POWER_LCORE_ID);
320 /* Check the current frequency */
321 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
328 /* Check rte_power_freq_max() */
330 check_power_freq_max(void)
334 /* test with an invalid lcore id */
335 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
337 printf("Unexpectedly scale up successfully the freq to max on "
338 "lcore %u\n", TEST_POWER_LCORE_INVALID);
341 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
343 printf("Fail to scale up the freq to max on lcore %u\n",
344 TEST_POWER_LCORE_ID);
348 /* Check the current frequency */
349 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
356 /* Check rte_power_freq_min() */
358 check_power_freq_min(void)
362 /* test with an invalid lcore id */
363 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
365 printf("Unexpectedly scale down successfully the freq to min "
366 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
369 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
371 printf("Fail to scale down the freq to min on lcore %u\n",
372 TEST_POWER_LCORE_ID);
376 /* Check the current frequency */
377 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
389 /* test of init power management for an invalid lcore */
390 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
392 printf("Unexpectedly initialise power management successfully "
393 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
397 ret = rte_power_init(TEST_POWER_LCORE_ID);
399 printf("Cannot initialise power management for lcore %u\n",
400 TEST_POWER_LCORE_ID);
405 * test of initialising power management for the lcore which has
408 ret = rte_power_init(TEST_POWER_LCORE_ID);
410 printf("Unexpectedly init successfully power twice on "
411 "lcore %u\n", TEST_POWER_LCORE_ID);
415 ret = check_power_freqs();
419 ret = check_power_get_freq();
423 ret = check_power_set_freq();
427 ret = check_power_freq_down();
431 ret = check_power_freq_up();
435 ret = check_power_freq_max();
439 ret = check_power_freq_min();
443 ret = rte_power_exit(TEST_POWER_LCORE_ID);
445 printf("Cannot exit power management for lcore %u\n",
446 TEST_POWER_LCORE_ID);
451 * test of exiting power management for the lcore which has been exited
453 ret = rte_power_exit(TEST_POWER_LCORE_ID);
455 printf("Unexpectedly exit successfully power management twice "
456 "on lcore %u\n", TEST_POWER_LCORE_ID);
460 /* test of exit power management for an invalid lcore */
461 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
463 printf("Unpectedly exit power management successfully for "
464 "lcore %u\n", TEST_POWER_LCORE_INVALID);
471 rte_power_exit(TEST_POWER_LCORE_ID);
476 #else /* RTE_LIBRTE_POWER */
481 printf("The power library is not included in this build\n");
485 #endif /* RTE_LIBRTE_POWER */