added display of hashing speed. Changed hashing buffer size to 10MB to improve performance

This commit is contained in:
csoler 2017-10-29 21:24:34 +01:00
parent 2e9f5202e0
commit 3bb694f439
4 changed files with 49 additions and 21 deletions

View File

@ -24,6 +24,7 @@
*/ */
#include "util/rsdir.h" #include "util/rsdir.h"
#include "util/rsprint.h" #include "util/rsprint.h"
#include "util/rsscopetimer.h"
#include "rsserver/p3face.h" #include "rsserver/p3face.h"
#include "pqi/authssl.h" #include "pqi/authssl.h"
#include "hash_cache.h" #include "hash_cache.h"
@ -43,8 +44,11 @@ HashStorage::HashStorage(const std::string& save_file_name)
mLastSaveTime = 0 ; mLastSaveTime = 0 ;
mTotalSizeToHash = 0; mTotalSizeToHash = 0;
mTotalFilesToHash = 0; mTotalFilesToHash = 0;
mCurrentHashingSpeed = 0 ;
mMaxStorageDurationDays = DEFAULT_HASH_STORAGE_DURATION_DAYS ; mMaxStorageDurationDays = DEFAULT_HASH_STORAGE_DURATION_DAYS ;
mHashingProcessPaused = false; mHashingProcessPaused = false;
mHashedBytes = 0 ;
mHashingTime = 0 ;
{ {
RS_STACK_MUTEX(mHashMtx) ; RS_STACK_MUTEX(mHashMtx) ;
@ -178,32 +182,48 @@ void HashStorage::data_tick()
#endif #endif
std::string tmpout; std::string tmpout;
rs_sprintf(tmpout, "%lu/%lu (%s - %d%%) : %s", (unsigned long int)mHashCounter+1, (unsigned long int)mTotalFilesToHash, friendlyUnit(mTotalHashedSize).c_str(), int(mTotalHashedSize/double(mTotalSizeToHash)*100.0), job.full_path.c_str()) ;
if(mCurrentHashingSpeed > 0)
rs_sprintf(tmpout, "%lu/%lu (%s - %d%%, %d MB/s) : %s", (unsigned long int)mHashCounter+1, (unsigned long int)mTotalFilesToHash, friendlyUnit(mTotalHashedSize).c_str(), int(mTotalHashedSize/double(mTotalSizeToHash)*100.0), mCurrentHashingSpeed,job.full_path.c_str()) ;
else
rs_sprintf(tmpout, "%lu/%lu (%s - %d%%) : %s", (unsigned long int)mHashCounter+1, (unsigned long int)mTotalFilesToHash, friendlyUnit(mTotalHashedSize).c_str(), int(mTotalHashedSize/double(mTotalSizeToHash)*100.0), job.full_path.c_str()) ;
RsServer::notify()->notifyHashingInfo(NOTIFY_HASHTYPE_HASH_FILE, tmpout) ; RsServer::notify()->notifyHashingInfo(NOTIFY_HASHTYPE_HASH_FILE, tmpout) ;
if(RsDirUtil::getFileHash(job.full_path, hash,size, this)) double seconds_origin = RsScopeTimer::currentTime() ;
{
// store the result if(RsDirUtil::getFileHash(job.full_path, hash,size, this))
{
// store the result
#ifdef HASHSTORAGE_DEBUG #ifdef HASHSTORAGE_DEBUG
std::cerr << "done."<< std::endl; std::cerr << "done."<< std::endl;
#endif #endif
RS_STACK_MUTEX(mHashMtx) ; RS_STACK_MUTEX(mHashMtx) ;
HashStorageInfo& info(mFiles[job.real_path]); HashStorageInfo& info(mFiles[job.real_path]);
info.filename = job.real_path ; info.filename = job.real_path ;
info.size = size ; info.size = size ;
info.modf_stamp = job.ts ; info.modf_stamp = job.ts ;
info.time_stamp = time(NULL); info.time_stamp = time(NULL);
info.hash = hash; info.hash = hash;
mChanged = true ; mChanged = true ;
mTotalHashedSize += size ; mTotalHashedSize += size ;
} }
else else
std::cerr << "ERROR: cannot hash file " << job.full_path << std::endl; std::cerr << "ERROR: cannot hash file " << job.full_path << std::endl;
mHashingTime += RsScopeTimer::currentTime() - seconds_origin ;
mHashedBytes += size ;
if(mHashingTime > 3)
{
mCurrentHashingSpeed = (int)(mHashedBytes / mHashingTime ) / (1024*1024) ;
mHashingTime = 0 ;
mHashedBytes = 0 ;
}
++mHashCounter ; ++mHashCounter ;
} }

View File

@ -140,5 +140,11 @@ private:
uint64_t mTotalHashedSize ; uint64_t mTotalHashedSize ;
uint64_t mTotalFilesToHash ; uint64_t mTotalFilesToHash ;
time_t mLastSaveTime ; time_t mLastSaveTime ;
// The following is used to estimate hashing speed.
double mHashingTime ;
uint64_t mHashedBytes ;
uint32_t mCurrentHashingSpeed ; // in MB/s
}; };

View File

@ -536,7 +536,10 @@ bool RsDirUtil::getFileHash(const std::string& filepath, RsFileHash &hash, uint6
int len; int len;
SHA_CTX *sha_ctx = new SHA_CTX; SHA_CTX *sha_ctx = new SHA_CTX;
unsigned char sha_buf[SHA_DIGEST_LENGTH]; unsigned char sha_buf[SHA_DIGEST_LENGTH];
unsigned char gblBuf[512];
static const uint32_t HASH_BUFFER_SIZE = 1024*1024*10 ;// allocate a 10MB buffer. Too small a buffer will cause multiple HD hits and slow down the hashing process.
RsTemporaryMemory gblBuf(HASH_BUFFER_SIZE) ;
//unsigned char gblBuf[512];
/* determine size */ /* determine size */
fseeko64(fd, 0, SEEK_END); fseeko64(fd, 0, SEEK_END);
@ -548,7 +551,7 @@ bool RsDirUtil::getFileHash(const std::string& filepath, RsFileHash &hash, uint6
int runningCheckCount = 0; int runningCheckCount = 0;
SHA1_Init(sha_ctx); SHA1_Init(sha_ctx);
while(isRunning && (len = fread(gblBuf,1, 512, fd)) > 0) while(isRunning && (len = fread(gblBuf,1, HASH_BUFFER_SIZE, fd)) > 0)
{ {
SHA1_Update(sha_ctx, gblBuf, len); SHA1_Update(sha_ctx, gblBuf, len);

View File

@ -43,8 +43,7 @@ public:
void start(); void start();
double duration(); double duration();
private: static double currentTime();
double currentTime();
private: private:
std::string _name ; std::string _name ;