xref: /dpdk/app/test/test_power_kvm_vm.c (revision f30a1bbd63f494f5ba623582d7e9166c817794a4)
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_cpufreq.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 	/* Test initialisation of an out of bounds lcore */
51 	ret = rte_power_init(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
52 	if (ret != -1) {
53 		printf("rte_power_init unexpectedly succeeded on an invalid lcore %u\n",
54 				TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
55 		rte_power_unset_env();
56 		return -1;
57 	}
58 
59 	/* Test initialisation of a valid lcore */
60 	ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
61 	if (ret < 0) {
62 		printf("Cannot initialise power management for lcore %u, this "
63 				"may occur if environment is not configured "
64 				"correctly(KVM VM) or operating in another valid "
65 				"Power management environment\n",
66 				TEST_POWER_VM_LCORE_ID);
67 		rte_power_unset_env();
68 		return TEST_SKIPPED;
69 	}
70 
71 	/* Test initialisation of previously initialised lcore */
72 	ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
73 	if (ret == 0) {
74 		printf("rte_power_init unexpectedly succeeded on calling init twice on"
75 				" lcore %u\n", TEST_POWER_VM_LCORE_ID);
76 		goto fail_all;
77 	}
78 
79 	/* Test frequency up of invalid lcore */
80 	ret = rte_power_freq_up(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
81 	if (ret == 1) {
82 		printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
83 				TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
84 		goto fail_all;
85 	}
86 
87 	/* Test frequency down of invalid lcore */
88 	ret = rte_power_freq_down(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
89 	if (ret == 1) {
90 		printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
91 				"%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
92 		goto fail_all;
93 	}
94 
95 	/* Test frequency min of invalid lcore */
96 	ret = rte_power_freq_min(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
97 	if (ret == 1) {
98 		printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
99 				"%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
100 		goto fail_all;
101 	}
102 
103 	/* Test frequency max of invalid lcore */
104 	ret = rte_power_freq_max(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
105 	if (ret == 1) {
106 		printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
107 				"%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
108 		goto fail_all;
109 	}
110 
111 	/* Test frequency up of valid but uninitialised lcore */
112 	ret = rte_power_freq_up(TEST_POWER_VM_LCORE_INVALID);
113 	if (ret == 1) {
114 		printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
115 				TEST_POWER_VM_LCORE_INVALID);
116 		goto fail_all;
117 	}
118 
119 	/* Test frequency down of valid but uninitialised lcore */
120 	ret = rte_power_freq_down(TEST_POWER_VM_LCORE_INVALID);
121 	if (ret == 1) {
122 		printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
123 				"%u\n", TEST_POWER_VM_LCORE_INVALID);
124 		goto fail_all;
125 	}
126 
127 	/* Test frequency min of valid but uninitialised lcore */
128 	ret = rte_power_freq_min(TEST_POWER_VM_LCORE_INVALID);
129 	if (ret == 1) {
130 		printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
131 				"%u\n", TEST_POWER_VM_LCORE_INVALID);
132 		goto fail_all;
133 	}
134 
135 	/* Test frequency max of valid but uninitialised lcore */
136 	ret = rte_power_freq_max(TEST_POWER_VM_LCORE_INVALID);
137 	if (ret == 1) {
138 		printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
139 				"%u\n", TEST_POWER_VM_LCORE_INVALID);
140 		goto fail_all;
141 	}
142 
143 	/* Test KVM_VM Enable Turbo of valid core */
144 	ret = rte_power_freq_enable_turbo(TEST_POWER_VM_LCORE_ID);
145 	if (ret == -1) {
146 		printf("rte_power_freq_enable_turbo failed on valid lcore"
147 			"%u\n", TEST_POWER_VM_LCORE_ID);
148 		goto fail_all;
149 	}
150 
151 	/* Test KVM_VM Disable Turbo of valid core */
152 	ret = rte_power_freq_disable_turbo(TEST_POWER_VM_LCORE_ID);
153 	if (ret == -1) {
154 		printf("rte_power_freq_disable_turbo failed on valid lcore"
155 		"%u\n", TEST_POWER_VM_LCORE_ID);
156 		goto fail_all;
157 	}
158 
159 	/* Test frequency up of valid lcore */
160 	ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
161 	if (ret != 1) {
162 		printf("rte_power_freq_up unexpectedly failed on valid lcore %u\n",
163 				TEST_POWER_VM_LCORE_ID);
164 		goto fail_all;
165 	}
166 
167 	/* Test frequency down of valid lcore */
168 	ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
169 	if (ret != 1) {
170 		printf("rte_power_freq_down unexpectedly failed on valid lcore "
171 				"%u\n", TEST_POWER_VM_LCORE_ID);
172 		goto fail_all;
173 	}
174 
175 	/* Test frequency min of valid lcore */
176 	ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
177 	if (ret != 1) {
178 		printf("rte_power_freq_min unexpectedly failed on valid lcore "
179 				"%u\n", TEST_POWER_VM_LCORE_ID);
180 		goto fail_all;
181 	}
182 
183 	/* Test frequency max of valid lcore */
184 	ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
185 	if (ret != 1) {
186 		printf("rte_power_freq_max unexpectedly failed on valid lcore "
187 				"%u\n", TEST_POWER_VM_LCORE_ID);
188 		goto fail_all;
189 	}
190 
191 	/* Test unsupported rte_power_freqs */
192 	ret = rte_power_freqs(TEST_POWER_VM_LCORE_ID, NULL, 0);
193 	if (ret != -ENOTSUP) {
194 		printf("rte_power_freqs did not return the expected -ENOTSUP(%d) but "
195 				"returned %d\n", -ENOTSUP, ret);
196 		goto fail_all;
197 	}
198 
199 	/* Test unsupported rte_power_get_freq */
200 	ret = rte_power_get_freq(TEST_POWER_VM_LCORE_ID);
201 	if (ret != -ENOTSUP) {
202 		printf("rte_power_get_freq did not return the expected -ENOTSUP(%d) but"
203 				" returned %d for lcore %u\n",
204 				-ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
205 		goto fail_all;
206 	}
207 
208 	/* Test unsupported rte_power_set_freq */
209 	ret = rte_power_set_freq(TEST_POWER_VM_LCORE_ID, 0);
210 	if (ret != -ENOTSUP) {
211 		printf("rte_power_set_freq did not return the expected -ENOTSUP(%d) but"
212 				" returned %d for lcore %u\n",
213 				-ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
214 		goto fail_all;
215 	}
216 
217 	/* Test removing of an lcore */
218 	ret = rte_power_exit(TEST_POWER_VM_LCORE_ID);
219 	if (ret != 0) {
220 		printf("rte_power_exit unexpectedly failed on valid lcore %u,"
221 				"please ensure that the environment has been configured "
222 				"correctly\n", TEST_POWER_VM_LCORE_ID);
223 		goto fail_all;
224 	}
225 
226 	/* Test frequency up of previously removed lcore */
227 	ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
228 	if (ret == 0) {
229 		printf("rte_power_freq_up unexpectedly succeeded on a removed "
230 				"lcore %u\n", TEST_POWER_VM_LCORE_ID);
231 		return -1;
232 	}
233 
234 	/* Test frequency down of previously removed lcore */
235 	ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
236 	if (ret == 0) {
237 		printf("rte_power_freq_down unexpectedly succeeded on a removed "
238 				"lcore %u\n", TEST_POWER_VM_LCORE_ID);
239 		return -1;
240 	}
241 
242 	/* Test frequency min of previously removed lcore */
243 	ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
244 	if (ret == 0) {
245 		printf("rte_power_freq_min unexpectedly succeeded on a removed "
246 				"lcore %u\n", TEST_POWER_VM_LCORE_ID);
247 		return -1;
248 	}
249 
250 	/* Test frequency max of previously removed lcore */
251 	ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
252 	if (ret == 0) {
253 		printf("rte_power_freq_max unexpectedly succeeded on a removed "
254 				"lcore %u\n", TEST_POWER_VM_LCORE_ID);
255 		return -1;
256 	}
257 	rte_power_unset_env();
258 	return 0;
259 fail_all:
260 	rte_power_exit(TEST_POWER_VM_LCORE_ID);
261 	rte_power_unset_env();
262 	return -1;
263 }
264 #endif
265 
266 REGISTER_FAST_TEST(power_kvm_vm_autotest, false, true, test_power_kvm_vm);
267