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>
13 thread_msg_recv(struct rte_ring *r)
16 int status = rte_ring_sc_dequeue(r, &msg);
25 thread_msg_send(struct rte_ring *r,
31 status = rte_ring_sp_enqueue(r, msg);
32 } while (status == -ENOBUFS);
36 thread_pipeline_enable(struct app_thread_data *t,
37 struct thread_pipeline_enable_msg_req *req)
39 struct app_thread_pipeline_data *p;
41 if (req->f_run == NULL) {
42 if (t->n_regular >= APP_MAX_THREAD_PIPELINES)
45 if (t->n_custom >= APP_MAX_THREAD_PIPELINES)
49 p = (req->f_run == NULL) ?
50 &t->regular[t->n_regular] :
51 &t->custom[t->n_custom];
53 p->pipeline_id = req->pipeline_id;
55 p->f_run = req->f_run;
56 p->f_timer = req->f_timer;
57 p->timer_period = req->timer_period;
60 if (req->f_run == NULL)
69 thread_pipeline_disable(struct app_thread_data *t,
70 struct thread_pipeline_disable_msg_req *req)
72 uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
73 uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));
76 /* search regular pipelines of current thread */
77 for (i = 0; i < n_regular; i++) {
78 if (t->regular[i].pipeline_id != req->pipeline_id)
81 if (i < n_regular - 1)
82 memcpy(&t->regular[i],
84 (n_regular - 1 - i) * sizeof(struct app_thread_pipeline_data));
87 t->n_regular = n_regular;
92 /* search custom pipelines of current thread */
93 for (i = 0; i < n_custom; i++) {
94 if (t->custom[i].pipeline_id != req->pipeline_id)
100 (n_custom - 1 - i) * sizeof(struct app_thread_pipeline_data));
103 t->n_custom = n_custom;
108 /* return if pipeline not found */
113 thread_msg_req_handle(struct app_thread_data *t)
116 struct thread_msg_req *req;
117 struct thread_msg_rsp *rsp;
119 msg_ptr = thread_msg_recv(t->msgq_in);
125 case THREAD_MSG_REQ_PIPELINE_ENABLE: {
126 rsp->status = thread_pipeline_enable(t,
127 (struct thread_pipeline_enable_msg_req *) req);
128 thread_msg_send(t->msgq_out, rsp);
132 case THREAD_MSG_REQ_PIPELINE_DISABLE: {
133 rsp->status = thread_pipeline_disable(t,
134 (struct thread_pipeline_disable_msg_req *) req);
135 thread_msg_send(t->msgq_out, rsp);
139 case THREAD_MSG_REQ_HEADROOM_READ: {
140 struct thread_headroom_read_msg_rsp *rsp =
141 (struct thread_headroom_read_msg_rsp *)
144 rsp->headroom_ratio = t->headroom_ratio;
146 thread_msg_send(t->msgq_out, rsp);
157 thread_headroom_update(struct app_thread_data *t, uint64_t time)
159 uint64_t time_diff = time - t->headroom_time;
162 ((double) t->headroom_cycles) / ((double) time_diff);
164 t->headroom_cycles = 0;
165 t->headroom_time = rte_rdtsc_precise();
169 app_thread(void *arg)
171 struct app_params *app = (struct app_params *) arg;
172 uint32_t core_id = rte_lcore_id(), i, j;
173 struct app_thread_data *t = &app->thread_data[core_id];
176 uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
177 uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));
180 if ((i & 0xF) == 0) {
181 uint64_t time = rte_get_tsc_cycles();
182 uint64_t t_deadline = UINT64_MAX;
184 if (time < t->deadline)
187 /* Timer for regular pipelines */
188 for (j = 0; j < n_regular; j++) {
189 struct app_thread_pipeline_data *data =
191 uint64_t p_deadline = data->deadline;
193 if (p_deadline <= time) {
194 data->f_timer(data->be);
195 p_deadline = time + data->timer_period;
196 data->deadline = p_deadline;
199 if (p_deadline < t_deadline)
200 t_deadline = p_deadline;
203 /* Timer for custom pipelines */
204 for (j = 0; j < n_custom; j++) {
205 struct app_thread_pipeline_data *data =
207 uint64_t p_deadline = data->deadline;
209 if (p_deadline <= time) {
210 data->f_timer(data->be);
211 p_deadline = time + data->timer_period;
212 data->deadline = p_deadline;
215 if (p_deadline < t_deadline)
216 t_deadline = p_deadline;
219 /* Timer for thread message request */
221 uint64_t deadline = t->thread_req_deadline;
223 if (deadline <= time) {
224 thread_msg_req_handle(t);
225 thread_headroom_update(t, time);
226 deadline = time + t->timer_period;
227 t->thread_req_deadline = deadline;
230 if (deadline < t_deadline)
231 t_deadline = deadline;
235 t->deadline = t_deadline;