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