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 #ifdef RTE_LIBRTE_POWER
44 #include <rte_power.h>
46 #define TEST_POWER_LCORE_ID 2U
47 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
48 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
50 #define TEST_POWER_SYSFILE_CUR_FREQ \
51 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
53 static uint32_t total_freq_num;
54 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
57 check_cur_freq(unsigned lcore_id, uint32_t idx)
59 #define TEST_POWER_CONVERT_TO_DECIMAL 10
61 char fullpath[PATH_MAX];
66 if (snprintf(fullpath, sizeof(fullpath),
67 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
70 f = fopen(fullpath, "r");
74 if (fgets(buf, sizeof(buf), f) == NULL) {
75 goto fail_get_cur_freq;
77 cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
78 ret = (freqs[idx] == cur_freq ? 0 : -1);
86 /* Check rte_power_freqs() */
88 check_power_freqs(void)
93 memset(freqs, 0, sizeof(freqs));
95 /* test with an invalid lcore id */
96 ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
97 TEST_POWER_FREQS_NUM_MAX);
99 printf("Unexpectedly get available freqs successfully on "
100 "lcore %u\n", TEST_POWER_LCORE_INVALID);
104 /* test with NULL buffer to save available freqs */
105 ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
106 TEST_POWER_FREQS_NUM_MAX);
108 printf("Unexpectedly get available freqs successfully with "
109 "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
113 /* test of getting zero number of freqs */
114 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
116 printf("Unexpectedly get available freqs successfully with "
117 "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
121 /* test with all valid input parameters */
122 ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
123 TEST_POWER_FREQS_NUM_MAX);
124 if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
125 printf("Fail to get available freqs on lcore %u\n",
126 TEST_POWER_LCORE_ID);
130 /* Save the total number of available freqs */
131 total_freq_num = ret;
136 /* Check rte_power_get_freq() */
138 check_power_get_freq(void)
143 /* test with an invalid lcore id */
144 count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
145 if (count < TEST_POWER_FREQS_NUM_MAX) {
146 printf("Unexpectedly get freq index successfully on "
147 "lcore %u\n", TEST_POWER_LCORE_INVALID);
151 count = rte_power_get_freq(TEST_POWER_LCORE_ID);
152 if (count >= TEST_POWER_FREQS_NUM_MAX) {
153 printf("Fail to get the freq index on lcore %u\n",
154 TEST_POWER_LCORE_ID);
158 /* Check the current frequency */
159 ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
166 /* Check rte_power_set_freq() */
168 check_power_set_freq(void)
172 /* test with an invalid lcore id */
173 ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
175 printf("Unexpectedly set freq index successfully on "
176 "lcore %u\n", TEST_POWER_LCORE_INVALID);
180 /* test with an invalid freq index */
181 ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
182 TEST_POWER_FREQS_NUM_MAX);
184 printf("Unexpectedly set an invalid freq index (%u)"
185 "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
186 TEST_POWER_LCORE_ID);
191 * test with an invalid freq index which is right one bigger than
192 * total number of freqs
194 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
196 printf("Unexpectedly set an invalid freq index (%u)"
197 "successfully on lcore %u\n", total_freq_num,
198 TEST_POWER_LCORE_ID);
201 ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
203 printf("Fail to set freq index on lcore %u\n",
204 TEST_POWER_LCORE_ID);
208 /* Check the current frequency */
209 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
216 /* Check rte_power_freq_down() */
218 check_power_freq_down(void)
222 /* test with an invalid lcore id */
223 ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
225 printf("Unexpectedly scale down successfully the freq on "
226 "lcore %u\n", TEST_POWER_LCORE_INVALID);
230 /* Scale down to min and then scale down one step */
231 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
233 printf("Fail to scale down the freq to min on lcore %u\n",
234 TEST_POWER_LCORE_ID);
237 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
239 printf("Fail to scale down the freq on lcore %u\n",
240 TEST_POWER_LCORE_ID);
244 /* Check the current frequency */
245 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
249 /* Scale up to max and then scale down one step */
250 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
252 printf("Fail to scale up the freq to max on lcore %u\n",
253 TEST_POWER_LCORE_ID);
256 ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
258 printf ("Fail to scale down the freq on lcore %u\n",
259 TEST_POWER_LCORE_ID);
263 /* Check the current frequency */
264 ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
271 /* Check rte_power_freq_up() */
273 check_power_freq_up(void)
277 /* test with an invalid lcore id */
278 ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
280 printf("Unexpectedly scale up successfully the freq on %u\n",
281 TEST_POWER_LCORE_INVALID);
285 /* Scale down to min and then scale up one step */
286 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
288 printf("Fail to scale down the freq to min on lcore %u\n",
289 TEST_POWER_LCORE_ID);
292 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
294 printf("Fail to scale up the freq on lcore %u\n",
295 TEST_POWER_LCORE_ID);
299 /* Check the current frequency */
300 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
304 /* Scale up to max and then scale up one step */
305 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
307 printf("Fail to scale up the freq to max on lcore %u\n",
308 TEST_POWER_LCORE_ID);
311 ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
313 printf("Fail to scale up the freq on lcore %u\n",
314 TEST_POWER_LCORE_ID);
318 /* Check the current frequency */
319 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
326 /* Check rte_power_freq_max() */
328 check_power_freq_max(void)
332 /* test with an invalid lcore id */
333 ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
335 printf("Unexpectedly scale up successfully the freq to max on "
336 "lcore %u\n", TEST_POWER_LCORE_INVALID);
339 ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
341 printf("Fail to scale up the freq to max on lcore %u\n",
342 TEST_POWER_LCORE_ID);
346 /* Check the current frequency */
347 ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
354 /* Check rte_power_freq_min() */
356 check_power_freq_min(void)
360 /* test with an invalid lcore id */
361 ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
363 printf("Unexpectedly scale down successfully the freq to min "
364 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
367 ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
369 printf("Fail to scale down the freq to min on lcore %u\n",
370 TEST_POWER_LCORE_ID);
374 /* Check the current frequency */
375 ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
387 /* test of init power management for an invalid lcore */
388 ret = rte_power_init(TEST_POWER_LCORE_INVALID);
390 printf("Unexpectedly initialise power management successfully "
391 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
395 ret = rte_power_init(TEST_POWER_LCORE_ID);
397 printf("Cannot initialise power management for lcore %u\n",
398 TEST_POWER_LCORE_ID);
403 * test of initialising power management for the lcore which has
406 ret = rte_power_init(TEST_POWER_LCORE_ID);
408 printf("Unexpectedly init successfully power twice on "
409 "lcore %u\n", TEST_POWER_LCORE_ID);
413 ret = check_power_freqs();
417 if (total_freq_num < 2) {
418 rte_power_exit(TEST_POWER_LCORE_ID);
419 printf("Frequency can not be changed due to CPU itself\n");
423 ret = check_power_get_freq();
427 ret = check_power_set_freq();
431 ret = check_power_freq_down();
435 ret = check_power_freq_up();
439 ret = check_power_freq_max();
443 ret = check_power_freq_min();
447 ret = rte_power_exit(TEST_POWER_LCORE_ID);
449 printf("Cannot exit power management for lcore %u\n",
450 TEST_POWER_LCORE_ID);
455 * test of exiting power management for the lcore which has been exited
457 ret = rte_power_exit(TEST_POWER_LCORE_ID);
459 printf("Unexpectedly exit successfully power management twice "
460 "on lcore %u\n", TEST_POWER_LCORE_ID);
464 /* test of exit power management for an invalid lcore */
465 ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
467 printf("Unpectedly exit power management successfully for "
468 "lcore %u\n", TEST_POWER_LCORE_INVALID);
475 rte_power_exit(TEST_POWER_LCORE_ID);
480 #else /* RTE_LIBRTE_POWER */
485 printf("The power library is not included in this build\n");
489 #endif /* RTE_LIBRTE_POWER */