doc: whitespace changes in licenses
[dpdk.git] / app / test / test_power.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <string.h>
39
40 #include <cmdline_parse.h>
41
42 #include "test.h"
43
44 #ifdef RTE_LIBRTE_POWER
45
46 #include <rte_power.h>
47
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)
51
52 #define TEST_POWER_SYSFILE_CUR_FREQ \
53         "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq"
54
55 static uint32_t total_freq_num;
56 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
57
58 static int
59 check_cur_freq(unsigned lcore_id, uint32_t idx)
60 {
61 #define TEST_POWER_CONVERT_TO_DECIMAL 10
62         FILE *f;
63         char fullpath[PATH_MAX];
64         char buf[BUFSIZ];
65         uint32_t cur_freq;
66         int ret = -1;
67
68         if (rte_snprintf(fullpath, sizeof(fullpath),
69                 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
70                 return 0;
71         }
72         f = fopen(fullpath, "r");
73         if (f == NULL) {
74                 return 0;
75         }
76         if (fgets(buf, sizeof(buf), f) == NULL) {
77                 goto fail_get_cur_freq;
78         }
79         cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
80         ret = (freqs[idx] == cur_freq ? 0 : -1);
81
82 fail_get_cur_freq:
83         fclose(f);
84
85         return ret;
86 }
87
88 /* Check rte_power_freqs() */
89 static int
90 check_power_freqs(void)
91 {
92         uint32_t ret;
93
94         total_freq_num = 0;
95         memset(freqs, 0, sizeof(freqs));
96
97         /* test with an invalid lcore id */
98         ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
99                                         TEST_POWER_FREQS_NUM_MAX);
100         if (ret > 0) {
101                 printf("Unexpectedly get available freqs successfully on "
102                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
103                 return -1;
104         }
105
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);
109         if (ret > 0) {
110                 printf("Unexpectedly get available freqs successfully with "
111                         "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
112                 return -1;
113         }
114
115         /* test of getting zero number of freqs */
116         ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
117         if (ret > 0) {
118                 printf("Unexpectedly get available freqs successfully with "
119                         "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
120                 return -1;
121         }
122
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);
129                 return -1;
130         }
131
132         /* Save the total number of available freqs */
133         total_freq_num = ret;
134
135         return 0;
136 }
137
138 /* Check rte_power_get_freq() */
139 static int
140 check_power_get_freq(void)
141 {
142         int ret;
143         uint32_t count;
144
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);
150                 return -1;
151         }
152
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);
157                 return -1;
158         }
159
160         /* Check the current frequency */
161         ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
162         if (ret < 0)
163                 return -1;
164
165         return 0;
166 }
167
168 /* Check rte_power_set_freq() */
169 static int
170 check_power_set_freq(void)
171 {
172         int ret;
173
174         /* test with an invalid lcore id */
175         ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
176         if (ret >= 0) {
177                 printf("Unexpectedly set freq index successfully on "
178                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
179                 return -1;
180         }
181
182         /* test with an invalid freq index */
183         ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
184                                 TEST_POWER_FREQS_NUM_MAX);
185         if (ret >= 0) {
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);
189                 return -1;
190         }
191
192         /**
193          * test with an invalid freq index which is right one bigger than
194          * total number of freqs
195          */
196         ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
197         if (ret >= 0) {
198                 printf("Unexpectedly set an invalid freq index (%u)"
199                         "successfully on lcore %u\n", total_freq_num,
200                                                 TEST_POWER_LCORE_ID);
201                 return -1;
202         }
203         ret = rte_power_set_freq(TEST_POWER_LCORE_ID, 3);
204         if (ret < 0) {
205                 printf("Fail to set freq index on lcore %u\n",
206                                         TEST_POWER_LCORE_ID);
207                 return -1;
208         }
209
210         /* Check the current frequency */
211         ret = check_cur_freq(TEST_POWER_LCORE_ID, 3);
212         if (ret < 0)
213                 return -1;
214
215         return 0;
216 }
217
218 /* Check rte_power_freq_down() */
219 static int
220 check_power_freq_down(void)
221 {
222         int ret;
223
224         /* test with an invalid lcore id */
225         ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
226         if (ret >= 0) {
227                 printf("Unexpectedly scale down successfully the freq on "
228                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
229                 return -1;
230         }
231
232         /* Scale down to min and then scale down one step */
233         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
234         if (ret < 0) {
235                 printf("Fail to scale down the freq to min on lcore %u\n",
236                                                         TEST_POWER_LCORE_ID);
237                 return -1;
238         }
239         ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
240         if (ret < 0) {
241                 printf("Fail to scale down the freq on lcore %u\n",
242                                                 TEST_POWER_LCORE_ID);
243                 return -1;
244         }
245
246         /* Check the current frequency */
247         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
248         if (ret < 0)
249                 return -1;
250
251         /* Scale up to max and then scale down one step */
252         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
253         if (ret < 0) {
254                 printf("Fail to scale up the freq to max on lcore %u\n",
255                                                         TEST_POWER_LCORE_ID);
256                 return -1;
257         }
258         ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
259         if (ret < 0) {
260                 printf ("Fail to scale down the freq on lcore %u\n",
261                                                 TEST_POWER_LCORE_ID);
262                 return -1;
263         }
264
265         /* Check the current frequency */
266         ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
267         if (ret < 0)
268                 return -1;
269
270         return 0;
271 }
272
273 /* Check rte_power_freq_up() */
274 static int
275 check_power_freq_up(void)
276 {
277         int ret;
278
279         /* test with an invalid lcore id */
280         ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
281         if (ret >= 0) {
282                 printf("Unexpectedly scale up successfully the freq on %u\n",
283                                                 TEST_POWER_LCORE_INVALID);
284                 return -1;
285         }
286
287         /* Scale down to min and then scale up one step */
288         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
289         if (ret < 0) {
290                 printf("Fail to scale down the freq to min on lcore %u\n",
291                                                         TEST_POWER_LCORE_ID);
292                 return -1;
293         }
294         ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
295         if (ret < 0) {
296                 printf("Fail to scale up the freq on lcore %u\n",
297                                                 TEST_POWER_LCORE_ID);
298                 return -1;
299         }
300
301         /* Check the current frequency */
302         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
303         if (ret < 0)
304                 return -1;
305
306         /* Scale up to max and then scale up one step */
307         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
308         if (ret < 0) {
309                 printf("Fail to scale up the freq to max on lcore %u\n",
310                                                 TEST_POWER_LCORE_ID);
311                 return -1;
312         }
313         ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
314         if (ret < 0) {
315                 printf("Fail to scale up the freq on lcore %u\n",
316                                                 TEST_POWER_LCORE_ID);
317                 return -1;
318         }
319
320         /* Check the current frequency */
321         ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
322         if (ret < 0)
323                 return -1;
324
325         return 0;
326 }
327
328 /* Check rte_power_freq_max() */
329 static int
330 check_power_freq_max(void)
331 {
332         int ret;
333
334         /* test with an invalid lcore id */
335         ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
336         if (ret >= 0) {
337                 printf("Unexpectedly scale up successfully the freq to max on "
338                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
339                 return -1;
340         }
341         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
342         if (ret < 0) {
343                 printf("Fail to scale up the freq to max on lcore %u\n",
344                                                 TEST_POWER_LCORE_ID);
345                 return -1;
346         }
347
348         /* Check the current frequency */
349         ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
350         if (ret < 0)
351                 return -1;
352
353         return 0;
354 }
355
356 /* Check rte_power_freq_min() */
357 static int
358 check_power_freq_min(void)
359 {
360         int ret;
361
362         /* test with an invalid lcore id */
363         ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
364         if (ret >= 0) {
365                 printf("Unexpectedly scale down successfully the freq to min "
366                                 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
367                 return -1;
368         }
369         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
370         if (ret < 0) {
371                 printf("Fail to scale down the freq to min on lcore %u\n",
372                                                         TEST_POWER_LCORE_ID);
373                 return -1;
374         }
375
376         /* Check the current frequency */
377         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
378         if (ret < 0)
379                 return -1;
380
381         return 0;
382 }
383
384 int
385 test_power(void)
386 {
387         int ret = -1;
388
389         /* test of init power management for an invalid lcore */
390         ret = rte_power_init(TEST_POWER_LCORE_INVALID);
391         if (ret == 0) {
392                 printf("Unexpectedly initialise power management successfully "
393                                 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
394                 return -1;
395         }
396
397         ret = rte_power_init(TEST_POWER_LCORE_ID);
398         if (ret < 0) {
399                 printf("Cannot initialise power management for lcore %u\n",
400                                                         TEST_POWER_LCORE_ID);
401                 return -1;
402         }
403
404         /**
405          * test of initialising power management for the lcore which has
406          * been initialised
407          */
408         ret = rte_power_init(TEST_POWER_LCORE_ID);
409         if (ret == 0) {
410                 printf("Unexpectedly init successfully power twice on "
411                                         "lcore %u\n", TEST_POWER_LCORE_ID);
412                 return -1;
413         }
414
415         ret = check_power_freqs();
416         if (ret < 0)
417                 goto fail_all;
418
419         ret = check_power_get_freq();
420         if (ret < 0)
421                 goto fail_all;
422
423         ret = check_power_set_freq();
424         if (ret < 0)
425                 goto fail_all;
426
427         ret = check_power_freq_down();
428         if (ret < 0)
429                 goto fail_all;
430
431         ret = check_power_freq_up();
432         if (ret < 0)
433                 goto fail_all;
434
435         ret = check_power_freq_max();
436         if (ret < 0)
437                 goto fail_all;
438
439         ret = check_power_freq_min();
440         if (ret < 0)
441                 goto fail_all;
442
443         ret = rte_power_exit(TEST_POWER_LCORE_ID);
444         if (ret < 0) {
445                 printf("Cannot exit power management for lcore %u\n",
446                                                 TEST_POWER_LCORE_ID);
447                 return -1;
448         }
449
450         /**
451          * test of exiting power management for the lcore which has been exited
452          */
453         ret = rte_power_exit(TEST_POWER_LCORE_ID);
454         if (ret == 0) {
455                 printf("Unexpectedly exit successfully power management twice "
456                                         "on lcore %u\n", TEST_POWER_LCORE_ID);
457                 return -1;
458         }
459
460         /* test of exit power management for an invalid lcore */
461         ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
462         if (ret == 0) {
463                 printf("Unpectedly exit power management successfully for "
464                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
465                 return -1;
466         }
467
468         return 0;
469
470 fail_all:
471         rte_power_exit(TEST_POWER_LCORE_ID);
472
473         return -1;
474 }
475
476 #else /* RTE_LIBRTE_POWER */
477
478 int
479 test_power(void)
480 {
481         printf("The power library is not included in this build\n");
482         return 0;
483 }
484
485 #endif /* RTE_LIBRTE_POWER */
486