1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-19 18:25:38 +00:00

make the stats code a little more difficult to corrupt

This commit is contained in:
Charles Kerr 2008-02-04 19:54:47 +00:00
parent 0219e3deff
commit 17e76707f2
2 changed files with 46 additions and 24 deletions

View file

@ -24,7 +24,7 @@
struct tr_stats_handle struct tr_stats_handle
{ {
tr_session_stats single; tr_session_stats single;
tr_session_stats cumulative; tr_session_stats old;
time_t startTime; time_t startTime;
}; };
@ -115,36 +115,64 @@ void
tr_statsInit( tr_handle * handle ) tr_statsInit( tr_handle * handle )
{ {
struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 ); struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
loadCumulativeStats( &stats->cumulative ); loadCumulativeStats( &stats->old );
stats->cumulative.sessionCount++; stats->single.sessionCount = 1;
stats->startTime = time(NULL); stats->startTime = time( NULL );
handle->sessionStats = stats; handle->sessionStats = stats;
} }
void void
tr_statsClose( tr_handle * handle ) tr_statsClose( tr_handle * handle )
{ {
tr_session_stats tmp; tr_session_stats cumulative;
tr_getCumulativeSessionStats( handle, &tmp ); tr_getCumulativeSessionStats( handle, &cumulative );
saveCumulativeStats( &tmp ); saveCumulativeStats( &cumulative );
tr_free( handle->sessionStats ); tr_free( handle->sessionStats );
handle->sessionStats = NULL; handle->sessionStats = NULL;
} }
static struct tr_stats_handle *
getStats( const tr_handle * handle )
{
static struct tr_stats_handle nullObject;
return handle && handle->sessionStats
? handle->sessionStats
: &nullObject;
}
/***
****
***/
static void static void
updateRatio( tr_session_stats * setme ) updateRatio( tr_session_stats * setme )
{ {
setme->ratio = tr_getRatio( setme->uploadedBytes, setme->downloadedBytes ); setme->ratio = tr_getRatio( setme->uploadedBytes,
setme->downloadedBytes );
}
static void
addStats( tr_session_stats * setme,
const tr_session_stats * a,
const tr_session_stats * b )
{
setme->uploadedBytes = a->uploadedBytes + b->uploadedBytes;
setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
setme->filesAdded = a->filesAdded + b->filesAdded;
setme->sessionCount = a->sessionCount + b->sessionCount;
setme->secondsActive = a->secondsActive + b->secondsActive;
updateRatio( setme );
} }
void void
tr_getSessionStats( const tr_handle * handle, tr_getSessionStats( const tr_handle * handle,
tr_session_stats * setme ) tr_session_stats * setme )
{ {
const struct tr_stats_handle * stats = handle->sessionStats; const struct tr_stats_handle * stats = getStats( handle );
*setme = stats->single; *setme = stats->single;
setme->secondsActive += ( time(NULL) - stats->startTime ); setme->secondsActive = time( NULL ) - stats->startTime;
updateRatio( setme ); updateRatio( setme );
} }
@ -152,10 +180,9 @@ void
tr_getCumulativeSessionStats( const tr_handle * handle, tr_getCumulativeSessionStats( const tr_handle * handle,
tr_session_stats * setme ) tr_session_stats * setme )
{ {
const struct tr_stats_handle * stats = handle->sessionStats; tr_session_stats current;
*setme = stats->cumulative; tr_getSessionStats( handle, &current );
setme->secondsActive += ( time(NULL) - stats->startTime ); addStats( setme, &getStats(handle)->old, &current );
updateRatio( setme );
} }
/** /**
@ -165,23 +192,17 @@ tr_getCumulativeSessionStats( const tr_handle * handle,
void void
tr_statsAddUploaded( tr_handle * handle, uint32_t bytes ) tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
{ {
struct tr_stats_handle * stats = handle->sessionStats; getStats(handle)->single.uploadedBytes += bytes;
stats->single.uploadedBytes += bytes;
stats->cumulative.uploadedBytes += bytes;
} }
void void
tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes ) tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
{ {
struct tr_stats_handle * stats = handle->sessionStats; getStats(handle)->single.downloadedBytes += bytes;
stats->single.downloadedBytes += bytes;
stats->cumulative.downloadedBytes += bytes;
} }
void void
tr_statsFileCreated( tr_handle * handle ) tr_statsFileCreated( tr_handle * handle )
{ {
struct tr_stats_handle * stats = handle->sessionStats; getStats(handle)->single.filesAdded++;
++stats->cumulative.filesAdded;
++stats->single.filesAdded;
} }

View file

@ -393,6 +393,8 @@ tr_close( tr_handle * h )
const int maxwait_msec = SHUTDOWN_MAX_SECONDS * 1000; const int maxwait_msec = SHUTDOWN_MAX_SECONDS * 1000;
const uint64_t deadline = tr_date( ) + maxwait_msec; const uint64_t deadline = tr_date( ) + maxwait_msec;
tr_statsClose( h );
tr_runInEventThread( h, tr_closeImpl, h ); tr_runInEventThread( h, tr_closeImpl, h );
while( !h->isClosed && !deadlineReached( deadline ) ) while( !h->isClosed && !deadlineReached( deadline ) )
tr_wait( 100 ); tr_wait( 100 );
@ -402,7 +404,6 @@ tr_close( tr_handle * h )
tr_wait( 100 ); tr_wait( 100 );
tr_fdClose( ); tr_fdClose( );
tr_statsClose( h );
tr_lockFree( h->lock ); tr_lockFree( h->lock );
free( h->tag ); free( h->tag );
free( h ); free( h );