xref: /dpdk/app/test/test_power_kvm_vm.c (revision 68a03efeed657e6e05f281479b33b51102797e15)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 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 
11 #include "test.h"
12 
13 #ifndef RTE_LIB_POWER
14 
15 static int
16 test_power_kvm_vm(void)
17 {
18 	printf("Power management library not supported, skipping test\n");
19 	return TEST_SKIPPED;
20 }
21 
22 #else
23 #include <rte_power.h>
24 
25 #define TEST_POWER_VM_LCORE_ID            0U
26 #define TEST_POWER_VM_LCORE_OUT_OF_BOUNDS (RTE_MAX_LCORE+1)
27 #define TEST_POWER_VM_LCORE_INVALID       1U
28 
29 static int
30 test_power_kvm_vm(void)
31 {
32 	int ret;
33 	enum power_management_env env;
34 
35 	ret = rte_power_set_env(PM_ENV_KVM_VM);
36 	if (ret != 0) {
37 		printf("Failed on setting environment to PM_ENV_KVM_VM\n");
38 		return -1;
39 	}
40 
41 	/* Test environment configuration */
42 	env = rte_power_get_env();
43 	if (env != PM_ENV_KVM_VM) {
44 		printf("Unexpectedly got a Power Management environment other than "
45 				"KVM VM\n");
46 		rte_power_unset_env();
47 		return -1;
48 	}
49 
50 	/* verify that function pointers are not NULL */
51 	if (rte_power_freqs == NULL) {
52 		printf("rte_power_freqs should not be NULL, environment has not been "
53 				"initialised\n");
54 		return -1;
55 	}
56 	if (rte_power_get_freq == NULL) {
57 		printf("rte_power_get_freq should not be NULL, environment has not "
58 				"been initialised\n");
59 		return -1;
60 	}
61 	if (rte_power_set_freq == NULL) {
62 		printf("rte_power_set_freq should not be NULL, environment has not "
63 				"been initialised\n");
64 		return -1;
65 	}
66 	if (rte_power_freq_up == NULL) {
67 		printf("rte_power_freq_up should not be NULL, environment has not "
68 				"been initialised\n");
69 		return -1;
70 	}
71 	if (rte_power_freq_down == NULL) {
72 		printf("rte_power_freq_down should not be NULL, environment has not "
73 				"been initialised\n");
74 		return -1;
75 	}
76 	if (rte_power_freq_max == NULL) {
77 		printf("rte_power_freq_max should not be NULL, environment has not "
78 				"been initialised\n");
79 		return -1;
80 	}
81 	if (rte_power_freq_min == NULL) {
82 		printf("rte_power_freq_min should not be NULL, environment has not "
83 				"been initialised\n");
84 		return -1;
85 	}
86 	/* Test initialisation of an out of bounds lcore */
87 	ret = rte_power_init(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
88 	if (ret != -1) {
89 		printf("rte_power_init unexpectedly succeeded on an invalid lcore %u\n",
90 				TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
91 		rte_power_unset_env();
92 		return -1;
93 	}
94 
95 	/* Test initialisation of a valid lcore */
96 	ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
97 	if (ret < 0) {
98 		printf("Cannot initialise power management for lcore %u, this "
99 				"may occur if environment is not configured "
100 				"correctly(KVM VM) or operating in another valid "
101 				"Power management environment\n",
102 				TEST_POWER_VM_LCORE_ID);
103 		rte_power_unset_env();
104 		return TEST_SKIPPED;
105 	}
106 
107 	/* Test initialisation of previously initialised lcore */
108 	ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
109 	if (ret == 0) {
110 		printf("rte_power_init unexpectedly succeeded on calling init twice on"
111 				" lcore %u\n", TEST_POWER_VM_LCORE_ID);
112 		goto fail_all;
113 	}
114 
115 	/* Test frequency up of invalid lcore */
116 	ret = rte_power_freq_up(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
117 	if (ret == 1) {
118 		printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
119 				TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
120 		goto fail_all;
121 	}
122 
123 	/* Test frequency down of invalid lcore */
124 	ret = rte_power_freq_down(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
125 	if (ret == 1) {
126 		printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
127 				"%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
128 		goto fail_all;
129 	}
130 
131 	/* Test frequency min of invalid lcore */
132 	ret = rte_power_freq_min(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
133 	if (ret == 1) {
134 		printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
135 				"%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
136 		goto fail_all;
137 	}
138 
139 	/* Test frequency max of invalid lcore */
140 	ret = rte_power_freq_max(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
141 	if (ret == 1) {
142 		printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
143 				"%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
144 		goto fail_all;
145 	}
146 
147 	/* Test frequency up of valid but uninitialised lcore */
148 	ret = rte_power_freq_up(TEST_POWER_VM_LCORE_INVALID);
149 	if (ret == 1) {
150 		printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
151 				TEST_POWER_VM_LCORE_INVALID);
152 		goto fail_all;
153 	}
154 
155 	/* Test frequency down of valid but uninitialised lcore */
156 	ret = rte_power_freq_down(TEST_POWER_VM_LCORE_INVALID);
157 	if (ret == 1) {
158 		printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
159 				"%u\n", TEST_POWER_VM_LCORE_INVALID);
160 		goto fail_all;
161 	}
162 
163 	/* Test frequency min of valid but uninitialised lcore */
164 	ret = rte_power_freq_min(TEST_POWER_VM_LCORE_INVALID);
165 	if (ret == 1) {
166 		printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
167 				"%u\n", TEST_POWER_VM_LCORE_INVALID);
168 		goto fail_all;
169 	}
170 
171 	/* Test frequency max of valid but uninitialised lcore */
172 	ret = rte_power_freq_max(TEST_POWER_VM_LCORE_INVALID);
173 	if (ret == 1) {
174 		printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
175 				"%u\n", TEST_POWER_VM_LCORE_INVALID);
176 		goto fail_all;
177 	}
178 
179 	/* Test KVM_VM Enable Turbo of valid core */
180 	ret = rte_power_freq_enable_turbo(TEST_POWER_VM_LCORE_ID);
181 	if (ret == -1) {
182 		printf("rte_power_freq_enable_turbo failed on valid lcore"
183 			"%u\n", TEST_POWER_VM_LCORE_ID);
184 		goto fail_all;
185 	}
186 
187 	/* Test KVM_VM Disable Turbo of valid core */
188 	ret = rte_power_freq_disable_turbo(TEST_POWER_VM_LCORE_ID);
189 	if (ret == -1) {
190 		printf("rte_power_freq_disable_turbo failed on valid lcore"
191 		"%u\n", TEST_POWER_VM_LCORE_ID);
192 		goto fail_all;
193 	}
194 
195 	/* Test frequency up of valid lcore */
196 	ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
197 	if (ret != 1) {
198 		printf("rte_power_freq_up unexpectedly failed on valid lcore %u\n",
199 				TEST_POWER_VM_LCORE_ID);
200 		goto fail_all;
201 	}
202 
203 	/* Test frequency down of valid lcore */
204 	ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
205 	if (ret != 1) {
206 		printf("rte_power_freq_down unexpectedly failed on valid lcore "
207 				"%u\n", TEST_POWER_VM_LCORE_ID);
208 		goto fail_all;
209 	}
210 
211 	/* Test frequency min of valid lcore */
212 	ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
213 	if (ret != 1) {
214 		printf("rte_power_freq_min unexpectedly failed on valid lcore "
215 				"%u\n", TEST_POWER_VM_LCORE_ID);
216 		goto fail_all;
217 	}
218 
219 	/* Test frequency max of valid lcore */
220 	ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
221 	if (ret != 1) {
222 		printf("rte_power_freq_max unexpectedly failed on valid lcore "
223 				"%u\n", TEST_POWER_VM_LCORE_ID);
224 		goto fail_all;
225 	}
226 
227 	/* Test unsupported rte_power_freqs */
228 	ret = rte_power_freqs(TEST_POWER_VM_LCORE_ID, NULL, 0);
229 	if (ret != -ENOTSUP) {
230 		printf("rte_power_freqs did not return the expected -ENOTSUP(%d) but "
231 				"returned %d\n", -ENOTSUP, ret);
232 		goto fail_all;
233 	}
234 
235 	/* Test unsupported rte_power_get_freq */
236 	ret = rte_power_get_freq(TEST_POWER_VM_LCORE_ID);
237 	if (ret != -ENOTSUP) {
238 		printf("rte_power_get_freq did not return the expected -ENOTSUP(%d) but"
239 				" returned %d for lcore %u\n",
240 				-ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
241 		goto fail_all;
242 	}
243 
244 	/* Test unsupported rte_power_set_freq */
245 	ret = rte_power_set_freq(TEST_POWER_VM_LCORE_ID, 0);
246 	if (ret != -ENOTSUP) {
247 		printf("rte_power_set_freq did not return the expected -ENOTSUP(%d) but"
248 				" returned %d for lcore %u\n",
249 				-ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
250 		goto fail_all;
251 	}
252 
253 	/* Test removing of an lcore */
254 	ret = rte_power_exit(TEST_POWER_VM_LCORE_ID);
255 	if (ret != 0) {
256 		printf("rte_power_exit unexpectedly failed on valid lcore %u,"
257 				"please ensure that the environment has been configured "
258 				"correctly\n", TEST_POWER_VM_LCORE_ID);
259 		goto fail_all;
260 	}
261 
262 	/* Test frequency up of previously removed lcore */
263 	ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
264 	if (ret == 0) {
265 		printf("rte_power_freq_up unexpectedly succeeded on a removed "
266 				"lcore %u\n", TEST_POWER_VM_LCORE_ID);
267 		return -1;
268 	}
269 
270 	/* Test frequency down of previously removed lcore */
271 	ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
272 	if (ret == 0) {
273 		printf("rte_power_freq_down unexpectedly succeeded on a removed "
274 				"lcore %u\n", TEST_POWER_VM_LCORE_ID);
275 		return -1;
276 	}
277 
278 	/* Test frequency min of previously removed lcore */
279 	ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
280 	if (ret == 0) {
281 		printf("rte_power_freq_min unexpectedly succeeded on a removed "
282 				"lcore %u\n", TEST_POWER_VM_LCORE_ID);
283 		return -1;
284 	}
285 
286 	/* Test frequency max of previously removed lcore */
287 	ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
288 	if (ret == 0) {
289 		printf("rte_power_freq_max unexpectedly succeeded on a removed "
290 				"lcore %u\n", TEST_POWER_VM_LCORE_ID);
291 		return -1;
292 	}
293 	rte_power_unset_env();
294 	return 0;
295 fail_all:
296 	rte_power_exit(TEST_POWER_VM_LCORE_ID);
297 	rte_power_unset_env();
298 	return -1;
299 }
300 #endif
301 
302 REGISTER_TEST_COMMAND(power_kvm_vm_autotest, test_power_kvm_vm);
303