net: add rte prefix to ESP structure
[dpdk.git] / app / test / test_power_acpi_cpufreq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <string.h>
10 #include <inttypes.h>
11
12 #include "test.h"
13
14 #ifndef RTE_LIBRTE_POWER
15
16 static int
17 test_power_acpi_cpufreq(void)
18 {
19         printf("Power management library not supported, skipping test\n");
20         return TEST_SKIPPED;
21 }
22
23 static int
24 test_power_acpi_caps(void)
25 {
26         printf("Power management library not supported, skipping test\n");
27         return TEST_SKIPPED;
28 }
29
30 #else
31 #include <rte_power.h>
32
33 #define TEST_POWER_LCORE_ID      2U
34 #define TEST_POWER_LCORE_INVALID ((unsigned)RTE_MAX_LCORE)
35 #define TEST_POWER_FREQS_NUM_MAX ((unsigned)RTE_MAX_LCORE_FREQS)
36
37 #define TEST_POWER_SYSFILE_CUR_FREQ \
38         "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_cur_freq"
39
40 static uint32_t total_freq_num;
41 static uint32_t freqs[TEST_POWER_FREQS_NUM_MAX];
42
43 static int
44 check_cur_freq(unsigned lcore_id, uint32_t idx)
45 {
46 #define TEST_POWER_CONVERT_TO_DECIMAL 10
47         FILE *f;
48         char fullpath[PATH_MAX];
49         char buf[BUFSIZ];
50         uint32_t cur_freq;
51         int ret = -1;
52
53         if (snprintf(fullpath, sizeof(fullpath),
54                 TEST_POWER_SYSFILE_CUR_FREQ, lcore_id) < 0) {
55                 return 0;
56         }
57         f = fopen(fullpath, "r");
58         if (f == NULL) {
59                 return 0;
60         }
61         if (fgets(buf, sizeof(buf), f) == NULL) {
62                 goto fail_get_cur_freq;
63         }
64         cur_freq = strtoul(buf, NULL, TEST_POWER_CONVERT_TO_DECIMAL);
65         ret = (freqs[idx] == cur_freq ? 0 : -1);
66
67 fail_get_cur_freq:
68         fclose(f);
69
70         return ret;
71 }
72
73 /* Check rte_power_freqs() */
74 static int
75 check_power_freqs(void)
76 {
77         uint32_t ret;
78
79         total_freq_num = 0;
80         memset(freqs, 0, sizeof(freqs));
81
82         /* test with an invalid lcore id */
83         ret = rte_power_freqs(TEST_POWER_LCORE_INVALID, freqs,
84                                         TEST_POWER_FREQS_NUM_MAX);
85         if (ret > 0) {
86                 printf("Unexpectedly get available freqs successfully on "
87                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
88                 return -1;
89         }
90
91         /* test with NULL buffer to save available freqs */
92         ret = rte_power_freqs(TEST_POWER_LCORE_ID, NULL,
93                                 TEST_POWER_FREQS_NUM_MAX);
94         if (ret > 0) {
95                 printf("Unexpectedly get available freqs successfully with "
96                         "NULL buffer on lcore %u\n", TEST_POWER_LCORE_ID);
97                 return -1;
98         }
99
100         /* test of getting zero number of freqs */
101         ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs, 0);
102         if (ret > 0) {
103                 printf("Unexpectedly get available freqs successfully with "
104                         "zero buffer size on lcore %u\n", TEST_POWER_LCORE_ID);
105                 return -1;
106         }
107
108         /* test with all valid input parameters */
109         ret = rte_power_freqs(TEST_POWER_LCORE_ID, freqs,
110                                 TEST_POWER_FREQS_NUM_MAX);
111         if (ret == 0 || ret > TEST_POWER_FREQS_NUM_MAX) {
112                 printf("Fail to get available freqs on lcore %u\n",
113                                                 TEST_POWER_LCORE_ID);
114                 return -1;
115         }
116
117         /* Save the total number of available freqs */
118         total_freq_num = ret;
119
120         return 0;
121 }
122
123 /* Check rte_power_get_freq() */
124 static int
125 check_power_get_freq(void)
126 {
127         int ret;
128         uint32_t count;
129
130         /* test with an invalid lcore id */
131         count = rte_power_get_freq(TEST_POWER_LCORE_INVALID);
132         if (count < TEST_POWER_FREQS_NUM_MAX) {
133                 printf("Unexpectedly get freq index successfully on "
134                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
135                 return -1;
136         }
137
138         count = rte_power_get_freq(TEST_POWER_LCORE_ID);
139         if (count >= TEST_POWER_FREQS_NUM_MAX) {
140                 printf("Fail to get the freq index on lcore %u\n",
141                                                 TEST_POWER_LCORE_ID);
142                 return -1;
143         }
144
145         /* Check the current frequency */
146         ret = check_cur_freq(TEST_POWER_LCORE_ID, count);
147         if (ret < 0)
148                 return -1;
149
150         return 0;
151 }
152
153 /* Check rte_power_set_freq() */
154 static int
155 check_power_set_freq(void)
156 {
157         int ret;
158
159         /* test with an invalid lcore id */
160         ret = rte_power_set_freq(TEST_POWER_LCORE_INVALID, 0);
161         if (ret >= 0) {
162                 printf("Unexpectedly set freq index successfully on "
163                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
164                 return -1;
165         }
166
167         /* test with an invalid freq index */
168         ret = rte_power_set_freq(TEST_POWER_LCORE_ID,
169                                 TEST_POWER_FREQS_NUM_MAX);
170         if (ret >= 0) {
171                 printf("Unexpectedly set an invalid freq index (%u)"
172                         "successfully on lcore %u\n", TEST_POWER_FREQS_NUM_MAX,
173                                                         TEST_POWER_LCORE_ID);
174                 return -1;
175         }
176
177         /**
178          * test with an invalid freq index which is right one bigger than
179          * total number of freqs
180          */
181         ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num);
182         if (ret >= 0) {
183                 printf("Unexpectedly set an invalid freq index (%u)"
184                         "successfully on lcore %u\n", total_freq_num,
185                                                 TEST_POWER_LCORE_ID);
186                 return -1;
187         }
188         ret = rte_power_set_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
189         if (ret < 0) {
190                 printf("Fail to set freq index on lcore %u\n",
191                                         TEST_POWER_LCORE_ID);
192                 return -1;
193         }
194
195         /* Check the current frequency */
196         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
197         if (ret < 0)
198                 return -1;
199
200         return 0;
201 }
202
203 /* Check rte_power_freq_down() */
204 static int
205 check_power_freq_down(void)
206 {
207         int ret;
208
209         /* test with an invalid lcore id */
210         ret = rte_power_freq_down(TEST_POWER_LCORE_INVALID);
211         if (ret >= 0) {
212                 printf("Unexpectedly scale down successfully the freq on "
213                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
214                 return -1;
215         }
216
217         /* Scale down to min and then scale down one step */
218         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
219         if (ret < 0) {
220                 printf("Fail to scale down the freq to min on lcore %u\n",
221                                                         TEST_POWER_LCORE_ID);
222                 return -1;
223         }
224         ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
225         if (ret < 0) {
226                 printf("Fail to scale down the freq on lcore %u\n",
227                                                 TEST_POWER_LCORE_ID);
228                 return -1;
229         }
230
231         /* Check the current frequency */
232         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
233         if (ret < 0)
234                 return -1;
235
236         /* Scale up to max and then scale down one step */
237         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
238         if (ret < 0) {
239                 printf("Fail to scale up the freq to max on lcore %u\n",
240                                                         TEST_POWER_LCORE_ID);
241                 return -1;
242         }
243         ret = rte_power_freq_down(TEST_POWER_LCORE_ID);
244         if (ret < 0) {
245                 printf("Fail to scale down the freq on lcore %u\n",
246                                                 TEST_POWER_LCORE_ID);
247                 return -1;
248         }
249
250         /* Check the current frequency */
251         ret = check_cur_freq(TEST_POWER_LCORE_ID, 1);
252         if (ret < 0)
253                 return -1;
254
255         return 0;
256 }
257
258 /* Check rte_power_freq_up() */
259 static int
260 check_power_freq_up(void)
261 {
262         int ret;
263
264         /* test with an invalid lcore id */
265         ret = rte_power_freq_up(TEST_POWER_LCORE_INVALID);
266         if (ret >= 0) {
267                 printf("Unexpectedly scale up successfully the freq on %u\n",
268                                                 TEST_POWER_LCORE_INVALID);
269                 return -1;
270         }
271
272         /* Scale down to min and then scale up one step */
273         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
274         if (ret < 0) {
275                 printf("Fail to scale down the freq to min on lcore %u\n",
276                                                         TEST_POWER_LCORE_ID);
277                 return -1;
278         }
279         ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
280         if (ret < 0) {
281                 printf("Fail to scale up the freq on lcore %u\n",
282                                                 TEST_POWER_LCORE_ID);
283                 return -1;
284         }
285
286         /* Check the current frequency */
287         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 2);
288         if (ret < 0)
289                 return -1;
290
291         /* Scale up to max and then scale up one step */
292         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
293         if (ret < 0) {
294                 printf("Fail to scale up the freq to max on lcore %u\n",
295                                                 TEST_POWER_LCORE_ID);
296                 return -1;
297         }
298         ret = rte_power_freq_up(TEST_POWER_LCORE_ID);
299         if (ret < 0) {
300                 printf("Fail to scale up the freq on lcore %u\n",
301                                                 TEST_POWER_LCORE_ID);
302                 return -1;
303         }
304
305         /* Check the current frequency */
306         ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
307         if (ret < 0)
308                 return -1;
309
310         return 0;
311 }
312
313 /* Check rte_power_freq_max() */
314 static int
315 check_power_freq_max(void)
316 {
317         int ret;
318
319         /* test with an invalid lcore id */
320         ret = rte_power_freq_max(TEST_POWER_LCORE_INVALID);
321         if (ret >= 0) {
322                 printf("Unexpectedly scale up successfully the freq to max on "
323                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
324                 return -1;
325         }
326         ret = rte_power_freq_max(TEST_POWER_LCORE_ID);
327         if (ret < 0) {
328                 printf("Fail to scale up the freq to max on lcore %u\n",
329                                                 TEST_POWER_LCORE_ID);
330                 return -1;
331         }
332
333         /* Check the current frequency */
334         ret = check_cur_freq(TEST_POWER_LCORE_ID, 0);
335         if (ret < 0)
336                 return -1;
337
338         return 0;
339 }
340
341 /* Check rte_power_freq_min() */
342 static int
343 check_power_freq_min(void)
344 {
345         int ret;
346
347         /* test with an invalid lcore id */
348         ret = rte_power_freq_min(TEST_POWER_LCORE_INVALID);
349         if (ret >= 0) {
350                 printf("Unexpectedly scale down successfully the freq to min "
351                                 "on lcore %u\n", TEST_POWER_LCORE_INVALID);
352                 return -1;
353         }
354         ret = rte_power_freq_min(TEST_POWER_LCORE_ID);
355         if (ret < 0) {
356                 printf("Fail to scale down the freq to min on lcore %u\n",
357                                                         TEST_POWER_LCORE_ID);
358                 return -1;
359         }
360
361         /* Check the current frequency */
362         ret = check_cur_freq(TEST_POWER_LCORE_ID, total_freq_num - 1);
363         if (ret < 0)
364                 return -1;
365
366         return 0;
367 }
368
369 static int
370 test_power_acpi_cpufreq(void)
371 {
372         int ret = -1;
373         enum power_management_env env;
374
375         /* Test initialisation of a valid lcore */
376         ret = rte_power_init(TEST_POWER_LCORE_ID);
377         if (ret < 0) {
378                 printf("Cannot initialise power management for lcore %u, this "
379                                 "may occur if environment is not configured "
380                                 "correctly(APCI cpufreq) or operating in another valid "
381                                 "Power management environment\n",
382                                 TEST_POWER_LCORE_ID);
383                 rte_power_unset_env();
384                 return TEST_SKIPPED;
385         }
386
387         /* Test environment configuration */
388         env = rte_power_get_env();
389         if ((env != PM_ENV_ACPI_CPUFREQ) && (env != PM_ENV_PSTATE_CPUFREQ)) {
390                 printf("Unexpectedly got an environment other than ACPI/PSTATE\n");
391                 goto fail_all;
392         }
393
394         /* verify that function pointers are not NULL */
395         if (rte_power_freqs == NULL) {
396                 printf("rte_power_freqs should not be NULL, environment has not been "
397                                 "initialised\n");
398                 goto fail_all;
399         }
400         if (rte_power_get_freq == NULL) {
401                 printf("rte_power_get_freq should not be NULL, environment has not "
402                                 "been initialised\n");
403                 goto fail_all;
404         }
405         if (rte_power_set_freq == NULL) {
406                 printf("rte_power_set_freq should not be NULL, environment has not "
407                                 "been initialised\n");
408                 goto fail_all;
409         }
410         if (rte_power_freq_up == NULL) {
411                 printf("rte_power_freq_up should not be NULL, environment has not "
412                                 "been initialised\n");
413                 goto fail_all;
414         }
415         if (rte_power_freq_down == NULL) {
416                 printf("rte_power_freq_down should not be NULL, environment has not "
417                                 "been initialised\n");
418                 goto fail_all;
419         }
420         if (rte_power_freq_max == NULL) {
421                 printf("rte_power_freq_max should not be NULL, environment has not "
422                                 "been initialised\n");
423                 goto fail_all;
424         }
425         if (rte_power_freq_min == NULL) {
426                 printf("rte_power_freq_min should not be NULL, environment has not "
427                                 "been initialised\n");
428                 goto fail_all;
429         }
430
431         ret = rte_power_exit(TEST_POWER_LCORE_ID);
432         if (ret < 0) {
433                 printf("Cannot exit power management for lcore %u\n",
434                                                 TEST_POWER_LCORE_ID);
435                 rte_power_unset_env();
436                 return -1;
437         }
438
439         /* test of init power management for an invalid lcore */
440         ret = rte_power_init(TEST_POWER_LCORE_INVALID);
441         if (ret == 0) {
442                 printf("Unexpectedly initialise power management successfully "
443                                 "for lcore %u\n", TEST_POWER_LCORE_INVALID);
444                 rte_power_unset_env();
445                 return -1;
446         }
447
448         /* Test initialisation of a valid lcore */
449         ret = rte_power_init(TEST_POWER_LCORE_ID);
450         if (ret < 0) {
451                 printf("Cannot initialise power management for lcore %u, this "
452                                 "may occur if environment is not configured "
453                                 "correctly(APCI cpufreq) or operating in another valid "
454                                 "Power management environment\n", TEST_POWER_LCORE_ID);
455                 rte_power_unset_env();
456                 return TEST_SKIPPED;
457         }
458
459         /**
460          * test of initialising power management for the lcore which has
461          * been initialised
462          */
463         ret = rte_power_init(TEST_POWER_LCORE_ID);
464         if (ret == 0) {
465                 printf("Unexpectedly init successfully power twice on "
466                                         "lcore %u\n", TEST_POWER_LCORE_ID);
467                 goto fail_all;
468         }
469
470         ret = check_power_freqs();
471         if (ret < 0)
472                 goto fail_all;
473
474         if (total_freq_num < 2) {
475                 rte_power_exit(TEST_POWER_LCORE_ID);
476                 printf("Frequency can not be changed due to CPU itself\n");
477                 rte_power_unset_env();
478                 return 0;
479         }
480
481         ret = check_power_get_freq();
482         if (ret < 0)
483                 goto fail_all;
484
485         ret = check_power_set_freq();
486         if (ret < 0)
487                 goto fail_all;
488
489         ret = check_power_freq_down();
490         if (ret < 0)
491                 goto fail_all;
492
493         ret = check_power_freq_up();
494         if (ret < 0)
495                 goto fail_all;
496
497         ret = check_power_freq_max();
498         if (ret < 0)
499                 goto fail_all;
500
501         ret = check_power_freq_min();
502         if (ret < 0)
503                 goto fail_all;
504
505         ret = rte_power_exit(TEST_POWER_LCORE_ID);
506         if (ret < 0) {
507                 printf("Cannot exit power management for lcore %u\n",
508                                                 TEST_POWER_LCORE_ID);
509                 rte_power_unset_env();
510                 return -1;
511         }
512
513         /**
514          * test of exiting power management for the lcore which has been exited
515          */
516         ret = rte_power_exit(TEST_POWER_LCORE_ID);
517         if (ret == 0) {
518                 printf("Unexpectedly exit successfully power management twice "
519                                         "on lcore %u\n", TEST_POWER_LCORE_ID);
520                 rte_power_unset_env();
521                 return -1;
522         }
523
524         /* test of exit power management for an invalid lcore */
525         ret = rte_power_exit(TEST_POWER_LCORE_INVALID);
526         if (ret == 0) {
527                 printf("Unpectedly exit power management successfully for "
528                                 "lcore %u\n", TEST_POWER_LCORE_INVALID);
529                 rte_power_unset_env();
530                 return -1;
531         }
532         rte_power_unset_env();
533         return 0;
534
535 fail_all:
536         rte_power_exit(TEST_POWER_LCORE_ID);
537         rte_power_unset_env();
538         return -1;
539 }
540
541 static int
542 test_power_acpi_caps(void)
543 {
544         struct rte_power_core_capabilities caps;
545         int ret;
546
547         ret = rte_power_init(TEST_POWER_LCORE_ID);
548         if (ret < 0) {
549                 printf("Cannot initialise power management for lcore %u, this "
550                         "may occur if environment is not configured "
551                         "correctly(APCI cpufreq) or operating in another valid "
552                         "Power management environment\n", TEST_POWER_LCORE_ID);
553                 rte_power_unset_env();
554                 return -1;
555         }
556
557         ret = rte_power_get_capabilities(TEST_POWER_LCORE_ID, &caps);
558         if (ret) {
559                 printf("POWER: Error getting capabilities\n");
560                 return -1;
561         }
562
563         printf("POWER: Capabilities %"PRIx64"\n", caps.capabilities);
564
565         rte_power_unset_env();
566         return 0;
567 }
568
569 #endif
570
571 REGISTER_TEST_COMMAND(power_acpi_cpufreq_autotest, test_power_acpi_cpufreq);
572 REGISTER_TEST_COMMAND(power_acpi_caps_autotest, test_power_acpi_caps);