(trunk libT) #2383: Faster verification for local data with small piece size

This commit is contained in:
Charles Kerr 2009-09-06 14:05:06 +00:00
parent ccd3999211
commit c930f805cb
1 changed files with 18 additions and 10 deletions

View File

@ -35,6 +35,11 @@
****
***/
enum
{
MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY = 200
};
/* #define STOPWATCH */
static tr_bool
@ -45,6 +50,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
int64_t filePos = 0;
tr_bool changed = 0;
tr_bool hadPiece = 0;
time_t lastSleptAt = 0;
uint32_t piecePos = 0;
uint32_t pieceBytesRead = 0;
tr_file_index_t fileIndex = 0;
@ -52,7 +58,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
const int64_t buflen = tor->info.pieceSize;
uint8_t * buffer = tr_new( uint8_t, buflen );
#ifdef STOPWATCH
time_t now = time( NULL );
const time_t begin = time( NULL );
#endif
SHA1_Init( &sha );
@ -105,6 +111,7 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
/* if we're finishing a piece... */
if( leftInPiece == 0 )
{
time_t now;
tr_bool hasPiece;
uint8_t hash[SHA_DIGEST_LENGTH];
@ -121,14 +128,15 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
changed = TRUE;
}
tr_torrentSetPieceChecked( tor, pieceIndex, TRUE );
tor->anyDate = time( NULL );
now = time( NULL );
tor->anyDate = now;
/* going full-throttle on a verify can choke other processes'
* disk IO, so wait a fwe msec between pieces.
* The msec is arbitrary, and the "if" clause is to make sure we
* don't slow down verification of files that don't exist */
if( pieceBytesRead == tr_torPieceCountBytes( tor, pieceIndex ) )
tr_wait( 50 );
/* sleeping even just a few msec per second goes a long
* way towards reducing IO load... */
if( lastSleptAt != now ) {
lastSleptAt = now;
tr_wait( MSEC_TO_SLEEP_PER_SECOND_DURING_VERIFY );
}
SHA1_Init( &sha );
++pieceIndex;
@ -153,9 +161,9 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
#ifdef STOPWATCH
{
time_t now2 = time( NULL );
const time_t end = time( NULL );
fprintf( stderr, "it took %d seconds to verify %"PRIu64" bytes (%"PRIu64" bytes per second)\n",
(int)(now2-now), tor->info.totalSize, (uint64_t)(tor->info.totalSize/(1+(now2-now))) );
(int)(end-begin), tor->info.totalSize, (uint64_t)(tor->info.totalSize/(1+(end-begin))) );
}
#endif