Changeset 10748


Ignore:
Timestamp:
Jun 14, 2010, 1:56:03 AM (12 years ago)
Author:
charles
Message:

(trunk libT) add a much faster (but more complicated) implementation of comparePeerCandidates(). For testing in the nightlies, leave both and the new implementations in the code and run them both (which will make the nightlies slower for awhile) and crash if the results differ from each other. When it doesn't crash anymore I'll remove the old implementation. Happy testing! >:)
-place and compare them to make sure they both give the same results. If no testers can make it crash

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-mgr.c

    r10747 r10748  
    32723272struct peer_candidate
    32733273{
     3274    uint64_t score;
    32743275    int from;
    3275     int salt;
     3276    uint8_t salt;
    32763277    int priority;
    32773278    int seedProbability;
     
    32813282    tr_torrent * tor;
    32823283    struct peer_atom * atom;
    3283    
    32843284};
    32853285
     
    32963296    else if( b == -1 ) b = 100;
    32973297
    3298     return a > b ? 1 : -1;
     3298    if( a > b ) return 1;
     3299    if( a < b ) return -1;
     3300    return 0;
    32993301}
    33003302
     
    33033305{
    33043306    return difftime( tr_time( ), tor->startDate ) < 120;
     3307}
     3308
     3309static int loud = FALSE;
     3310
     3311static inline uint64_t
     3312addValToKey( uint64_t value, int width, uint64_t addme )
     3313{
     3314if(loud) fprintf( stderr, "old value %"PRIu64"... width %d, addme %d\n", value, width, (int)addme );
     3315    value = (value << (uint64_t)width);
     3316if(loud) fprintf( stderr, "mid value %"PRIu64"\n", value );
     3317    value |= addme;
     3318if(loud) fprintf( stderr, "new value %"PRIu64"\n", value );
     3319    return value;
     3320}
     3321
     3322/* smaller value is better */
     3323static uint64_t
     3324getPeerCandidateScore( const tr_torrent * tor, const struct peer_atom * atom, uint8_t salt  )
     3325{
     3326    uint64_t i;
     3327    uint64_t score = 0;
     3328    const tr_bool failed = atom->lastConnectionAt < atom->lastConnectionAttemptAt;
     3329
     3330    /* prefer peers we've connected to, or never tried, over peers we failed to connect to. */
     3331    i = failed ? 1 : 0;
     3332    score = addValToKey( score, 1, i );
     3333
     3334    /* prefer the one we attempted least recently (to cycle through all peers) */
     3335    i = atom->lastConnectionAttemptAt;
     3336    score = addValToKey( score, 32, i );
     3337
     3338    /* prefer peers belonging to a torrent of a higher priority */
     3339    switch( tr_torrentGetPriority( tor ) ) {
     3340        case TR_PRI_HIGH:    i = 0; break;
     3341        case TR_PRI_NORMAL:  i = 1; break;
     3342        case TR_PRI_LOW:     i = 2; break;
     3343    }
     3344    score = addValToKey( score, 4, i );
     3345
     3346    /* prefer recently-started torrents */
     3347    i = torrentWasRecentlyStarted( tor ) ? 0 : 1;
     3348    score = addValToKey( score, 1, i );
     3349
     3350    /* prefer peers that we might have a chance of uploading to...
     3351       so lower seed probability is better */
     3352    if( atom->seedProbability == 100 ) i = 101;
     3353    else if( atom->seedProbability == -1 ) i = 100;
     3354    else i = atom->seedProbability;
     3355    score = addValToKey( score, 8, i );
     3356
     3357    /* Prefer peers that we got from more trusted sources.
     3358     * lower `from' values indicate more trusted sources */
     3359    score = addValToKey( score, 4, atom->from );
     3360
     3361    /* salt */
     3362    score = addValToKey( score, 8, salt );
     3363
     3364    return score;
     3365}
     3366
     3367static int
     3368oldComparePeerCandidates( const void * va, const void * vb )
     3369{
     3370    int i;
     3371    const struct peer_candidate * a = va;
     3372    const struct peer_candidate * b = vb;
     3373
     3374    /* prefer peers we've connected to, or never tried, over peers we failed to connect to. */
     3375    const tr_bool a_failed = a->atom->lastConnectionAt < a->atom->lastConnectionAttemptAt;
     3376    const tr_bool b_failed = b->atom->lastConnectionAt < b->atom->lastConnectionAttemptAt;
     3377    if( a_failed != b_failed ) {
     3378if(loud) fprintf( stderr, "old: a_failed %d\n", (int)a_failed );
     3379if(loud) fprintf( stderr, "old: b_failed %d\n", (int)b_failed );
     3380        return a_failed ? 1 : -1;
     3381    }
     3382
     3383    /* prefer the one we attempted least recently (to cycle through all peers) */
     3384    if( a->lastConnectionAttemptAt != b->lastConnectionAttemptAt ) {
     3385if(loud) fprintf( stderr, "old: a->lastConnectionAttemptAt %u\n", (unsigned int)a->lastConnectionAttemptAt );
     3386if(loud) fprintf( stderr, "old: b->lastConnectionAttemptAt %u\n", (unsigned int)b->lastConnectionAttemptAt );
     3387        return a->lastConnectionAttemptAt < b->lastConnectionAttemptAt ? -1 : 1;
     3388    }
     3389
     3390    /* prefer peers belonging to a torrent of a higher priority */
     3391    if( a->priority != b->priority ) {
     3392if(loud) fprintf( stderr, "old: a->priority %d\n", (int)a->priority );
     3393if(loud) fprintf( stderr, "old: b->priority %d\n", (int)b->priority );
     3394        return a->priority > b->priority ? -1 : 1;
     3395    }
     3396
     3397    /* prefer recently-started torrents */
     3398    if( a->wasRecentlyStarted != b->wasRecentlyStarted ) {
     3399if(loud) fprintf( stderr, "old: a->wasRecentlyStarted %d\n", (int)a->wasRecentlyStarted );
     3400if(loud) fprintf( stderr, "old: b->wasRecentlyStarted %d\n", (int)b->wasRecentlyStarted );
     3401        return a->wasRecentlyStarted ? -1 : 1;
     3402     }
     3403
     3404    /* prefer peers that we might have a chance of uploading to */
     3405    if(( i = compareSeedProbabilities( a->seedProbability, b->seedProbability ))) {
     3406if(loud) fprintf( stderr, "old: a->seedProbability %d\n", (int)a->seedProbability );
     3407if(loud) fprintf( stderr, "old: b->seedProbability %d\n", (int)b->seedProbability );
     3408        return i;
     3409    }
     3410
     3411    /* prefer peers that we got from more trusted sources */
     3412    if( a->from != b->from ) {
     3413if(loud) fprintf( stderr, "old: a->from %d\n", (int)a->from );
     3414if(loud) fprintf( stderr, "old: b->from %d\n", (int)b->from );
     3415        return a->from < b->from ? -1 : 1;
     3416    }
     3417
     3418    /* salt */
     3419if(loud) fprintf( stderr, "old: a->salt %d\n", (int)a->salt );
     3420if(loud) fprintf( stderr, "old: b->salt %d\n", (int)b->salt );
     3421    if( a->salt != b->salt )
     3422        return a->salt < b->salt ? -1 : 1;
     3423
     3424    return 0;
    33053425}
    33063426
     
    33093429comparePeerCandidates( const void * va, const void * vb )
    33103430{
    3311     int i;
    3312     tr_bool af, bf;
     3431    int ret;
     3432    int oldret;
    33133433    const struct peer_candidate * a = va;
    33143434    const struct peer_candidate * b = vb;
    33153435
    3316     /* prefer peers we've connected to, or never tried, over peers we failed to connect to. */
    3317     af = a->lastConnectionAt < a->lastConnectionAttemptAt;
    3318     bf = b->lastConnectionAt < b->lastConnectionAttemptAt;
    3319     if( af != bf )
    3320         return af ? 1 : -1;
    3321 
    3322     /* prefer the one we attempted least recently (to cycle through all peers) */
    3323     if( a->lastConnectionAttemptAt != b->lastConnectionAttemptAt )
    3324         return a->lastConnectionAttemptAt < b->lastConnectionAttemptAt ? -1 : 1;
    3325 
    3326     /* prefer peers belonging to a torrent of a higher priority */
    3327     if( a->priority != b->priority )
    3328         return a->priority > b->priority ? -1 : 1;
    3329 
    3330     /* prefer recently-started torrents */
    3331     if( a->wasRecentlyStarted != b->wasRecentlyStarted )
    3332         return a->wasRecentlyStarted ? -1 : 1;
    3333 
    3334     /* prefer peers that we might have a chance of uploading to */
    3335     if(( i = compareSeedProbabilities( a->seedProbability, b->seedProbability )))
    3336         return i;
    3337 
    3338     /* prefer peers that we got from more trusted sources */
    3339     if( a->from != b->from )
    3340         return a->from < b->from ? -1 : 1;
    3341 
    3342     /* salt */
    3343     return a->salt - b->salt;
     3436    if( a->score != b->score )
     3437        ret = a->score < b->score ? -1 : 1;
     3438    else
     3439        ret = 0;
     3440   
     3441    oldret = oldComparePeerCandidates( va, vb );
     3442
     3443    if( ret != oldret ) {
     3444        loud = TRUE;
     3445        oldret = oldComparePeerCandidates( va, vb );
     3446        fprintf( stderr, "ccc =========================\n" );
     3447        fprintf( stderr, "oldret %d\n", oldret );
     3448        fprintf( stderr, "ret %d\n", ret );
     3449        fprintf( stderr, "a score %"PRIu64"\n", a->score );
     3450        fprintf( stderr, "b score %"PRIu64"\n", b->score );
     3451        fprintf( stderr, "generating a score\n" );
     3452        getPeerCandidateScore( a->tor, a->atom, a->salt );
     3453        fprintf( stderr, "generating b bcore\n" );
     3454        getPeerCandidateScore( b->tor, b->atom, b->salt );
     3455        assert( 0 );
     3456    }
     3457
     3458    return ret;
    33443459}
    33453460
     
    34033518                walk->from = atom->from;
    34043519                walk->salt = tr_cryptoWeakRandInt( 4096 );
     3520                walk->score = getPeerCandidateScore( tor, atom, walk->salt );
    34053521                walk->priority = tr_torrentGetPriority( tor );
    34063522                walk->seedProbability = atom->seedProbability;
Note: See TracChangeset for help on using the changeset viewer.