forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.c
235 lines (198 loc) · 6.64 KB
/
scheduler.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <assert.h>
#include "conf/common.h"
#include "scheduler.h"
/* Note: lockless, lcore_job can only be register on initialization stage and
* unregistered on cleanup stage.
*/
static struct list_head dpvs_lcore_jobs[LCORE_ROLE_MAX][LCORE_JOB_TYPE_MAX];
struct dpvs_role_str {
dpvs_lcore_role_t role;
const char *str;
};
const char *dpvs_lcore_role_str(dpvs_lcore_role_t role)
{
int i;
const static struct dpvs_role_str role_str_tab[] = {
{ LCORE_ROLE_IDLE, "lcore_role_idle" },
{ LCORE_ROLE_MASTER, "lcore_role_master" },
{ LCORE_ROLE_FWD_WORKER, "lcore_role_fwd_worker"},
{ LCORE_ROLE_ISOLRX_WORKER, "lcore_role_isolrx_worker"},
{ LCORE_ROLE_MAX, "lcore_role_null"},
};
for (i = 0; i < NELEMS(role_str_tab); i++) {
if (role == role_str_tab[i].role)
return role_str_tab[i].str;
}
return "lcore_role_unkown";
}
int dpvs_scheduler_init(void)
{
int ii, jj;
for (ii = 0; ii < LCORE_ROLE_MAX; ii++) {
for (jj = 0; jj < LCORE_JOB_TYPE_MAX; jj++) {
INIT_LIST_HEAD(&dpvs_lcore_jobs[ii][jj]);
}
}
return EDPVS_OK;
}
int dpvs_scheduler_term(void)
{
return EDPVS_OK;
}
int dpvs_lcore_job_register(struct dpvs_lcore_job *lcore_job, dpvs_lcore_role_t role)
{
struct dpvs_lcore_job *cur;
if (unlikely(NULL == lcore_job || role >= LCORE_ROLE_MAX))
return EDPVS_INVAL;
if (unlikely(LCORE_JOB_SLOW == lcore_job->type && lcore_job->skip_loops <= 0))
return EDPVS_INVAL;
list_for_each_entry(cur, &dpvs_lcore_jobs[role][lcore_job->type], list) {
if (cur == lcore_job) {
return EDPVS_EXIST;
}
}
list_add_tail(&lcore_job->list, &dpvs_lcore_jobs[role][lcore_job->type]);
return EDPVS_OK;
}
int dpvs_lcore_job_unregister(struct dpvs_lcore_job *lcore_job, dpvs_lcore_role_t role)
{
struct dpvs_lcore_job *cur;
if (unlikely(NULL == lcore_job || role >= LCORE_ROLE_MAX))
return EDPVS_INVAL;
list_for_each_entry(cur, &dpvs_lcore_jobs[role][lcore_job->type], list) {
if (cur == lcore_job) {
list_del_init(&cur->list);
return EDPVS_OK;
}
}
return EDPVS_NOTEXIST;
}
#ifdef CONFIG_RECORD_BIG_LOOP
static void print_job_time(char *buf, size_t len, dpvs_lcore_role_t role)
{
int ii, jj;
size_t pos = 0;
struct dpvs_lcore_job *job;
lcoreid_t cid;
assert(buf);
buf[0] = '\0';
cid = rte_lcore_id();
if (role < LCORE_ROLE_MAX) {
for (jj = 0; jj < LCORE_JOB_TYPE_MAX; jj++) {
list_for_each_entry(job, &dpvs_lcore_jobs[role][jj], list) {
if (unlikely(pos + 1 >= len))
return;
snprintf(buf + pos, len - pos -1, "%s=%d ",
job->name, job->job_time[cid]);
pos = strlen(buf);
}
}
return;
}
for (ii = 0; ii < LCORE_ROLE_MAX; ii++) {
for (jj = 0; jj < LCORE_JOB_TYPE_MAX; jj++) {
list_for_each_entry(job, &dpvs_lcore_jobs[ii][jj], list) {
if (unlikely(pos + 1 >= len))
return;
snprintf(buf + pos, len - pos - 1, "%s=%d ",
job->name, job->job_time[cid]);
pos = strlen(buf);
}
}
}
}
#endif
static inline void do_lcore_job(struct dpvs_lcore_job *job)
{
#ifdef CONFIG_RECORD_BIG_LOOP
uint64_t job_start, job_end;
job_start = rte_get_timer_cycles();
#endif
job->func(job->data);
#ifdef CONFIG_RECORD_BIG_LOOP
job_end = rte_get_timer_cycles();
job->job_time[rte_lcore_id()] = (job_end - job_start) * 1000000 / g_cycles_per_sec;
#endif
}
static int dpvs_job_loop(void *arg)
{
struct dpvs_lcore_job *job;
lcoreid_t cid = rte_lcore_id();
dpvs_lcore_role_t role = g_lcore_role[cid];
static uint32_t dpvs_job_loop_tick[DPVS_MAX_LCORE] = { 0 };
#ifdef CONFIG_RECORD_BIG_LOOP
char buf[512];
uint32_t loop_time, thres_time;
uint64_t loop_start, loop_end;
static uint32_t longest_lcore_loop[DPVS_MAX_LCORE] = { 0 };
if (likely(role != LCORE_ROLE_MASTER))
thres_time = BIG_LOOP_THRESH_SLAVE;
else
thres_time = BIG_LOOP_THRESH_MASTER;
#endif
/* skip irrelative job loops */
if (role == LCORE_ROLE_MAX)
return EDPVS_INVAL;
if (role == LCORE_ROLE_IDLE)
return EDPVS_IDLE;
RTE_LOG(INFO, DSCHED, "lcore %02d enter %s loop\n", cid, dpvs_lcore_role_str(role));
/* do init job */
list_for_each_entry(job, &dpvs_lcore_jobs[role][LCORE_JOB_INIT], list) {
do_lcore_job(job);
}
while (1) {
#ifdef CONFIG_RECORD_BIG_LOOP
loop_start = rte_get_timer_cycles();
#endif
++dpvs_job_loop_tick[cid];
netif_update_worker_loop_cnt();
/* do normal job */
list_for_each_entry(job, &dpvs_lcore_jobs[role][LCORE_JOB_LOOP], list) {
do_lcore_job(job);
}
/* do slow job */
list_for_each_entry(job, &dpvs_lcore_jobs[role][LCORE_JOB_SLOW], list) {
if (dpvs_job_loop_tick[cid] % job->skip_loops == 0) {
do_lcore_job(job);
}
}
#ifdef CONFIG_RECORD_BIG_LOOP
loop_end = rte_get_timer_cycles();
loop_time = (loop_end - loop_start) * 1000000 / g_cycles_per_sec;
if (loop_time > longest_lcore_loop[cid]) {
RTE_LOG(WARNING, DSCHED, "update longest_lcore_loop[%d] = %d (<- %d)\n",
cid, loop_time, longest_lcore_loop[cid]);
longest_lcore_loop[cid] = loop_time;
}
if (loop_time > thres_time) {
print_job_time(buf, sizeof(buf), role);
RTE_LOG(WARNING, DSCHED, "lcore[%d] loop over %d usecs (actual=%d, max=%d):\n%s\n",
cid, thres_time, loop_time, longest_lcore_loop[cid], buf);
}
#endif
}
return EDPVS_OK;
}
int dpvs_lcore_start(int is_master)
{
if (is_master)
return dpvs_job_loop(NULL);
return rte_eal_mp_remote_launch(dpvs_job_loop, NULL, SKIP_MASTER);
}