From 9db7bf5f52194b0018272ce3f719115f8637da5c Mon Sep 17 00:00:00 2001 From: Satoshi Nakamoto Date: Thu, 12 Aug 2010 13:23:02 +0000 Subject: [PATCH] gethashespersec and added version and hashespersec to getinfo --- main.cpp | 29 +++++++++++++++++++---------- main.h | 2 ++ rpc.cpp | 20 +++++++++++++++++++- serialize.h | 2 +- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index a03b956526..76ce85732a 100644 --- a/main.cpp +++ b/main.cpp @@ -53,6 +53,9 @@ CCriticalSection cs_mapAddressBook; vector vchDefaultKey; +double dHashesPerSec; +int64 nHPSTimerStart; + // Settings int fGenerateBitcoins = false; int64 nTransactionFee = 0; @@ -2542,6 +2545,9 @@ void ThreadBitcoinMiner(void* parg) PrintException(NULL, "ThreadBitcoinMiner()"); } UIThreadCall(bind(CalledSetStatusBar, "", 0)); + nHPSTimerStart = 0; + if (vnThreadsRunning[3] == 0) + dHashesPerSec = 0; printf("ThreadBitcoinMiner exiting, %d threads remaining\n", vnThreadsRunning[3]); } @@ -2768,25 +2774,28 @@ void BitcoinMiner() // Update nTime every few seconds const unsigned int nMask = 0xffff; + const int nHashesPerCycle = (nMask+1); if ((++tmp.block.nNonce & nMask) == 0) { // Meter hashes/sec - static int64 nTimerStart; - static int nHashCounter; - if (nTimerStart == 0) - nTimerStart = GetTimeMillis(); + static int nCycleCounter; + if (nHPSTimerStart == 0) + { + nHPSTimerStart = GetTimeMillis(); + nCycleCounter = 0; + } else - nHashCounter++; - if (GetTimeMillis() - nTimerStart > 4000) + nCycleCounter++; + if (GetTimeMillis() - nHPSTimerStart > 4000) { static CCriticalSection cs; CRITICAL_BLOCK(cs) { - if (GetTimeMillis() - nTimerStart > 4000) + if (GetTimeMillis() - nHPSTimerStart > 4000) { - double dHashesPerSec = 1000.0 * (nMask+1) * nHashCounter / (GetTimeMillis() - nTimerStart); - nTimerStart = GetTimeMillis(); - nHashCounter = 0; + dHashesPerSec = 1000.0 * nHashesPerCycle * nCycleCounter / (GetTimeMillis() - nHPSTimerStart); + nHPSTimerStart = GetTimeMillis(); + nCycleCounter = 0; string strStatus = strprintf(" %.0f khash/s", dHashesPerSec/1000.0); UIThreadCall(bind(CalledSetStatusBar, strStatus, 0)); static int64 nLogTime; diff --git a/main.h b/main.h index 1d49c866b8..871224b709 100644 --- a/main.h +++ b/main.h @@ -42,6 +42,8 @@ extern CCriticalSection cs_mapRequestCount; extern map mapAddressBook; extern CCriticalSection cs_mapAddressBook; extern vector vchDefaultKey; +extern double dHashesPerSec; +extern int64 nHPSTimerStart; // Settings extern int fGenerateBitcoins; diff --git a/rpc.cpp b/rpc.cpp index 2a30cda0fc..b464915bbf 100644 --- a/rpc.cpp +++ b/rpc.cpp @@ -217,13 +217,28 @@ Value setgenerate(const Array& params, bool fHelp) } +Value gethashespersec(const Array& params, bool fHelp) +{ + if (fHelp || params.size() != 0) + throw runtime_error( + "gethashespersec\n" + "Returns a recent hashes per second performance measurement while generating."); + + if (GetTimeMillis() - nHPSTimerStart > 8000) + return (int64)0; + return (int64)dHashesPerSec; +} + + Value getinfo(const Array& params, bool fHelp) { if (fHelp || params.size() != 0) throw runtime_error( - "getinfo"); + "getinfo\n" + "Returns an object containing various state info."); Object obj; + obj.push_back(Pair("version", (int)VERSION)); obj.push_back(Pair("balance", (double)GetBalance() / (double)COIN)); obj.push_back(Pair("blocks", (int)nBestHeight + 1)); obj.push_back(Pair("connections", (int)vNodes.size())); @@ -231,6 +246,7 @@ Value getinfo(const Array& params, bool fHelp) obj.push_back(Pair("generate", (bool)fGenerateBitcoins)); obj.push_back(Pair("genproclimit", (int)(fLimitProcessors ? nLimitProcessors : -1))); obj.push_back(Pair("difficulty", (double)GetDifficulty())); + obj.push_back(Pair("hashespersec", gethashespersec(params, false))); return obj; } @@ -467,6 +483,7 @@ Value getreceivedbylabel(const Array& params, bool fHelp) } + struct tallyitem { int64 nAmount; @@ -635,6 +652,7 @@ pair pCallTable[] = make_pair("getbalance", &getbalance), make_pair("getgenerate", &getgenerate), make_pair("setgenerate", &setgenerate), + make_pair("gethashespersec", &gethashespersec), make_pair("getinfo", &getinfo), make_pair("getnewaddress", &getnewaddress), make_pair("setlabel", &setlabel), diff --git a/serialize.h b/serialize.h index d3c4deabd3..09707586e2 100644 --- a/serialize.h +++ b/serialize.h @@ -20,7 +20,7 @@ class CDataStream; class CAutoFile; static const int VERSION = 308; -static const char* pszSubVer = ".2"; +static const char* pszSubVer = ".3";