some progress on the overall statistics, though probably not visible to end users yet

This commit is contained in:
Charles Kerr 2007-11-21 20:03:53 +00:00
parent 4a5a95b606
commit 91e672d428
8 changed files with 224 additions and 62 deletions

View File

@ -26,6 +26,7 @@ libtransmission_a_SOURCES = \
publish.c \
ratecontrol.c \
shared.c \
stats.c \
torrent.c \
tracker.c \
transmission.c \

View File

@ -34,6 +34,29 @@
#include "bencode.h"
#include "utils.h"
/**
***
**/
static int
tr_bencIsInt ( const benc_val_t * val ) {
return val!=NULL && val->type==TYPE_INT;
}
static int
tr_bencIsList( const benc_val_t * val ) {
return val!=NULL && val->type==TYPE_LIST;
}
static int
tr_bencIsDict( const benc_val_t * val ) {
return val!=NULL && val->type==TYPE_DICT;
}
/**
***
**/
/* setting to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */
#define LIST_SIZE 20 /* number of items to increment list/dict buffer by */
@ -194,7 +217,7 @@ static void __bencPrint( benc_val_t * val, int space )
switch( val->type )
{
case TYPE_INT:
fprintf( stderr, "int: %"PRId64"\n", val->val.i );
fprintf( stderr, "int: %"PRId64"\n", tr_bencGetInt(val) );
break;
case TYPE_STR:
@ -413,7 +436,7 @@ saveImpl( struct evbuffer * out, const benc_val_t * val )
switch( val->type )
{
case TYPE_INT:
evbuffer_add_printf( out, "i%"PRId64"e", val->val.i );
evbuffer_add_printf( out, "i%"PRId64"e", tr_bencGetInt(val) );
break;
case TYPE_STR:
@ -470,26 +493,16 @@ tr_bencSave( const benc_val_t * val, int * len )
***
**/
int
tr_bencIsStr ( const benc_val_t * val )
benc_val_t*
tr_bencDictFindType( benc_val_t * val, const char * key, int type )
{
return val!=NULL && val->type==TYPE_STR;
benc_val_t * ret = tr_bencDictFind( val, key );
return ret && ret->type == type ? ret : NULL;
}
int
tr_bencIsInt ( const benc_val_t * val )
int64_t
tr_bencGetInt ( const benc_val_t * val )
{
return val!=NULL && val->type==TYPE_INT;
}
int
tr_bencIsList( const benc_val_t * val )
{
return val!=NULL && val->type==TYPE_LIST;
}
int
tr_bencIsDict( const benc_val_t * val )
{
return val!=NULL && val->type==TYPE_DICT;
assert( tr_bencIsInt( val ) );
return val->val.i;
}

View File

@ -61,6 +61,7 @@ int _tr_bencLoad( char * buf, int len, benc_val_t * val,
void tr_bencPrint( benc_val_t * val );
void tr_bencFree( benc_val_t * val );
benc_val_t * tr_bencDictFind( benc_val_t * val, const char * key );
benc_val_t * tr_bencDictFindType( benc_val_t * val, const char * key, int type );
benc_val_t * tr_bencDictFindFirst( benc_val_t * val, ... );
/* marks a string as 'do not free' and returns it */
@ -88,9 +89,6 @@ benc_val_t * tr_bencDictAdd( benc_val_t * dict, const char * key );
char* tr_bencSave( const benc_val_t * val, int * len );
int tr_bencIsStr ( const benc_val_t * val );
int tr_bencIsInt ( const benc_val_t * val );
int tr_bencIsList ( const benc_val_t * val );
int tr_bencIsDict ( const benc_val_t * val );
int64_t tr_bencGetInt ( const benc_val_t * val );
#endif

View File

@ -43,7 +43,6 @@ typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
#define FALSE 0
#endif
void tr_torrentRecheckCompleteness( tr_torrent * );
int tr_trackerInfoInit( struct tr_tracker_info * info,
@ -192,6 +191,9 @@ struct tr_handle
int statCur;
uint8_t isClosed;
tr_session_stats sessionStats;
tr_session_stats cumulativeStats;
};
void tr_globalLock ( struct tr_handle * );

View File

@ -32,6 +32,7 @@
#include "peer-mgr-private.h"
#include "peer-msgs.h"
#include "ratecontrol.h"
#include "stats.h"
#include "trevent.h"
#include "utils.h"
@ -783,17 +784,14 @@ parseLtepHandshake( tr_peermsgs * msgs, int len, struct evbuffer * inbuf )
#endif
/* does the peer prefer encrypted connections? */
sub = tr_bencDictFind( &val, "e" );
if( tr_bencIsInt( sub ) )
if(( sub = tr_bencDictFindType( &val, "e", TYPE_INT )))
msgs->info->encryption_preference = sub->val.i
? ENCRYPTION_PREFERENCE_YES
: ENCRYPTION_PREFERENCE_NO;
/* check supported messages for utorrent pex */
sub = tr_bencDictFind( &val, "m" );
if( tr_bencIsDict( sub ) ) {
sub = tr_bencDictFind( sub, "ut_pex" );
if( tr_bencIsInt( sub ) ) {
if(( sub = tr_bencDictFindType( &val, "m", TYPE_DICT ))) {
if(( sub = tr_bencDictFindType( sub, "ut_pex", TYPE_INT ))) {
msgs->peerSupportsPex = 1;
msgs->ut_pex_id = (uint8_t) sub->val.i;
dbgmsg( msgs, "msgs->ut_pex is %d", (int)msgs->ut_pex_id );
@ -801,8 +799,7 @@ parseLtepHandshake( tr_peermsgs * msgs, int len, struct evbuffer * inbuf )
}
/* get peer's listening port */
sub = tr_bencDictFind( &val, "p" );
if( tr_bencIsInt( sub ) ) {
if(( sub = tr_bencDictFindType( &val, "p", TYPE_INT ))) {
msgs->info->port = htons( (uint16_t)sub->val.i );
dbgmsg( msgs, "msgs->port is now %hu", msgs->info->port );
}
@ -823,14 +820,13 @@ parseUtPex( tr_peermsgs * msgs, int msglen, struct evbuffer * inbuf )
tmp = tr_new( uint8_t, msglen );
tr_peerIoReadBytes( msgs->io, inbuf, tmp, msglen );
if( tr_bencLoad( tmp, msglen, &val, NULL ) || !tr_bencIsDict( &val ) ) {
if( tr_bencLoad( tmp, msglen, &val, NULL ) || ( val.type != TYPE_DICT ) ) {
dbgmsg( msgs, "GET can't read extended-pex dictionary" );
tr_free( tmp );
return;
}
sub = tr_bencDictFind( &val, "added" );
if( tr_bencIsStr(sub) && ((sub->val.s.i % 6) == 0)) {
if(( sub = tr_bencDictFindType( &val, "added", TYPE_STR ))) {
const int n = sub->val.s.i / 6 ;
dbgmsg( msgs, "got %d peers from uT pex", n );
tr_peerMgrAddPeers( msgs->handle->peerMgr,
@ -1042,9 +1038,9 @@ clientGotBytes( tr_peermsgs * msgs, uint32_t byteCount )
tr_rcTransferred( msgs->info->rcToClient, byteCount );
tr_rcTransferred( tor->download, byteCount );
tr_rcTransferred( tor->handle->download, byteCount );
tr_statsAddDownloaded( msgs->handle, byteCount );
}
static int
readBtPiece( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen )
{
@ -1266,6 +1262,7 @@ peerGotBytes( tr_peermsgs * msgs, uint32_t byteCount )
tr_rcTransferred( msgs->info->rcToPeer, byteCount );
tr_rcTransferred( tor->upload, byteCount );
tr_rcTransferred( tor->handle->upload, byteCount );
tr_statsAddUploaded( msgs->handle, byteCount );
}
static size_t

147
libtransmission/stats.c Normal file
View File

@ -0,0 +1,147 @@
/*
* This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
*
* This file is licensed by the GPL version 2. Works owned by the
* Transmission project are granted a special exemption to clause 2(b)
* so that the bulk of its code can remain under the MIT license.
* This exemption does not extend to derived works not owned by
* the Transmission project.
*
* $Id: peer-msgs.c 3906 2007-11-20 17:29:56Z charles $
*/
#include <string.h> /* memset */
#include "transmission.h"
#include "bencode.h"
#include "platform.h" /* tr_getPrefsDirectory */
#include "utils.h" /* tr_buildPath */
/***
****
***/
static void
parseCumulativeStats( tr_session_stats * setme,
const uint8_t * content,
size_t len )
{
benc_val_t top;
if( !tr_bencLoad( content, len, &top, NULL ) )
{
const benc_val_t * val;
if(( val = tr_bencDictFindType( &top, "uploaded-gigabytes", TYPE_INT )))
setme->uploadedGigs = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "uploaded-bytes", TYPE_INT )))
setme->uploadedBytes = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "downloaded-gigabytes", TYPE_INT )))
setme->downloadedGigs = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "downloaded-bytes", TYPE_INT )))
setme->downloadedBytes = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "files-added", TYPE_INT )))
setme->filesAdded = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "session-count", TYPE_INT )))
setme->sessionCount = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "seconds-active", TYPE_INT )))
setme->secondsActive = (uint64_t) tr_bencGetInt( val );
tr_bencFree( &top );
}
}
static void
loadCumulativeStats( tr_session_stats * setme )
{
size_t len;
uint8_t * content;
char path[MAX_PATH_LENGTH];
tr_buildPath( path, sizeof(path), tr_getPrefsDirectory(), "stats.benc", NULL );
content = tr_loadFile( path, &len );
if( content != NULL )
parseCumulativeStats( setme, content, len );
tr_free( content );
}
/***
****
***/
void
tr_statsInit( tr_handle * handle )
{
memset( &handle->sessionStats, 0, sizeof( tr_session_stats ) );
memset( &handle->cumulativeStats, 0, sizeof( tr_session_stats ) );
loadCumulativeStats( &handle->cumulativeStats );
}
void
tr_statsClose( const tr_handle * handle UNUSED )
{
fprintf( stderr, "FIXME" );
}
static void
updateRatio( tr_session_stats * stats UNUSED )
{
fprintf( stderr, "FIXME" );
}
void
tr_getSessionStats( const tr_handle * handle,
tr_session_stats * setme )
{
*setme = handle->sessionStats;
updateRatio( setme );
}
void
tr_getCumulativeSessionStats( const tr_handle * handle,
tr_session_stats * setme )
{
*setme = handle->cumulativeStats;
updateRatio( setme );
}
/**
***
**/
static void
add( uint64_t * gigs, uint64_t * bytes, uint32_t addme )
{
uint64_t i;
const uint64_t GIGABYTE = 1073741824;
i = *bytes;
i += addme;
*gigs += i / GIGABYTE;
*bytes = i % GIGABYTE;
}
void
tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
{
add( &handle->sessionStats.uploadedGigs,
&handle->sessionStats.uploadedBytes, bytes );
add( &handle->cumulativeStats.uploadedGigs,
&handle->cumulativeStats.uploadedBytes, bytes );
}
void
tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
{
add( &handle->sessionStats.downloadedGigs,
&handle->sessionStats.downloadedBytes, bytes );
add( &handle->cumulativeStats.downloadedGigs,
&handle->cumulativeStats.downloadedBytes, bytes );
}

25
libtransmission/stats.h Normal file
View File

@ -0,0 +1,25 @@
/*
* This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
*
* This file is licensed by the GPL version 2. Works owned by the
* Transmission project are granted a special exemption to clause 2(b)
* so that the bulk of its code can remain under the MIT license.
* This exemption does not extend to derived works not owned by
* the Transmission project.
*
* $Id: peer-msgs.h 3818 2007-11-13 05:36:43Z charles $
*/
#ifndef TR_STATS_H
#define TR_STATS_H
void tr_statsInit( tr_handle * handle );
void tr_statsClose( const tr_handle * handle );
void tr_statsAddUploaded( tr_handle * handle, uint32_t bytes );
void tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes );
#endif

View File

@ -41,6 +41,7 @@
#include "platform.h"
#include "ratecontrol.h"
#include "shared.h"
#include "stats.h"
#include "trevent.h"
#include "utils.h"
@ -149,6 +150,8 @@ tr_handle * tr_init( const char * tag )
tr_inf( TR_NAME " " LONG_VERSION_STRING " started" );
tr_statsInit( h );
return h;
}
@ -335,36 +338,12 @@ tr_close( tr_handle * h )
while( h->events && !deadlineReached( deadline ) )
tr_wait( 100 );
tr_statsClose( h );
tr_lockFree( h->lock );
free( h->tag );
free( h );
}
void
tr_getSessionStats( const tr_handle * handle,
tr_session_stats * setme )
{
assert( handle != NULL );
assert( setme != NULL );
/* FIXME */
setme->downloadedGigs = 4;
setme->downloadedBytes = 8;
setme->uploadedGigs = 15;
setme->uploadedBytes = 16;
setme->ratio = 23;
setme->filesAdded = 42;
setme->sessionCount = 666;
setme->secondsActive = 2112;
}
void
tr_getCumulativeSessionStats( const tr_handle * handle,
tr_session_stats * setme )
{
tr_getSessionStats( handle, setme );
}
tr_torrent **
tr_loadTorrents ( tr_handle * h,
const char * fallbackDestination,