test: move unit tests to separate directory
[dpdk.git] / test / test / test_power_acpi_cpufreq.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_acpi_cpufreq(void)
382 {
383         int ret = -1;
384         enum power_management_env env;
385
386         ret = rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
387         if (ret != 0) {
388                 printf("Failed on setting environment to PM_ENV_ACPI_CPUFREQ, this "
389                                 "may occur if environment is not configured correctly or "
390                                 " operating in another valid Power management environment\n");
391                 return -1;
392         }
393
394         /* Test environment configuration */
395         env = rte_power_get_env();
396         if (env != PM_ENV_ACPI_CPUFREQ) {
397                 printf("Unexpectedly got an environment other than ACPI cpufreq\n");
398                 goto fail_all;
399         }
400
401         /* verify that function pointers are not NULL */
402         if (rte_power_freqs == NULL) {
403                 printf("rte_power_freqs should not be NULL, environment has not been "
404                                 "initialised\n");
405                 goto fail_all;
406         }
407         if (rte_power_get_freq == NULL) {
408                 printf("rte_power_get_freq should not be NULL, environment has not "
409                                 "been initialised\n");
410                 goto fail_all;
411         }
412         if (rte_power_set_freq == NULL) {
413                 printf("rte_power_set_freq should not be NULL, environment has not "
414                                 "been initialised\n");
415                 goto fail_all;
416         }
417         if (rte_power_freq_up == NULL) {
418                 printf("rte_power_freq_up should not be NULL, environment has not "
419                                 "been initialised\n");
420                 goto fail_all;
421         }
422         if (rte_power_freq_down == NULL) {
423                 printf("rte_power_freq_down should not be NULL, environment has not "
424                                 "been initialised\n");
425                 goto fail_all;
426         }
427         if (rte_power_freq_max == NULL) {
428                 printf("rte_power_freq_max should not be NULL, environment has not "
429                                 "been initialised\n");
430                 goto fail_all;
431         }
432         if (rte_power_freq_min == NULL) {
433                 printf("rte_power_freq_min should not be NULL, environment has not "
434                                 "been initialised\n");
435                 goto fail_all;
436         }
437
438         /* test of init power management for an invalid lcore */
439         ret = rte_power_init(TEST_POWER_LCORE_INVALID);
440         if (ret == 0) {
441                 printf("Unexpectedly initialise power management successfully "
442                                 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
443                 rte_power_unset_env();
444                 return -1;
445         }
446
447         /* Test initialisation of a valid lcore */
448         ret = rte_power_init(TEST_POWER_LCORE_ID);
449         if (ret < 0) {
450                 printf("Cannot initialise power management for lcore %u, this "
451                                 "may occur if environment is not configured "
452                                 "correctly(APCI cpufreq) or operating in another valid "
453                                 "Power management environment\n", TEST_POWER_LCORE_ID);
454                 rte_power_unset_env();
455                 return -1;
456         }
457
458         /**
459          * test of initialising power management for the lcore which has
460          * been initialised
461          */
462         ret = rte_power_init(TEST_POWER_LCORE_ID);
463         if (ret == 0) {
464                 printf("Unexpectedly init successfully power twice on "
465                                         "lcore %u\n", TEST_POWER_LCORE_ID);
466                 goto fail_all;
467         }
468
469         ret = check_power_freqs();
470         if (ret < 0)
471                 goto fail_all;
472
473         if (total_freq_num < 2) {
474                 rte_power_exit(TEST_POWER_LCORE_ID);
475                 printf("Frequency can not be changed due to CPU itself\n");
476                 rte_power_unset_env();
477                 return 0;
478         }
479
480         ret = check_power_get_freq();
481         if (ret < 0)
482                 goto fail_all;
483
484         ret = check_power_set_freq();
485         if (ret < 0)
486                 goto fail_all;
487
488         ret = check_power_freq_down();
489         if (ret < 0)
490                 goto fail_all;
491
492         ret = check_power_freq_up();
493         if (ret < 0)
494                 goto fail_all;
495
496         ret = check_power_freq_max();
497         if (ret < 0)
498                 goto fail_all;
499
500         ret = check_power_freq_min();
501         if (ret < 0)
502                 goto fail_all;
503
504         ret = rte_power_exit(TEST_POWER_LCORE_ID);
505         if (ret < 0) {
506                 printf("Cannot exit power management for lcore %u\n",
507                                                 TEST_POWER_LCORE_ID);
508                 rte_power_unset_env();
509                 return -1;
510         }
511
512         /**
513          * test of exiting power management for the lcore which has been exited
514          */
515         ret = rte_power_exit(TEST_POWER_LCORE_ID);
516         if (ret == 0) {
517                 printf("Unexpectedly exit successfully power management twice "
518                                         "on lcore %u\n", TEST_POWER_LCORE_ID);
519                 rte_power_unset_env();
520                 return -1;
521         }
522
523         /* test of exit power management for an invalid lcore */
524         ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
525         if (ret == 0) {
526                 printf("Unpectedly exit power management successfully for "
527                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
528                 rte_power_unset_env();
529                 return -1;
530         }
531         rte_power_unset_env();
532         return 0;
533
534 fail_all:
535         rte_power_exit(TEST_POWER_LCORE_ID);
536         rte_power_unset_env();
537         return -1;
538 }
539
540 REGISTER_TEST_COMMAND(power_acpi_cpufreq_autotest, test_power_acpi_cpufreq);