1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
5 #include <rte_common.h>
6 #include <rte_cycles.h>
7 #include <rte_pipeline.h>
9 #include "pipeline_common_be.h"
13 #if APP_THREAD_HEADROOM_STATS_COLLECT
15 #define PIPELINE_RUN_REGULAR(thread, pipeline) \
17 uint64_t t0 = rte_rdtsc_precise(); \
18 int n_pkts = rte_pipeline_run(pipeline->p); \
21 uint64_t t1 = rte_rdtsc_precise(); \
23 thread->headroom_cycles += t1 - t0; \
28 #define PIPELINE_RUN_CUSTOM(thread, data) \
30 uint64_t t0 = rte_rdtsc_precise(); \
31 int n_pkts = data->f_run(data->be); \
34 uint64_t t1 = rte_rdtsc_precise(); \
36 thread->headroom_cycles += t1 - t0; \
42 #define PIPELINE_RUN_REGULAR(thread, pipeline) \
43 rte_pipeline_run(pipeline->p)
45 #define PIPELINE_RUN_CUSTOM(thread, data) \
51 thread_msg_recv(struct rte_ring *r)
54 int status = rte_ring_sc_dequeue(r, &msg);
63 thread_msg_send(struct rte_ring *r,
69 status = rte_ring_sp_enqueue(r, msg);
70 } while (status == -ENOBUFS);
74 thread_pipeline_enable(struct app_thread_data *t,
75 struct thread_pipeline_enable_msg_req *req)
77 struct app_thread_pipeline_data *p;
79 if (req->f_run == NULL) {
80 if (t->n_regular >= APP_MAX_THREAD_PIPELINES)
83 if (t->n_custom >= APP_MAX_THREAD_PIPELINES)
87 p = (req->f_run == NULL) ?
88 &t->regular[t->n_regular] :
89 &t->custom[t->n_custom];
91 p->pipeline_id = req->pipeline_id;
93 p->f_run = req->f_run;
94 p->f_timer = req->f_timer;
95 p->timer_period = req->timer_period;
98 if (req->f_run == NULL)
107 thread_pipeline_disable(struct app_thread_data *t,
108 struct thread_pipeline_disable_msg_req *req)
110 uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
111 uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));
114 /* search regular pipelines of current thread */
115 for (i = 0; i < n_regular; i++) {
116 if (t->regular[i].pipeline_id != req->pipeline_id)
119 if (i < n_regular - 1)
120 memcpy(&t->regular[i],
122 (n_regular - 1 - i) * sizeof(struct app_thread_pipeline_data));
125 t->n_regular = n_regular;
130 /* search custom pipelines of current thread */
131 for (i = 0; i < n_custom; i++) {
132 if (t->custom[i].pipeline_id != req->pipeline_id)
135 if (i < n_custom - 1)
136 memcpy(&t->custom[i],
138 (n_custom - 1 - i) * sizeof(struct app_thread_pipeline_data));
141 t->n_custom = n_custom;
146 /* return if pipeline not found */
151 thread_msg_req_handle(struct app_thread_data *t)
154 struct thread_msg_req *req;
155 struct thread_msg_rsp *rsp;
157 msg_ptr = thread_msg_recv(t->msgq_in);
163 case THREAD_MSG_REQ_PIPELINE_ENABLE: {
164 rsp->status = thread_pipeline_enable(t,
165 (struct thread_pipeline_enable_msg_req *) req);
166 thread_msg_send(t->msgq_out, rsp);
170 case THREAD_MSG_REQ_PIPELINE_DISABLE: {
171 rsp->status = thread_pipeline_disable(t,
172 (struct thread_pipeline_disable_msg_req *) req);
173 thread_msg_send(t->msgq_out, rsp);
177 case THREAD_MSG_REQ_HEADROOM_READ: {
178 struct thread_headroom_read_msg_rsp *rsp =
179 (struct thread_headroom_read_msg_rsp *)
182 rsp->headroom_ratio = t->headroom_ratio;
184 thread_msg_send(t->msgq_out, rsp);
195 thread_headroom_update(struct app_thread_data *t, uint64_t time)
197 uint64_t time_diff = time - t->headroom_time;
200 ((double) t->headroom_cycles) / ((double) time_diff);
202 t->headroom_cycles = 0;
203 t->headroom_time = rte_rdtsc_precise();
207 app_thread(void *arg)
209 struct app_params *app = (struct app_params *) arg;
210 uint32_t core_id = rte_lcore_id(), i, j;
211 struct app_thread_data *t = &app->thread_data[core_id];
214 uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
215 uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));
217 /* Run regular pipelines */
218 for (j = 0; j < n_regular; j++) {
219 struct app_thread_pipeline_data *data = &t->regular[j];
220 struct pipeline *p = data->be;
222 PIPELINE_RUN_REGULAR(t, p);
225 /* Run custom pipelines */
226 for (j = 0; j < n_custom; j++) {
227 struct app_thread_pipeline_data *data = &t->custom[j];
229 PIPELINE_RUN_CUSTOM(t, data);
233 if ((i & 0xF) == 0) {
234 uint64_t time = rte_get_tsc_cycles();
235 uint64_t t_deadline = UINT64_MAX;
237 if (time < t->deadline)
240 /* Timer for regular pipelines */
241 for (j = 0; j < n_regular; j++) {
242 struct app_thread_pipeline_data *data =
244 uint64_t p_deadline = data->deadline;
246 if (p_deadline <= time) {
247 data->f_timer(data->be);
248 p_deadline = time + data->timer_period;
249 data->deadline = p_deadline;
252 if (p_deadline < t_deadline)
253 t_deadline = p_deadline;
256 /* Timer for custom pipelines */
257 for (j = 0; j < n_custom; j++) {
258 struct app_thread_pipeline_data *data =
260 uint64_t p_deadline = data->deadline;
262 if (p_deadline <= time) {
263 data->f_timer(data->be);
264 p_deadline = time + data->timer_period;
265 data->deadline = p_deadline;
268 if (p_deadline < t_deadline)
269 t_deadline = p_deadline;
272 /* Timer for thread message request */
274 uint64_t deadline = t->thread_req_deadline;
276 if (deadline <= time) {
277 thread_msg_req_handle(t);
278 thread_headroom_update(t, time);
279 deadline = time + t->timer_period;
280 t->thread_req_deadline = deadline;
283 if (deadline < t_deadline)
284 t_deadline = deadline;
288 t->deadline = t_deadline;