da1d67c0ab49121ac8c031117ffceeb699f01e50
[dpdk.git] / app / test / test_power.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 <stdbool.h>
11
12 #include "test.h"
13
14 #ifndef RTE_LIB_POWER
15
16 static int
17 test_power(void)
18 {
19         printf("Power management library not supported, skipping test\n");
20         return TEST_SKIPPED;
21 }
22
23 #else
24
25 #include <rte_power.h>
26
27 static int
28 check_function_ptrs(void)
29 {
30         enum power_management_env env = rte_power_get_env();
31
32         const bool not_null_expected = !(env == PM_ENV_NOT_SET);
33
34         const char *inject_not_string1 = not_null_expected ? " not" : "";
35         const char *inject_not_string2 = not_null_expected ? "" : " not";
36
37         if ((rte_power_freqs == NULL) == not_null_expected) {
38                 printf("rte_power_freqs should%s be NULL, environment has%s been "
39                                 "initialised\n", inject_not_string1,
40                                         inject_not_string2);
41                 return -1;
42         }
43         if ((rte_power_get_freq == NULL) == not_null_expected) {
44                 printf("rte_power_get_freq should%s be NULL, environment has%s been "
45                                 "initialised\n", inject_not_string1,
46                                         inject_not_string2);
47                 return -1;
48         }
49         if ((rte_power_set_freq == NULL) == not_null_expected) {
50                 printf("rte_power_set_freq should%s be NULL, environment has%s been "
51                                 "initialised\n", inject_not_string1,
52                                 inject_not_string2);
53                 return -1;
54         }
55         if ((rte_power_freq_up == NULL) == not_null_expected) {
56                 printf("rte_power_freq_up should%s be NULL, environment has%s been "
57                                 "initialised\n", inject_not_string1,
58                                 inject_not_string2);
59                 return -1;
60         }
61         if ((rte_power_freq_down == NULL) == not_null_expected) {
62                 printf("rte_power_freq_down should%s be NULL, environment has%s been "
63                                 "initialised\n", inject_not_string1,
64                                 inject_not_string2);
65                 return -1;
66         }
67         if ((rte_power_freq_max == NULL) == not_null_expected) {
68                 printf("rte_power_freq_max should%s be NULL, environment has%s been "
69                                 "initialised\n", inject_not_string1,
70                                 inject_not_string2);
71                 return -1;
72         }
73         if ((rte_power_freq_min == NULL) == not_null_expected) {
74                 printf("rte_power_freq_min should%s be NULL, environment has%s been "
75                                 "initialised\n", inject_not_string1,
76                                 inject_not_string2);
77                 return -1;
78         }
79         if ((rte_power_turbo_status == NULL) == not_null_expected) {
80                 printf("rte_power_turbo_status should%s be NULL, environment has%s been "
81                                 "initialised\n", inject_not_string1,
82                                 inject_not_string2);
83                 return -1;
84         }
85         if ((rte_power_freq_enable_turbo == NULL) == not_null_expected) {
86                 printf("rte_power_freq_enable_turbo should%s be NULL, environment has%s been "
87                                 "initialised\n", inject_not_string1,
88                                 inject_not_string2);
89                 return -1;
90         }
91         if ((rte_power_freq_disable_turbo == NULL) == not_null_expected) {
92                 printf("rte_power_freq_disable_turbo should%s be NULL, environment has%s been "
93                                 "initialised\n", inject_not_string1,
94                                 inject_not_string2);
95                 return -1;
96         }
97         if ((rte_power_get_capabilities == NULL) == not_null_expected) {
98                 printf("rte_power_get_capabilities should%s be NULL, environment has%s been "
99                                 "initialised\n", inject_not_string1,
100                                 inject_not_string2);
101                 return -1;
102         }
103
104         return 0;
105 }
106
107 static int
108 test_power(void)
109 {
110         int ret = -1;
111         enum power_management_env env;
112
113         /* Test setting an invalid environment */
114         ret = rte_power_set_env(PM_ENV_NOT_SET);
115         if (ret == 0) {
116                 printf("Unexpectedly succeeded on setting an invalid environment\n");
117                 return -1;
118         }
119
120         /* Test that the environment has not been set */
121         env = rte_power_get_env();
122         if (env != PM_ENV_NOT_SET) {
123                 printf("Unexpectedly got a valid environment configuration\n");
124                 return -1;
125         }
126
127         /* Verify that function pointers are NULL */
128         if (check_function_ptrs() < 0)
129                 goto fail_all;
130
131         rte_power_unset_env();
132
133         /* Perform tests for valid environments.*/
134         const enum power_management_env envs[] = {PM_ENV_ACPI_CPUFREQ,
135                         PM_ENV_KVM_VM,
136                         PM_ENV_PSTATE_CPUFREQ};
137
138         unsigned int i;
139         for (i = 0; i < RTE_DIM(envs); ++i) {
140
141                 /* Test setting a valid environment */
142                 ret = rte_power_set_env(envs[i]);
143                 if (ret != 0) {
144                         printf("Unexpectedly unsucceeded on setting a valid environment\n");
145                         return -1;
146                 }
147
148                 /* Test that the environment has been set */
149                 env = rte_power_get_env();
150                 if (env != envs[i]) {
151                         printf("Not expected environment configuration\n");
152                         return -1;
153                 }
154
155                 /* Verify that function pointers are NOT NULL */
156                 if (check_function_ptrs() < 0)
157                         goto fail_all;
158
159                 rte_power_unset_env();
160
161                 /* Verify that function pointers are NULL */
162                 if (check_function_ptrs() < 0)
163                         goto fail_all;
164
165         }
166
167         return 0;
168 fail_all:
169         rte_power_unset_env();
170         return -1;
171 }
172 #endif
173
174 REGISTER_TEST_COMMAND(power_autotest, test_power);