app/test: enhance power manager unit tests
[dpdk.git] / test / test / test_power_kvm_vm.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
11 #include "test.h"
12
13 #ifndef RTE_LIBRTE_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 #define TEMP_POWER_MANAGER_FILE_PATH  "/tmp/testpm"
29
30 int guest_channel_host_connect(const char *path, unsigned int lcore_id);
31
32 static int
33 test_power_kvm_vm(void)
34 {
35         int ret;
36         enum power_management_env env;
37         char fPath[PATH_MAX];
38         FILE *fPtr = NULL;
39
40         ret = rte_power_set_env(PM_ENV_KVM_VM);
41         if (ret != 0) {
42                 printf("Failed on setting environment to PM_ENV_KVM_VM\n");
43                 return -1;
44         }
45
46         /* Test environment configuration */
47         env = rte_power_get_env();
48         if (env != PM_ENV_KVM_VM) {
49                 printf("Unexpectedly got a Power Management environment other than "
50                                 "KVM VM\n");
51                 rte_power_unset_env();
52                 return -1;
53         }
54
55         /* verify that function pointers are not NULL */
56         if (rte_power_freqs == NULL) {
57                 printf("rte_power_freqs should not be NULL, environment has not been "
58                                 "initialised\n");
59                 return -1;
60         }
61         if (rte_power_get_freq == NULL) {
62                 printf("rte_power_get_freq should not be NULL, environment has not "
63                                 "been initialised\n");
64                 return -1;
65         }
66         if (rte_power_set_freq == NULL) {
67                 printf("rte_power_set_freq should not be NULL, environment has not "
68                                 "been initialised\n");
69                 return -1;
70         }
71         if (rte_power_freq_up == NULL) {
72                 printf("rte_power_freq_up should not be NULL, environment has not "
73                                 "been initialised\n");
74                 return -1;
75         }
76         if (rte_power_freq_down == NULL) {
77                 printf("rte_power_freq_down should not be NULL, environment has not "
78                                 "been initialised\n");
79                 return -1;
80         }
81         if (rte_power_freq_max == NULL) {
82                 printf("rte_power_freq_max should not be NULL, environment has not "
83                                 "been initialised\n");
84                 return -1;
85         }
86         if (rte_power_freq_min == NULL) {
87                 printf("rte_power_freq_min should not be NULL, environment has not "
88                                 "been initialised\n");
89                 return -1;
90         }
91         /* Test initialisation of an out of bounds lcore */
92         ret = rte_power_init(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
93         if (ret != -1) {
94                 printf("rte_power_init unexpectedly succeeded on an invalid lcore %u\n",
95                                 TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
96                 rte_power_unset_env();
97                 return -1;
98         }
99
100         /* Test initialisation of a valid lcore */
101         ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
102         if (ret < 0) {
103                 printf("rte_power_init failed as expected in host\n");
104                 /* This test would be successful when run on VM,
105                  * in order to run in Host itself, temporary file path
106                  * is created and same is used for further communication
107                  */
108
109                 snprintf(fPath, PATH_MAX, "%s.%u",
110                         TEMP_POWER_MANAGER_FILE_PATH, TEST_POWER_VM_LCORE_ID);
111                 fPtr = fopen(fPath, "w");
112                 if (fPtr == NULL) {
113                         printf(" Unable to create file\n");
114                         rte_power_unset_env();
115                         return -1;
116                 }
117                 ret = guest_channel_host_connect(TEMP_POWER_MANAGER_FILE_PATH,
118                         TEST_POWER_VM_LCORE_ID);
119                 if (ret == 0)
120                         printf("guest_channel_host_connect successful\n");
121                 else {
122                         printf("guest_channel_host_connect failed\n");
123                         rte_power_unset_env();
124                         fclose(fPtr);
125                         remove(fPath);
126                         return -1;
127                 }
128         }
129
130         /* Test initialisation of previously initialised lcore */
131         ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
132         if (ret == 0) {
133                 printf("rte_power_init unexpectedly succeeded on calling init twice on"
134                                 " lcore %u\n", TEST_POWER_VM_LCORE_ID);
135                 goto fail_all;
136         }
137
138         /* Test frequency up of invalid lcore */
139         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
140         if (ret == 1) {
141                 printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
142                                 TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
143                 goto fail_all;
144         }
145
146         /* Test frequency down of invalid lcore */
147         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
148         if (ret == 1) {
149                 printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
150                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
151                 goto fail_all;
152         }
153
154         /* Test frequency min of invalid lcore */
155         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
156         if (ret == 1) {
157                 printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
158                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
159                 goto fail_all;
160         }
161
162         /* Test frequency max of invalid lcore */
163         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
164         if (ret == 1) {
165                 printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
166                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
167                 goto fail_all;
168         }
169
170         /* Test frequency up of valid but uninitialised lcore */
171         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_INVALID);
172         if (ret == 1) {
173                 printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
174                                 TEST_POWER_VM_LCORE_INVALID);
175                 goto fail_all;
176         }
177
178         /* Test frequency down of valid but uninitialised lcore */
179         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_INVALID);
180         if (ret == 1) {
181                 printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
182                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
183                 goto fail_all;
184         }
185
186         /* Test frequency min of valid but uninitialised lcore */
187         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_INVALID);
188         if (ret == 1) {
189                 printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
190                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
191                 goto fail_all;
192         }
193
194         /* Test frequency max of valid but uninitialised lcore */
195         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_INVALID);
196         if (ret == 1) {
197                 printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
198                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
199                 goto fail_all;
200         }
201
202         /* Test KVM_VM Enable Turbo of valid core */
203         ret = rte_power_freq_enable_turbo(TEST_POWER_VM_LCORE_ID);
204         if (ret == -1) {
205                 printf("rte_power_freq_enable_turbo failed on valid lcore"
206                         "%u\n", TEST_POWER_VM_LCORE_ID);
207                 goto fail_all;
208         }
209
210         /* Test KVM_VM Disable Turbo of valid core */
211         ret = rte_power_freq_disable_turbo(TEST_POWER_VM_LCORE_ID);
212         if (ret == -1) {
213                 printf("rte_power_freq_disable_turbo failed on valid lcore"
214                 "%u\n", TEST_POWER_VM_LCORE_ID);
215                 goto fail_all;
216         }
217
218         /* Test frequency up of valid lcore */
219         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
220         if (ret != 1) {
221                 printf("rte_power_freq_up unexpectedly failed on valid lcore %u\n",
222                                 TEST_POWER_VM_LCORE_ID);
223                 goto fail_all;
224         }
225
226         /* Test frequency down of valid lcore */
227         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
228         if (ret != 1) {
229                 printf("rte_power_freq_down unexpectedly failed on valid lcore "
230                                 "%u\n", TEST_POWER_VM_LCORE_ID);
231                 goto fail_all;
232         }
233
234         /* Test frequency min of valid lcore */
235         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
236         if (ret != 1) {
237                 printf("rte_power_freq_min unexpectedly failed on valid lcore "
238                                 "%u\n", TEST_POWER_VM_LCORE_ID);
239                 goto fail_all;
240         }
241
242         /* Test frequency max of valid lcore */
243         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
244         if (ret != 1) {
245                 printf("rte_power_freq_max unexpectedly failed on valid lcore "
246                                 "%u\n", TEST_POWER_VM_LCORE_ID);
247                 goto fail_all;
248         }
249
250         /* Test unsupported rte_power_freqs */
251         ret = rte_power_freqs(TEST_POWER_VM_LCORE_ID, NULL, 0);
252         if (ret != -ENOTSUP) {
253                 printf("rte_power_freqs did not return the expected -ENOTSUP(%d) but "
254                                 "returned %d\n", -ENOTSUP, ret);
255                 goto fail_all;
256         }
257
258         /* Test unsupported rte_power_get_freq */
259         ret = rte_power_get_freq(TEST_POWER_VM_LCORE_ID);
260         if (ret != -ENOTSUP) {
261                 printf("rte_power_get_freq did not return the expected -ENOTSUP(%d) but"
262                                 " returned %d for lcore %u\n",
263                                 -ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
264                 goto fail_all;
265         }
266
267         /* Test unsupported rte_power_set_freq */
268         ret = rte_power_set_freq(TEST_POWER_VM_LCORE_ID, 0);
269         if (ret != -ENOTSUP) {
270                 printf("rte_power_set_freq did not return the expected -ENOTSUP(%d) but"
271                                 " returned %d for lcore %u\n",
272                                 -ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
273                 goto fail_all;
274         }
275
276         /* Test removing of an lcore */
277         ret = rte_power_exit(TEST_POWER_VM_LCORE_ID);
278         if (ret != 0) {
279                 printf("rte_power_exit unexpectedly failed on valid lcore %u,"
280                                 "please ensure that the environment has been configured "
281                                 "correctly\n", TEST_POWER_VM_LCORE_ID);
282                 goto fail_all;
283         }
284
285         /* Test frequency up of previously removed lcore */
286         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
287         if (ret == 0) {
288                 printf("rte_power_freq_up unexpectedly succeeded on a removed "
289                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
290                 return -1;
291         }
292
293         /* Test frequency down of previously removed lcore */
294         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
295         if (ret == 0) {
296                 printf("rte_power_freq_down unexpectedly succeeded on a removed "
297                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
298                 return -1;
299         }
300
301         /* Test frequency min of previously removed lcore */
302         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
303         if (ret == 0) {
304                 printf("rte_power_freq_min unexpectedly succeeded on a removed "
305                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
306                 return -1;
307         }
308
309         /* Test frequency max of previously removed lcore */
310         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
311         if (ret == 0) {
312                 printf("rte_power_freq_max unexpectedly succeeded on a removed "
313                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
314                 return -1;
315         }
316         rte_power_unset_env();
317         if (fPtr != NULL) {
318                 fclose(fPtr);
319                 remove(fPath);
320         }
321         return 0;
322 fail_all:
323         rte_power_exit(TEST_POWER_VM_LCORE_ID);
324         rte_power_unset_env();
325         if (fPtr != NULL) {
326                 fclose(fPtr);
327                 remove(fPath);
328         }
329         return -1;
330 }
331 #endif
332
333 REGISTER_TEST_COMMAND(power_kvm_vm_autotest, test_power_kvm_vm);