Skip to content

Commit 88b8b6f

Browse files
authored
be less chatty when run non-interactively (#767)
1 parent 76ef3e8 commit 88b8b6f

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
lines changed

include/helpers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <sstream>
77
#include <vector>
88

9+
#ifdef _MSVC_LANG
10+
#define ISATTY true
11+
#else
12+
#define ISATTY isatty(1)
13+
#endif
14+
915
// General helper routines
1016

1117
inline void endian_swap(unsigned int& x) {

include/tile_data.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ template<typename OO> void finalizeObjects(
6969
int i = -1;
7070
for (auto it = begin; it != end; it++) {
7171
i++;
72-
if (it->size() > 0 || i % 10 == 0 || i == 4095) {
72+
if (it->size() > 0 || i % 50 == 0 || i == 4095) {
7373
std::cout << "\r" << name << ": finalizing z6 tile " << (i + 1) << "/" << CLUSTER_ZOOM_AREA;
7474

7575
#ifdef CLOCK_MONOTONIC

src/pbf_processor.cpp

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const std::string OptionSortTypeThenID = "Sort.Type_then_ID";
1717
const std::string OptionLocationsOnWays = "LocationsOnWays";
1818
std::atomic<uint64_t> blocksProcessed(0), blocksToProcess(0);
1919

20+
// Access is guarded by ioMutex.
21+
// This counter decreases the chattiness of tilemaker's progress updates,
22+
// especially when run in a non-interactive context.
23+
uint64_t phaseProgress = 0;
24+
2025
// Thread-local so that we can re-use buffers during parsing.
2126
thread_local PbfReader::PbfReader reader;
2227

@@ -370,16 +375,27 @@ bool PbfProcessor::ReadBlock(
370375
auto output_progress = [&]()
371376
{
372377
if (ioMutex.try_lock()) {
373-
std::ostringstream str;
374-
str << "\r";
375-
void_mmap_allocator::reportStoreSize(str);
376-
if (effectiveShards > 1)
377-
str << std::to_string(shard + 1) << "/" << std::to_string(effectiveShards) << " ";
378-
379-
// TODO: revive showing the # of ways/relations?
380-
str << "Block " << blocksProcessed.load() << "/" << blocksToProcess.load() << " ";
381-
std::cout << str.str();
382-
std::cout.flush();
378+
// If we're interactive, show an update for each block.
379+
// If we're not interactive, show an update for each 1% of blocks.
380+
uint64_t blockProgress = blocksProcessed.load();
381+
uint64_t minimumIncrement = blocksToProcess.load() / 100;
382+
if (minimumIncrement < 1 || ISATTY)
383+
minimumIncrement = 1;
384+
385+
if (phaseProgress == 0 || phaseProgress + minimumIncrement <= blockProgress) {
386+
phaseProgress = blockProgress;
387+
388+
std::ostringstream str;
389+
str << "\r";
390+
void_mmap_allocator::reportStoreSize(str);
391+
if (effectiveShards > 1)
392+
str << std::to_string(shard + 1) << "/" << std::to_string(effectiveShards) << " ";
393+
394+
// TODO: revive showing the # of ways/relations?
395+
str << "Block " << blocksProcessed.load() << "/" << blocksToProcess.load() << " ";
396+
std::cout << str.str();
397+
std::cout.flush();
398+
}
383399
ioMutex.unlock();
384400
}
385401
};
@@ -397,8 +413,13 @@ bool PbfProcessor::ReadBlock(
397413
bool done = ScanWays(output, pg, pb, wayKeys);
398414
if(done) {
399415
if (ioMutex.try_lock()) {
400-
std::cout << "\r(Scanning for nodes used in ways: " << (100*blocksProcessed.load()/blocksToProcess.load()) << "%) ";
401-
std::cout.flush();
416+
size_t scanProgress = 100*blocksProcessed.load()/blocksToProcess.load();
417+
418+
if (scanProgress != phaseProgress) {
419+
phaseProgress = scanProgress;
420+
std::cout << "\r(Scanning for nodes used in ways: " << (100*blocksProcessed.load()/blocksToProcess.load()) << "%) ";
421+
std::cout.flush();
422+
}
402423
ioMutex.unlock();
403424
}
404425
continue;
@@ -410,8 +431,13 @@ bool PbfProcessor::ReadBlock(
410431
bool done = ScanRelations(output, pg, pb, wayKeys);
411432
if(done) {
412433
if (ioMutex.try_lock()) {
413-
std::cout << "\r(Scanning for ways used in relations: " << (100*blocksProcessed.load()/blocksToProcess.load()) << "%) ";
414-
std::cout.flush();
434+
size_t scanProgress = 100*blocksProcessed.load()/blocksToProcess.load();
435+
436+
if (scanProgress != phaseProgress) {
437+
phaseProgress = scanProgress;
438+
std::cout << "\r(Scanning for ways used in relations: " << (100*blocksProcessed.load()/blocksToProcess.load()) << "%) ";
439+
std::cout.flush();
440+
}
415441
ioMutex.unlock();
416442
}
417443
continue;
@@ -591,6 +617,7 @@ int PbfProcessor::ReadPbfFile(
591617
all_phases.push_back(ReadPhase::Relations);
592618

593619
for(auto phase: all_phases) {
620+
phaseProgress = 0;
594621
uint effectiveShards = 1;
595622

596623
// On memory-constrained machines, we might read ways/relations

src/tilemaker.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ int main(const int argc, const char* argv[]) {
345345
std::mutex io_mutex;
346346

347347
// Loop through tiles
348-
std::atomic<uint64_t> tilesWritten(0);
348+
std::atomic<uint64_t> tilesWritten(0), lastTilesWritten(0);
349349

350350
for (auto source : sources) {
351351
source->finalize(options.threadNum);
@@ -518,7 +518,7 @@ int main(const int argc, const char* argv[]) {
518518
batchSize++;
519519
}
520520

521-
boost::asio::post(pool, [=, &tileCoordinates, &pool, &sharedData, &sources, &attributeStore, &io_mutex, &tilesWritten]() {
521+
boost::asio::post(pool, [=, &tileCoordinates, &pool, &sharedData, &sources, &attributeStore, &io_mutex, &tilesWritten, &lastTilesWritten]() {
522522
std::vector<std::string> tileTimings;
523523
std::size_t endIndex = std::min(tileCoordinates.size(), startIndex + batchSize);
524524
for(std::size_t i = startIndex; i < endIndex; ++i) {
@@ -557,16 +557,21 @@ int main(const int argc, const char* argv[]) {
557557
tilesWritten += (endIndex - startIndex);
558558

559559
if (io_mutex.try_lock()) {
560-
// Show progress grouped by z6 (or lower)
561-
size_t z = tileCoordinates[startIndex].first;
562-
size_t x = tileCoordinates[startIndex].second.x;
563-
size_t y = tileCoordinates[startIndex].second.y;
564-
if (z > CLUSTER_ZOOM) {
565-
x = x / (1 << (z - CLUSTER_ZOOM));
566-
y = y / (1 << (z - CLUSTER_ZOOM));
567-
z = CLUSTER_ZOOM;
560+
uint64_t written = tilesWritten.load();
561+
562+
if (written >= lastTilesWritten + tileCoordinates.size() / 100 || ISATTY) {
563+
lastTilesWritten = written;
564+
// Show progress grouped by z6 (or lower)
565+
size_t z = tileCoordinates[startIndex].first;
566+
size_t x = tileCoordinates[startIndex].second.x;
567+
size_t y = tileCoordinates[startIndex].second.y;
568+
if (z > CLUSTER_ZOOM) {
569+
x = x / (1 << (z - CLUSTER_ZOOM));
570+
y = y / (1 << (z - CLUSTER_ZOOM));
571+
z = CLUSTER_ZOOM;
572+
}
573+
cout << "z" << z << "/" << x << "/" << y << ", writing tile " << written << " of " << tileCoordinates.size() << " \r" << std::flush;
568574
}
569-
cout << "z" << z << "/" << x << "/" << y << ", writing tile " << tilesWritten.load() << " of " << tileCoordinates.size() << " \r" << std::flush;
570575
io_mutex.unlock();
571576
}
572577
});

0 commit comments

Comments
 (0)