4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include <sys/queue.h>
40 #include <rte_memory.h>
41 #include <rte_launch.h>
43 #include <rte_debug.h>
44 #include <rte_cycles.h>
46 /* allow application scheduling of the services */
47 #include <rte_service.h>
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.
54 #include <rte_service_component.h>
56 #define PROFILE_CORES_MAX 8
57 #define PROFILE_SERVICE_PER_CORE 5
59 /* dummy function to do "work" */
60 static int32_t service_func(void *args)
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},
74 #define NUM_SERVICES RTE_DIM(services)
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];
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.
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
93 struct profile_for_core cores[PROFILE_CORES_MAX];
96 static struct profile profiles[] = {
97 /* profile 0: high performance */
99 .name = "High Performance",
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} },
107 /* profile 1: mid performance with single service priority */
109 .name = "Mid-High Performance",
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} },
117 /* profile 2: mid performance with single service priority */
119 .name = "Mid-Low Performance",
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} },
127 /* profile 3: scale down performance on single core */
129 .name = "Scale down performance",
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} },
138 #define NUM_PROFILES RTE_DIM(profiles)
141 apply_profile(int profile_id)
146 struct profile *p = &profiles[profile_id];
147 const uint8_t core_off = 1;
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);
155 ret = rte_service_lcore_start(core);
156 if (ret && ret != -EALREADY)
157 printf("core %d start ret %d\n", core, ret);
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);
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__,
175 ret = rte_service_lcore_stop(core);
176 if (ret && ret != -EALREADY) {
177 printf("%s %d: lcore stop = %d\n", __func__,
180 ret = rte_service_lcore_del(core);
181 if (ret && ret != -EINVAL) {
182 printf("%s %d: lcore del = %d\n", __func__,
189 main(int argc, char **argv)
193 ret = rte_eal_init(argc, argv);
195 rte_panic("Cannot init EAL\n");
198 for (i = 0; i < NUM_SERVICES; i++) {
199 services[i].callback_userdata = 0;
201 ret = rte_service_component_register(&services[i], &id);
203 rte_exit(-1, "service register() failed");
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.
209 rte_service_component_runstate_set(id, 1);
211 /* Collect statistics for the service */
212 rte_service_set_stats_enable(id, 1);
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.
220 ret = rte_service_runstate_set(id, 1);
227 const char clr[] = { 27, '[', '2', 'J', '\0' };
228 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
229 printf("%s%s", clr, topLeft);
232 printf("\n==> Profile: %s\n\n", profiles[i].name);
235 rte_service_dump(stdout, UINT32_MAX);
238 rte_service_dump(stdout, UINT32_MAX);
241 if (i >= NUM_PROFILES)