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