fixed bugs in upload speed computation

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1007 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2009-02-05 00:01:51 +00:00
parent 6ac5d40df9
commit bb904ea8e8
2 changed files with 28 additions and 11 deletions

View file

@ -5,9 +5,9 @@
#include <stdlib.h> #include <stdlib.h>
ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string
hash) : mSize(size), hash(hash), file_name(path), fd(NULL),transfer_rate(0) hash) : mSize(size), hash(hash), file_name(path), fd(NULL),transfer_rate(0),total_size(0)
{ {
lastTS = times(NULL) ; lastTS = time(NULL) ;
} }
ftFileProvider::~ftFileProvider(){ ftFileProvider::~ftFileProvider(){
@ -48,14 +48,31 @@ bool ftFileProvider::FileDetails(FileInfo &info)
info.path = file_name; info.path = file_name;
info.fname = RsDirUtil::getTopDir(file_name); info.fname = RsDirUtil::getTopDir(file_name);
info.transfered = req_loc ; info.transfered = req_loc ;
info.lastTS = lastTS;
info.status = FT_STATE_DOWNLOADING ; info.status = FT_STATE_DOWNLOADING ;
info.peers.clear() ; info.peers.clear() ;
TransferInfo inf ; TransferInfo inf ;
inf.peerId = lastRequestor ; inf.peerId = lastRequestor ;
inf.tfRate = transfer_rate/1024.0 ;
inf.status = FT_STATE_DOWNLOADING ; inf.status = FT_STATE_DOWNLOADING ;
long int clk = sysconf(_SC_CLK_TCK) ;
time_t now = time(NULL) ;
clock_t now_t = times(NULL) ;
long int diff = (long int)now_t - (long int)lastTS_t ; // in bytes/s. Average over multiple samples
if(diff > 100)
{
transfer_rate = total_size / (float)diff * clk ;
total_size = 0 ;
lastTS_t = now_t ;
}
inf.tfRate = transfer_rate/1024.0 ;
info.tfRate = transfer_rate/1024.0 ;
info.peers.push_back(inf) ; info.peers.push_back(inf) ;
/* Use req_loc / req_size to estimate data rate */ /* Use req_loc / req_size to estimate data rate */
@ -123,15 +140,11 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t &chunk_size, void *da
* (d) timestamp * (d) timestamp
*/ */
clock_t now = times(NULL); time_t now = time(NULL);
req_loc = offset; req_loc = offset;
req_size = data_size;
transfer_rate = req_size / (float)(std::max(sysconf(_SC_CLK_TCK),(long int)now - (long int)lastTS) / (float)sysconf(_SC_CLK_TCK)) ; // in bytes/s. Average over two samples
std::cout << "req_size = " << req_size << ", now="<< now << ", lastTS=" << lastTS << ", lastTS-now=" << now-lastTS << ", speed = " << transfer_rate << ", clk=" << sysconf(_SC_CLK_TCK) << std::endl ;
lastTS = now ; lastTS = now ;
req_size = data_size;
total_size += req_size ;
} }
else { else {
std::cerr << "No data to read" << std::endl; std::cerr << "No data to read" << std::endl;

View file

@ -65,8 +65,12 @@ virtual int initializeFileAttrs(); /* does for both */
std::string lastRequestor; std::string lastRequestor;
uint64_t req_loc; uint64_t req_loc;
uint32_t req_size; uint32_t req_size;
clock_t lastTS; time_t lastTS;
clock_t lastTS_t;
// these two are used for speed estimation
float transfer_rate ; float transfer_rate ;
uint32_t total_size ;
/* /*
* Mutex Required for stuff below * Mutex Required for stuff below