Skip to content

Commit 62f9607

Browse files
committed
Better progress output when processing input files
1 parent 1ea23cc commit 62f9607

File tree

6 files changed

+88
-73
lines changed

6 files changed

+88
-73
lines changed

src/input.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ static void apply(osmium::OSMObject &object, osmdata_t &osmdata,
212212
if (last_type != object.type()) {
213213
if (last_type == osmium::item_type::node) {
214214
osmdata.after_nodes();
215-
progress.after_nodes();
215+
progress.start_way_counter();
216216
} else if (last_type == osmium::item_type::way) {
217217
osmdata.after_ways();
218-
progress.after_ways();
218+
progress.start_relation_counter();
219219
}
220220
last_type = object.type();
221221
}
@@ -242,15 +242,16 @@ void process_file(osmium::io::File const &file, osmdata_t &osmdata,
242242
}
243243

244244
osmdata.after_relations();
245-
progress.after_relations();
245+
progress.print_summary();
246246

247247
reader.close();
248248
}
249249

250250
void process_files(std::vector<osmium::io::File> const &files,
251-
osmdata_t &osmdata, progress_display_t &progress,
252-
bool append)
251+
osmdata_t &osmdata, bool append, bool show_progress)
253252
{
253+
progress_display_t progress{show_progress};
254+
254255
if (files.size() == 1) {
255256
process_file(files.front(), osmdata, progress, append);
256257
return;
@@ -288,7 +289,7 @@ void process_files(std::vector<osmium::io::File> const &files,
288289
}
289290

290291
osmdata.after_relations();
291-
progress.after_relations();
292+
progress.print_summary();
292293

293294
for (auto &data_source : data_sources) {
294295
data_source.close();

src/input.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ prepare_input_files(std::vector<std::string> const &input_files,
4747
* Process the specified OSM files (stage 1a).
4848
*/
4949
void process_files(std::vector<osmium::io::File> const &files,
50-
osmdata_t &osmdata, progress_display_t &progress,
51-
bool append);
50+
osmdata_t &osmdata, bool append, bool show_progress);
5251

5352
#endif // OSM2PGSQL_INPUT_HPP

src/osm2pgsql.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "options.hpp"
3333
#include "osmdata.hpp"
3434
#include "output.hpp"
35-
#include "progress-display.hpp"
3635
#include "reprojection.hpp"
3736
#include "util.hpp"
3837
#include "version.hpp"
@@ -83,15 +82,7 @@ int main(int argc, char *argv[])
8382

8483
// Processing: In this phase the input file(s) are read and parsed,
8584
// populating some of the tables.
86-
util::timer_t timer_parse;
87-
88-
progress_display_t progress{get_logger().show_progress()};
89-
process_files(files, osmdata, progress, options.append);
90-
91-
fmt::print(stderr, " parse time: {}\n",
92-
util::human_readable_duration(timer_parse.stop()));
93-
94-
progress.print_summary();
85+
process_files(files, osmdata, options.append, get_logger().show_progress());
9586

9687
// Process pending ways and relations. Cluster database tables and
9788
// create indexes.

src/progress-display.cpp

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
#include "format.hpp"
22
#include "logging.hpp"
33
#include "progress-display.hpp"
4-
5-
void progress_display_t::print_summary() const
6-
{
7-
std::time_t const now = std::time(nullptr);
8-
9-
log_info("Node stats: total({}), max({}) in {}s", m_node.count, m_node.max,
10-
nodes_time(now));
11-
log_info("Way stats: total({}), max({}) in {}s", m_way.count, m_way.max,
12-
ways_time(now));
13-
log_info("Relation stats: total({}), max({}) in {}s", m_rel.count,
14-
m_rel.max, rels_time(now));
15-
}
4+
#include "util.hpp"
165

176
static double count_per_second(osmid_t count, uint64_t elapsed) noexcept
187
{
@@ -27,6 +16,40 @@ static double count_per_second(osmid_t count, uint64_t elapsed) noexcept
2716
return static_cast<double>(count) / elapsed;
2817
}
2918

19+
static std::string cps_display(osmid_t count, uint64_t elapsed) noexcept
20+
{
21+
double const cps = count_per_second(count, elapsed);
22+
23+
if (cps >= 1000.0) {
24+
return "{:.0f}k/s"_format(cps / 1000);
25+
}
26+
return "{:.0f}/s"_format(cps);
27+
}
28+
29+
void progress_display_t::print_summary() const
30+
{
31+
std::time_t const now = std::time(nullptr);
32+
33+
if (m_enabled) {
34+
fmt::print(stderr, "\r{:78s}\r", "");
35+
}
36+
37+
log_info("Reading input files done in {}.",
38+
util::human_readable_duration(overall_time(now)));
39+
40+
auto const nt = nodes_time(now);
41+
log_info(" Processed {} nodes in {} - {}", m_node.count,
42+
util::human_readable_duration(nt), cps_display(m_node.count, nt));
43+
44+
auto const wt = ways_time(now);
45+
log_info(" Processed {} ways in {} - {}", m_way.count,
46+
util::human_readable_duration(wt), cps_display(m_way.count, wt));
47+
48+
auto const rt = rels_time(now);
49+
log_info(" Processed {} relations in {} - {}", m_rel.count,
50+
util::human_readable_duration(rt), cps_display(m_rel.count, rt));
51+
}
52+
3053
void progress_display_t::print_status(std::time_t now) const
3154
{
3255
if (m_enabled) {
@@ -50,3 +73,33 @@ void progress_display_t::possibly_print_status()
5073
print_status(now);
5174
}
5275
}
76+
77+
uint64_t progress_display_t::nodes_time(std::time_t now) const noexcept
78+
{
79+
if (m_node.count == 0) {
80+
return 0;
81+
}
82+
return (m_way.start > 0 ? m_way.start : now) - m_node.start;
83+
}
84+
85+
uint64_t progress_display_t::ways_time(std::time_t now) const noexcept
86+
{
87+
if (m_way.count == 0) {
88+
return 0;
89+
}
90+
return (m_rel.start > 0 ? m_rel.start : now) - m_way.start;
91+
}
92+
93+
uint64_t progress_display_t::rels_time(std::time_t now) const noexcept
94+
{
95+
if (m_rel.count == 0) {
96+
return 0;
97+
}
98+
return now - m_rel.start;
99+
}
100+
101+
uint64_t progress_display_t::overall_time(std::time_t now) const noexcept
102+
{
103+
return now - m_node.start;
104+
}
105+

src/progress-display.hpp

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <osmium/handler.hpp>
1616

17+
#include "format.hpp"
1718
#include "osmtypes.hpp"
1819

1920
/**
@@ -25,16 +26,9 @@ class progress_display_t : public osmium::handler::Handler
2526
struct Counter
2627
{
2728
std::size_t count = 0;
28-
osmid_t max = 0;
2929
std::time_t start = 0;
3030

3131
osmid_t count_k() const noexcept { return count / 1000; }
32-
33-
std::size_t add(osmid_t id) noexcept
34-
{
35-
max = id;
36-
return ++count;
37-
}
3832
};
3933

4034
public:
@@ -45,60 +39,40 @@ class progress_display_t : public osmium::handler::Handler
4539

4640
void node(osmium::Node const &node)
4741
{
48-
if (m_node.add(node.id()) % 10000 == 0) {
42+
if (++m_node.count % 10000 == 0) {
4943
possibly_print_status();
5044
}
5145
}
5246

53-
void after_nodes() { m_way.start = std::time(nullptr); }
54-
5547
void way(osmium::Way const &way)
5648
{
57-
if (m_way.add(way.id()) % 1000 == 0) {
49+
if (++m_way.count % 1000 == 0) {
5850
possibly_print_status();
5951
}
6052
}
6153

62-
void after_ways() { m_rel.start = std::time(nullptr); }
6354

6455
void relation(osmium::Relation const &relation)
6556
{
66-
if (m_rel.add(relation.id()) % 10 == 0) {
57+
if (++m_rel.count % 10 == 0) {
6758
possibly_print_status();
6859
}
6960
}
7061

71-
void after_relations() const { print_status(std::time(nullptr)); }
62+
void start_way_counter() { m_way.start = std::time(nullptr); }
63+
64+
void start_relation_counter() { m_rel.start = std::time(nullptr); }
7265

7366
void print_summary() const;
7467

7568
private:
76-
void possibly_print_status();
7769
void print_status(std::time_t now) const;
70+
void possibly_print_status();
7871

79-
uint64_t nodes_time(std::time_t now) const noexcept
80-
{
81-
if (m_node.count == 0) {
82-
return 0;
83-
}
84-
return (m_way.start > 0 ? m_way.start : now) - m_node.start;
85-
}
86-
87-
uint64_t ways_time(std::time_t now) const noexcept
88-
{
89-
if (m_way.count == 0) {
90-
return 0;
91-
}
92-
return (m_rel.start > 0 ? m_rel.start : now) - m_way.start;
93-
}
94-
95-
uint64_t rels_time(std::time_t now) const noexcept
96-
{
97-
if (m_rel.count == 0) {
98-
return 0;
99-
}
100-
return now - m_rel.start;
101-
}
72+
uint64_t nodes_time(std::time_t now) const noexcept;
73+
uint64_t ways_time(std::time_t now) const noexcept;
74+
uint64_t rels_time(std::time_t now) const noexcept;
75+
uint64_t overall_time(std::time_t now) const noexcept;
10276

10377
Counter m_node{};
10478
Counter m_way{};

tests/common-import.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "osmdata.hpp"
1717
#include "output-multi.hpp"
1818
#include "output.hpp"
19-
#include "progress-display.hpp"
2019
#include "taginfo-impl.hpp"
2120

2221
#include "common-pg.hpp"
@@ -46,8 +45,7 @@ inline void parse_file(options_t const &options,
4645
filepath += options.input_files[0];
4746
}
4847
osmium::io::File file{filepath};
49-
progress_display_t progress;
50-
process_files({file}, osmdata, progress, options.append);
48+
process_files({file}, osmdata, options.append, false);
5149

5250
if (do_stop) {
5351
osmdata.stop();
@@ -158,8 +156,7 @@ class import_t
158156
for (auto const &data : input_data) {
159157
files.emplace_back(data.data(), data.size(), format);
160158
}
161-
progress_display_t progress;
162-
process_files(files, osmdata, progress, options.append);
159+
process_files(files, osmdata, options.append, false);
163160

164161
osmdata.stop();
165162
}

0 commit comments

Comments
 (0)