summaryrefslogtreecommitdiffstats
path: root/common/vnf_common/thread.c
blob: f6f1105f3781bf643495608cce1c73eb6cb763bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } 
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include <rte_common.h>
#include <rte_cycles.h>
#include <rte_pipeline.h>

#include "pipeline_common_be.h"
#include "app.h"
#include "thread.h"

#if APP_THREAD_HEADROOM_STATS_COLLECT

#define PIPELINE_RUN_REGULAR(thread, pipeline)		\
do {							\
	uint64_t t0 = rte_rdtsc_precise();		\
	int n_pkts = rte_pipeline_run(pipeline->p);	\
							\
	if (n_pkts == 0) {				\
		uint64_t t1 = rte_rdtsc_precise();	\
							\
		thread->headroom_cycles += t1 - t0;	\
	}						\
} while (0)


#define PIPELINE_RUN_CUSTOM(thread, data)		\
do {							\
	uint64_t t0 = rte_rdtsc_precise();		\
	int n_pkts = data->f_run(data->be);		\
							\
	if (n_pkts == 0) {				\
		uint64_t t1 = rte_rdtsc_precise();	\
							\
		thread->headroom_cycles += t1 - t0;	\
	}						\
} while (0)

#else

#define PIPELINE_RUN_REGULAR(thread, pipeline)		\
	rte_pipeline_run(pipeline->p)

#define PIPELINE_RUN_CUSTOM(thread, data)		\
	data->f_run(data->be)

#endif

uint32_t exit_app_thread = 0;

static inline void *
thread_msg_recv(struct rte_ring *r)
{
	void *msg;
	int status = rte_ring_sc_dequeue(r, &msg);

	if (status != 0)
		return NULL;

	return msg;
}

static inline void
thread_msg_send(struct rte_ring *r,
	void *msg)
{
	int status;

	do {
		status = rte_ring_sp_enqueue(r, msg);
	} while (status == -ENOBUFS);
}

static int
thread_pipeline_enable(struct app_thread_data *t,
		struct thread_pipeline_enable_msg_req *req)
{
	struct app_thread_pipeline_data *p;

	if (req->f_run == NULL) {
		if (t->n_regular >= APP_MAX_THREAD_PIPELINES)
			return -1;
	} else {
		if (t->n_custom >= APP_MAX_THREAD_PIPELINES)
			return -1;
	}

	p = (req->f_run == NULL) ?
		&t->regular[t->n_regular] :
		&t->custom[t->n_custom];

	p->pipeline_id = req->pipeline_id;
	p->be = req->be;
	p->f_run = req->f_run;
	p->f_timer = req->f_timer;
	p->timer_period = req->timer_period;
	p->deadline = 0;

	if (req->f_run == NULL)
		t->n_regular++;
	else
		t->n_custom++;

	return 0;
}

static int
thread_pipeline_disable(struct app_thread_data *t,
		struct thread_pipeline_disable_msg_req *req)
{
	uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
	uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));
	uint32_t i;

	/* search regular pipelines of current thread */
	for (i = 0; i < n_regular; i++) {
		if (t->regular[i].pipeline_id != req->pipeline_id)
			continue;

		if (i < n_regular - 1)
			memcpy(&t->regular[i],
				&t->regular[i+1],
				(n_regular - 1 - i) * sizeof(struct app_thread_pipeline_data));

		n_regular--;
		t->n_regular = n_regular;

		return 0;
	}

	/* search custom pipelines of current thread */
	for (i = 0; i < n_custom; i++) {
		if (t->custom[i].pipeline_id != req->pipeline_id)
			continue;

		if (i < n_custom - 1)
			memcpy(&t->custom[i],
				&t->custom[i+1],
				(n_custom - 1 - i) * sizeof(struct app_thread_pipeline_data));

		n_custom--;
		t->n_custom = n_custom;

		return 0;
	}

	/* return if pipeline not found */
	return -1;
}

static int
thread_msg_req_handle(struct app_thread_data *t)
{
	void *msg_ptr;
	struct thread_msg_req *req;
	struct thread_msg_rsp *rsp;

	msg_ptr = thread_msg_recv(t->msgq_in);
	req = msg_ptr;
	rsp = msg_ptr;

	if (req != NULL)
		switch (req->type) {
		case THREAD_MSG_REQ_PIPELINE_ENABLE: {
			rsp->status = thread_pipeline_enable(t,
					(struct thread_pipeline_enable_msg_req *) req);
			thread_msg_send(t->msgq_out, rsp);
			break;
		}

		case THREAD_MSG_REQ_PIPELINE_DISABLE: {
			rsp->status = thread_pipeline_disable(t,
					(struct thread_pipeline_disable_msg_req *) req);
			thread_msg_send(t->msgq_out, rsp);
			break;
		}

		case THREAD_MSG_REQ_HEADROOM_READ: {
			struct thread_headroom_read_msg_rsp *rsp =
				(struct thread_headroom_read_msg_rsp *)
				req;

			rsp->headroom_ratio = t->headroom_ratio;
			rsp->status = 0;
			thread_msg_send(t->msgq_out, rsp);
			break;
		}
		default:
			break;
		}

	return 0;
}

static void
thread_headroom_update(struct app_thread_data *t, uint64_t time)
{
	uint64_t time_diff = time - t->headroom_time;

	t->headroom_ratio =
		((double) t->headroom_cycles) / ((double) time_diff);

	t->headroom_cycles = 0;
	t->headroom_time = rte_rdtsc_precise();
}

int
app_thread(void *arg)
{
	struct app_params *app = (struct app_params *) arg;
	uint32_t core_id = rte_lcore_id(), i, j;
	struct app_thread_data *t = &app->thread_data[core_id];

	for (i = 0; ; i++) {
		uint32_t n_regular = RTE_MIN(t->n_regular, RTE_DIM(t->regular));
		uint32_t n_custom = RTE_MIN(t->n_custom, RTE_DIM(t->custom));

		if (exit_app_thread)
			break;

		/* Run regular pipelines */
		for (j = 0; j < n_regular; j++) {
			struct app_thread_pipeline_data *data = &t->regular[j];
			struct pipeline *p = data->be;

			PIPELINE_RUN_REGULAR(t, p);
		}

		/* Run custom pipelines */
		for (j = 0; j < n_custom; j++) {
			struct app_thread_pipeline_data *data = &t->custom[j];

			PIPELINE_RUN_CUSTOM(t, data);
		}

		/* Timer */
		if ((i & 0xF) == 0) {
			uint64_t time = rte_get_tsc_cycles();
			uint64_t t_deadline = UINT64_MAX;

			if (time < t->deadline)
				continue;

			/* Timer for regular pipelines */
			for (j = 0; j < n_regular; j++) {
				struct app_thread_pipeline_data *data =
					&t->regular[j];
				uint64_t p_deadline = data->deadline;

				if (p_deadline <= time) {
					data->f_timer(data->be);
					p_deadline = time + data->timer_period;
					data->deadline = p_deadline;
				}

				if (p_deadline < t_deadline)
					t_deadline = p_deadline;
			}

			/* Timer for custom pipelines */
			for (j = 0; j < n_custom; j++) {
				struct app_thread_pipeline_data *data =
					&t->custom[j];
				uint64_t p_deadline = data->deadline;

				if (p_deadline <= time) {
					data->f_timer(data->be);
					p_deadline = time + data->timer_period;
					data->deadline = p_deadline;
				}

				if (p_deadline < t_deadline)
					t_deadline = p_deadline;
			}

			/* Timer for thread message request */
			{
				uint64_t deadline = t->thread_req_deadline;

				if (deadline <= time) {
					thread_msg_req_handle(t);
					thread_headroom_update(t, time);
					deadline = time + t->timer_period;
					t->thread_req_deadline = deadline;
				}

				if (deadline < t_deadline)
					t_deadline = deadline;
			}


			t->deadline = t_deadline;
		}
	}

	return 0;
}