Catch bad_alloc and provide human reasable error messages

This commit is contained in:
Daniel J. Hofmann 2015-10-18 12:34:42 +02:00 committed by Patrick Niklaus
parent 79650ca3fd
commit 1dfa2d6e91
4 changed files with 43 additions and 10 deletions

View File

@ -62,6 +62,7 @@ using QueryGraph = StaticGraph<QueryEdge::EdgeData>;
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <new>
// delete a shared memory region. report warning if it could not be deleted // delete a shared memory region. report warning if it could not be deleted
void delete_region(const SharedDataType region) void delete_region(const SharedDataType region)
@ -123,7 +124,7 @@ int main(const int argc, const char *argv[]) try
ServerPaths server_paths; ServerPaths server_paths;
if (!GenerateDataStoreOptions(argc, argv, server_paths)) if (!GenerateDataStoreOptions(argc, argv, server_paths))
{ {
return 0; return EXIT_SUCCESS;
} }
if (server_paths.find("hsgrdata") == server_paths.end()) if (server_paths.find("hsgrdata") == server_paths.end())
@ -585,6 +586,13 @@ int main(const int argc, const char *argv[]) try
shared_layout_ptr->PrintInformation(); shared_layout_ptr->PrintInformation();
} }
catch (const std::bad_alloc &e)
{
SimpleLogger().Write(logWARNING) << "[exception] " << e.what();
SimpleLogger().Write(logWARNING) << "Please provide more memory or disable locking the virtual "
"address space (note: this makes OSRM swap, i.e. slow)";
return EXIT_FAILURE;
}
catch (const std::exception &e) catch (const std::exception &e)
{ {
SimpleLogger().Write(logWARNING) << "caught exception: " << e.what(); SimpleLogger().Write(logWARNING) << "caught exception: " << e.what();

View File

@ -33,6 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstdlib> #include <cstdlib>
#include <exception> #include <exception>
#include <new>
int main(int argc, char *argv[]) try int main(int argc, char *argv[]) try
{ {
@ -74,6 +75,13 @@ int main(int argc, char *argv[]) try
} }
return extractor(extractor_config).run(); return extractor(extractor_config).run();
} }
catch (const std::bad_alloc &e)
{
SimpleLogger().Write(logWARNING) << "[exception] " << e.what();
SimpleLogger().Write(logWARNING)
<< "Please provide more memory or consider using a larger swapfile";
return EXIT_FAILURE;
}
catch (const std::exception &e) catch (const std::exception &e)
{ {
SimpleLogger().Write(logWARNING) << "[exception] " << e.what(); SimpleLogger().Write(logWARNING) << "[exception] " << e.what();

View File

@ -36,6 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstdlib> #include <cstdlib>
#include <exception> #include <exception>
#include <ostream> #include <ostream>
#include <new>
int main(int argc, char *argv[]) try int main(int argc, char *argv[]) try
{ {
@ -46,12 +47,12 @@ int main(int argc, char *argv[]) try
if (return_code::fail == result) if (return_code::fail == result)
{ {
return 1; return EXIT_FAILURE;
} }
if (return_code::exit == result) if (return_code::exit == result)
{ {
return 0; return EXIT_SUCCESS;
} }
ContractorOptions::GenerateOutputFilesNames(contractor_config); ContractorOptions::GenerateOutputFilesNames(contractor_config);
@ -59,7 +60,7 @@ int main(int argc, char *argv[]) try
if (1 > contractor_config.requested_num_threads) if (1 > contractor_config.requested_num_threads)
{ {
SimpleLogger().Write(logWARNING) << "Number of threads must be 1 or larger"; SimpleLogger().Write(logWARNING) << "Number of threads must be 1 or larger";
return 1; return EXIT_FAILURE;
} }
const unsigned recommended_num_threads = tbb::task_scheduler_init::default_num_threads(); const unsigned recommended_num_threads = tbb::task_scheduler_init::default_num_threads();
@ -75,14 +76,14 @@ int main(int argc, char *argv[]) try
{ {
SimpleLogger().Write(logWARNING) SimpleLogger().Write(logWARNING)
<< "Input file " << contractor_config.osrm_input_path.string() << " not found!"; << "Input file " << contractor_config.osrm_input_path.string() << " not found!";
return 1; return EXIT_FAILURE;
} }
if (!boost::filesystem::is_regular_file(contractor_config.profile_path)) if (!boost::filesystem::is_regular_file(contractor_config.profile_path))
{ {
SimpleLogger().Write(logWARNING) << "Profile " << contractor_config.profile_path.string() SimpleLogger().Write(logWARNING) << "Profile " << contractor_config.profile_path.string()
<< " not found!"; << " not found!";
return 1; return EXIT_FAILURE;
} }
SimpleLogger().Write() << "Input file: " SimpleLogger().Write() << "Input file: "
@ -94,6 +95,13 @@ int main(int argc, char *argv[]) try
return Prepare(contractor_config).Run(); return Prepare(contractor_config).Run();
} }
catch (const std::bad_alloc &e)
{
SimpleLogger().Write(logWARNING) << "[exception] " << e.what();
SimpleLogger().Write(logWARNING)
<< "Please provide more memory or consider using a larger swapfile";
return EXIT_FAILURE;
}
catch (const std::exception &e) catch (const std::exception &e)
{ {
SimpleLogger().Write(logWARNING) << "[exception] " << e.what(); SimpleLogger().Write(logWARNING) << "[exception] " << e.what();

View File

@ -43,6 +43,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <future> #include <future>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <new>
#ifdef _WIN32 #ifdef _WIN32
boost::function0<void> console_ctrl_function; boost::function0<void> console_ctrl_function;
@ -81,11 +82,11 @@ int main(int argc, const char *argv[]) try
lib_config.max_locations_map_matching); lib_config.max_locations_map_matching);
if (init_result == INIT_OK_DO_NOT_START_ENGINE) if (init_result == INIT_OK_DO_NOT_START_ENGINE)
{ {
return 0; return EXIT_SUCCESS;
} }
if (init_result == INIT_FAILED) if (init_result == INIT_FAILED)
{ {
return 1; return EXIT_FAILURE;
} }
#ifdef __linux__ #ifdef __linux__
@ -117,6 +118,7 @@ int main(int argc, const char *argv[]) try
SimpleLogger().Write(logDEBUG) << "Threads:\t" << requested_thread_num; SimpleLogger().Write(logDEBUG) << "Threads:\t" << requested_thread_num;
SimpleLogger().Write(logDEBUG) << "IP address:\t" << ip_address; SimpleLogger().Write(logDEBUG) << "IP address:\t" << ip_address;
SimpleLogger().Write(logDEBUG) << "IP port:\t" << ip_port; SimpleLogger().Write(logDEBUG) << "IP port:\t" << ip_port;
#ifndef _WIN32 #ifndef _WIN32
int sig = 0; int sig = 0;
sigset_t new_mask; sigset_t new_mask;
@ -182,8 +184,15 @@ int main(int argc, const char *argv[]) try
routing_server.reset(); routing_server.reset();
SimpleLogger().Write() << "shutdown completed"; SimpleLogger().Write() << "shutdown completed";
} }
catch (const std::bad_alloc &e)
{
SimpleLogger().Write(logWARNING) << "[exception] " << e.what();
SimpleLogger().Write(logWARNING)
<< "Please provide more memory or consider using a larger swapfile";
return EXIT_FAILURE;
}
catch (const std::exception &e) catch (const std::exception &e)
{ {
SimpleLogger().Write(logWARNING) << "exception: " << e.what(); SimpleLogger().Write(logWARNING) << "[exception] " << e.what();
return 1; return EXIT_FAILURE;
} }