New upstream version 17.11-rc3
[deb_dpdk.git] / examples / service_cores / main.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <errno.h>
38 #include <sys/queue.h>
39
40 #include <rte_memory.h>
41 #include <rte_launch.h>
42 #include <rte_eal.h>
43 #include <rte_debug.h>
44 #include <rte_cycles.h>
45
46 /* allow application scheduling of the services */
47 #include <rte_service.h>
48
49 /* Allow application registration of its own services. An application does not
50  * have to register services, but it can be useful if it wishes to run a
51  * function on a core that is otherwise in use as a service core. In this
52  * example, all services are dummy services registered by the sample app itself.
53  */
54 #include <rte_service_component.h>
55
56 #define PROFILE_CORES_MAX 8
57 #define PROFILE_SERVICE_PER_CORE 5
58
59 /* dummy function to do "work" */
60 static int32_t service_func(void *args)
61 {
62         RTE_SET_USED(args);
63         rte_delay_us(2000);
64         return 0;
65 }
66
67 static struct rte_service_spec services[] = {
68         {"service_1", service_func, NULL, 0, 0},
69         {"service_2", service_func, NULL, 0, 0},
70         {"service_3", service_func, NULL, 0, 0},
71         {"service_4", service_func, NULL, 0, 0},
72         {"service_5", service_func, NULL, 0, 0},
73 };
74 #define NUM_SERVICES RTE_DIM(services)
75
76 /* this struct holds the mapping of a particular core to all services */
77 struct profile_for_core {
78         uint32_t mapped_services[PROFILE_SERVICE_PER_CORE];
79 };
80
81 /* struct that can be applied as the service core mapping. Items in this
82  * struct will be passed to the ordinary rte_service_* APIs to configure the
83  * service cores at runtime, based on the requirements.
84  *
85  * These profiles can be considered a "configuration" for the service cores,
86  * where switching profile just changes the number of cores and the mappings
87  * for each of them. As a result, the core requirements and performance of the
88  * application scales.
89  */
90 struct profile {
91         char name[64];
92         uint32_t num_cores;
93         struct profile_for_core cores[PROFILE_CORES_MAX];
94 };
95
96 static struct profile profiles[] = {
97         /* profile 0: high performance */
98         {
99                 .name = "High Performance",
100                 .num_cores = 5,
101                 .cores[0] = {.mapped_services = {1, 0, 0, 0, 0} },
102                 .cores[1] = {.mapped_services = {0, 1, 0, 0, 0} },
103                 .cores[2] = {.mapped_services = {0, 0, 1, 0, 0} },
104                 .cores[3] = {.mapped_services = {0, 0, 0, 1, 0} },
105                 .cores[4] = {.mapped_services = {0, 0, 0, 0, 1} },
106         },
107         /* profile 1: mid performance with single service priority */
108         {
109                 .name = "Mid-High Performance",
110                 .num_cores = 3,
111                 .cores[0] = {.mapped_services = {1, 1, 0, 0, 0} },
112                 .cores[1] = {.mapped_services = {0, 0, 1, 1, 0} },
113                 .cores[2] = {.mapped_services = {0, 0, 0, 0, 1} },
114                 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
115                 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
116         },
117         /* profile 2: mid performance with single service priority */
118         {
119                 .name = "Mid-Low Performance",
120                 .num_cores = 2,
121                 .cores[0] = {.mapped_services = {1, 1, 1, 0, 0} },
122                 .cores[1] = {.mapped_services = {1, 1, 0, 1, 1} },
123                 .cores[2] = {.mapped_services = {0, 0, 0, 0, 0} },
124                 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
125                 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
126         },
127         /* profile 3: scale down performance on single core */
128         {
129                 .name = "Scale down performance",
130                 .num_cores = 1,
131                 .cores[0] = {.mapped_services = {1, 1, 1, 1, 1} },
132                 .cores[1] = {.mapped_services = {0, 0, 0, 0, 0} },
133                 .cores[2] = {.mapped_services = {0, 0, 0, 0, 0} },
134                 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
135                 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
136         },
137 };
138 #define NUM_PROFILES RTE_DIM(profiles)
139
140 static void
141 apply_profile(int profile_id)
142 {
143         uint32_t i;
144         uint32_t s;
145         int ret;
146         struct profile *p = &profiles[profile_id];
147         const uint8_t core_off = 1;
148
149         for (i = 0; i < p->num_cores; i++) {
150                 uint32_t core = i + core_off;
151                 ret = rte_service_lcore_add(core);
152                 if (ret && ret != -EALREADY)
153                         printf("core %d added ret %d\n", core, ret);
154
155                 ret = rte_service_lcore_start(core);
156                 if (ret && ret != -EALREADY)
157                         printf("core %d start ret %d\n", core, ret);
158
159                 for (s = 0; s < NUM_SERVICES; s++) {
160                         if (rte_service_map_lcore_set(s, core,
161                                         p->cores[i].mapped_services[s]))
162                                 printf("failed to map lcore %d\n", core);
163                 }
164         }
165
166         for ( ; i < PROFILE_CORES_MAX; i++) {
167                 uint32_t core = i + core_off;
168                 for (s = 0; s < NUM_SERVICES; s++) {
169                         ret = rte_service_map_lcore_set(s, core, 0);
170                         if (ret && ret != -EINVAL) {
171                                 printf("%s %d: map lcore set = %d\n", __func__,
172                                                 __LINE__, ret);
173                         }
174                 }
175                 ret = rte_service_lcore_stop(core);
176                 if (ret && ret != -EALREADY) {
177                         printf("%s %d: lcore stop = %d\n", __func__,
178                                         __LINE__, ret);
179                 }
180                 ret = rte_service_lcore_del(core);
181                 if (ret && ret != -EINVAL) {
182                         printf("%s %d: lcore del = %d\n", __func__,
183                                         __LINE__, ret);
184                 }
185         }
186 }
187
188 int
189 main(int argc, char **argv)
190 {
191         int ret;
192
193         ret = rte_eal_init(argc, argv);
194         if (ret < 0)
195                 rte_panic("Cannot init EAL\n");
196
197         uint32_t i;
198         for (i = 0; i < NUM_SERVICES; i++) {
199                 services[i].callback_userdata = 0;
200                 uint32_t id;
201                 ret = rte_service_component_register(&services[i], &id);
202                 if (ret)
203                         rte_exit(-1, "service register() failed");
204
205                 /* set the service itself to be ready to run. In the case of
206                  * ethdev, eventdev etc PMDs, this will be set when the
207                  * appropriate configure or setup function is called.
208                  */
209                 rte_service_component_runstate_set(id, 1);
210
211                 /* Collect statistics for the service */
212                 rte_service_set_stats_enable(id, 1);
213
214                 /* the application sets the service to be active. Note that the
215                  * previous component_runstate_set() is the PMD indicating
216                  * ready, while this function is the application setting the
217                  * service to run. Applications can choose to not run a service
218                  * by setting runstate to 0 at any time.
219                  */
220                 ret = rte_service_runstate_set(id, 1);
221                 if (ret)
222                         return -ENOEXEC;
223         }
224
225         i = 0;
226         while (1) {
227                 const char clr[] = { 27, '[', '2', 'J', '\0' };
228                 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
229                 printf("%s%s", clr, topLeft);
230
231                 apply_profile(i);
232                 printf("\n==> Profile: %s\n\n", profiles[i].name);
233
234                 sleep(1);
235                 rte_service_dump(stdout, UINT32_MAX);
236
237                 sleep(5);
238                 rte_service_dump(stdout, UINT32_MAX);
239
240                 i++;
241                 if (i >= NUM_PROFILES)
242                         i = 0;
243         }
244
245         return 0;
246 }