Changeset 8254


Ignore:
Timestamp:
Apr 18, 2009, 11:17:30 PM (12 years ago)
Author:
charles
Message:

(trunk) experimental support for tr_torrentSetPriority()

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/rpc-spec.txt

    r8230 r8254  
    8989   string                            | value type & description
    9090   ----------------------------------+-------------------------------------------------
     91   "bandwidthPriority"               | number     this torrent's bandwidth tr_priority_t
    9192   "downloadLimit"                   | number     maximum download speed (in K/s)
    9293   "downloadLimited"                 | boolean    true if "downloadLimit" is honored
     
    133134   announceResponse                | string                      | tr_stat
    134135   announceURL                     | string                      | tr_stat
     136   bandwidthPriority               | number                      | tr_priority_t
    135137   comment                         | string                      | tr_info
    136138   corruptEver                     | number                      | tr_stat
     
    470472         |         | yes       | torrent-get    | new arg "fileStats"
    471473         |         | yes       | torrent-set    | new arg "ratio"
     474         |         | yes       | torrent-get    | new arg "bandwidthPriority"
     475         |         | yes       | torrent-set    | new arg "bandwidthPriority"
    472476         |         | yes       | torrent-set    | new arg "downloadLimited"
    473477         |         | yes       | torrent-set    | new arg "uploadLimited"
  • trunk/libtransmission/bandwidth.c

    r7791 r8254  
    149149static void
    150150allocateBandwidth( tr_bandwidth  * b,
     151                   tr_priority_t   parent_priority,
    151152                   tr_direction    dir,
    152153                   int             period_msec,
    153154                   tr_ptrArray   * peer_pool )
    154155{
     156    tr_priority_t priority;
     157
    155158    assert( tr_isBandwidth( b ) );
    156159    assert( tr_isDirection( dir ) );
     
    171174    }
    172175
     176    priority = MAX( parent_priority, b->priority );
     177
    173178    /* add this bandwidth's peer, if any, to the peer pool */
    174     if( b->peer != NULL )
     179    if( b->peer != NULL ) {
     180        b->peer->priority = priority;
    175181        tr_ptrArrayAppend( peer_pool, b->peer );
     182    }
    176183
    177184#ifdef DEBUG_DIRECTION
     
    186193        const int n = tr_ptrArraySize( &b->children );
    187194        for( i=0; i<n; ++i )
    188             allocateBandwidth( children[i], dir, period_msec, peer_pool );
    189     }
    190 }
    191 
    192 void
    193 tr_bandwidthAllocate( tr_bandwidth  * b,
    194                       tr_direction    dir,
    195                       int             period_msec )
    196 {
    197     int i, n, peerCount;
    198     tr_ptrArray tmp = TR_PTR_ARRAY_INIT;
    199     struct tr_peerIo ** peers;
    200 
    201     /* allocateBandwidth() is a helper function with two purposes:
    202      * 1. allocate bandwidth to b and its subtree
    203      * 2. accumulate an array of all the peerIos from b and its subtree. */
    204     allocateBandwidth( b, dir, period_msec, &tmp );
    205     peers = (struct tr_peerIo**) tr_ptrArrayBase( &tmp );
    206     peerCount = tr_ptrArraySize( &tmp );
    207 
    208     for( i=0; i<peerCount; ++i )
    209         tr_peerIoRef( peers[i] );
     195            allocateBandwidth( children[i], priority, dir, period_msec, peer_pool );
     196    }
     197}
     198
     199static void
     200phaseOne( tr_ptrArray * peerArray, tr_direction dir )
     201{
     202    int i, n;
     203    int peerCount = tr_ptrArraySize( peerArray );
     204    struct tr_peerIo ** peers = (struct tr_peerIo**) tr_ptrArrayBase( peerArray );
    210205
    211206    /* First phase of IO.  Tries to distribute bandwidth fairly to keep faster
     
    236231            i = 0;
    237232    }
     233}
     234
     235void
     236tr_bandwidthAllocate( tr_bandwidth  * b,
     237                      tr_direction    dir,
     238                      int             period_msec )
     239{
     240    int i, peerCount;
     241    tr_ptrArray tmp = TR_PTR_ARRAY_INIT;
     242    tr_ptrArray low = TR_PTR_ARRAY_INIT;
     243    tr_ptrArray high = TR_PTR_ARRAY_INIT;
     244    tr_ptrArray normal = TR_PTR_ARRAY_INIT;
     245    struct tr_peerIo ** peers;
     246
     247    /* allocateBandwidth() is a helper function with two purposes:
     248     * 1. allocate bandwidth to b and its subtree
     249     * 2. accumulate an array of all the peerIos from b and its subtree. */
     250    allocateBandwidth( b, TR_PRI_LOW, dir, period_msec, &tmp );
     251    peers = (struct tr_peerIo**) tr_ptrArrayBase( &tmp );
     252    peerCount = tr_ptrArraySize( &tmp );
     253
     254    for( i=0; i<peerCount; ++i ) {
     255        tr_peerIoRef( peers[i] );
     256        switch( peers[i]->priority ) {
     257            case TR_PRI_HIGH: tr_ptrArrayAppend( &high,   peers[i] ); break;
     258            case TR_PRI_LOW:  tr_ptrArrayAppend( &low,    peers[i] ); break;
     259            default:          tr_ptrArrayAppend( &normal, peers[i] ); break;
     260        }
     261    }
     262
     263    /* First phase of IO.  Tries to distribute bandwidth fairly to keep faster
     264     * peers from starving the others.  Loop through the peers, giving each a
     265     * small chunk of bandwidth.  Keep looping until we run out of bandwidth
     266     * and/or peers that can use it */
     267    phaseOne( &high, dir );
     268    phaseOne( &normal, dir );
     269    phaseOne( &low, dir );
    238270
    239271    /* Second phase of IO.  To help us scale in high bandwidth situations,
     
    248280
    249281    /* cleanup */
     282    tr_ptrArrayDestruct( &normal, NULL );
     283    tr_ptrArrayDestruct( &high, NULL );
     284    tr_ptrArrayDestruct( &low, NULL );
    250285    tr_ptrArrayDestruct( &tmp, NULL );
    251286}
  • trunk/libtransmission/bandwidth.h

    r8021 r8254  
    101101    struct tr_band band[2];
    102102    struct tr_bandwidth * parent;
     103    tr_priority_t priority;
    103104    int magicNumber;
    104105    tr_session * session;
  • trunk/libtransmission/fastresume.c

    r8021 r8254  
    182182
    183183        case FR_ID_PRIORITY:
    184             ret = TR_FR_PRIORITY;      break;
     184            ret = TR_FR_FILE_PRIORITIES; break;
    185185
    186186        case FR_ID_SPEED:
     
    367367        tr_free( dl );
    368368
    369         ret = TR_FR_PRIORITY;
     369        ret = TR_FR_FILE_PRIORITIES;
    370370    }
    371371
  • trunk/libtransmission/peer-io.h

    r7792 r8254  
    7070    tr_bool               hasFinishedConnecting;
    7171
     72    tr_priority_t         priority;
     73
    7274    int                   pendingEvents;
    7375
  • trunk/libtransmission/resume.c

    r8110 r8254  
    2626#include "utils.h" /* tr_buildPath */
    2727
    28 #define KEY_ACTIVITY_DATE   "activity-date"
    29 #define KEY_ADDED_DATE      "added-date"
    30 #define KEY_CORRUPT         "corrupt"
    31 #define KEY_DONE_DATE       "done-date"
    32 #define KEY_DOWNLOAD_DIR    "destination"
    33 #define KEY_DND             "dnd"
    34 #define KEY_DOWNLOADED      "downloaded"
    35 #define KEY_MAX_PEERS       "max-peers"
    36 #define KEY_PAUSED          "paused"
    37 #define KEY_PEERS           "peers"
    38 #define KEY_PEERS6          "peers6"
    39 #define KEY_PRIORITY        "priority"
    40 #define KEY_PROGRESS        "progress"
    41 #define KEY_SPEEDLIMIT_OLD  "speed-limit"
    42 #define KEY_SPEEDLIMIT_UP   "speed-limit-up"
    43 #define KEY_SPEEDLIMIT_DOWN "speed-limit-down"
    44 #define KEY_RATIOLIMIT      "ratio-limit"
    45 #define KEY_UPLOADED        "uploaded"
     28#define KEY_ACTIVITY_DATE       "activity-date"
     29#define KEY_ADDED_DATE          "added-date"
     30#define KEY_CORRUPT             "corrupt"
     31#define KEY_DONE_DATE           "done-date"
     32#define KEY_DOWNLOAD_DIR        "destination"
     33#define KEY_DND                 "dnd"
     34#define KEY_DOWNLOADED          "downloaded"
     35#define KEY_MAX_PEERS           "max-peers"
     36#define KEY_PAUSED              "paused"
     37#define KEY_PEERS               "peers"
     38#define KEY_PEERS6              "peers6"
     39#define KEY_FILE_PRIORITIES     "priority"
     40#define KEY_BANDWIDTH_PRIORITY  "bandwidth-priority"
     41#define KEY_PROGRESS            "progress"
     42#define KEY_SPEEDLIMIT_OLD      "speed-limit"
     43#define KEY_SPEEDLIMIT_UP       "speed-limit-up"
     44#define KEY_SPEEDLIMIT_DOWN     "speed-limit-down"
     45#define KEY_RATIOLIMIT          "ratio-limit"
     46#define KEY_UPLOADED            "uploaded"
    4647
    4748#define KEY_SPEED                  "speed"
     
    190191        tr_free( dnd );
    191192        tr_free( dl );
    192         ret = TR_FR_PRIORITY;
     193        ret = TR_FR_DND;
    193194    }
    194195    else
     
    208209
    209210static void
    210 savePriorities( tr_benc *          dict,
    211                 const tr_torrent * tor )
     211saveFilePriorities( tr_benc * dict, const tr_torrent * tor )
    212212{
    213213    const tr_info *       inf = tr_torrentInfo( tor );
     
    216216    tr_benc *             list;
    217217
    218     list = tr_bencDictAddList( dict, KEY_PRIORITY, n );
     218    list = tr_bencDictAddList( dict, KEY_FILE_PRIORITIES, n );
    219219    for( i = 0; i < n; ++i )
    220220        tr_bencListAddInt( list, inf->files[i].priority );
     
    222222
    223223static uint64_t
    224 loadPriorities( tr_benc *    dict,
    225                 tr_torrent * tor )
     224loadFilePriorities( tr_benc * dict, tr_torrent * tor )
    226225{
    227226    uint64_t              ret = 0;
     
    230229    tr_benc *             list;
    231230
    232     if( tr_bencDictFindList( dict, KEY_PRIORITY, &list )
     231    if( tr_bencDictFindList( dict, KEY_FILE_PRIORITIES, &list )
    233232      && ( tr_bencListSize( list ) == n ) )
    234233    {
     
    238237            if( tr_bencGetInt( tr_bencListChild( list, i ), &priority ) )
    239238                tr_torrentInitFilePriority( tor, i, priority );
    240         ret = TR_FR_PRIORITY;
     239        ret = TR_FR_FILE_PRIORITIES;
    241240    }
    242241
     
    503502    tr_bencDictAddInt( &top, KEY_MAX_PEERS,
    504503                       tor->maxConnectedPeers );
     504    tr_bencDictAddInt( &top, KEY_BANDWIDTH_PRIORITY,
     505                       tr_torrentGetPriority( tor ) );
    505506    tr_bencDictAddBool( &top, KEY_PAUSED, !tor->isRunning );
    506507    savePeers( &top, tor );
    507     savePriorities( &top, tor );
     508    saveFilePriorities( &top, tor );
    508509    saveDND( &top, tor );
    509510    saveProgress( &top, tor );
     
    615616    }
    616617
     618    if( ( fieldsToLoad & TR_FR_BANDWIDTH_PRIORITY )
     619      && tr_bencDictFindInt( &top, KEY_BANDWIDTH_PRIORITY, &i )
     620      && tr_isPriority( i ) )
     621    {
     622        tr_torrentSetPriority( tor, i );
     623        fieldsLoaded |= TR_FR_BANDWIDTH_PRIORITY;
     624    }
     625
    617626    if( fieldsToLoad & TR_FR_PEERS )
    618627        fieldsLoaded |= loadPeers( &top, tor );
    619628
    620     if( fieldsToLoad & TR_FR_PRIORITY )
    621         fieldsLoaded |= loadPriorities( &top, tor );
     629    if( fieldsToLoad & TR_FR_FILE_PRIORITIES )
     630        fieldsLoaded |= loadFilePriorities( &top, tor );
    622631
    623632    if( fieldsToLoad & TR_FR_PROGRESS )
  • trunk/libtransmission/resume.h

    r7888 r8254  
    2020enum
    2121{
    22     TR_FR_DOWNLOADED     = ( 1 << 0 ),
    23     TR_FR_UPLOADED       = ( 1 << 1 ),
    24     TR_FR_CORRUPT        = ( 1 << 2 ),
    25     TR_FR_PEERS          = ( 1 << 3 ),
    26     TR_FR_PROGRESS       = ( 1 << 4 ),
    27     TR_FR_DND            = ( 1 << 5 ),
    28     TR_FR_PRIORITY       = ( 1 << 6 ),
    29     TR_FR_SPEEDLIMIT     = ( 1 << 7 ),
    30     TR_FR_RUN            = ( 1 << 8 ),
    31     TR_FR_DOWNLOAD_DIR   = ( 1 << 9 ),
    32     TR_FR_MAX_PEERS      = ( 1 << 10 ),
    33     TR_FR_ADDED_DATE     = ( 1 << 11 ),
    34     TR_FR_DONE_DATE      = ( 1 << 12 ),
    35     TR_FR_ACTIVITY_DATE  = ( 1 << 13 ),
    36     TR_FR_RATIOLIMIT     = ( 1 << 14 )
     22    TR_FR_DOWNLOADED          = ( 1 << 0 ),
     23    TR_FR_UPLOADED            = ( 1 << 1 ),
     24    TR_FR_CORRUPT             = ( 1 << 2 ),
     25    TR_FR_PEERS               = ( 1 << 3 ),
     26    TR_FR_PROGRESS            = ( 1 << 4 ),
     27    TR_FR_DND                 = ( 1 << 5 ),
     28    TR_FR_FILE_PRIORITIES     = ( 1 << 6 ),
     29    TR_FR_BANDWIDTH_PRIORITY  = ( 1 << 7 ),
     30    TR_FR_SPEEDLIMIT          = ( 1 << 8 ),
     31    TR_FR_RUN                 = ( 1 << 9 ),
     32    TR_FR_DOWNLOAD_DIR        = ( 1 << 10 ),
     33    TR_FR_MAX_PEERS           = ( 1 << 11 ),
     34    TR_FR_ADDED_DATE          = ( 1 << 12 ),
     35    TR_FR_DONE_DATE           = ( 1 << 13 ),
     36    TR_FR_ACTIVITY_DATE       = ( 1 << 14 ),
     37    TR_FR_RATIOLIMIT          = ( 1 << 15 )
    3738};
    3839
  • trunk/libtransmission/rpcimpl.c

    r8233 r8254  
    410410    else if( !strcmp( key, "announceURL" ) )
    411411        tr_bencDictAddStr( d, key, st->announceURL );
     412    else if( !strcmp( key, "bandwidthPriority" ) )
     413        tr_bencDictAddInt( d, key, tr_torrentGetPriority( tor ) );
    412414    else if( !strcmp( key, "comment" ) )
    413415        tr_bencDictAddStr( d, key, inf->comment ? inf->comment : "" );
     
    722724        tr_torrent * tor = torrents[i];
    723725
     726        if( tr_bencDictFindInt( args_in, "bandwidthPriority", &tmp ) )
     727            if( tr_isPriority( tmp ) )
     728                tr_torrentSetPriority( tor, tmp );
    724729        if( tr_bencDictFindList( args_in, "files-unwanted", &files ) )
    725730            setFileDLs( tor, FALSE, files );
  • trunk/libtransmission/torrent.c

    r8244 r8254  
    15221522    assert( tr_isTorrent( tor ) );
    15231523    assert( fileIndex < tor->info.fileCount );
    1524     assert( priority == TR_PRI_LOW || priority == TR_PRI_NORMAL || priority == TR_PRI_HIGH );
     1524    assert( tr_isPriority( priority ) );
    15251525
    15261526    file = &tor->info.files[fileIndex];
     
    16911691    tr_torrentSaveResume( tor );
    16921692    tr_torrentUnlock( tor );
     1693}
     1694
     1695/***
     1696****
     1697***/
     1698
     1699tr_priority_t
     1700tr_torrentGetPriority( const tr_torrent * tor )
     1701{
     1702    assert( tr_isTorrent( tor ) );
     1703
     1704    return tor->bandwidth->priority;
     1705}
     1706
     1707void
     1708tr_torrentSetPriority( tr_torrent * tor, tr_priority_t priority )
     1709{
     1710    assert( tr_isTorrent( tor ) );
     1711    assert( tr_isPriority( priority ) );
     1712
     1713    tor->bandwidth->priority = priority;
    16931714}
    16941715
  • trunk/libtransmission/transmission.h

    r8252 r8254  
    7272
    7373typedef int8_t tr_priority_t;
     74
     75static TR_INLINE tr_bool tr_isPriority( tr_priority_t p )
     76{
     77    return ( p == TR_PRI_LOW )
     78        || ( p == TR_PRI_NORMAL )
     79        || ( p == TR_PRI_HIGH );
     80}
    7481
    7582/**
     
    651658uint16_t   tr_sessionGetPeerLimitPerTorrent( const tr_session * );
    652659
     660tr_priority_t   tr_torrentGetPriority( const tr_torrent * );
     661void            tr_torrentSetPriority( tr_torrent *, tr_priority_t );
     662
    653663
    654664/**
Note: See TracChangeset for help on using the changeset viewer.