Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions backends/osmand/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,15 @@ target_link_options(route_bench PRIVATE -static-libstdc++ -static-libgcc -Wl,-Bs
add_executable(route_bench_run ${ONC_ROOT}/deploy/bench/route_bench_run.c)
target_link_libraries(route_bench_run PRIVATE c)
set_target_properties(route_bench_run PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bench)

# Full worker diagnostic: queueing, routing and OM turns.
add_library(route_worker_bench SHARED ${ONC_ROOT}/deploy/bench/route_worker_bench.cpp
navigation_worker.cpp ch_planner.cpp route_maneuvers.cpp config.cpp)
target_include_directories(route_worker_bench PRIVATE ${ONC_OSMAND_INCLUDES})
target_compile_definitions(route_worker_bench PRIVATE ${ONC_OSMAND_DEFINES} ONC_ROUTE_DIAGNOSTICS)
target_compile_options(route_worker_bench PRIVATE ${ONC_WARNINGS} ${ONC_OSMAND_FLAGS})
target_link_libraries(route_worker_bench PRIVATE navigation_ui organicmaps_turns osmand_core m c)
set_target_properties(route_worker_bench PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bench
SKIP_BUILD_RPATH ON)
target_link_options(route_worker_bench PRIVATE -static-libstdc++ -static-libgcc -Wl,-Bsymbolic
-Wl,-rpath,/open-navicore -Wl,--no-undefined)
92 changes: 75 additions & 17 deletions backends/osmand/ch_planner.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ch_planner.h"

#include "ch/ch_sidecar.h"
#include "snap_policy.h"

#include "binaryRoutePlanner.h"
#include "generalRouter.h"
Expand All @@ -13,6 +14,7 @@
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>

Expand Down Expand Up @@ -42,7 +44,6 @@ constexpr double kSnapMsPerMetre = 120.0; // the distance to the road, as t
constexpr double kReversePenaltyMs = 60000.0; // starting against the car's heading
constexpr double kMaxSnapMetres = 1000.0;
constexpr double kMaxApproachMetres = 5000.0; // along a road to its first junction
constexpr std::size_t kSnapRoads = 8; // nearest roads tried at each end

double metres_between(int x1, int y1, int x2, int y2)
{
Expand Down Expand Up @@ -76,7 +77,6 @@ std::vector<RoadSnap> snap_roads(RoutingContext* context, int x, int y)
points.insert(points.end(), nearest->others.begin(), nearest->others.end());
const auto& router = context->config->router;
for (const auto& point : points) {
if (snaps.size() >= kSnapRoads) break;
if (!point || !point->road || !router->acceptLine(point->road)) continue;
RoadSnap snap;
snap.road = point->road;
Expand All @@ -91,6 +91,13 @@ std::vector<RoadSnap> snap_roads(RoutingContext* context, int x, int y)
continue;
snaps.push_back(snap);
}
keep_nearest_roads(snaps);
#ifdef ONC_ROUTE_DIAGNOSTICS
// Causal probe for the route survey, never compiled into the backend.
const char* nearest_only = std::getenv("ROUTE_BENCH_NEAREST_ONLY");
if (nearest_only && std::strcmp(nearest_only, "1") == 0 && snaps.size() > 1)
snaps.resize(1);
#endif
return snaps;
}

Expand Down Expand Up @@ -283,7 +290,7 @@ std::string ChPlanner::describe() const { return impl_->description; }

ChPlanner::Segments ChPlanner::route(RoutePlannerFrontEnd& front_end, const SHARED_PTR<RoutingContext>& context,
int start_x, int start_y, int end_x, int end_y, std::string& why,
double start_heading_degrees)
double start_heading_degrees, RouteRoads* roads)
{
timing_ = Timing();
why.clear();
Expand All @@ -310,6 +317,23 @@ ChPlanner::Segments ChPlanner::route(RoutePlannerFrontEnd& front_end, const SHAR
}
timing_.snap_ms = ms_since(step);

#ifdef ONC_ROUTE_DIAGNOSTICS
// Standalone route survey only; absent from the installed backend.
std::size_t start_nodes = 0, end_nodes = 0;
for (const auto& approach : starts) start_nodes += approach.nodes.size();
for (const auto& approach : ends) end_nodes += approach.nodes.size();
std::fprintf(stderr, "ch_diag start_roads=%zu end_roads=%zu start_approaches=%zu end_approaches=%zu "
"start_nodes=%zu end_nodes=%zu direct=%d snap_ms=%.1f\n", from_roads.size(), to_roads.size(),
starts.size(), ends.size(), start_nodes, end_nodes, has_direct ? 1 : 0, timing_.snap_ms);
const auto print_snaps = [](const char* side, const std::vector<RoadSnap>& snaps) {
for (const auto& snap : snaps)
std::fprintf(stderr, "ch_snap side=%s road=%lld gap_m=%.2f lo=%d hi=%d oneway=%d\n",
side, static_cast<long long>(snap.road->getId()), snap.metres, snap.lo, snap.hi, snap.oneway);
};
print_snaps("start", from_roads);
print_snaps("end", to_roads);
#endif

std::vector<onc_ch::RoadRun> runs;
onc_ch::u32 cost = 0;
const Approach* first = nullptr;
Expand Down Expand Up @@ -354,32 +378,52 @@ ChPlanner::Segments ChPlanner::route(RoutePlannerFrontEnd& front_end, const SHAR
} else if (has_direct) {
use_direct = true;
} else {
// No road near enough to snap onto: the nearest junctions, as before.
step = Clock::now();
const auto status = impl_->router->route(start_x, start_y, end_x, end_y, runs, cost,
start_heading_degrees, cancelled);
timing_.search_ms = ms_since(step);
timing_.settled = impl_->router->settled();
if (status == onc_ch::Router::Status::NoRoute) { why = "no_route_in_sidecar"; return {}; }
if (status == onc_ch::Router::Status::Inconsistent) { why = "sidecar_inconsistent"; return {}; }
if (status == onc_ch::Router::Status::Cancelled) { why = "cancelled"; return {}; }
if (runs.empty()) { why = "too_short_for_sidecar"; return {}; }
// Do not silently bypass the selected endpoint road with a nearby node.
why = from_roads.empty() ? "no_start_road" : to_roads.empty() ? "no_destination_road" :
starts.empty() ? "start_road_not_connected" : "destination_road_not_connected";
return {};
}
timing_.base_edges = use_direct ? 0 : impl_->router->edges().size();
timing_.road_runs = use_direct ? 0 : runs.size();

Segments results;
// Capture adjacent roads as geometry is loaded in travel order. OsmAnd's
// prepareResult and OM can then share that snapshot instead of decoding
// the same long route's evicted tiles on two further passes.
const auto append = [&](const SHARED_PTR<RouteDataObject>& road, int start, int end) {
auto result = std::make_shared<RouteSegmentResult>(road, start, end);
if (roads) {
const int direction = start <= end ? 1 : -1;
result->preAttachedRoutes.reserve(std::abs(end - start) + 1);
for (int i = start;; i += direction) {
const auto key = route_node_key(road->pointsX[i], road->pointsY[i]);
auto found = roads->find(key);
if (found == roads->end()) {
std::vector<std::shared_ptr<RouteSegmentResult>> adjacent;
for (const auto& segment : context->loadRouteSegment(road->pointsX[i], road->pointsY[i])) {
if (segment) adjacent.push_back(std::make_shared<RouteSegmentResult>(
segment->getRoad(), segment->getSegmentStart(), segment->getSegmentStart()));
}
found = roads->emplace(key, std::move(adjacent)).first;
}
result->preAttachedRoutes.push_back(found->second);
if (i == end) break;
}
}
results.push_back(std::move(result));
};
if (use_direct) {
results.push_back(std::make_shared<RouteSegmentResult>(direct.road, direct.start, direct.end));
append(direct.road, direct.start, direct.end);
timing_.direct = true;
} else {
// Each stretch needs its RouteDataObject: loading the tile at a point of the road
// yields the road, found by its id (the way HHRoutePlanner finds its network roads).
step = Clock::now();
results.reserve(runs.size() + 2);
if (first && first->start != first->end)
results.push_back(std::make_shared<RouteSegmentResult>(first->road, first->start, first->end));
append(first->road, first->start, first->end);
for (const onc_ch::RoadRun& run : runs) {
if (cancelled()) { why = "cancelled"; return {}; }
SHARED_PTR<RouteDataObject> road;
for (const auto& segment : context->loadRouteSegment(run.anchor_x, run.anchor_y)) {
if (segment && segment->getRoad()->getId() == run.road) {
Expand All @@ -389,20 +433,34 @@ ChPlanner::Segments ChPlanner::route(RoutePlannerFrontEnd& front_end, const SHAR
}
const int points = road ? static_cast<int>(road->pointsX.size()) : 0;
if (!road || run.start < 0 || run.end < 0 || run.start >= points || run.end >= points) {
#ifdef ONC_ROUTE_DIAGNOSTICS
std::fprintf(stderr, "ch_diag missing_road=%lld from=%d to=%d points=%d anchor=%d,%d\n",
static_cast<long long>(run.road), run.start, run.end, points, run.anchor_x, run.anchor_y);
#endif
why = "road_missing_in_maps";
return {};
}
results.push_back(std::make_shared<RouteSegmentResult>(road, run.start, run.end));
append(road, run.start, run.end);
}
if (last && last->start != last->end)
results.push_back(std::make_shared<RouteSegmentResult>(last->road, last->start, last->end));
append(last->road, last->start, last->end);
timing_.roads_ms = ms_since(step);
if (results.empty()) { why = "too_short_for_sidecar"; return {}; }
}

step = Clock::now();
prepareResult(context.get(), results);
front_end.makeStartEndPointsPrecise(context.get(), results, start_x, start_y, end_x, end_y);
#ifdef ONC_ROUTE_DIAGNOSTICS
if (!results.empty()) {
const auto& first_result = results.front();
const auto& last_result = results.back();
std::fprintf(stderr, "ch_selected start_road=%lld end_road=%lld start_index=%d end_index=%d direct=%d\n",
static_cast<long long>(first_result->object->getId()),
static_cast<long long>(last_result->object->getId()), first_result->getStartPointIndex(),
last_result->getEndPointIndex(), use_direct ? 1 : 0);
}
#endif
timing_.prepare_ms = ms_since(step);
return results;
}
Expand Down
15 changes: 8 additions & 7 deletions backends/osmand/ch_planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <string>
#include <vector>
#include "route_roads.h"

// Core and core-legacy both ship a Logging.h with the same public names but
// different include guards. The provider uses Core's logger consistently.
Expand All @@ -19,27 +20,27 @@ namespace navicore_osmand {
// Routes with a contraction hierarchy built offline from the same OBF maps (a
// "sidecar", tools/ch) and hands the result to the core's own result
// preparation, so turn instructions, street names and attached roads are
// exactly what the A* and HH routers give. The search itself takes well under
// a millisecond; what remains is loading the route's roads from the maps.
// exactly what the A* and HH routers give. Search, geometry loading and result preparation are measured separately;
// long routes are dominated by map loading and turn preparation.
class ChPlanner {
public:
using Segments = std::vector<SHARED_PTR<RouteSegmentResult>>;

struct Timing {
double snap_ms = 0; // start/end onto roads, along them to junctions
double search_ms = 0; // hierarchy query, unpack
double roads_ms = 0; // load the route's roads from the maps
double roads_ms = 0; // load route roads and optional junction snapshots
double prepare_ms = 0; // prepareResult + makeStartEndPointsPrecise
std::size_t road_runs = 0;
std::size_t base_edges = 0;
std::size_t settled = 0;
bool road_snap = false; // snapped onto roads (else to the nearest junctions)
bool road_snap = false; // selected endpoint roads connected to the hierarchy
bool direct = false; // start and end on one road: no hierarchy needed
};

// Maps the sidecar and checks it was built from exactly these map files
// (same names and sizes, whatever the order). On failure returns null and
// says why in `error`; the caller then routes without it.
// says why in `error`; the caller reports that its selected router is unavailable.
static std::unique_ptr<ChPlanner> open(const std::string& sidecar_path,
const std::vector<std::string>& routable_maps,
std::string& error);
Expand All @@ -48,12 +49,12 @@ class ChPlanner {
// The fastest route between two points (31-bit tile numbers), or empty
// when the sidecar cannot serve it (a point too far from every road,
// start and end on one road node, a road missing from the loaded maps);
// `why` then names the reason and the caller falls back to HH or A*.
// `why` then names the reason; the caller reports failure.
// start_heading_degrees: the car's direction (clockwise from north), or
// negative when unknown; start nodes behind it cost a reverse penalty.
Segments route(RoutePlannerFrontEnd& front_end, const SHARED_PTR<RoutingContext>& context,
int start_x, int start_y, int end_x, int end_y, std::string& why,
double start_heading_degrees = -1.0);
double start_heading_degrees = -1.0, RouteRoads* roads = nullptr);

const Timing& last_timing() const { return timing_; }
// One-line description for the log: nodes, edges, maps.
Expand Down
8 changes: 5 additions & 3 deletions backends/osmand/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ struct Config {
std::string vector_cache = "/datastore/navicore-backend/vectorcache";
// Contraction-hierarchy routing sidecar built from the maps (tools/ch):
// "car.chr" in the map directory, or "<map file>.chr" next to a single
// map file. Routing falls back to HH and A* when it is absent, stale or
// cannot serve a route. Empty when disabled (marker
// /datastore/navicore-backend/disable-ch, or OPEN_NAVICORE_OSMAND_CH_DISABLE=1).
// map file. A configured sidecar that is absent, stale or cannot serve a
// route reports failure. Empty explicitly selects HH (or A* for maps
// without HH data); the routers never retry each other's failures. Disabled by
// the marker
// /datastore/navicore-backend/disable-ch, or OPEN_NAVICORE_OSMAND_CH_DISABLE=1.
std::string ch_sidecar = "";
// Turn instructions come from Organic Maps' turn generator (omaps_turns.h).
// OsmAnd's own only with the marker /datastore/navicore-backend/osmand-turns
Expand Down
Loading