4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <rte_common.h>
35 #include <rte_cycles.h>
36 #include <rte_pipeline.h>
38 #include "pipeline_common_be.h"
42 #if APP_THREAD_HEADROOM_STATS_COLLECT
44 #define PIPELINE_RUN_REGULAR(thread, pipeline) \
46 uint64_t t0 = rte_rdtsc_precise(); \
47 int n_pkts = rte_pipeline_run(pipeline->p); \
50 uint64_t t1 = rte_rdtsc_precise(); \
52 thread->headroom_cycles += t1 - t0; \
57 #define PIPELINE_RUN_CUSTOM(thread, data) \
59 uint64_t t0 = rte_rdtsc_precise(); \
60 int n_pkts = data->f_run(data->be); \
63 uint64_t t1 = rte_rdtsc_precise(); \
65 thread->headroom_cycles += t1 - t0; \
71 #define PIPELINE_RUN_REGULAR(thread, pipeline) \
72 rte_pipeline_run(pipeline->p)
74 #define PIPELINE_RUN_CUSTOM(thread, data) \
80 thread_msg_recv(struct rte_ring *r)
83 int status = rte_ring_sc_dequeue(r, &msg);
92 thread_msg_send(struct rte_ring *r,
98 status = rte_ring_sp_enqueue(r, msg);
99 } while (status == -ENOBUFS);
103 thread_pipeline_enable(struct app_thread_data *t,
104 struct thread_pipeline_enable_msg_req *req)
106 struct app_thread_pipeline_data *p;
108 if (req->f_run == NULL) {
109 if (t->n_regular >= APP_MAX_THREAD_PIPELINES)
112 if (t->n_custom >= APP_MAX_THREAD_PIPELINES)
116 p = (req->f_run == NULL) ?
117 &t->regular[t->n_regular] :
118 &t->custom[t->n_custom];
120 p->pipeline_id = req->pipeline_id;
122 p->f_run = req->f_run;
123 p->f_timer = req->f_timer;
124 p->timer_period = req->timer_period;
127 if (req->f_run == NULL)
136 thread_pipeline_disable(struct app_thread_data *t,
137 struct thread_pipeline_disable_msg_req *req)
139 uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
140 uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));
143 /* search regular pipelines of current thread */
144 for (i = 0; i < n_regular; i++) {
145 if (t->regular[i].pipeline_id != req->pipeline_id)
148 if (i < n_regular - 1)
149 memcpy(&t->regular[i],
151 (n_regular - 1 - i) * sizeof(struct app_thread_pipeline_data));
154 t->n_regular = n_regular;
159 /* search custom pipelines of current thread */
160 for (i = 0; i < n_custom; i++) {
161 if (t->custom[i].pipeline_id != req->pipeline_id)
164 if (i < n_custom - 1)
165 memcpy(&t->custom[i],
167 (n_custom - 1 - i) * sizeof(struct app_thread_pipeline_data));
170 t->n_custom = n_custom;
175 /* return if pipeline not found */
180 thread_msg_req_handle(struct app_thread_data *t)
183 struct thread_msg_req *req;
184 struct thread_msg_rsp *rsp;
186 msg_ptr = thread_msg_recv(t->msgq_in);
192 case THREAD_MSG_REQ_PIPELINE_ENABLE: {
193 rsp->status = thread_pipeline_enable(t,
194 (struct thread_pipeline_enable_msg_req *) req);
195 thread_msg_send(t->msgq_out, rsp);
199 case THREAD_MSG_REQ_PIPELINE_DISABLE: {
200 rsp->status = thread_pipeline_disable(t,
201 (struct thread_pipeline_disable_msg_req *) req);
202 thread_msg_send(t->msgq_out, rsp);
206 case THREAD_MSG_REQ_HEADROOM_READ: {
207 struct thread_headroom_read_msg_rsp *rsp =
208 (struct thread_headroom_read_msg_rsp *)
211 rsp->headroom_ratio = t->headroom_ratio;
213 thread_msg_send(t->msgq_out, rsp);
224 thread_headroom_update(struct app_thread_data *t, uint64_t time)
226 uint64_t time_diff = time - t->headroom_time;
229 ((double) t->headroom_cycles) / ((double) time_diff);
231 t->headroom_cycles = 0;
232 t->headroom_time = rte_rdtsc_precise();
236 app_thread(void *arg)
238 struct app_params *app = (struct app_params *) arg;
239 uint32_t core_id = rte_lcore_id(), i, j;
240 struct app_thread_data *t = &app->thread_data[core_id];
243 uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
244 uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));
246 /* Run regular pipelines */
247 for (j = 0; j < n_regular; j++) {
248 struct app_thread_pipeline_data *data = &t->regular[j];
249 struct pipeline *p = data->be;
251 PIPELINE_RUN_REGULAR(t, p);
254 /* Run custom pipelines */
255 for (j = 0; j < n_custom; j++) {
256 struct app_thread_pipeline_data *data = &t->custom[j];
258 PIPELINE_RUN_CUSTOM(t, data);
262 if ((i & 0xF) == 0) {
263 uint64_t time = rte_get_tsc_cycles();
264 uint64_t t_deadline = UINT64_MAX;
266 if (time < t->deadline)
269 /* Timer for regular pipelines */
270 for (j = 0; j < n_regular; j++) {
271 struct app_thread_pipeline_data *data =
273 uint64_t p_deadline = data->deadline;
275 if (p_deadline <= time) {
276 data->f_timer(data->be);
277 p_deadline = time + data->timer_period;
278 data->deadline = p_deadline;
281 if (p_deadline < t_deadline)
282 t_deadline = p_deadline;
285 /* Timer for custom pipelines */
286 for (j = 0; j < n_custom; j++) {
287 struct app_thread_pipeline_data *data =
289 uint64_t p_deadline = data->deadline;
291 if (p_deadline <= time) {
292 data->f_timer(data->be);
293 p_deadline = time + data->timer_period;
294 data->deadline = p_deadline;
297 if (p_deadline < t_deadline)
298 t_deadline = p_deadline;
301 /* Timer for thread message request */
303 uint64_t deadline = t->thread_req_deadline;
305 if (deadline <= time) {
306 thread_msg_req_handle(t);
307 thread_headroom_update(t, time);
308 deadline = time + t->timer_period;
309 t->thread_req_deadline = deadline;
312 if (deadline < t_deadline)
313 t_deadline = deadline;
317 t->deadline = t_deadline;