-
Notifications
You must be signed in to change notification settings - Fork 909
/
async_task.c
472 lines (409 loc) · 9.95 KB
/
async_task.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Copyright (C) 2014 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*!
* \file
* \brief Kamailio core :: Asynchronus tasks
* \ingroup core
* Module: \ref core
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include "dprint.h"
#include "sr_module.h"
#include "ut.h"
#include "pt.h"
#include "cfg/cfg_struct.h"
#include "parser/parse_param.h"
#include "async_task.h"
static async_wgroup_t *_async_wgroup_list = NULL;
int async_task_run(async_wgroup_t *awg, int idx);
/**
*
*/
int async_task_workers_get(void)
{
return (_async_wgroup_list)?_async_wgroup_list->workers:0;
}
/**
*
*/
int async_task_workers_active(void)
{
if(_async_wgroup_list==NULL || _async_wgroup_list->workers<=0)
return 0;
return 1;
}
/**
*
*/
int async_task_init_sockets(void)
{
int val;
async_wgroup_t *awg;
for(awg=_async_wgroup_list; awg!=NULL; awg=awg->next) {
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, awg->sockets) < 0) {
LM_ERR("opening tasks dgram socket pair\n");
return -1;
}
if (awg->nonblock) {
val = fcntl(awg->sockets[1], F_GETFL, 0);
if(val<0) {
LM_WARN("failed to get socket flags\n");
} else {
if(fcntl(awg->sockets[1], F_SETFL, val | O_NONBLOCK)<0) {
LM_WARN("failed to set socket nonblock flag\n");
}
}
}
}
LM_DBG("inter-process event notification sockets initialized\n");
return 0;
}
/**
*
*/
void async_task_close_sockets_child(void)
{
async_wgroup_t *awg;
LM_DBG("closing the notification socket used by children\n");
for(awg=_async_wgroup_list; awg!=NULL; awg=awg->next) {
close(awg->sockets[1]);
}
}
/**
*
*/
void async_task_close_sockets_parent(void)
{
async_wgroup_t *awg;
LM_DBG("closing the notification socket used by parent\n");
for(awg=_async_wgroup_list; awg!=NULL; awg=awg->next) {
close(awg->sockets[0]);
}
}
/**
*
*/
int async_task_init(void)
{
int nrg = 0;
async_wgroup_t *awg;
LM_DBG("start initializing asynk task framework\n");
if(_async_wgroup_list==NULL || _async_wgroup_list->workers<=0)
return 0;
/* overall number of processes */
for(awg=_async_wgroup_list; awg!=NULL; awg=awg->next) {
nrg += awg->workers;
}
/* advertise new processes to core */
register_procs(nrg);
/* advertise new processes to cfg framework */
cfg_register_child(nrg);
return 0;
}
/**
*
*/
int async_task_initialized(void)
{
if(_async_wgroup_list==NULL || _async_wgroup_list->workers<=0)
return 0;
return 1;
}
/**
*
*/
int async_task_child_init(int rank)
{
int pid;
int i;
char pname[64];
async_wgroup_t *awg;
if(_async_wgroup_list==NULL || _async_wgroup_list->workers<=0)
return 0;
LM_DBG("child initializing asynk task framework\n");
if (rank==PROC_INIT) {
if(async_task_init_sockets()<0) {
LM_ERR("failed to initialize tasks sockets\n");
return -1;
}
return 0;
}
if(rank>0) {
/* no need to close the socket from sip workers */
/* async_task_close_sockets_parent(); */
return 0;
}
if (rank!=PROC_MAIN)
return 0;
for(awg=_async_wgroup_list; awg!=NULL; awg=awg->next) {
snprintf(pname, 62, "Async Task Worker - %s",
(awg->name.s)?awg->name.s:"unknown");
for(i=0; i<awg->workers; i++) {
pid=fork_process(PROC_RPC, pname, 1);
if (pid<0) {
return -1; /* error */
}
if(pid==0) {
/* child */
/* initialize the config framework */
if (cfg_child_init()) {
return -1;
}
/* main function for workers */
if(async_task_run(awg, i+1)<0) {
LM_ERR("failed to initialize task worker process: %d\n", i);
return -1;
}
}
}
}
return 0;
}
/**
*
*/
int async_task_set_workers(int n)
{
str gname = str_init("default");
if(_async_wgroup_list!=NULL && _async_wgroup_list->workers>0) {
LM_WARN("task workers already set\n");
return 0;
}
if(n<=0)
return 0;
if(_async_wgroup_list==NULL) {
_async_wgroup_list = (async_wgroup_t*)pkg_malloc(sizeof(async_wgroup_t)
+ (gname.len+1)*sizeof(char));
if(_async_wgroup_list==NULL) {
LM_ERR("failed to create async wgroup\n");
return -1;
}
memset(_async_wgroup_list, 0, sizeof(async_wgroup_t)
+ (gname.len+1)*sizeof(char));
_async_wgroup_list->name.s = (char*)_async_wgroup_list
+ sizeof(async_wgroup_t);
memcpy(_async_wgroup_list->name.s, gname.s, gname.len);
_async_wgroup_list->name.len = gname.len;
}
_async_wgroup_list->workers = n;
return 0;
}
/**
*
*/
int async_task_set_nonblock(int n)
{
if(n>0 && _async_wgroup_list!=NULL) {
_async_wgroup_list->nonblock = 1;
}
return 0;
}
/**
*
*/
int async_task_set_usleep(int n)
{
int v = 0;
if(_async_wgroup_list!=NULL) {
v = _async_wgroup_list->usleep;
_async_wgroup_list->usleep = n;
}
return v;
}
/**
*
*/
int async_task_set_workers_group(char *data)
{
str sval;
param_t* params_list = NULL;
param_hooks_t phooks;
param_t *pit=NULL;
async_wgroup_t awg;
async_wgroup_t *newg;
if(data==NULL) {
return -1;
}
sval.s = data;
sval.len = strlen(sval.s);
if(sval.len<=0) {
LM_ERR("invalid parameter value\n");
return -1;
}
if(sval.s[sval.len-1]==';') {
sval.len--;
}
if (parse_params(&sval, CLASS_ANY, &phooks, ¶ms_list)<0) {
return -1;
}
memset(&awg, 0, sizeof(async_wgroup_t));
for (pit = params_list; pit; pit=pit->next) {
if (pit->name.len==4
&& strncasecmp(pit->name.s, "name", 4)==0) {
awg.name = pit->body;
} else if (pit->name.len==7
&& strncasecmp(pit->name.s, "workers", 7)==0) {
if (str2sint(&pit->body, &awg.workers) < 0) {
LM_ERR("invalid workers value: %.*s\n", pit->body.len, pit->body.s);
return -1;
}
} else if (pit->name.len==6
&& strncasecmp(pit->name.s, "usleep", 6)==0) {
if (str2sint(&pit->body, &awg.usleep) < 0) {
LM_ERR("invalid usleep value: %.*s\n", pit->body.len, pit->body.s);
return -1;
}
} else if (pit->name.len==8
&& strncasecmp(pit->name.s, "nonblock", 8)==0) {
if (str2sint(&pit->body, &awg.nonblock) < 0) {
LM_ERR("invalid nonblock value: %.*s\n", pit->body.len, pit->body.s);
return -1;
}
}
}
if(awg.name.len<=0) {
LM_ERR("invalid name value: [%.*s]\n", sval.len, sval.s);
return -1;
}
if (awg.workers<=0) {
LM_ERR("invalid workers value: %d\n", awg.workers);
return -1;
}
if(awg.name.len==7 && strncmp(awg.name.s, "default", 7)==0) {
if(async_task_set_workers(awg.workers)<0) {
LM_ERR("failed to create the default group\n");
return -1;
}
async_task_set_nonblock(awg.nonblock);
async_task_set_usleep(awg.usleep);
return 0;
}
if(_async_wgroup_list==NULL) {
if(async_task_set_workers(1)<0) {
LM_ERR("failed to create the initial default group\n");
return -1;
}
}
newg = (async_wgroup_t*)pkg_malloc(sizeof(async_wgroup_t)
+ (awg.name.len+1)*sizeof(char));
if(newg==NULL) {
LM_ERR("failed to create async wgroup [%.*s]\n", sval.len, sval.s);
return -1;
}
memset(newg, 0, sizeof(async_wgroup_t)
+ (awg.name.len+1)*sizeof(char));
newg->name.s = (char*)newg + sizeof(async_wgroup_t);
memcpy(newg->name.s, awg.name.s, awg.name.len);
newg->name.len = awg.name.len;
newg->workers = awg.workers;
newg->nonblock = awg.nonblock;
newg->usleep = awg.usleep;
newg->next = _async_wgroup_list->next;
_async_wgroup_list->next = newg;
return 0;
}
/**
*
*/
int async_task_push(async_task_t *task)
{
int len;
if(_async_wgroup_list==NULL || _async_wgroup_list->workers<=0) {
LM_WARN("async task pushed, but no async workers - ignoring\n");
return 0;
}
len = write(_async_wgroup_list->sockets[1], &task, sizeof(async_task_t*));
if(len<=0) {
LM_ERR("failed to pass the task to asynk workers\n");
return -1;
}
LM_DBG("task sent [%p]\n", task);
return 0;
}
/**
*
*/
int async_task_group_push(str *gname, async_task_t *task)
{
int len;
async_wgroup_t *awg = NULL;
if(_async_wgroup_list==NULL) {
LM_WARN("async task pushed, but no async group - ignoring\n");
return 0;
}
for(awg=_async_wgroup_list; awg!=NULL; awg=awg->next) {
if(awg->name.len==gname->len
&& memcmp(awg->name.s, gname->s, gname->len)==0) {
break;
}
}
if(awg==NULL) {
LM_WARN("group [%.*s] not found - ignoring\n", gname->len, gname->s);
return 0;
}
len = write(awg->sockets[1], &task, sizeof(async_task_t*));
if(len<=0) {
LM_ERR("failed to pass the task [%p] to group [%.*s]\n", task,
gname->len, gname->s);
return -1;
}
LM_DBG("task [%p] sent to groupt [%.*s]\n", task, gname->len, gname->s);
return 0;
}
/**
*
*/
int async_task_run(async_wgroup_t *awg, int idx)
{
async_task_t *ptask;
int received;
LM_DBG("async task worker [%.*s] idx [%d] ready\n", awg->name.len,
awg->name.s, idx);
for( ; ; ) {
if(unlikely(awg->usleep)) sleep_us(awg->usleep);
if ((received = recvfrom(awg->sockets[0],
&ptask, sizeof(async_task_t*),
0, NULL, 0)) < 0) {
LM_ERR("failed to received task (%d: %s)\n", errno, strerror(errno));
continue;
}
if(received != sizeof(async_task_t*)) {
LM_ERR("invalid task size %d\n", received);
continue;
}
if(ptask->exec!=NULL) {
LM_DBG("task executed [%p] (%p/%p)\n", (void*)ptask,
(void*)ptask->exec, (void*)ptask->param);
ptask->exec(ptask->param);
} else {
LM_DBG("task with no callback function - ignoring\n");
}
shm_free(ptask);
}
return 0;
}