[asterisk-dev] [Code Review] Use a shuffling algorithm to find unused IAX2 call numbers

Mark Michelson reviewboard at asterisk.org
Fri Feb 8 11:52:03 CST 2013


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviewboard.asterisk.org/r/2288/#review7820
-----------------------------------------------------------

Ship it!


I can't find fault in the implementation, and I'd be willing to bet large sums of money this performs better than an ao2_container.


/trunk/channels/chan_iax2.c
<https://reviewboard.asterisk.org/r/2288/#comment14834>

    I'm curious why you set the value at x to 0. It shouldn't cause a problem but if anything after the number of available callnos is essentially invisible, why bother setting to 0?



/trunk/channels/chan_iax2.c
<https://reviewboard.asterisk.org/r/2288/#comment14833>

    s/passed/past/



/trunk/channels/chan_iax2.c
<https://reviewboard.asterisk.org/r/2288/#comment14832>

    This shouldn't be an issue.
    
    Modulo bias typically is an issue for older implementations of rand() as well as some non-Linux implementations of rand().
    
    Modern day rand() uses the same random number generation as random(), so the lower bits of the random number are as random as the higher bits.
    
    On top of that, ast_random() reads from /dev/urandom for its random numbers if possible. If it cannot, it calls random().


- Mark


On Jan. 29, 2013, 8:10 a.m., Sean Bright wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviewboard.asterisk.org/r/2288/
> -----------------------------------------------------------
> 
> (Updated Jan. 29, 2013, 8:10 a.m.)
> 
> 
> Review request for Asterisk Developers and rmudgett.
> 
> 
> Summary
> -------
> 
> While adding red-black tree containers to astobj2 in r376575, Richard pointed out the way chan_iax2 finds unused call numbers will prevent ao2_container integrity checks at runtime.
> 
> This patch removes the ao2_container and instead uses fixed sized arrays and a modified Fisher-Yates-Durstenfeld shuffle to maintain the call number list.  This involves treating the lower indexes of the call number array as the available numbers and the higher indexes as the used ones.  As new call numbers are requested, we choose a random call number from the lower indices of the array, and swap it with the call number that is currently at the end of the available list.  The swapped value becomes the new start to the used list.  Here's an example to illustrate, pretending that there are only 10 call numbers instead of 2^15:
> 
> Starting state:
> 
> Numbers:   [0][1][2][3][4][5][6][7][8][9]
> Available: 10
> 
> Choose a random index, pull out the call number at that index, and swap it with the value at the end of our range, and decrement our available numbers:
> 
> Chosen:    6
> Numbers:   [0][1][2][3][4][5][9][7][8] | [6]
> Available: 9
> 
> Choose a random index, pull out the call number at that index, and swap it with the value at the end of our range, and decrement our available numbers:
> 
> Chosen:    3
> Numbers:   [0][1][2][8][4][5][9][7] | [3][6]
> Available: 8
> 
> And so on.  In the example above, we put the call number that we are extracting at the end of the list, but in reality we don't really care.  The numbers after the end of the usable range are effectively "checked out" and we don't track them - we simply append the returned number to the list of available numbers and increment:
> 
> Return:    6
> Numbers:   [0][1][2][8][4][5][9][7][6] | [6]
> Available: 9
> 
> While 3 is outstanding.  In short, the numbers after the "pivot" are irrelevant and we never look at them again, we simply overwrite them with returned numbers.
> 
> I haven't done performance testing but I assume this is a bit faster than the current implementation, although the locking behavior is similar.
> 
> 
> Diffs
> -----
> 
>   /trunk/channels/chan_iax2.c 380330 
> 
> Diff: https://reviewboard.asterisk.org/r/2288/diff
> 
> 
> Testing
> -------
> 
> Basic testing generating trunked and non-trunked calls between Asterisk servers.  The call number assignment behavior appears to match the ao2_container approach.
> 
> 
> Thanks,
> 
> Sean
> 
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-dev/attachments/20130208/4e366a7a/attachment.htm>


More information about the asterisk-dev mailing list