Skip to content

Commit

Permalink
Add verbose flag to osmcoastline_filter. Use util code from libosmium.
Browse files Browse the repository at this point in the history
The VerboseOutput and Memory usage code has been moved to libosmium.
  • Loading branch information
joto committed Feb 4, 2016
1 parent e515a63 commit b770a31
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 86 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -8,9 +8,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Add verbose option to osmcoastline_filter.
- osmcoastline_filter now shows memory used in verbose mode.

### Changed

- Optimized osmcoastline_filter program
- Optimized osmcoastline_filter program.
- Use more features from newest libosmium.

### Fixed

Expand Down
39 changes: 6 additions & 33 deletions src/osmcoastline.cpp
Expand Up @@ -25,6 +25,8 @@
#include <unistd.h>

#include <osmium/io/any_input.hpp>
#include <osmium/util/memory.hpp>
#include <osmium/util/verbose_output.hpp>
#include <osmium/visitor.hpp>

#include "return_codes.hpp"
Expand All @@ -38,7 +40,6 @@
#include "coastline_handlers.hpp"
#include "srs.hpp"
#include "util.hpp"
#include "verbose_output.hpp"

// The global SRS object is used in many places to transform
// from WGS84 to the output SRS etc.
Expand Down Expand Up @@ -104,38 +105,10 @@ polygon_vector_type create_polygons(CoastlineRingCollection& coastline_rings, Ou

/* ================================================== */

std::pair<int, int> get_memory_usage() {
char filename[100];
sprintf(filename, "/proc/%d/status", getpid());
std::ifstream status_file(filename);
std::string line;

int vmpeak = 0;
int vmsize = 0;
if (status_file.is_open()) {
while (! status_file.eof() ) {
std::getline(status_file, line);
if (line.substr(0, 6) == "VmPeak") {
int f = line.find_first_of("0123456789");
int l = line.find_last_of("0123456789");
vmpeak = atoi(line.substr(f, l-f+1).c_str());
}
if (line.substr(0, 6) == "VmSize") {
int f = line.find_first_of("0123456789");
int l = line.find_last_of("0123456789");
vmsize = atoi(line.substr(f, l-f+1).c_str());
}
}
status_file.close();
}

return std::make_pair(vmsize / 1024, vmpeak / 1024);
}

std::string memory_usage() {
std::pair<int, int> mem = get_memory_usage();
osmium::MemoryUsage mem;
std::ostringstream s;
s << "Memory used currently: " << mem.first << " MB (Peak was: " << mem.second << " MB).\n";
s << "Memory used: current: " << mem.current() << " MBytes, peak: " << mem.peak() << " MBytes\n";
return s.str();
}

Expand All @@ -152,7 +125,7 @@ int main(int argc, char *argv[]) {
// The vout object is an output stream we can write to instead of
// std::cerr. Nothing is written if we are not in verbose mode.
// The running time will be prepended to output lines.
VerboseOutput vout(options.verbose);
osmium::util::VerboseOutput vout(options.verbose);

debug = options.debug;

Expand Down Expand Up @@ -320,7 +293,7 @@ int main(int argc, char *argv[]) {
vout << memory_usage();

vout << "Committing database transactions...\n";
output_database.set_meta(vout.runtime(), get_memory_usage().second, stats);
output_database.set_meta(vout.runtime(), osmium::MemoryUsage{}.peak(), stats);
output_database.commit();
vout << "All done.\n";
vout << memory_usage();
Expand Down
25 changes: 24 additions & 1 deletion src/osmcoastline_filter.cpp
Expand Up @@ -31,6 +31,8 @@
#include <osmium/io/pbf_output.hpp>
#include <osmium/handler.hpp>
#include <osmium/osm/entity_bits.hpp>
#include <osmium/util/memory.hpp>
#include <osmium/util/verbose_output.hpp>

#include "return_codes.hpp"

Expand All @@ -39,22 +41,25 @@ void print_help() {
<< "\nOptions:\n"
<< " -h, --help - This help message\n"
<< " -o, --output=OSMFILE - Where to write output (default: none)\n"
<< " -v, --verbose - Verbose output\n"
<< " -V, --version - Show version and exit\n"
<< "\n";
}

int main(int argc, char* argv[]) {
std::string output_filename;
bool verbose = false;

static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"output", required_argument, 0, 'o'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{0, 0, 0, 0}
};

while (1) {
int c = getopt_long(argc, argv, "ho:V", long_options, 0);
int c = getopt_long(argc, argv, "ho:vV", long_options, 0);
if (c == -1)
break;

Expand All @@ -65,6 +70,9 @@ int main(int argc, char* argv[]) {
case 'o':
output_filename = optarg;
break;
case 'v':
verbose = true;
break;
case 'V':
std::cout << "osmcoastline_filter version " OSMCOASTLINE_VERSION "\n"
<< "Copyright (C) 2012-2016 Jochen Topf <jochen@topf.org>\n"
Expand All @@ -77,6 +85,11 @@ int main(int argc, char* argv[]) {
}
}

// The vout object is an output stream we can write to instead of
// std::cerr. Nothing is written if we are not in verbose mode.
// The running time will be prepended to output lines.
osmium::util::VerboseOutput vout(verbose);

if (output_filename.empty()) {
std::cerr << "Missing -o/--output=OSMFILE option\n";
exit(return_code_cmdline);
Expand All @@ -99,6 +112,7 @@ int main(int argc, char* argv[]) {

std::vector<osmium::object_id_type> ids;

vout << "Reading ways (1st pass through input file)...\n";
{
osmium::io::Reader reader(infile, osmium::osm_entity_bits::way);
auto ways = osmium::io::make_input_iterator_range<const osmium::Way>(reader);
Expand All @@ -114,9 +128,11 @@ int main(int argc, char* argv[]) {
reader.close();
}

vout << "Preparing node ID list...\n";
std::sort(ids.begin(), ids.end());
auto last = std::unique(ids.begin(), ids.end());

vout << "Reading nodes (2nd pass through input file)...\n";
{
osmium::io::Reader reader(infile, osmium::osm_entity_bits::node);
auto nodes = osmium::io::make_input_iterator_range<const osmium::Node>(reader);
Expand Down Expand Up @@ -146,5 +162,12 @@ int main(int argc, char* argv[]) {
std::cerr << "io error: " << e.what() << "'\n";
exit(return_code_fatal);
}

vout << "All done.\n";
osmium::MemoryUsage mem;
if (mem.current() > 0) {
vout << "Memory used: current: " << mem.current() << " MBytes\n"
<< " peak: " << mem.peak() << " MBytes\n";
}
}

51 changes: 0 additions & 51 deletions src/verbose_output.hpp

This file was deleted.

0 comments on commit b770a31

Please sign in to comment.