Changeset 4920


Ignore:
Timestamp:
Feb 4, 2008, 7:52:00 PM (14 years ago)
Author:
charles
Message:

make the stats code a little more difficult to corrupt

Location:
branches/1.0x/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.0x/libtransmission/stats.c

    r4778 r4920  
    2525{
    2626    tr_session_stats single;
    27     tr_session_stats cumulative;
     27    tr_session_stats old;
    2828    time_t startTime;
    2929};
     
    116116{
    117117    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
    118     loadCumulativeStats( &stats->cumulative );
    119     stats->cumulative.sessionCount++;
    120     stats->startTime = time(NULL);
     118    loadCumulativeStats( &stats->old );
     119    stats->single.sessionCount = 1;
     120    stats->startTime = time( NULL );
    121121    handle->sessionStats = stats;
    122122}
     
    125125tr_statsClose( tr_handle * handle )
    126126{
    127     tr_session_stats tmp;
    128     tr_getCumulativeSessionStats( handle, &tmp );
    129     saveCumulativeStats( &tmp );
     127    tr_session_stats cumulative;
     128    tr_getCumulativeSessionStats( handle, &cumulative );
     129    saveCumulativeStats( &cumulative );
    130130
    131131    tr_free( handle->sessionStats );
     
    133133}
    134134
     135static struct tr_stats_handle *
     136getStats( const tr_handle * handle )
     137{
     138    static struct tr_stats_handle nullObject;
     139
     140    return handle && handle->sessionStats
     141        ? handle->sessionStats
     142        : &nullObject;
     143}
     144
     145/***
     146****
     147***/
     148
    135149static void
    136150updateRatio( tr_session_stats * setme )
    137151{
    138     setme->ratio = tr_getRatio( setme->uploadedBytes, setme->downloadedBytes );
     152    setme->ratio = tr_getRatio( setme->uploadedBytes,
     153                                setme->downloadedBytes );
     154}
     155
     156static void
     157addStats( tr_session_stats       * setme,
     158          const tr_session_stats * a,
     159          const tr_session_stats * b )
     160{
     161    setme->uploadedBytes   = a->uploadedBytes   + b->uploadedBytes;
     162    setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
     163    setme->filesAdded      = a->filesAdded      + b->filesAdded;
     164    setme->sessionCount    = a->sessionCount    + b->sessionCount;
     165    setme->secondsActive   = a->secondsActive   + b->secondsActive;
     166    updateRatio( setme );
    139167}
    140168
     
    143171                    tr_session_stats  * setme )
    144172{
    145     const struct tr_stats_handle * stats = handle->sessionStats;
     173    const struct tr_stats_handle * stats = getStats( handle );
    146174    *setme = stats->single;
    147     setme->secondsActive += ( time(NULL) - stats->startTime );
     175    setme->secondsActive = time( NULL ) - stats->startTime;
    148176    updateRatio( setme );
    149177}
     
    153181                              tr_session_stats  * setme )
    154182{
    155     const struct tr_stats_handle * stats = handle->sessionStats;
    156     *setme = stats->cumulative;
    157     setme->secondsActive += ( time(NULL) - stats->startTime );
    158     updateRatio( setme );
     183    tr_session_stats current;
     184    tr_getSessionStats( handle, &current );
     185    addStats( setme, &getStats(handle)->old, &current );
    159186}
    160187
     
    166193tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
    167194{
    168     struct tr_stats_handle * stats = handle->sessionStats;
    169     stats->single.uploadedBytes += bytes;
    170     stats->cumulative.uploadedBytes += bytes;
     195    getStats(handle)->single.uploadedBytes += bytes;
    171196}
    172197
     
    174199tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
    175200{
    176     struct tr_stats_handle * stats = handle->sessionStats;
    177     stats->single.downloadedBytes += bytes;
    178     stats->cumulative.downloadedBytes += bytes;
     201    getStats(handle)->single.downloadedBytes += bytes;
    179202}
    180203
     
    182205tr_statsFileCreated( tr_handle * handle )
    183206{
    184     struct tr_stats_handle * stats = handle->sessionStats;
    185     ++stats->cumulative.filesAdded;
    186     ++stats->single.filesAdded;
    187 }
     207    getStats(handle)->single.filesAdded++;
     208}
  • branches/1.0x/libtransmission/transmission.c

    r4776 r4920  
    393393    const uint64_t deadline = tr_date( ) + maxwait_msec;
    394394
     395    tr_statsClose( h );
     396
    395397    tr_runInEventThread( h, tr_closeImpl, h );
    396398    while( !h->isClosed && !deadlineReached( deadline ) )
     
    402404
    403405    tr_fdClose( );
    404     tr_statsClose( h );
    405406    tr_lockFree( h->lock );
    406407    free( h->tag );
Note: See TracChangeset for help on using the changeset viewer.