Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shows reindex progress in metrics screen #4368

Merged
merged 4 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,20 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
// -reindex
if (fReindex) {
CImportingNow imp;
nSizeReindexed = 0; // will be modified inside LoadExternalBlockFile
// Find the summary size of all block files first
int nFile = 0;
size_t fullSize = 0;
while (true) {
CDiskBlockPos pos(nFile, 0);
boost::filesystem::path blkFile = GetBlockPosFilename(pos, "blk");
if (!boost::filesystem::exists(blkFile))
break; // No block files left to reindex
nFile++;
fullSize += boost::filesystem::file_size(blkFile);
}
nFullSizeToReindex = fullSize;
Copy link
Contributor

@daira daira Mar 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed a potential divide-by-zero. If all block files are zero-length, then https://github.com/zcash/zcash/pull/4368/files#diff-9bee7abc3e254b85a1ce1ed330601054R291 can divide by zero.

nFile = 0;
while (true) {
CDiskBlockPos pos(nFile, 0);
if (!boost::filesystem::exists(GetBlockPosFilename(pos, "blk")))
Expand All @@ -605,6 +618,8 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
}
pblocktree->WriteReindexing(false);
fReindex = false;
nSizeReindexed = 0;
nFullSizeToReindex = 1;
LogPrintf("Reindexing finished\n");
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
InitBlockIndex(chainparams);
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4855,9 +4855,13 @@ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskB
// This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
uint64_t nRewind = blkdat.GetPos();
size_t initialSize = nSizeReindexed;
while (!blkdat.eof()) {
boost::this_thread::interruption_point();

if (fReindex)
nSizeReindexed = initialSize + nRewind;

blkdat.SetPos(nRewind);
nRewind++; // start one byte further next time, in case of failure
blkdat.SetLimit(); // remove former limit
Expand Down
33 changes: 29 additions & 4 deletions src/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ AtomicCounter ehSolverRuns;
AtomicCounter solutionTargetChecks;
static AtomicCounter minedBlocks;
AtomicTimer miningTimer;
std::atomic<size_t> nSizeReindexed(0); // valid only during reindex
std::atomic<size_t> nFullSizeToReindex(1); // valid only during reindex

static boost::synchronized_value<std::list<uint256>> trackedBlocks;

Expand Down Expand Up @@ -235,6 +237,24 @@ std::string DisplayDuration(int64_t time, DurationFormat format)
return strDuration;
}

std::string DisplaySize(size_t value)
{
double coef = 1.0;
if (value < 1024.0 * coef)
return strprintf(_("%d Bytes"), value);
coef *= 1024.0;
if (value < 1024.0 * coef)
return strprintf(_("%.2f KiB"), value / coef);
coef *= 1024.0;
if (value < 1024.0 * coef)
return strprintf(_("%.2f MiB"), value / coef);
coef *= 1024.0;
if (value < 1024.0 * coef)
return strprintf(_("%.2f GiB"), value / coef);
coef *= 1024.0;
return strprintf(_("%.2f TiB"), value / coef);
}

boost::optional<int64_t> SecondsLeftToNextEpoch(const Consensus::Params& params, int currentHeight)
{
auto nextHeight = NextActivationHeight(currentHeight, params);
Expand Down Expand Up @@ -267,10 +287,15 @@ int printStats(bool mining)
auto localsolps = GetLocalSolPS();

if (IsInitialBlockDownload(Params())) {
int netheight = currentHeadersHeight == -1 || currentHeadersTime == 0 ?
0 : EstimateNetHeight(params, currentHeadersHeight, currentHeadersTime);
int downloadPercent = height * 100 / netheight;
std::cout << " " << _("Downloading blocks") << " | " << height << " / ~" << netheight << " (" << downloadPercent << "%)" << std::endl;
if (fReindex) {
str4d marked this conversation as resolved.
Show resolved Hide resolved
int downloadPercent = nSizeReindexed * 100 / nFullSizeToReindex;
std::cout << " " << _("Reindexing blocks") << " | " << DisplaySize(nSizeReindexed) << " / " << DisplaySize(nFullSizeToReindex) << " (" << downloadPercent << "%, " << height << " " << _("blocks") << ")" << std::endl;
} else {
int netheight = currentHeadersHeight == -1 || currentHeadersTime == 0 ?
0 : EstimateNetHeight(params, currentHeadersHeight, currentHeadersTime);
int downloadPercent = height * 100 / netheight;
std::cout << " " << _("Downloading blocks") << " | " << height << " / ~" << netheight << " (" << downloadPercent << "%)" << std::endl;
}
} else {
std::cout << " " << _("Block height") << " | " << height << std::endl;
}
Expand Down
2 changes: 2 additions & 0 deletions src/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ extern AtomicCounter transactionsValidated;
extern AtomicCounter ehSolverRuns;
extern AtomicCounter solutionTargetChecks;
extern AtomicTimer miningTimer;
extern std::atomic<size_t> nSizeReindexed; // valid only during reindex
extern std::atomic<size_t> nFullSizeToReindex; // valid only during reindex

void TrackMinedBlock(uint256 hash);

Expand Down