Skip to content

Commit

Permalink
Merge pull request bitcoin#3 from gandrewstone/marcusdiaz-getstat-times
Browse files Browse the repository at this point in the history
2 small changes and 2 additions
  • Loading branch information
marcusdiaz committed Oct 31, 2017
2 parents aad85d6 + d93e81c commit 6331a25
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ boost::thread_specific_ptr<LockStack> lockstack;


std::atomic<bool> fIsInitialBlockDownload{false};
std::atomic<bool> fRescan{false}; // this flag is set to true when a wallet rescan has been invoked.

// main.cpp CriticalSections:
CCriticalSection cs_LastBlockFile;
Expand Down Expand Up @@ -128,7 +129,7 @@ int interruptIntervals[] = {30, 30 * 12, 30 * 12 * 24, 30 * 12 * 24 * 30};

CTxMemPool mempool(::minRelayTxFee);

boost::posix_time::milliseconds statMinInterval(10000);
std::chrono::milliseconds statMinInterval(10000);
boost::asio::io_service stat_io_service;

std::list<CStatBase *> mallocedStats;
Expand Down
3 changes: 2 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "ui_interface.h"
#include "unlimited.h"
#include "utilstrencodings.h"
#include "wallet/wallet.h"

#ifdef WIN32
#include <string.h>
Expand Down Expand Up @@ -68,6 +67,8 @@
#endif
#endif

extern std::atomic<bool> fRescan;

using namespace std;

namespace
Expand Down
58 changes: 30 additions & 28 deletions src/stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#define STAT_H

#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <chrono>
// c++11 #include <type_traits>
#include "univalue/include/univalue.h"

Expand All @@ -34,7 +36,7 @@ typedef std::map<CStatKey, CStatBase *> CStatMap;

extern CStatMap statistics;
extern boost::asio::io_service stat_io_service;
extern boost::posix_time::milliseconds statMinInterval;
extern std::chrono::milliseconds statMinInterval;

template <typename NUM>
void statAverage(NUM &tally, const NUM &cur, unsigned int sampleCounts)
Expand Down Expand Up @@ -181,32 +183,30 @@ class CStatHistory : public CStat<DataType, RecordType>
{
protected:
unsigned int op;
boost::asio::deadline_timer timer;
boost::asio::steady_timer timer;
RecordType history[STATISTICS_NUM_RANGES][STATISTICS_SAMPLES];
int64_t **historyTime = new int64_t *[STATISTICS_NUM_RANGES];
int64_t historyTime[STATISTICS_NUM_RANGES][STATISTICS_SAMPLES];
int loc[STATISTICS_NUM_RANGES];
int len[STATISTICS_NUM_RANGES];
uint64_t timerCount;
std::chrono::steady_clock::time_point timerStartSteady;
unsigned int sampleCount;
RecordType total;

public:
CStatHistory() : CStat<DataType, RecordType>(), op(STAT_OP_SUM | STAT_KEEP_COUNT), timer(stat_io_service)
{
initHistoryTime();
Clear(false);
}
CStatHistory(const char *name, unsigned int operation = STAT_OP_SUM)
: CStat<DataType, RecordType>(name), op(operation), timer(stat_io_service)
{
initHistoryTime();
Clear();
}

CStatHistory(const std::string &name, unsigned int operation = STAT_OP_SUM)
: CStat<DataType, RecordType>(name), op(operation), timer(stat_io_service)
{
initHistoryTime();
Clear();
}

Expand All @@ -224,24 +224,6 @@ class CStatHistory : public CStat<DataType, RecordType>
Clear();
}


void initHistoryTime(void)
{
for (int i = 0; i < STATISTICS_NUM_RANGES; i++)
{
historyTime[i] = new int64_t[STATISTICS_SAMPLES];
}
}

void delHistoryTime(void)
{
for (int i = STATISTICS_NUM_RANGES; i > 0;)
{
delete[] historyTime[--i];
}
delete[] historyTime;
}

void Clear(bool fStart = true)
{
timerCount = 0;
Expand All @@ -263,7 +245,7 @@ class CStatHistory : public CStat<DataType, RecordType>
Start();
}

virtual ~CStatHistory() { delHistoryTime(); }
virtual ~CStatHistory() {}
CStatHistory &operator<<(const DataType &rhs)
{
if (op & STAT_INDIVIDUAL)
Expand Down Expand Up @@ -301,13 +283,19 @@ class CStatHistory : public CStat<DataType, RecordType>
void Start()
{
if (!(op & STAT_INDIVIDUAL))
{
timerStartSteady = std::chrono::steady_clock::now();
timerCount = 0;
wait();
}
}

void Stop()
{
if (!op & STAT_INDIVIDUAL)
{
timer.cancel();
}
}

int Series(int series, DataType *array, int len)
Expand Down Expand Up @@ -368,6 +356,7 @@ class CStatHistory : public CStat<DataType, RecordType>
int pos = loc[series] - 1 + ago;
if (pos < 0)
pos += STATISTICS_SAMPLES;
assert(pos < STATISTICS_SAMPLES);
return history[series][pos];
}

Expand Down Expand Up @@ -408,6 +397,9 @@ class CStatHistory : public CStat<DataType, RecordType>
int pos = loc[series] - 1 + ago;
if (pos < 0)
pos += STATISTICS_SAMPLES;
assert(pos < STATISTICS_SAMPLES);
assert(series >= 0);
assert(pos >= 0);
return historyTime[series][pos];
}

Expand All @@ -431,6 +423,8 @@ class CStatHistory : public CStat<DataType, RecordType>
sampleCount = 0;

int64_t cur_time = GetTimeMillis();
assert(loc[0] < STATISTICS_SAMPLES);
assert(loc[0] >= 0);
history[0][loc[0]] = samples[0];
historyTime[0][loc[0]] = cur_time;
loc[0]++;
Expand Down Expand Up @@ -493,9 +487,14 @@ class CStatHistory : public CStat<DataType, RecordType>
// series.
if (op & STAT_OP_AVE)
accumulator /= ((DataType)operateSampleCount[i]);
assert(i + 1 < STATISTICS_NUM_RANGES);
assert(loc[i + 1] < STATISTICS_SAMPLES);
assert(start < STATISTICS_SAMPLES);
assert(start >= 0);
assert(loc[i + 1] >= 0);
history[i + 1][loc[i + 1]] = accumulator;
historyTime[i + 1][loc[i + 1]] = cur_time;

// times for accumulated statistics indicate the beginning of the interval
historyTime[i + 1][loc[i + 1]] = historyTime[i][start];
loc[i + 1]++;
len[i + 1]++;
if (loc[i + 1] >= STATISTICS_SAMPLES)
Expand All @@ -511,7 +510,10 @@ class CStatHistory : public CStat<DataType, RecordType>
protected:
void wait()
{
timer.expires_from_now(statMinInterval);
// to account for drift we keep checking back against the start time.
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
auto next = std::chrono::milliseconds((timerCount + 1) * statMinInterval) + timerStartSteady - now;
timer.expires_from_now(next);
timer.async_wait(boost::bind(&CStatHistory::timeout, this, boost::asio::placeholders::error));
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/test/stat_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE(stat_testvectors)
BOOST_CHECK(s3.History(1,0) == 120000);
BOOST_CHECK(s4.History(1,0) == 3.3);

statMinInterval=boost::posix_time::milliseconds(10); // boost::posix_time::seconds(1); // Speed things up
statMinInterval=std::chrono::milliseconds(30); // Speed things up
for (int i=0;i<12;i++)
for (int j=0;j<30;j++)
{
Expand Down
7 changes: 4 additions & 3 deletions src/unlimited.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1411,11 +1411,12 @@ UniValue getstat(const UniValue &params, bool fHelp)
"\nReturns the current settings for the network send and receive bandwidth and burst in "
"kilobytes per second.\n"
"\nArguments: \n"
"1. \"statistic\" (string, required) Specify what statistic you want\n"
"2. \"series\" (string, optional) Specify what data series you want. Options are "
"1. \"-v\" or \"--verbose\" (string, optional) full details\n"
"2. \"statistic\" (string, required) Specify what statistic you want\n"
"3. \"series\" (string, optional) Specify what data series you want. Options are "
"\"total\", \"now\",\"all\", \"sec10\", \"min5\", \"hourly\", \"daily\",\"monthly\". "
"Default is all.\n"
"3. \"count\" (string, optional) Specify the number of samples you want.\n"
"4. \"count\" (string, optional) Specify the number of samples you want.\n"

"\nResult:\n"
" {\n"
Expand Down
2 changes: 0 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
bool bSpendZeroConfChange = DEFAULT_SPEND_ZEROCONF_CHANGE;
bool fSendFreeTransactions = DEFAULT_SEND_FREE_TRANSACTIONS;

std::atomic<bool> fRescan{false}; // this flag is set to true when a wallet rescan has been invoked.

const char * DEFAULT_WALLET_DAT = "wallet.dat";

/**
Expand Down

0 comments on commit 6331a25

Please sign in to comment.