Skip to content

Commit 8fe8128

Browse files
bogdan-iancurazvancrainea
authored andcommitted
[tm] migrated to dynamic allocated branches per transaction
This allow a really high number of branches per transaction (like the default is 256, but can be increased). The branches are dynamically allocated, on demand, so some transactions which are not subjet to forking (typically non-INVITE) may have the minimum of 4 branches, while the INVITE transaction, depending on the need can scale up to 256
1 parent 26297af commit 8fe8128

13 files changed

Lines changed: 309 additions & 235 deletions

File tree

modules/tm/async.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ int t_resume_async_request(int fd, void*param, int was_timeout)
131131
backup_list = set_avp_list( &t->user_avps );
132132
/* set default send address to the saved value */
133133
backup_si = bind_address;
134-
bind_address = t->uac[0].request.dst.send_sock;
134+
bind_address = TM_BRANCH( t, 0).request.dst.send_sock;
135135

136136
async_status = ASYNC_DONE; /* assume default status as done */
137137
/* call the resume function in order to read and handle data */
@@ -260,7 +260,7 @@ int t_resume_async_reply(int fd, void*param, int was_timeout)
260260
set_t( t );
261261

262262
msg_status=ctx->reply->REPLY_STATUS;
263-
reply_uac=&t->uac[branch];
263+
reply_uac=&TM_BRANCH( t, branch);
264264
LM_DBG("org. status uas=%d, uac[%d]=%d local=%d is_invite=%d)\n",
265265
t->uas.status, branch, reply_uac->last_received,
266266
is_local(t), is_invite(t));

modules/tm/h_table.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ void free_cell( struct cell* dead_cell )
120120
struct sip_msg *rpl;
121121
struct totag_elem *tt, *foo;
122122
struct proxy_l *p;
123+
struct ua_client *uac;
123124

124125
if ( has_tran_tmcbs( dead_cell, TMCB_TRANS_DELETED) )
125126
run_trans_callbacks( TMCB_TRANS_DELETED, dead_cell, 0, 0, 0);
@@ -142,17 +143,18 @@ void free_cell( struct cell* dead_cell )
142143
/* UA Clients */
143144
for ( i =0 ; i<dead_cell->nr_of_outgoings; i++ )
144145
{
146+
uac = & TM_BRANCH( dead_cell, i);
145147
/* retransmission buffer */
146-
if ( (b=dead_cell->uac[i].request.buffer.s) )
148+
if ( (b=uac->request.buffer.s) )
147149
shm_free_bulk( b );
148-
b=dead_cell->uac[i].local_cancel.buffer.s;
150+
b=uac->local_cancel.buffer.s;
149151
if (b!=0 && b!=BUSY_BUFFER)
150152
shm_free_bulk( b );
151-
rpl=dead_cell->uac[i].reply;
153+
rpl=uac->reply;
152154
if (rpl && rpl!=FAKED_REPLY && rpl->msg_flags&FL_SHM_CLONE) {
153155
free_cloned_msg_unsafe( rpl );
154156
}
155-
if ( (p=dead_cell->uac[i].proxy)!=NULL ) {
157+
if ( (p=uac->proxy)!=NULL ) {
156158
if ( p->host.h_addr_list )
157159
shm_free_bulk( p->host.h_addr_list );
158160
if ( p->dn ) {
@@ -163,13 +165,16 @@ void free_cell( struct cell* dead_cell )
163165
shm_free_bulk(p);
164166
}
165167

166-
_clean_branch(dead_cell->uac[i],
167-
shm_free_bulk, destroy_avp_list_bulk);
168+
_clean_branch( *uac, shm_free_bulk, destroy_avp_list_bulk);
168169

169-
if (dead_cell->uac[i].on_reply)
170-
shm_free_bulk(dead_cell->uac[i].on_reply);
170+
if (uac->on_reply)
171+
shm_free_bulk(uac->on_reply);
171172
}
172173

174+
/* destroy the UAC arrays now */
175+
for (i=0 ; i<TM_BRANCH_CHUNKS_NO && dead_cell->uac[i] ; i++ )
176+
shm_free_bulk(dead_cell->uac[i]);
177+
173178
/* collected to tags */
174179
tt=dead_cell->fwded_totags;
175180
while(tt) {
@@ -239,7 +244,7 @@ static inline void init_branches(struct cell *t, unsigned int set)
239244
unsigned int i;
240245

241246
for (i=0; i<MAX_BRANCHES; i++)
242-
init_branch(&t->uac[i], i, set, t);
247+
init_branch( &TM_BRANCH(t,i), i, set, t);
243248
}
244249

245250

modules/tm/h_table.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ struct cell;
5151
struct timer;
5252
struct retr_buf;
5353

54+
55+
/* be sure the MAX devides to CHUNK_SIZE, otherwise the behavor may be
56+
* bogus (meaning crashes) */
57+
/* IMPORTANT - TM_BRANCH_MAX must be a multiple of 32, to align perfectly
58+
* with the size of the bitmask (which is array of uint32)
59+
* Even more, this multiple of 32 guarantees the devision with 4, the
60+
* TM_BRANCH_CHUNK_SIZE (so, if you plan to change the chunk size, pick
61+
* something that devides 32, like 16) */
62+
#define TM_BRANCH_MAX_FACTOR 8
63+
#define TM_BRANCH_MAX (32*TM_BRANCH_MAX_FACTOR)
64+
#define TM_BRANCH_CHUNK_SIZE 4
65+
#define TM_BRANCH_CHUNKS_NO (TM_BRANCH_MAX/TM_BRANCH_CHUNK_SIZE)
66+
67+
#define TM_BRANCH( _t, _idx) \
68+
( (_t)->uac[(_idx)/TM_BRANCH_CHUNK_SIZE][(_idx)%TM_BRANCH_CHUNK_SIZE] )
69+
5470
#include "../../lib/container.h"
5571
#include "../../mem/shm_mem.h"
5672
#include "lock.h"
@@ -216,7 +232,6 @@ struct totag_elem {
216232
#define T_UAC_IS_PHONY (1<<2)
217233

218234

219-
220235
/* transaction context */
221236

222237
typedef struct cell
@@ -274,7 +289,9 @@ typedef struct cell
274289
/* UA Server */
275290
struct ua_server uas;
276291
/* UA Clients */
277-
struct ua_client uac[ MAX_BRANCHES ];
292+
/* each UAC pointer is a pointer to a
293+
* TM_BRANCH_CHUNK_SIZE array of UACs */
294+
struct ua_client* uac[TM_BRANCH_CHUNKS_NO];
278295

279296
/* protection against concurrent reply processing */
280297
ser_lock_t reply_mutex;

modules/tm/t_cancel.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void cancel_uacs( struct cell *t, branch_bm_t cancel_bm )
6868
for( i=0 ; i<t->nr_of_outgoings ; i++ )
6969
if ( BRANCH_BM_TST_IDX( cancel_bm, i) ) {
7070
/* any reply actually received on this branch */
71-
if (t->uac[i].last_received!=0) {
71+
if (TM_BRANCH(t,i).last_received!=0) {
7272
/* send a cancel out */
7373
cancel_branch(t, i);
7474
} else {
@@ -77,7 +77,7 @@ void cancel_uacs( struct cell *t, branch_bm_t cancel_bm )
7777
* one out IF we receive later a reply on this branch, so let's
7878
* flag it for catching (and cancelling) such delaied replies
7979
*/
80-
t->uac[i].flags |= T_UAC_TO_CANCEL_FLAG;
80+
TM_BRANCH(t,i).flags |= T_UAC_TO_CANCEL_FLAG;
8181
}
8282
}
8383
}
@@ -90,8 +90,8 @@ void cancel_branch( struct cell *t, int branch )
9090
struct retr_buf *crb, *irb;
9191
struct usr_avp **backup_list;
9292

93-
crb=&t->uac[branch].local_cancel;
94-
irb=&t->uac[branch].request;
93+
crb=&TM_BRANCH(t,branch).local_cancel;
94+
irb=&TM_BRANCH(t,branch).request;
9595

9696
# ifdef EXTRA_DEBUG
9797
if (crb->buffer.s!=0 && crb->buffer.s!=BUSY_BUFFER) {
@@ -124,10 +124,10 @@ void cancel_branch( struct cell *t, int branch )
124124
}
125125

126126
LM_DBG("sending cancel...\n");
127-
if (t->uac[branch].br_flags & tcp_no_new_conn_bflag)
127+
if (TM_BRANCH(t,branch).br_flags & tcp_no_new_conn_bflag)
128128
tcp_no_new_conn = 1;
129129
backup_list = set_avp_list( &t->user_avps );
130-
set_bavp_list(&t->uac[branch].user_avps);
130+
set_bavp_list(&TM_BRANCH(t,branch).user_avps);
131131
if (SEND_BUFFER( crb )==0) {
132132
if ( has_tran_tmcbs( t, TMCB_MSG_SENT_OUT) ) {
133133
set_extra_tmcb_params( &crb->buffer, &crb->dst);

modules/tm/t_cancel.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,17 @@ char *build_cancel(struct cell *Trans,unsigned int branch,
5555

5656
inline static short should_cancel_branch( struct cell *t, int b )
5757
{
58+
struct ua_client* uac = & TM_BRANCH( t, b);
59+
5860
/* cancel only if provisional received and no one else
5961
attempted to cancel yet; also skip PHONY branches
6062
(they have no signaling, so no canceling */
61-
if ( t->uac[b].local_cancel.buffer.s==NULL &&
62-
(t->uac[b].flags & T_UAC_IS_PHONY)==0 ) {
63-
if ( t->uac[b].last_received<200 ) {
63+
if ( uac->local_cancel.buffer.s==NULL &&
64+
(uac->flags & T_UAC_IS_PHONY)==0 ) {
65+
if ( uac->last_received<200 ) {
6466
/* we'll cancel -- label it so that no one else
6567
(e.g. another 200 branch) will try to do the same */
66-
t->uac[b].local_cancel.buffer.s=BUSY_BUFFER;
68+
uac->local_cancel.buffer.s=BUSY_BUFFER;
6769
return 1;
6870
}
6971
}

0 commit comments

Comments
 (0)