1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
10 #include <sys/queue.h>
12 #include <rte_memory.h>
13 #include <rte_launch.h>
15 #include <rte_debug.h>
16 #include <rte_cycles.h>
18 /* allow application scheduling of the services */
19 #include <rte_service.h>
21 /* Allow application registration of its own services. An application does not
22 * have to register services, but it can be useful if it wishes to run a
23 * function on a core that is otherwise in use as a service core. In this
24 * example, all services are dummy services registered by the sample app itself.
26 #include <rte_service_component.h>
28 #define PROFILE_CORES_MAX 8
29 #define PROFILE_SERVICE_PER_CORE 5
31 /* dummy function to do "work" */
32 static int32_t service_func(void *args)
39 static struct rte_service_spec services[] = {
40 {"service_1", service_func, NULL, 0, 0},
41 {"service_2", service_func, NULL, 0, 0},
42 {"service_3", service_func, NULL, 0, 0},
43 {"service_4", service_func, NULL, 0, 0},
44 {"service_5", service_func, NULL, 0, 0},
46 #define NUM_SERVICES RTE_DIM(services)
48 /* this struct holds the mapping of a particular core to all services */
49 struct profile_for_core {
50 uint32_t mapped_services[PROFILE_SERVICE_PER_CORE];
53 /* struct that can be applied as the service core mapping. Items in this
54 * struct will be passed to the ordinary rte_service_* APIs to configure the
55 * service cores at runtime, based on the requirements.
57 * These profiles can be considered a "configuration" for the service cores,
58 * where switching profile just changes the number of cores and the mappings
59 * for each of them. As a result, the core requirements and performance of the
65 struct profile_for_core cores[PROFILE_CORES_MAX];
68 static struct profile profiles[] = {
69 /* profile 0: high performance */
71 .name = "High Performance",
73 .cores[0] = {.mapped_services = {1, 0, 0, 0, 0} },
74 .cores[1] = {.mapped_services = {0, 1, 0, 0, 0} },
75 .cores[2] = {.mapped_services = {0, 0, 1, 0, 0} },
76 .cores[3] = {.mapped_services = {0, 0, 0, 1, 0} },
77 .cores[4] = {.mapped_services = {0, 0, 0, 0, 1} },
79 /* profile 1: mid performance with single service priority */
81 .name = "Mid-High Performance",
83 .cores[0] = {.mapped_services = {1, 1, 0, 0, 0} },
84 .cores[1] = {.mapped_services = {0, 0, 1, 1, 0} },
85 .cores[2] = {.mapped_services = {0, 0, 0, 0, 1} },
86 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
87 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
89 /* profile 2: mid performance with single service priority */
91 .name = "Mid-Low Performance",
93 .cores[0] = {.mapped_services = {1, 1, 1, 0, 0} },
94 .cores[1] = {.mapped_services = {1, 1, 0, 1, 1} },
95 .cores[2] = {.mapped_services = {0, 0, 0, 0, 0} },
96 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
97 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
99 /* profile 3: scale down performance on single core */
101 .name = "Scale down performance",
103 .cores[0] = {.mapped_services = {1, 1, 1, 1, 1} },
104 .cores[1] = {.mapped_services = {0, 0, 0, 0, 0} },
105 .cores[2] = {.mapped_services = {0, 0, 0, 0, 0} },
106 .cores[3] = {.mapped_services = {0, 0, 0, 0, 0} },
107 .cores[4] = {.mapped_services = {0, 0, 0, 0, 0} },
110 #define NUM_PROFILES RTE_DIM(profiles)
113 apply_profile(int profile_id)
118 struct profile *p = &profiles[profile_id];
119 const uint8_t core_off = 1;
121 for (i = 0; i < p->num_cores; i++) {
122 uint32_t core = i + core_off;
123 ret = rte_service_lcore_add(core);
124 if (ret && ret != -EALREADY)
125 printf("core %d added ret %d\n", core, ret);
127 ret = rte_service_lcore_start(core);
128 if (ret && ret != -EALREADY)
129 printf("core %d start ret %d\n", core, ret);
131 for (s = 0; s < NUM_SERVICES; s++) {
132 if (rte_service_map_lcore_set(s, core,
133 p->cores[i].mapped_services[s]))
134 printf("failed to map lcore %d\n", core);
138 for ( ; i < PROFILE_CORES_MAX; i++) {
139 uint32_t core = i + core_off;
140 for (s = 0; s < NUM_SERVICES; s++) {
141 ret = rte_service_map_lcore_set(s, core, 0);
142 if (ret && ret != -EINVAL) {
143 printf("%s %d: map lcore set = %d\n", __func__,
147 ret = rte_service_lcore_stop(core);
148 if (ret && ret != -EALREADY) {
149 printf("%s %d: lcore stop = %d\n", __func__,
152 ret = rte_service_lcore_del(core);
153 if (ret && ret != -EINVAL) {
154 printf("%s %d: lcore del = %d\n", __func__,
161 main(int argc, char **argv)
165 ret = rte_eal_init(argc, argv);
167 rte_panic("Cannot init EAL\n");
170 for (i = 0; i < NUM_SERVICES; i++) {
171 services[i].callback_userdata = 0;
173 ret = rte_service_component_register(&services[i], &id);
175 rte_exit(-1, "service register() failed");
177 /* set the service itself to be ready to run. In the case of
178 * ethdev, eventdev etc PMDs, this will be set when the
179 * appropriate configure or setup function is called.
181 rte_service_component_runstate_set(id, 1);
183 /* Collect statistics for the service */
184 rte_service_set_stats_enable(id, 1);
186 /* the application sets the service to be active. Note that the
187 * previous component_runstate_set() is the PMD indicating
188 * ready, while this function is the application setting the
189 * service to run. Applications can choose to not run a service
190 * by setting runstate to 0 at any time.
192 ret = rte_service_runstate_set(id, 1);
199 const char clr[] = { 27, '[', '2', 'J', '\0' };
200 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
201 printf("%s%s", clr, topLeft);
204 printf("\n==> Profile: %s\n\n", profiles[i].name);
207 rte_service_dump(stdout, UINT32_MAX);
210 rte_service_dump(stdout, UINT32_MAX);
213 if (i >= NUM_PROFILES)