xref: /dpdk/examples/ip_pipeline/thread.c (revision c4bcc342c8ee37b4692e79e7fac816df4f55d8ec)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 
34 #include <rte_common.h>
35 #include <rte_cycles.h>
36 #include <rte_pipeline.h>
37 
38 #include "pipeline_common_be.h"
39 #include "app.h"
40 
41 int app_thread(void *arg)
42 {
43 	struct app_params *app = (struct app_params *) arg;
44 	uint32_t core_id = rte_lcore_id(), i, j;
45 	struct app_thread_data *t = &app->thread_data[core_id];
46 	uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
47 	uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));
48 
49 	for (i = 0; ; i++) {
50 		/* Run regular pipelines */
51 		for (j = 0; j < n_regular; j++) {
52 			struct app_thread_pipeline_data *data = &t->regular[j];
53 			struct pipeline *p = data->be;
54 
55 			rte_pipeline_run(p->p);
56 		}
57 
58 		/* Run custom pipelines */
59 		for (j = 0; j < n_custom; j++) {
60 			struct app_thread_pipeline_data *data = &t->custom[j];
61 
62 			data->f_run(data->be);
63 		}
64 
65 		/* Timer */
66 		if ((i & 0xF) == 0) {
67 			uint64_t time = rte_get_tsc_cycles();
68 			uint64_t t_deadline = UINT64_MAX;
69 
70 			if (time < t->deadline)
71 				continue;
72 
73 			/* Timer for regular pipelines */
74 			for (j = 0; j < n_regular; j++) {
75 				struct app_thread_pipeline_data *data =
76 					&t->regular[j];
77 				uint64_t p_deadline = data->deadline;
78 
79 				if (p_deadline <= time) {
80 					data->f_timer(data->be);
81 					p_deadline = time + data->timer_period;
82 					data->deadline = p_deadline;
83 				}
84 
85 				if (p_deadline < t_deadline)
86 					t_deadline = p_deadline;
87 			}
88 
89 			/* Timer for custom pipelines */
90 			for (j = 0; j < n_custom; j++) {
91 				struct app_thread_pipeline_data *data =
92 					&t->custom[j];
93 				uint64_t p_deadline = data->deadline;
94 
95 				if (p_deadline <= time) {
96 					data->f_timer(data->be);
97 					p_deadline = time + data->timer_period;
98 					data->deadline = p_deadline;
99 				}
100 
101 				if (p_deadline < t_deadline)
102 					t_deadline = p_deadline;
103 			}
104 
105 			t->deadline = t_deadline;
106 		}
107 	}
108 
109 	return 0;
110 }
111