1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "resource_manager.h" 20#include "../data/abstract_processing.h" 21#include "../data/utils.h" 22#include "../statements/statement.h" 27uint64 eval_map(const std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes) 30156881670 q for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator 3121763080670 q it(nodes.begin()); it != nodes.end(); ++it) 3221606199326 q size += it->second.size()*8 + 64; 37uint64 eval_map(const std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways) 40154238670 q for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator 412817048670 q it(ways.begin()); it != ways.end(); ++it) 422662810349 q size += it->second.size()*128 + 64; 47uint64 eval_map(const std::map< Uint31_Index, std::vector< Relation_Skeleton > >& relations) 50153693670 q for (std::map< Uint31_Index, std::vector< Relation_Skeleton > >::const_iterator 51394076670 q it(relations.begin()); it != relations.end(); ++it) 52240383211 q size += it->second.size()*192 + 64; 57uint64 eval_map(const std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >& nodes) 60155701670 q for (std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator 618943490670 q it(nodes.begin()); it != nodes.end(); ++it) 628787789140 q size += it->second.size()*16 + 64; 67uint64 eval_map(const std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >& ways) 70153693670 q for (std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >::const_iterator 71187346670 q it(ways.begin()); it != ways.end(); ++it) 7233653133 q size += it->second.size()*136 + 64; 77uint64 eval_map(const std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >& relations) 80153693670 q for (std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >::const_iterator 81263878670 q it(relations.begin()); it != relations.end(); ++it) 82110185101 q size += it->second.size()*200 + 64; 87uint64 eval_map(const std::map< Uint31_Index, std::vector< Area_Skeleton > >& areas) 90153693670 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator 91155300670 q it(areas.begin()); it != areas.end(); ++it) 92160791 q size += it->second.size()*128 + 64; 97uint64 eval_set(const Set& set_) 99153693670 q return eval_map(set_.nodes) + eval_map(set_.ways) + eval_map(set_.relations) 100153693670 q + eval_map(set_.attic_nodes) + eval_map(set_.attic_ways) 101153693670 q + eval_map(set_.attic_relations) + eval_map(set_.areas); 105Set* Runtime_Stack_Frame::get_set(const std::string& set_name) 107110058666 q std::map< std::string, Set >::iterator it = sets.find(set_name); 11128141254 q std::map< std::string, Diff_Set >::iterator it_diff = diff_sets.find(set_name); 11228141254 q if (it_diff != diff_sets.end()) 11627416161 q return parent->get_set(set_name); 122Diff_Set* Runtime_Stack_Frame::get_diff_set(const std::string& set_name) 124102781 q std::map< std::string, Diff_Set >::iterator it = diff_sets.find(set_name); 125102781 q if (it != diff_sets.end()) 12895981 q std::map< std::string, Set >::iterator it_set = sets.find(set_name); 12995981 q if (it_set != sets.end()) 1332211 q return parent->get_diff_set(set_name); 139void Runtime_Stack_Frame::swap_set(const std::string& set_name, Set& set_) 141127625670 q diff_sets.erase(set_name); 142127625670 q key_values.erase(set_name); 143127625670 q Set& to_swap = sets[set_name]; 145127625670 q size_per_set[set_name] = eval_set(to_swap); 149void Runtime_Stack_Frame::swap_diff_set(const std::string& set_name, Diff_Set& set_) 1522020 q key_values.erase(set_name); 1532020 q Diff_Set& to_swap = diff_sets[set_name]; 158const std::string* Runtime_Stack_Frame::get_value(const std::string& set_name, const std::string& key) 1600 const std::map< std::string, std::string >* key_values = get_set_key_values(set_name); 1630 std::map< std::string, std::string >::const_iterator kvit = key_values->find(key); 1640 return kvit == key_values->end() ? 0 : &kvit->second; 170const std::map< std::string, std::string >* Runtime_Stack_Frame::get_set_key_values(const std::string& set_name) 1721233 q std::map< std::string, std::map< std::string, std::string > >::iterator it = key_values.find(set_name); 1731233 q if (it != key_values.end()) 1761233 q std::map< std::string, Set >::iterator it_set = sets.find(set_name); 1771233 q if (it_set != sets.end()) 17963 q std::map< std::string, Diff_Set >::iterator it_diff = diff_sets.find(set_name); 18063 q if (it_diff != diff_sets.end()) 18433 q return parent->get_set_key_values(set_name); 190void Runtime_Stack_Frame::set_value(const std::string& set_name, const std::string& key, const std::string& value) 19240414 q key_values[set_name][key] = value; 196void Runtime_Stack_Frame::erase_set(const std::string& set_name) 1980 sets.erase(set_name); 1990 diff_sets.erase(set_name); 2000 key_values.erase(set_name); 2010 size_per_set[set_name] = 0; 205void Runtime_Stack_Frame::clear_sets() 214void Runtime_Stack_Frame::copy_outward(const std::string& inner_set_name, const std::string& top_set_name) 21920869 q from = parent->get_set(inner_set_name); 22320468 q size_per_set[top_set_name] = eval_set(*from); 22420468 q sets[top_set_name] = *from; 22842 q size_per_set[top_set_name] = 0; 22942 q sets[top_set_name].clear(); 23120869 q diff_sets.erase(top_set_name); 23220869 q key_values.erase(top_set_name); 236void Runtime_Stack_Frame::move_outward(const std::string& inner_set_name, const std::string& top_set_name) 2382747796 q Runtime_Stack_Frame* source = parent; 2402929496 q while (source && source->parent) 2422738621 q std::map< std::string, Set >::iterator it = source->sets.find(inner_set_name); 2432738621 q if (it != source->sets.end()) 246181714 q std::map< std::string, Diff_Set >::iterator it_diff = source->diff_sets.find(inner_set_name); 247181714 q if (it_diff != source->diff_sets.end()) 250181714 q source = source->parent; 2552747796 q size_per_set[top_set_name] = source->size_per_set[inner_set_name]; 2562747796 q sets[top_set_name].clear(); 2582566087 q source->swap_set(inner_set_name, sets[top_set_name]); 260181714 q sets[top_set_name] = source->sets[inner_set_name]; 2622747796 q diff_sets.erase(top_set_name); 2632747796 q key_values.erase(top_set_name); 267bool Runtime_Stack_Frame::union_inward(const std::string& top_set_name, const std::string& inner_set_name) 26925841137 q bool new_elements_found = false; 27025841137 q Set* source = get_set(top_set_name); 27425839136 q Set& target = parent->sets[inner_set_name]; 27625839136 q new_elements_found |= indexed_set_union(target.nodes, source->nodes); 27725839136 q new_elements_found |= indexed_set_union(target.attic_nodes, source->attic_nodes); 27925839136 q new_elements_found |= indexed_set_union(target.ways, source->ways); 28025839136 q new_elements_found |= indexed_set_union(target.attic_ways, source->attic_ways); 28225839136 q new_elements_found |= indexed_set_union(target.relations, source->relations); 28325839136 q new_elements_found |= indexed_set_union(target.attic_relations, source->attic_relations); 28525839136 q new_elements_found |= indexed_set_union(target.areas, source->areas); 28625839136 q new_elements_found |= indexed_set_union(target.deriveds, source->deriveds); 28825839136 q parent->size_per_set[inner_set_name] = eval_set(target); 29025841137 q parent->diff_sets.erase(inner_set_name); 29125841137 q parent->key_values.erase(top_set_name); 29325841137 q return new_elements_found; 297void Runtime_Stack_Frame::copy_inward(const std::string& top_set_name, const std::string& inner_set_name) 3022521 q std::map< std::string, Set >::iterator it = sets.find(top_set_name); 3050 Set* source = parent->get_set(top_set_name); 3070 parent->sets[inner_set_name] = Set(); 3080 else if (source != &parent->sets[inner_set_name]) 3090 parent->sets[inner_set_name] = *source; 3122521 q parent->sets[inner_set_name] = it->second; 3142521 q parent->diff_sets.erase(inner_set_name); 3152521 q parent->key_values.erase(inner_set_name); 319void Runtime_Stack_Frame::substract_from_inward(const std::string& top_set_name, const std::string& inner_set_name) 3212521 q Set* source = get_set(top_set_name); 3252521 q Set& target = parent->sets[inner_set_name]; 3272521 q indexed_set_difference(target.nodes, source->nodes); 3282521 q indexed_set_difference(target.ways, source->ways); 3292521 q indexed_set_difference(target.relations, source->relations); 3312521 q indexed_set_difference(target.attic_nodes, source->attic_nodes); 3322521 q indexed_set_difference(target.attic_ways, source->attic_ways); 3332521 q indexed_set_difference(target.attic_relations, source->attic_relations); 3352521 q indexed_set_difference(target.areas, source->areas); 3362521 q indexed_set_difference(target.deriveds, source->deriveds); 3382521 q parent->size_per_set[inner_set_name] = eval_set(target); 3402521 q parent->diff_sets.erase(inner_set_name); 3412521 q parent->key_values.erase(inner_set_name); 345void Runtime_Stack_Frame::move_all_inward() 34923369 q for (std::map< std::string, Set >::iterator it = sets.begin(); it != sets.end(); ++it) 35112969 q parent->swap_set(it->first, it->second); 35212969 q parent->diff_sets.erase(it->first); 35312969 q parent->key_values.erase(it->first); 359void Runtime_Stack_Frame::move_all_inward_except(const std::string& set_name) 36380681139 q for (std::map< std::string, Set >::iterator it = sets.begin(); it != sets.end(); ++it) 36553127134 q if (it->first != set_name) 3672560674 q parent->swap_set(it->first, it->second); 3682560674 q parent->diff_sets.erase(it->first); 3692560674 q parent->key_values.erase(it->first); 376uint64 Runtime_Stack_Frame::total_size() 3801484620673 q for (std::map< std::string, uint64 >::const_iterator it = size_per_set.begin(); it != size_per_set.end(); ++it) 38425784072 q return result + parent->total_size(); 390std::vector< std::pair< uint, uint > > Runtime_Stack_Frame::stack_progress() const 39211 q std::vector< std::pair< uint, uint > > result; 3940 parent->stack_progress().swap(result); 39611 q result.push_back(std::make_pair(loop_count, loop_size)); 402Resource_Manager::Resource_Manager( 403 Transaction& transaction_, Parsed_Query* global_settings_, Watchdog_Callback* watchdog_, 404 Error_Output* error_output_) 405551551 q : transaction(&transaction_), error_output(error_output_), 406551551 q area_transaction(0), area_updater_(0), 407551551 q watchdog(watchdog_), global_settings(global_settings_), global_settings_owned(false), 408551551 q start_time(time(NULL)), last_ping_time(0), last_report_time(0), 409551551 q max_allowed_time(0), max_allowed_space(0) 4130 global_settings = new Parsed_Query(); 4140 global_settings_owned = true; 417551551 q runtime_stack.push_back(new Runtime_Stack_Frame()); 421Resource_Manager::Resource_Manager( 422 Transaction& transaction_, Parsed_Query& global_settings_, Error_Output* error_output_, 423 Transaction& area_transaction_, Watchdog_Callback* watchdog_, Area_Usage_Listener* area_updater__) 424122122 q : transaction(&transaction_), error_output(error_output_), 425122122 q area_transaction(&area_transaction_), area_updater_(area_updater__), 426122122 q watchdog(watchdog_), global_settings(&global_settings_), global_settings_owned(false), 427122122 q start_time(time(NULL)), last_ping_time(0), last_report_time(0), 428122122 q max_allowed_time(0), max_allowed_space(0) 430122122 q runtime_stack.push_back(new Runtime_Stack_Frame()); 434const Set* Resource_Manager::get_set(const std::string& set_name) 43656568666 q if (runtime_stack.empty()) 43956568666 q return runtime_stack.back()->get_set(set_name); 443const Diff_Set* Resource_Manager::get_diff_set(const std::string& set_name) 44598381 q if (runtime_stack.empty()) 44898381 q return runtime_stack.back()->get_diff_set(set_name); 452void Resource_Manager::swap_set(const std::string& set_name, Set& set_) 45476208669 q if (runtime_stack.empty()) 4550 runtime_stack.push_back(new Runtime_Stack_Frame()); 45876208669 q runtime_stack.back()->swap_set(set_name, set_); 462void Resource_Manager::swap_diff_set(const std::string& set_name, Diff_Set& set_) 4642020 q if (runtime_stack.empty()) 4650 runtime_stack.push_back(new Runtime_Stack_Frame()); 4672020 q runtime_stack.back()->swap_diff_set(set_name, set_); 471const std::string* Resource_Manager::get_value(const std::string& set_name, const std::string& key) 4730 if (runtime_stack.empty()) 4760 return runtime_stack.back()->get_value(set_name, key); 480const std::map< std::string, std::string >* Resource_Manager::get_set_key_values(const std::string& set_name) 4821203 q if (runtime_stack.empty()) 4851203 q return runtime_stack.back()->get_set_key_values(set_name); 489void Resource_Manager::set_value(const std::string& set_name, const std::string& key, const std::string& value) 49140414 q if (runtime_stack.empty()) 4920 runtime_stack.push_back(new Runtime_Stack_Frame()); 49440414 q runtime_stack.back()->set_value(set_name, key, value); 498void Resource_Manager::erase_set(const std::string& set_name) 5000 if (runtime_stack.empty()) 5010 runtime_stack.push_back(new Runtime_Stack_Frame()); 5030 runtime_stack.back()->erase_set(set_name); 507void Resource_Manager::clear_sets() 5090 if (!runtime_stack.empty()) 5100 runtime_stack.back()->clear_sets(); 515void Resource_Manager::push_stack_frame() 51727680203 q runtime_stack.push_back(new Runtime_Stack_Frame(runtime_stack.empty() ? 0 : runtime_stack.back())); 521void Resource_Manager::copy_outward(const std::string& inner_set_name, const std::string& top_set_name) 52320869 q if (!runtime_stack.empty()) 52420869 q runtime_stack.back()->copy_outward(inner_set_name, top_set_name); 528void Resource_Manager::move_outward(const std::string& inner_set_name, const std::string& top_set_name) 5302747796 q if (!runtime_stack.empty()) 5312747796 q runtime_stack.back()->move_outward(inner_set_name, top_set_name); 535bool Resource_Manager::union_inward(const std::string& top_set_name, const std::string& inner_set_name) 53725841137 q if (runtime_stack.empty()) 54025841137 q return runtime_stack.back()->union_inward(top_set_name, inner_set_name); 544void Resource_Manager::copy_inward(const std::string& top_set_name, const std::string& inner_set_name) 5462521 q if (!runtime_stack.empty()) 5472521 q runtime_stack.back()->copy_inward(top_set_name, inner_set_name); 551void Resource_Manager::substract_from_inward(const std::string& top_set_name, const std::string& inner_set_name) 5532521 q if (!runtime_stack.empty()) 5542521 q runtime_stack.back()->substract_from_inward(top_set_name, inner_set_name); 558void Resource_Manager::move_all_inward() 56010469 q if (!runtime_stack.empty()) 56110469 q runtime_stack.back()->move_all_inward(); 565void Resource_Manager::move_all_inward_except(const std::string& set_name) 56727554139 q if (!runtime_stack.empty()) 56827554139 q runtime_stack.back()->move_all_inward_except(set_name); 572void Resource_Manager::pop_stack_frame() 57427680203 q if (!runtime_stack.empty()) 57627680203 q delete runtime_stack.back(); 582uint count_set(const Set& set_) 5850 for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator 5860 it(set_.nodes.begin()); it != set_.nodes.end(); ++it) 5870 size += it->second.size(); 5880 for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator 5890 it(set_.ways.begin()); it != set_.ways.end(); ++it) 5900 size += it->second.size(); 5910 for (std::map< Uint31_Index, std::vector< Relation_Skeleton > >::const_iterator 5920 it(set_.relations.begin()); it != set_.relations.end(); ++it) 5930 size += it->second.size(); 5950 for (std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator 5960 it(set_.attic_nodes.begin()); it != set_.attic_nodes.end(); ++it) 5970 size += it->second.size(); 5980 for (std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >::const_iterator 5990 it(set_.attic_ways.begin()); it != set_.attic_ways.end(); ++it) 6000 size += it->second.size(); 6010 for (std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >::const_iterator 6020 it(set_.attic_relations.begin()); it != set_.attic_relations.end(); ++it) 6030 size += it->second.size(); 6050 for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator 6060 it(set_.areas.begin()); it != set_.areas.end(); ++it) 6070 size += it->second.size(); 613void Resource_Manager::count_loop() 6154860340 q if (runtime_stack.empty()) 6184860340 q runtime_stack.back()->count_loop(); 622void Resource_Manager::log_and_display_error(std::string message) 6250 error_output->runtime_error(message); 6270 Logger logger(transaction->get_db_dir()); 6280 logger.annotated_log(message); 632bool Resource_Manager::health_check(const Statement& stmt, uint32 extra_time, uint64 extra_space) 634187735673 q bool extra_space_uses_half_empty = false; 637187735673 q if (max_allowed_time > 0) 638187735673 q elapsed_time = time(NULL) - start_time + extra_time; 640187735673 q if (elapsed_time >= last_ping_time + 5) 6446360 q last_ping_time = elapsed_time; 6466360 q if (elapsed_time >= last_report_time + 15) 65011 q error_output->display_statement_progress 65111 q (elapsed_time, stmt.get_name(), stmt.get_progress(), stmt.get_line_number(), 65211 q runtime_stack.back()->stack_progress()); 65411 q last_report_time = elapsed_time; 659187735673 q if (max_allowed_space > 0) 661187735673 q if (!runtime_stack.empty()) 662187735673 q size += runtime_stack.back()->total_size(); 663187735673 q extra_space_uses_half_empty = (extra_space*2 >= max_allowed_space - size); 667187735673 q if (elapsed_time > max_allowed_time || size > max_allowed_space) 6710 error_output->display_statement_progress 6720 (elapsed_time, stmt.get_name(), stmt.get_progress(), stmt.get_line_number(), 6730 runtime_stack.back()->stack_progress()); 6760 Resource_Error error; 6770 error.timed_out = (elapsed_time > max_allowed_time); 6780 error.stmt_name = stmt.get_name(); 6790 error.line_number = stmt.get_line_number(); 6810 error.runtime = elapsed_time; 6830 Logger logger(transaction->get_db_dir()); 6840 std::ostringstream out; 6850 if (elapsed_time > max_allowed_time) 6890 out<<" runtime "<<error.runtime<<" seconds, size "<<error.size<<" bytes, " 6900 "in line "<<error.line_number<<", statement "<<error.stmt_name; 6910 logger.annotated_log(out.str()); 696187735673 q return extra_space_uses_half_empty; 700uint64 Resource_Manager::get_desired_timestamp() const 702243965669 q return runtime_stack.empty() ? NOW : runtime_stack.back()->get_desired_timestamp(); 706Diff_Action::_ Resource_Manager::get_desired_action() const 70829115665 q return runtime_stack.empty() ? Diff_Action::positive : runtime_stack.back()->get_desired_action(); 712uint64 Resource_Manager::get_diff_from_timestamp() const 714129447 q return runtime_stack.empty() ? NOW : runtime_stack.back()->get_diff_from_timestamp(); 718uint64 Resource_Manager::get_diff_to_timestamp() const 72093847 q return runtime_stack.empty() ? NOW : runtime_stack.back()->get_diff_to_timestamp(); 724void Resource_Manager::start_diff(uint64 comparison_timestamp, uint64 desired_timestamp) 7268181 q if (!runtime_stack.empty()) 7288181 q runtime_stack.back()->set_desired_action(Diff_Action::collect_lhs); 7298181 q runtime_stack.back()->set_diff_from_timestamp(comparison_timestamp); 7308181 q runtime_stack.back()->set_diff_to_timestamp(desired_timestamp); 7318181 q runtime_stack.back()->set_desired_timestamp(comparison_timestamp); 736void Resource_Manager::switch_diff_rhs(bool add_deletion_information) 7388181 q if (!runtime_stack.empty()) 7408181 q runtime_stack.back()->clear_sets(); 7418181 q runtime_stack.back()->set_desired_action( 7428181 q add_deletion_information ? Diff_Action::collect_rhs_with_del : Diff_Action::collect_rhs_no_del); 7438181 q runtime_stack.back()->set_desired_timestamp(runtime_stack.back()->get_diff_to_timestamp()); 748void Resource_Manager::switch_diff_show_from(const std::string& diff_set_name) 7501111 q if (!runtime_stack.empty()) 7521111 q runtime_stack.back()->set_desired_action(Diff_Action::show_old); 7531111 q runtime_stack.back()->set_desired_timestamp(runtime_stack.back()->get_diff_from_timestamp()); 7551111 q Diff_Set* diff_set = runtime_stack.back()->get_diff_set(diff_set_name); 7581111 q Set set = diff_set->make_from_set(); 7591111 q runtime_stack.back()->swap_set(diff_set_name, set); 765void Resource_Manager::switch_diff_show_to(const std::string& diff_set_name) 7671111 q if (!runtime_stack.empty()) 7691111 q runtime_stack.back()->set_desired_action(Diff_Action::show_new); 7701111 q runtime_stack.back()->set_desired_timestamp(runtime_stack.back()->get_diff_to_timestamp()); 7721111 q Diff_Set* diff_set = runtime_stack.back()->get_diff_set(diff_set_name); 7751111 q Set set = diff_set->make_to_set(); 7761111 q runtime_stack.back()->swap_set(diff_set_name, set); 782void Resource_Manager::set_desired_timestamp(uint64 timestamp) 7841621673 q if (!runtime_stack.empty()) 7851621673 q runtime_stack.back()->set_desired_timestamp(timestamp); 789void Resource_Manager::start_cpu_timer(uint index) 79129750673 q if (cpu_start_time.size() <= index) 7921905673 q cpu_start_time.resize(index+1, 0); 79329750673 q cpu_start_time[index] = clock()/1000; 797void Resource_Manager::stop_cpu_timer(uint index) 79929750673 q if (cpu_runtime.size() <= index) 8001236673 q cpu_runtime.resize(index+1, 0); 80129750673 q if (index < cpu_start_time.size()) 80229750673 q cpu_runtime[index] += clock()/1000 - cpu_start_time[index];