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.
41 #include <cmdline_parse.h>
45 #ifdef RTE_LIBRTE_POWER
47 #include <rte_power.h>
49 #define TEST_POWER_LCORE_ID 2U
50 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
51 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
53 #define TEST_POWER_SYSFILE_CUR_FREQ \
54 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
56 static uint32_t total_freq_num;
57 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
60 check_cur_freq(unsigned lcore_id, uint32_t idx)
62 #define TEST_POWER_CONVERT_TO_DECIMAL 10
64 char fullpath[PATH_MAX];
69 if (rte_snprintf(fullpath, sizeof(fullpath),
70 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
73 f = fopen(fullpath, "r");
77 if (fgets(buf, sizeof(buf), f) == NULL) {
78 goto fail_get_cur_freq;
80 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
81 ret = (freqs[idx] == cur_freq ? 0 : -1);
89 /* Check rte_power_freqs() */
91 check_power_freqs(void)
96 memset(freqs, 0, sizeof(freqs));
98 /* test with an invalid lcore id */
99 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
100 TEST_POWER_FREQS_NUM_MAX);
102 printf("Unexpectedly get available freqs successfully on "
103 "lcore %u\n", TEST_POWER_LCORE_INVALID);
107 /* test with NULL buffer to save available freqs */
108 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
109 TEST_POWER_FREQS_NUM_MAX);
111 printf("Unexpectedly get available freqs successfully with "
112 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
116 /* test of getting zero number of freqs */
117 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
119 printf("Unexpectedly get available freqs successfully with "
120 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
124 /* test with all valid input parameters */
125 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
126 TEST_POWER_FREQS_NUM_MAX);
127 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
128 printf("Fail to get available freqs on lcore %u\n",
129 TEST_POWER_LCORE_ID);
133 /* Save the total number of available freqs */
134 total_freq_num = ret;
139 /* Check rte_power_get_freq() */
141 check_power_get_freq(void)
146 /* test with an invalid lcore id */
147 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
148 if (count < TEST_POWER_FREQS_NUM_MAX) {
149 printf("Unexpectedly get freq index successfully on "
150 "lcore %u\n", TEST_POWER_LCORE_INVALID);
154 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
155 if (count >= TEST_POWER_FREQS_NUM_MAX) {
156 printf("Fail to get the freq index on lcore %u\n",
157 TEST_POWER_LCORE_ID);
161 /* Check the current frequency */
162 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
169 /* Check rte_power_set_freq() */
171 check_power_set_freq(void)
175 /* test with an invalid lcore id */
176 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
178 printf("Unexpectedly set freq index successfully on "
179 "lcore %u\n", TEST_POWER_LCORE_INVALID);
183 /* test with an invalid freq index */
184 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
185 TEST_POWER_FREQS_NUM_MAX);
187 printf("Unexpectedly set an invalid freq index (%u)"
188 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
189 TEST_POWER_LCORE_ID);
194 * test with an invalid freq index which is right one bigger than
195 * total number of freqs
197 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
199 printf("Unexpectedly set an invalid freq index (%u)"
200 "successfully on lcore %u\n", total_freq_num,
201 TEST_POWER_LCORE_ID);
204 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, 3);
206 printf("Fail to set freq index on lcore %u\n",
207 TEST_POWER_LCORE_ID);
211 /* Check the current frequency */
212 ret = check_cur_freq(TEST_POWER_LCORE_ID, 3);
219 /* Check rte_power_freq_down() */
221 check_power_freq_down(void)
225 /* test with an invalid lcore id */
226 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
228 printf("Unexpectedly scale down successfully the freq on "
229 "lcore %u\n", TEST_POWER_LCORE_INVALID);
233 /* Scale down to min and then scale down one step */
234 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
236 printf("Fail to scale down the freq to min on lcore %u\n",
237 TEST_POWER_LCORE_ID);
240 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
242 printf("Fail to scale down the freq on lcore %u\n",
243 TEST_POWER_LCORE_ID);
247 /* Check the current frequency */
248 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
252 /* Scale up to max and then scale down one step */
253 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
255 printf("Fail to scale up the freq to max on lcore %u\n",
256 TEST_POWER_LCORE_ID);
259 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
261 printf ("Fail to scale down the freq on lcore %u\n",
262 TEST_POWER_LCORE_ID);
266 /* Check the current frequency */
267 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
274 /* Check rte_power_freq_up() */
276 check_power_freq_up(void)
280 /* test with an invalid lcore id */
281 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
283 printf("Unexpectedly scale up successfully the freq on %u\n",
284 TEST_POWER_LCORE_INVALID);
288 /* Scale down to min and then scale up one step */
289 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
291 printf("Fail to scale down the freq to min on lcore %u\n",
292 TEST_POWER_LCORE_ID);
295 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
297 printf("Fail to scale up the freq on lcore %u\n",
298 TEST_POWER_LCORE_ID);
302 /* Check the current frequency */
303 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
307 /* Scale up to max and then scale up one step */
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);
314 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
316 printf("Fail to scale up the freq on lcore %u\n",
317 TEST_POWER_LCORE_ID);
321 /* Check the current frequency */
322 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
329 /* Check rte_power_freq_max() */
331 check_power_freq_max(void)
335 /* test with an invalid lcore id */
336 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
338 printf("Unexpectedly scale up successfully the freq to max on "
339 "lcore %u\n", TEST_POWER_LCORE_INVALID);
342 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
344 printf("Fail to scale up the freq to max on lcore %u\n",
345 TEST_POWER_LCORE_ID);
349 /* Check the current frequency */
350 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
357 /* Check rte_power_freq_min() */
359 check_power_freq_min(void)
363 /* test with an invalid lcore id */
364 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
366 printf("Unexpectedly scale down successfully the freq to min "
367 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
370 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
372 printf("Fail to scale down the freq to min on lcore %u\n",
373 TEST_POWER_LCORE_ID);
377 /* Check the current frequency */
378 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
390 /* test of init power management for an invalid lcore */
391 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
393 printf("Unexpectedly initialise power management successfully "
394 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
398 ret = rte_power_init(TEST_POWER_LCORE_ID);
400 printf("Cannot initialise power management for lcore %u\n",
401 TEST_POWER_LCORE_ID);
406 * test of initialising power management for the lcore which has
409 ret = rte_power_init(TEST_POWER_LCORE_ID);
411 printf("Unexpectedly init successfully power twice on "
412 "lcore %u\n", TEST_POWER_LCORE_ID);
416 ret = check_power_freqs();
420 ret = check_power_get_freq();
424 ret = check_power_set_freq();
428 ret = check_power_freq_down();
432 ret = check_power_freq_up();
436 ret = check_power_freq_max();
440 ret = check_power_freq_min();
444 ret = rte_power_exit(TEST_POWER_LCORE_ID);
446 printf("Cannot exit power management for lcore %u\n",
447 TEST_POWER_LCORE_ID);
452 * test of exiting power management for the lcore which has been exited
454 ret = rte_power_exit(TEST_POWER_LCORE_ID);
456 printf("Unexpectedly exit successfully power management twice "
457 "on lcore %u\n", TEST_POWER_LCORE_ID);
461 /* test of exit power management for an invalid lcore */
462 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
464 printf("Unpectedly exit power management successfully for "
465 "lcore %u\n", TEST_POWER_LCORE_INVALID);
472 rte_power_exit(TEST_POWER_LCORE_ID);
477 #else /* RTE_LIBRTE_POWER */
482 printf("The power library is not included in this build\n");
486 #endif /* RTE_LIBRTE_POWER */