From e0ab5f452e92fa4a2aabfb51cbc7e644bcdd8f88 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Fri, 11 Jan 2019 13:50:06 -0800 Subject: [PATCH] Add osrm-util for inspecting various bits of the graph. --- CMakeLists.txt | 4 + include/storage/view_factory.hpp | 39 --------- src/tools/dump.cpp | 143 ------------------------------- src/tools/util.cpp | 92 ++++++++++++++++++++ 4 files changed, 96 insertions(+), 182 deletions(-) delete mode 100644 src/tools/dump.cpp create mode 100644 src/tools/util.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e1767b961..a2cc5528a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,6 +167,7 @@ add_executable(osrm-customize src/tools/customize.cpp) add_executable(osrm-contract src/tools/contract.cpp) add_executable(osrm-routed src/tools/routed.cpp $ $) add_executable(osrm-datastore src/tools/store.cpp $ $) +add_executable(osrm-util src/tools/util.cpp $ $) add_library(osrm src/osrm/osrm.cpp $ $ $ $) add_library(osrm_contract src/osrm/contractor.cpp $ $) add_library(osrm_extract src/osrm/extractor.cpp $ $ $) @@ -633,6 +634,7 @@ target_link_libraries(osrm-partition osrm_partition ${Boost_PROGRAM_OPTIONS_LIBR target_link_libraries(osrm-customize osrm_customize ${Boost_PROGRAM_OPTIONS_LIBRARY}) target_link_libraries(osrm-contract osrm_contract ${Boost_PROGRAM_OPTIONS_LIBRARY}) target_link_libraries(osrm-routed osrm ${Boost_PROGRAM_OPTIONS_LIBRARY} ${OPTIONAL_SOCKET_LIBS} ${ZLIB_LIBRARY}) +target_link_libraries(osrm-util osrm) set(EXTRACTOR_LIBRARIES ${BZIP2_LIBRARIES} @@ -736,6 +738,7 @@ set_property(TARGET osrm-partition PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) set_property(TARGET osrm-contract PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) set_property(TARGET osrm-datastore PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) set_property(TARGET osrm-routed PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) +set_property(TARGET osrm-util PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) file(GLOB VariantGlob third_party/variant/include/mapbox/*.hpp) file(GLOB LibraryGlob include/osrm/*.hpp) @@ -761,6 +764,7 @@ install(TARGETS osrm-customize DESTINATION bin) install(TARGETS osrm-contract DESTINATION bin) install(TARGETS osrm-datastore DESTINATION bin) install(TARGETS osrm-routed DESTINATION bin) +install(TARGETS osrm-util DESTINATION bin) install(TARGETS osrm DESTINATION lib) install(TARGETS osrm_extract DESTINATION lib) install(TARGETS osrm_partition DESTINATION lib) diff --git a/include/storage/view_factory.hpp b/include/storage/view_factory.hpp index b38223157..b04e3026e 100644 --- a/include/storage/view_factory.hpp +++ b/include/storage/view_factory.hpp @@ -372,45 +372,6 @@ inline auto make_filtered_graph_view(const SharedDataIndex &index, auto edge_list = make_vector_view( index, name + "/contracted_graph/edge_array"); - // DEBUG - dump CH - std::cout << "digraph {" << std::endl; - for (std::size_t i = 0; i < node_list.size(); i++) - { - for (auto edge = node_list[i].first_edge; - edge < (i == node_list.size() - 1 ? edge_list.size() : node_list[i + 1].first_edge); - edge++) - { - const auto &e = edge_list[edge]; - if (e.data.forward) - { - std::cout << i << " -> " << e.target; - std::cout << "["; - std::cout << "label=\"↑" << e.data.weight << "\",weight=\"" << e.data.weight - << "\""; - if (e.data.maneuver_restricted) - { - std::cout << ",color=red,penwidth=3.0"; - } - std::cout << "];"; - std::cout << std::endl; - } - if (e.data.backward) - { - std::cout << e.target << " -> " << i; - std::cout << "["; - std::cout << "label=\"↓" << e.data.weight << "\",weight=\"" << e.data.weight - << "\""; - if (e.data.maneuver_restricted) - { - std::cout << ",color=red,penwidth=3.0"; - } - std::cout << "];"; - std::cout << std::endl; - } - } - } - std::cout << "}" << std::endl; - return util::FilteredGraphView({node_list, edge_list}, edge_filter); } } diff --git a/src/tools/dump.cpp b/src/tools/dump.cpp deleted file mode 100644 index 0ba4adc75..000000000 --- a/src/tools/dump.cpp +++ /dev/null @@ -1,143 +0,0 @@ -#include "storage/serialization.hpp" -#include "storage/shared_memory.hpp" -#include "storage/shared_monitor.hpp" -#include "storage/storage.hpp" - -#include "osrm/exception.hpp" -#include "util/log.hpp" -#include "util/meminfo.hpp" -#include "util/typedefs.hpp" -#include "util/version.hpp" - -#include -#include - -#include -#include - -using namespace osrm; - -// generate boost::program_options object for the routing part -bool generateDataStoreOptions(const int argc, - const char *argv[], - std::string &verbosity, - boost::filesystem::path &base_path, - int &max_wait, - std::string &dataset_name, - bool &list_datasets, - bool &list_blocks, - bool &only_metric) -{ - // declare a group of options that will be allowed only on command line - boost::program_options::options_description generic_options("Options"); - generic_options.add_options() // - ("contraction-heirarchy,ch", "Dump CH graph")("cells,c", "Dump partition structure"); - - try - { - boost::program_options::store( - boost::program_options::command_line_parser(argc, argv, generic_options), - option_variables); - } - catch (const boost::program_options::error &e) - { - util::Log(logERROR) << e.what(); - return false; - } - - if (option_variables.count("version")) - { - util::Log() << OSRM_VERSION; - return false; - } - - if (option_variables.count("help")) - { - util::Log() << visible_options; - return false; - } - - if (option_variables.count("remove-locks")) - { - removeLocks(); - return false; - } - - if (option_variables.count("spring-clean")) - { - springClean(); - return false; - } - - boost::program_options::notify(option_variables); - - return true; -} - -[[noreturn]] void CleanupSharedBarriers(int signum) -{ // Here the lock state of named mutexes is unknown, make a hard cleanup - removeLocks(); - std::_Exit(128 + signum); -} - -int main(const int argc, const char *argv[]) try -{ - int signals[] = {SIGTERM, SIGSEGV, SIGINT, SIGILL, SIGABRT, SIGFPE}; - for (auto sig : signals) - { - std::signal(sig, CleanupSharedBarriers); - } - - util::LogPolicy::GetInstance().Unmute(); - - std::string verbosity; - boost::filesystem::path base_path; - int max_wait = -1; - std::string dataset_name; - bool list_datasets = false; - bool list_blocks = false; - bool only_metric = false; - if (!generateDataStoreOptions(argc, - argv, - verbosity, - base_path, - max_wait, - dataset_name, - list_datasets, - list_blocks, - only_metric)) - { - return EXIT_SUCCESS; - } - - util::LogPolicy::GetInstance().SetLevel(verbosity); - - if (list_datasets || list_blocks) - { - listRegions(list_blocks); - return EXIT_SUCCESS; - } - - storage::StorageConfig config(base_path); - if (!config.IsValid()) - { - util::Log(logERROR) << "Config contains invalid file paths. Exiting!"; - return EXIT_FAILURE; - } - storage::Storage storage(std::move(config)); - - return storage.Run(max_wait, dataset_name, only_metric); -} -catch (const osrm::RuntimeError &e) -{ - util::Log(logERROR) << e.what(); - return e.GetCode(); -} -catch (const std::bad_alloc &e) -{ - util::DumpMemoryStats(); - util::Log(logERROR) << "[exception] " << e.what(); - util::Log(logERROR) << "Please provide more memory or disable locking the virtual " - "address space (note: this makes OSRM swap, i.e. slow)"; - return EXIT_FAILURE; -} diff --git a/src/tools/util.cpp b/src/tools/util.cpp new file mode 100644 index 000000000..b3ecd06df --- /dev/null +++ b/src/tools/util.cpp @@ -0,0 +1,92 @@ +#include "storage/shared_data_index.hpp" +#include "storage/view_factory.hpp" + +#include "engine/datafacade/mmap_memory_allocator.hpp" +#include "osrm/storage_config.hpp" + +#include +#include + +#include +#include + +using namespace osrm; + +void dump_ch(const std::string &filename) { + std::cout << filename << std::endl; + + StorageConfig config(filename); + engine::datafacade::MMapMemoryAllocator allocator(config); + + auto &index = allocator.GetIndex(); + + //auto exclude_prefix = name + "/exclude/" + std::to_string(exclude_index); + //auto edge_filter = make_vector_view(index, exclude_prefix + "/edge_filter"); + auto node_list = storage::make_vector_view( + index, "/ch/metrics/routability/contracted_graph/node_array"); + auto edge_list = storage::make_vector_view( + index, "/ch/metrics/routability/contracted_graph/edge_array"); + + std::cout << "digraph {" << std::endl; + for (std::size_t i = 0; i < node_list.size(); i++) + { + for (auto edge = node_list[i].first_edge; + edge < (i == node_list.size() - 1 ? edge_list.size() : node_list[i + 1].first_edge); + edge++) + { + const auto &e = edge_list[edge]; + if (e.data.forward) + { + std::cout << i << " -> " << e.target; + std::cout << "["; + std::cout << "label=\"↑" << e.data.weight << "\",weight=\"" << e.data.weight + << "\""; + if (e.data.maneuver_restricted) + { + std::cout << ",color=red,penwidth=3.0"; + } + std::cout << "];"; + std::cout << std::endl; + } + if (e.data.backward) + { + std::cout << e.target << " -> " << i; + std::cout << "["; + std::cout << "label=\"↓" << e.data.weight << "\",weight=\"" << e.data.weight + << "\""; + if (e.data.maneuver_restricted) + { + std::cout << ",color=red,penwidth=3.0"; + } + std::cout << "];"; + std::cout << std::endl; + } + } + } + std::cout << "}" << std::endl; +} + +int main(const int argc, const char *argv[]) +{ + if (argc < 3) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + std::cerr << std::endl; + std::cerr << "Commands:" << std::endl; + std::cerr << " dump-ch - dump the CH graph as a GraphViz .dot file" << std::endl; + return EXIT_FAILURE; + } + + if ("dump-ch" != std::string(argv[1])) { + util::Log(logERROR) << "Invalid command '" << argv[1] << "'" << std::endl; + return EXIT_FAILURE; + } + + boost::filesystem::path p(std::string(argv[2]) + ".hsgr"); + if (!boost::filesystem::exists(p)) { + util::Log(logERROR) << "File '" << argv[2] << "' does not exist" << std::endl; + return EXIT_FAILURE; + } + + // If we make it to here, do it + dump_ch(std::string(argv[2])); +} \ No newline at end of file