Changeset 5932 for trunk/libtransmission/peer-mgr.c
- Timestamp:
- May 26, 2008, 12:14:35 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r5843 r5932 36 36 #include "utils.h" 37 37 38 /**39 *** The "SWIFT" system is described by Karthik Tamilmani,40 *** Vinay Pai, and Alexander Mohr of Stony Brook University41 *** in their paper "SWIFT: A System With Incentives For Trading"42 *** http://citeseer.ist.psu.edu/tamilmani04swift.html43 ***44 *** More SWIFT constants are defined in peer-mgr-private.h45 **/46 47 /**48 * Allow new peers to download this many bytes from49 * us when getting started. This can prevent gridlock50 * with other peers using tit-for-tat algorithms51 */52 static const int SWIFT_INITIAL_CREDIT = 64 * 1024; /* 64 KiB */53 54 /**55 * We expend a fraction of our torrent's total upload speed56 * on largesse by uniformly distributing free credit to57 * all of our peers. This too helps prevent gridlock.58 */59 static const double SWIFT_LARGESSE = 0.15; /* 15% of our UL */60 61 /**62 * How frequently to extend largesse-based credit63 */64 static const int SWIFT_PERIOD_MSEC = 5000;65 66 67 38 enum 68 39 { … … 136 107 tr_timer * rechokeTimer; 137 108 tr_timer * refillTimer; 138 tr_timer * swiftTimer;139 109 tr_torrent * tor; 140 110 tr_peer * optimistic; /* the optimistic peer, or NULL if none */ … … 309 279 tr_peer * p; 310 280 p = tr_new0( tr_peer, 1 ); 311 p->credit = SWIFT_INITIAL_CREDIT;312 281 p->rcToClient = tr_rcInit( ); 313 282 p->rcToPeer = tr_rcInit( ); … … 393 362 tr_timerFree( &t->rechokeTimer ); 394 363 tr_timerFree( &t->refillTimer ); 395 tr_timerFree( &t->swiftTimer );396 364 397 365 tr_bitfieldFree( t->requested ); … … 1236 1204 static int reconnectPulse( void * vtorrent ); 1237 1205 static int rechokePulse( void * vtorrent ); 1238 static int swiftPulse( void * vtorrent );1239 1206 1240 1207 void … … 1251 1218 assert( ( t->isRunning != 0 ) == ( t->reconnectTimer != NULL ) ); 1252 1219 assert( ( t->isRunning != 0 ) == ( t->rechokeTimer != NULL ) ); 1253 assert( ( t->isRunning != 0 ) == ( t->swiftTimer != NULL ) );1254 1220 1255 1221 if( !t->isRunning ) … … 1265 1231 RECHOKE_PERIOD_MSEC ); 1266 1232 1267 t->swiftTimer = tr_timerNew( t->manager->handle,1268 swiftPulse, t,1269 SWIFT_PERIOD_MSEC );1270 1271 1233 reconnectPulse( t ); 1272 1234 1273 1235 rechokePulse( t ); 1274 1275 swiftPulse( t );1276 1236 } 1277 1237 … … 1287 1247 tr_timerFree( &t->rechokeTimer ); 1288 1248 tr_timerFree( &t->reconnectTimer ); 1289 tr_timerFree( &t->swiftTimer );1290 1249 1291 1250 /* disconnect the peers. */ … … 1564 1523 **/ 1565 1524 1566 static double1525 static int 1567 1526 getWeightedRate( const tr_peer * peer, int clientIsSeed ) 1568 1527 { … … 1661 1620 /*** 1662 1621 **** 1663 ***/1664 1665 static int1666 swiftPulse( void * vtorrent )1667 {1668 Torrent * t = vtorrent;1669 torrentLock( t );1670 1671 if( !tr_torrentIsSeed( t->tor ) )1672 {1673 int i;1674 int peerCount = 0;1675 int deadbeatCount = 0;1676 tr_peer ** peers = getConnectedPeers( t, &peerCount );1677 tr_peer ** deadbeats = tr_new( tr_peer*, peerCount );1678 1679 const double ul_KiBsec = tr_rcRate( t->tor->upload );1680 const double ul_KiB = ul_KiBsec * (SWIFT_PERIOD_MSEC/1000.0);1681 const double ul_bytes = ul_KiB * 1024;1682 const double freeCreditTotal = ul_bytes * SWIFT_LARGESSE;1683 int freeCreditPerPeer;1684 1685 for( i=0; i<peerCount; ++i ) {1686 tr_peer * peer = peers[i];1687 if( peer->credit <= 0 )1688 deadbeats[deadbeatCount++] = peer;1689 }1690 1691 freeCreditPerPeer = (int)( freeCreditTotal / deadbeatCount );1692 for( i=0; i<deadbeatCount; ++i )1693 deadbeats[i]->credit = freeCreditPerPeer;1694 1695 tordbg( t, "%d deadbeats, "1696 "who are each being granted %d bytes' credit "1697 "for a total of %.1f KiB, "1698 "%d%% of the torrent's ul speed %.1f\n",1699 deadbeatCount, freeCreditPerPeer,1700 ul_KiBsec*SWIFT_LARGESSE, (int)(SWIFT_LARGESSE*100), ul_KiBsec );1701 1702 tr_free( deadbeats );1703 tr_free( peers );1704 }1705 1706 torrentUnlock( t );1707 return TRUE;1708 }1709 1710 /***1711 ****1712 1622 **** Life and Death 1713 1623 **** … … 1753 1663 /* if we have >= relaxIfFewerThan, strictness is 100%. 1754 1664 * if we have zero connections, strictness is 0% */ 1755 const doublestrictness = peerCount >= relaxStrictnessIfFewerThanN1665 const float strictness = peerCount >= relaxStrictnessIfFewerThanN 1756 1666 ? 1.0 1757 : peerCount / ( double)relaxStrictnessIfFewerThanN;1667 : peerCount / (float)relaxStrictnessIfFewerThanN; 1758 1668 const int lo = MIN_UPLOAD_IDLE_SECS; 1759 1669 const int hi = MAX_UPLOAD_IDLE_SECS;
Note: See TracChangeset
for help on using the changeset viewer.