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_memzone.h>
42 #include <rte_launch.h>
44 #include <rte_debug.h>
45 #include <rte_cycles.h>
47 /* allow application scheduling of the services */
48 #include <rte_service.h>
50 /* Allow application registration of its own services. An application does not
51 * have to register services, but it can be useful if it wishes to run a
52 * function on a core that is otherwise in use as a service core. In this
53 * example, all services are dummy services registered by the sample app itself.
55 #include <rte_service_component.h>
57 #define PROFILE_CORES_MAX 8
58 #define PROFILE_SERVICE_PER_CORE 5
60 /* dummy function to do "work" */
61 static int32_t service_func(void *args)
68 static struct rte_service_spec services[] = {
69 {"service_1", service_func, NULL, 0, 0},
70 {"service_2", service_func, NULL, 0, 0},
71 {"service_3", service_func, NULL, 0, 0},
72 {"service_4", service_func, NULL, 0, 0},
73 {"service_5", service_func, NULL, 0, 0},
75 #define NUM_SERVICES RTE_DIM(services)
77 /* this struct holds the mapping of a particular core to all services */
78 struct profile_for_core {
79 uint32_t mapped_services[PROFILE_SERVICE_PER_CORE];
82 /* struct that can be applied as the service core mapping. Items in this
83 * struct will be passed to the ordinary rte_service_* APIs to configure the
84 * service cores at runtime, based on the requirements.
86 * These profiles can be considered a "configuration" for the service cores,
87 * where switching profile just changes the number of cores and the mappings
88 * for each of them. As a result, the core requirements and performance of the
94 struct profile_for_core cores[PROFILE_CORES_MAX];
97 static struct profile profiles[] = {
98 /* profile 0: high performance */
100 .name = "High Performance",
102 .cores[0] = {.mapped_services = {1, 0, 0, 0, 0} },
103 .cores[1] = {.mapped_services = {0, 1, 0, 0, 0} },
104 .cores[2] = {.mapped_services = {0, 0, 1, 0, 0} },
105 .cores[3] = {.mapped_services = {0, 0, 0, 1, 0} },
106 .cores[4] = {.mapped_services = {0, 0, 0, 0, 1} },
108 /* profile 1: mid performance with single service priority */
110 .name = "Mid-High Performance",
112 .cores[0] = {.mapped_services = {1, 1, 0, 0, 0} },
113 .cores[1] = {.mapped_services = {0, 0, 1, 1, 0} },
114 .cores[2] = {.mapped_services = {0, 0, 0, 0, 1} },
115 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
116 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
118 /* profile 2: mid performance with single service priority */
120 .name = "Mid-Low Performance",
122 .cores[0] = {.mapped_services = {1, 1, 1, 0, 0} },
123 .cores[1] = {.mapped_services = {1, 1, 0, 1, 1} },
124 .cores[2] = {.mapped_services = {0, 0, 0, 0, 0} },
125 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
126 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
128 /* profile 3: scale down performance on single core */
130 .name = "Scale down performance",
132 .cores[0] = {.mapped_services = {1, 1, 1, 1, 1} },
133 .cores[1] = {.mapped_services = {0, 0, 0, 0, 0} },
134 .cores[2] = {.mapped_services = {0, 0, 0, 0, 0} },
135 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
136 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
139 #define NUM_PROFILES RTE_DIM(profiles)
142 apply_profile(int profile_id)
147 struct profile *p = &profiles[profile_id];
148 const uint8_t core_off = 1;
150 for (i = 0; i < p->num_cores; i++) {
151 uint32_t core = i + core_off;
152 ret = rte_service_lcore_add(core);
153 if (ret && ret != -EALREADY)
154 printf("core %d added ret %d\n", core, ret);
156 ret = rte_service_lcore_start(core);
157 if (ret && ret != -EALREADY)
158 printf("core %d start ret %d\n", core, ret);
160 for (s = 0; s < NUM_SERVICES; s++) {
161 if (rte_service_map_lcore_set(s, core,
162 p->cores[i].mapped_services[s]))
163 printf("failed to map lcore %d\n", core);
167 for ( ; i < PROFILE_CORES_MAX; i++) {
168 uint32_t core = i + core_off;
169 for (s = 0; s < NUM_SERVICES; s++) {
170 ret = rte_service_map_lcore_set(s, core, 0);
171 if (ret && ret != -EINVAL) {
172 printf("%s %d: map lcore set = %d\n", __func__,
176 ret = rte_service_lcore_stop(core);
177 if (ret && ret != -EALREADY) {
178 printf("%s %d: lcore stop = %d\n", __func__,
181 ret = rte_service_lcore_del(core);
182 if (ret && ret != -EINVAL) {
183 printf("%s %d: lcore del = %d\n", __func__,
190 main(int argc, char **argv)
194 ret = rte_eal_init(argc, argv);
196 rte_panic("Cannot init EAL\n");
199 for (i = 0; i < NUM_SERVICES; i++) {
200 services[i].callback_userdata = 0;
202 ret = rte_service_component_register(&services[i], &id);
204 rte_exit(-1, "service register() failed");
206 /* set the service itself to be ready to run. In the case of
207 * ethdev, eventdev etc PMDs, this will be set when the
208 * appropriate configure or setup function is called.
210 rte_service_component_runstate_set(id, 1);
212 /* Collect statistics for the service */
213 rte_service_set_stats_enable(id, 1);
215 /* the application sets the service to be active. Note that the
216 * previous component_runstate_set() is the PMD indicating
217 * ready, while this function is the application setting the
218 * service to run. Applications can choose to not run a service
219 * by setting runstate to 0 at any time.
221 ret = rte_service_runstate_set(id, 1);
228 const char clr[] = { 27, '[', '2', 'J', '\0' };
229 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
230 printf("%s%s", clr, topLeft);
233 printf("\n==> Profile: %s\n\n", profiles[i].name);
236 rte_service_dump(stdout, UINT32_MAX);
239 rte_service_dump(stdout, UINT32_MAX);
242 if (i >= NUM_PROFILES)