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) 3041573592 q for (std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator 3154559962592 q it(nodes.begin()); it != nodes.end(); ++it) 3254518389312 q size += it->second.size()*8 + 64; 37uint64 eval_map(const std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways) 4039653592 q for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator 412428063592 q it(ways.begin()); it != ways.end(); ++it) 422388410334 q size += it->second.size()*128 + 64; 47uint64 eval_map(const std::map< Uint31_Index, std::vector< Relation_Skeleton > >& relations) 5039568592 q for (std::map< Uint31_Index, std::vector< Relation_Skeleton > >::const_iterator 51265647592 q it(relations.begin()); it != relations.end(); ++it) 52226079212 q size += it->second.size()*192 + 64; 57uint64 eval_map(const std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >& nodes) 6041001592 q for (std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator 6138796519592 q it(nodes.begin()); it != nodes.end(); ++it) 6238755518122 q size += it->second.size()*16 + 64; 67uint64 eval_map(const std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >& ways) 7039568592 q for (std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >::const_iterator 71104890592 q it(ways.begin()); it != ways.end(); ++it) 7265322127 q size += it->second.size()*136 + 64; 77uint64 eval_map(const std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >& relations) 8039568592 q for (std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >::const_iterator 81136518592 q it(relations.begin()); it != relations.end(); ++it) 829695097 q size += it->second.size()*200 + 64; 87uint64 eval_map(const std::map< Uint31_Index, std::vector< Area_Skeleton > >& areas) 9039568592 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator 9143093592 q it(areas.begin()); it != areas.end(); ++it) 92352585 q size += it->second.size()*128 + 64; 97uint64 eval_set(const Set& set_) 9939568592 q return eval_map(set_.nodes) + eval_map(set_.ways) + eval_map(set_.relations) 10039568592 q + eval_map(set_.attic_nodes) + eval_map(set_.attic_ways) 10139568592 q + eval_map(set_.attic_relations) + eval_map(set_.areas); 105Set* Runtime_Stack_Frame::get_set(const std::string& set_name) 10719157586 q std::map< std::string, Set >::iterator it = sets.find(set_name); 1113537215 q std::map< std::string, Diff_Set >::iterator it_diff = diff_sets.find(set_name); 1123537215 q if (it_diff != diff_sets.end()) 1162838145 q return parent->get_set(set_name); 122Diff_Set* Runtime_Stack_Frame::get_diff_set(const std::string& set_name) 12496876 q std::map< std::string, Diff_Set >::iterator it = diff_sets.find(set_name); 12596876 q if (it != diff_sets.end()) 12891676 q std::map< std::string, Set >::iterator it_set = sets.find(set_name); 12991676 q if (it_set != sets.end()) 133168 q return parent->get_diff_set(set_name); 139void Runtime_Stack_Frame::swap_set(const std::string& set_name, Set& set_) 14136705592 q diff_sets.erase(set_name); 14236705592 q key_values.erase(set_name); 14336705592 q Set& to_swap = sets[set_name]; 14536705592 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_) 1521616 q key_values.erase(set_name); 1531616 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) 172353 q std::map< std::string, std::map< std::string, std::string > >::iterator it = key_values.find(set_name); 173353 q if (it != key_values.end()) 176353 q std::map< std::string, Set >::iterator it_set = sets.find(set_name); 177353 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) 19241117 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) 21915457 q from = parent->get_set(inner_set_name); 22315256 q size_per_set[top_set_name] = eval_set(*from); 22415256 q sets[top_set_name] = *from; 22821 q size_per_set[top_set_name] = 0; 22921 q sets[top_set_name].clear(); 23115457 q diff_sets.erase(top_set_name); 23215457 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) 238287374 q Runtime_Stack_Frame* source = parent; 240318774 q while (source && source->parent) 242280114 q std::map< std::string, Set >::iterator it = source->sets.find(inner_set_name); 243280114 q if (it != source->sets.end()) 24631411 q std::map< std::string, Diff_Set >::iterator it_diff = source->diff_sets.find(inner_set_name); 24731411 q if (it_diff != source->diff_sets.end()) 25031411 q source = source->parent; 255287374 q size_per_set[top_set_name] = source->size_per_set[inner_set_name]; 256287374 q sets[top_set_name].clear(); 258255970 q source->swap_set(inner_set_name, sets[top_set_name]); 26031411 q sets[top_set_name] = source->sets[inner_set_name]; 262287374 q diff_sets.erase(top_set_name); 263287374 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) 2692686104 q bool new_elements_found = false; 2702686104 q Set* source = get_set(top_set_name); 2742685104 q Set& target = parent->sets[inner_set_name]; 2762685104 q new_elements_found |= indexed_set_union(target.nodes, source->nodes); 2772685104 q new_elements_found |= indexed_set_union(target.attic_nodes, source->attic_nodes); 2792685104 q new_elements_found |= indexed_set_union(target.ways, source->ways); 2802685104 q new_elements_found |= indexed_set_union(target.attic_ways, source->attic_ways); 2822685104 q new_elements_found |= indexed_set_union(target.relations, source->relations); 2832685104 q new_elements_found |= indexed_set_union(target.attic_relations, source->attic_relations); 2852685104 q new_elements_found |= indexed_set_union(target.areas, source->areas); 2862685104 q new_elements_found |= indexed_set_union(target.deriveds, source->deriveds); 2882685104 q parent->size_per_set[inner_set_name] = eval_set(target); 2902686104 q parent->diff_sets.erase(inner_set_name); 2912686104 q parent->key_values.erase(top_set_name); 297void Runtime_Stack_Frame::copy_inward(const std::string& top_set_name, const std::string& inner_set_name) 3022622 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; 3122622 q parent->sets[inner_set_name] = it->second; 3142622 q parent->diff_sets.erase(inner_set_name); 3152622 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) 3212622 q Set* source = get_set(top_set_name); 3252622 q Set& target = parent->sets[inner_set_name]; 3272622 q indexed_set_difference(target.nodes, source->nodes); 3282622 q indexed_set_difference(target.ways, source->ways); 3292622 q indexed_set_difference(target.relations, source->relations); 3312622 q indexed_set_difference(target.attic_nodes, source->attic_nodes); 3322622 q indexed_set_difference(target.attic_ways, source->attic_ways); 3332622 q indexed_set_difference(target.attic_relations, source->attic_relations); 3352622 q indexed_set_difference(target.areas, source->areas); 3362622 q indexed_set_difference(target.deriveds, source->deriveds); 3382622 q parent->size_per_set[inner_set_name] = eval_set(target); 3402622 q parent->diff_sets.erase(inner_set_name); 3412622 q parent->key_values.erase(inner_set_name); 345void Runtime_Stack_Frame::move_all_inward() 34916857 q for (std::map< std::string, Set >::iterator it = sets.begin(); it != sets.end(); ++it) 3519157 q parent->swap_set(it->first, it->second); 3529157 q parent->diff_sets.erase(it->first); 3539157 q parent->key_values.erase(it->first); 359void Runtime_Stack_Frame::move_all_inward_except(const std::string& set_name) 3638396126 q for (std::map< std::string, Set >::iterator it = sets.begin(); it != sets.end(); ++it) 367252159 q parent->swap_set(it->first, it->second); 368252159 q parent->diff_sets.erase(it->first); 369252159 q parent->key_values.erase(it->first); 376uint64 Runtime_Stack_Frame::total_size() 380238928595 q for (std::map< std::string, uint64 >::const_iterator it = size_per_set.begin(); it != size_per_set.end(); ++it) 3842808064 q return result + parent->total_size(); 390std::vector< std::pair< uint, uint > > Runtime_Stack_Frame::stack_progress() const 3920 std::vector< std::pair< uint, uint > > result; 3940 parent->stack_progress().swap(result); 3960 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_) 405493493 q : transaction(&transaction_), error_output(error_output_), 406493493 q area_transaction(0), area_updater_(0), 407493493 q watchdog(watchdog_), global_settings(global_settings_), global_settings_owned(false), 408493493 q start_time(time(NULL)), last_ping_time(0), last_report_time(0), 409493493 q max_allowed_time(0), max_allowed_space(0) 4130 global_settings = new Parsed_Query(); 4140 global_settings_owned = true; 417493493 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__) 424102102 q : transaction(&transaction_), error_output(error_output_), 425102102 q area_transaction(&area_transaction_), area_updater_(area_updater__), 426102102 q watchdog(watchdog_), global_settings(&global_settings_), global_settings_owned(false), 427102102 q start_time(time(NULL)), last_ping_time(0), last_report_time(0), 428102102 q max_allowed_time(0), max_allowed_space(0) 430102102 q runtime_stack.push_back(new Runtime_Stack_Frame()); 434const Set* Resource_Manager::get_set(const std::string& set_name) 43613453586 q if (runtime_stack.empty()) 43913453586 q return runtime_stack.back()->get_set(set_name); 443const Diff_Set* Resource_Manager::get_diff_set(const std::string& set_name) 44593676 q if (runtime_stack.empty()) 44893676 q return runtime_stack.back()->get_diff_set(set_name); 452void Resource_Manager::swap_set(const std::string& set_name, Set& set_) 45431518590 q if (runtime_stack.empty()) 4550 runtime_stack.push_back(new Runtime_Stack_Frame()); 45831518590 q runtime_stack.back()->swap_set(set_name, set_); 462void Resource_Manager::swap_diff_set(const std::string& set_name, Diff_Set& set_) 4641616 q if (runtime_stack.empty()) 4650 runtime_stack.push_back(new Runtime_Stack_Frame()); 4671616 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) 482323 q if (runtime_stack.empty()) 485323 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) 49141117 q if (runtime_stack.empty()) 4920 runtime_stack.push_back(new Runtime_Stack_Frame()); 49441117 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() 5173049177 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) 52315457 q if (!runtime_stack.empty()) 52415457 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) 530287374 q if (!runtime_stack.empty()) 531287374 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) 5402686104 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) 5462622 q if (!runtime_stack.empty()) 5472622 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) 5532622 q if (!runtime_stack.empty()) 5542622 q runtime_stack.back()->substract_from_inward(top_set_name, inner_set_name); 558void Resource_Manager::move_all_inward() 5607757 q if (!runtime_stack.empty()) 5617757 q runtime_stack.back()->move_all_inward(); 565void Resource_Manager::move_all_inward_except(const std::string& set_name) 5672956126 q if (!runtime_stack.empty()) 5682956126 q runtime_stack.back()->move_all_inward_except(set_name); 572void Resource_Manager::pop_stack_frame() 5743049177 q if (!runtime_stack.empty()) 5763049177 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() 6152727445 q if (runtime_stack.empty()) 6182727445 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) 63446279595 q bool extra_space_uses_half_empty = false; 63846279595 q elapsed_time = time(NULL) - start_time + extra_time; 64046279595 q if (elapsed_time >= last_ping_time + 5) 6443328 q last_ping_time = elapsed_time; 6463328 q if (elapsed_time >= last_report_time + 15) 6500 error_output->display_statement_progress 6510 (elapsed_time, stmt.get_name(), stmt.get_progress(), stmt.get_line_number(), 6520 runtime_stack.back()->stack_progress()); 6540 last_report_time = elapsed_time; 65946279595 q if (max_allowed_space > 0) 66146279595 q if (!runtime_stack.empty()) 66246279595 q size += runtime_stack.back()->total_size(); 66346279595 q extra_space_uses_half_empty = (extra_space*2 >= max_allowed_space - size); 66746279595 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()); 69646279595 q return extra_space_uses_half_empty; 700uint64 Resource_Manager::get_desired_timestamp() const 70255372591 q return runtime_stack.empty() ? NOW : runtime_stack.back()->get_desired_timestamp(); 706Diff_Action::_ Resource_Manager::get_desired_action() const 7089519585 q return runtime_stack.empty() ? Diff_Action::positive : runtime_stack.back()->get_desired_action(); 712uint64 Resource_Manager::get_diff_from_timestamp() const 714130842 q return runtime_stack.empty() ? NOW : runtime_stack.back()->get_diff_from_timestamp(); 718uint64 Resource_Manager::get_diff_to_timestamp() const 72093042 q return runtime_stack.empty() ? NOW : runtime_stack.back()->get_diff_to_timestamp(); 724void Resource_Manager::start_diff(uint64 comparison_timestamp, uint64 desired_timestamp) 7267676 q if (!runtime_stack.empty()) 7287676 q runtime_stack.back()->set_desired_action(Diff_Action::collect_lhs); 7297676 q runtime_stack.back()->set_diff_from_timestamp(comparison_timestamp); 7307676 q runtime_stack.back()->set_diff_to_timestamp(desired_timestamp); 7317676 q runtime_stack.back()->set_desired_timestamp(comparison_timestamp); 736void Resource_Manager::switch_diff_rhs(bool add_deletion_information) 7387676 q if (!runtime_stack.empty()) 7407676 q runtime_stack.back()->clear_sets(); 7417676 q runtime_stack.back()->set_desired_action( 7427676 q add_deletion_information ? Diff_Action::collect_rhs_with_del : Diff_Action::collect_rhs_no_del); 7437676 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) 75088 q if (!runtime_stack.empty()) 75288 q runtime_stack.back()->set_desired_action(Diff_Action::show_old); 75388 q runtime_stack.back()->set_desired_timestamp(runtime_stack.back()->get_diff_from_timestamp()); 75588 q Diff_Set* diff_set = runtime_stack.back()->get_diff_set(diff_set_name); 75888 q Set set = diff_set->make_from_set(); 75988 q runtime_stack.back()->swap_set(diff_set_name, set); 765void Resource_Manager::switch_diff_show_to(const std::string& diff_set_name) 76788 q if (!runtime_stack.empty()) 76988 q runtime_stack.back()->set_desired_action(Diff_Action::show_new); 77088 q runtime_stack.back()->set_desired_timestamp(runtime_stack.back()->get_diff_to_timestamp()); 77288 q Diff_Set* diff_set = runtime_stack.back()->get_diff_set(diff_set_name); 77588 q Set set = diff_set->make_to_set(); 77688 q runtime_stack.back()->swap_set(diff_set_name, set); 782void Resource_Manager::set_desired_timestamp(uint64 timestamp) 7841487595 q if (!runtime_stack.empty()) 7851487595 q runtime_stack.back()->set_desired_timestamp(timestamp); 789void Resource_Manager::start_cpu_timer(uint index) 79110023595 q if (cpu_start_time.size() <= index) 7921687595 q cpu_start_time.resize(index+1, 0); 79310023595 q cpu_start_time[index] = clock()/1000; 797void Resource_Manager::stop_cpu_timer(uint index) 79910023595 q if (cpu_runtime.size() <= index) 8001097595 q cpu_runtime.resize(index+1, 0); 80110023595 q if (index < cpu_start_time.size()) 80210023595 q cpu_runtime[index] += clock()/1000 - cpu_start_time[index];