Changeset 10748 for trunk/libtransmission/peer-mgr.c
- Timestamp:
- Jun 14, 2010, 1:56:03 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r10747 r10748 3272 3272 struct peer_candidate 3273 3273 { 3274 uint64_t score; 3274 3275 int from; 3275 int salt;3276 uint8_t salt; 3276 3277 int priority; 3277 3278 int seedProbability; … … 3281 3282 tr_torrent * tor; 3282 3283 struct peer_atom * atom; 3283 3284 3284 }; 3285 3285 … … 3296 3296 else if( b == -1 ) b = 100; 3297 3297 3298 return a > b ? 1 : -1; 3298 if( a > b ) return 1; 3299 if( a < b ) return -1; 3300 return 0; 3299 3301 } 3300 3302 … … 3303 3305 { 3304 3306 return difftime( tr_time( ), tor->startDate ) < 120; 3307 } 3308 3309 static int loud = FALSE; 3310 3311 static inline uint64_t 3312 addValToKey( uint64_t value, int width, uint64_t addme ) 3313 { 3314 if(loud) fprintf( stderr, "old value %"PRIu64"... width %d, addme %d\n", value, width, (int)addme ); 3315 value = (value << (uint64_t)width); 3316 if(loud) fprintf( stderr, "mid value %"PRIu64"\n", value ); 3317 value |= addme; 3318 if(loud) fprintf( stderr, "new value %"PRIu64"\n", value ); 3319 return value; 3320 } 3321 3322 /* smaller value is better */ 3323 static uint64_t 3324 getPeerCandidateScore( 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 3367 static int 3368 oldComparePeerCandidates( 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 ) { 3378 if(loud) fprintf( stderr, "old: a_failed %d\n", (int)a_failed ); 3379 if(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 ) { 3385 if(loud) fprintf( stderr, "old: a->lastConnectionAttemptAt %u\n", (unsigned int)a->lastConnectionAttemptAt ); 3386 if(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 ) { 3392 if(loud) fprintf( stderr, "old: a->priority %d\n", (int)a->priority ); 3393 if(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 ) { 3399 if(loud) fprintf( stderr, "old: a->wasRecentlyStarted %d\n", (int)a->wasRecentlyStarted ); 3400 if(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 ))) { 3406 if(loud) fprintf( stderr, "old: a->seedProbability %d\n", (int)a->seedProbability ); 3407 if(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 ) { 3413 if(loud) fprintf( stderr, "old: a->from %d\n", (int)a->from ); 3414 if(loud) fprintf( stderr, "old: b->from %d\n", (int)b->from ); 3415 return a->from < b->from ? -1 : 1; 3416 } 3417 3418 /* salt */ 3419 if(loud) fprintf( stderr, "old: a->salt %d\n", (int)a->salt ); 3420 if(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; 3305 3425 } 3306 3426 … … 3309 3429 comparePeerCandidates( const void * va, const void * vb ) 3310 3430 { 3311 int i;3312 tr_bool af, bf;3431 int ret; 3432 int oldret; 3313 3433 const struct peer_candidate * a = va; 3314 3434 const struct peer_candidate * b = vb; 3315 3435 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; 3344 3459 } 3345 3460 … … 3403 3518 walk->from = atom->from; 3404 3519 walk->salt = tr_cryptoWeakRandInt( 4096 ); 3520 walk->score = getPeerCandidateScore( tor, atom, walk->salt ); 3405 3521 walk->priority = tr_torrentGetPriority( tor ); 3406 3522 walk->seedProbability = atom->seedProbability;
Note: See TracChangeset
for help on using the changeset viewer.