From d50a590cfb08f9f45bab9fd355eed8dbdcf765ea Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Tue, 2 Apr 2019 15:38:40 +0200 Subject: [PATCH 1/1] Use std::vector for lists of model descriptions. --- src/simgrid/sg_config.cpp | 64 +++++++++++++---------------- src/surf/surf_interface.cpp | 82 ++++++++++++++++++------------------- src/surf/surf_interface.hpp | 19 ++++----- 3 files changed, 77 insertions(+), 88 deletions(-) diff --git a/src/simgrid/sg_config.cpp b/src/simgrid/sg_config.cpp index d479532be7..29ed3104fb 100644 --- a/src/simgrid/sg_config.cpp +++ b/src/simgrid/sg_config.cpp @@ -93,10 +93,8 @@ static void sg_config_cmd_line(int *argc, char **argv) printf("\n"); model_help("network", surf_network_model_description); printf("\nLong description of all optimization levels accepted by the models of this simulator:\n"); - for (int k = 0; surf_optimization_mode_description[k].name; k++) - printf(" %s: %s\n", - surf_optimization_mode_description[k].name, - surf_optimization_mode_description[k].description); + for (auto const& item : surf_optimization_mode_description) + printf(" %s: %s\n", item.name, item.description); printf("Both network and CPU models have 'Lazy' as default optimization level\n\n"); shall_exit = true; } else if (parse_args && not strcmp(argv[i], "--help-tracing")) { @@ -123,12 +121,12 @@ static void _sg_cfg_cb__plugin(const std::string& value) return; if (value == "help") { - model_help("plugin", surf_plugin_description); + model_help("plugin", *surf_plugin_description); exit(0); } - int plugin_id = find_model_description(surf_plugin_description, value); - surf_plugin_description[plugin_id].model_init_preparse(); + int plugin_id = find_model_description(*surf_plugin_description, value); + (*surf_plugin_description)[plugin_id].model_init_preparse(); } /* callback of the host/model variable */ @@ -215,28 +213,24 @@ static void _sg_cfg_cb_contexts_parallel_mode(const std::string& mode_name) } /* build description line with possible values */ -static void describe_model(char *result,int resultsize, - const s_surf_model_description_t model_description[], - const char *name, - const char *description) +static void declare_model_flag(const std::string& name, const std::string& value, + const std::function& callback, + const std::vector& model_description, const std::string& type, + const std::string& descr) { - result[0] = '\0'; - char *p = result; - p += snprintf(result,resultsize-1, "%s. Possible values: %s", description, - model_description[0].name ? model_description[0].name : "n/a"); - for (int i = 1; model_description[i].name; i++) - p += snprintf(p,resultsize-(p-result)-1, ", %s", model_description[i].name); - p += snprintf(p,resultsize-(p-result)-1, ".\n (use 'help' as a value to see the long description of each %s)", name); - - xbt_assert(p(name, description, value, callback); } /* create the config set, register what should be and parse the command line*/ void sg_config_init(int *argc, char **argv) { - const int descsize = 1024; - char description[descsize]; - /* Create the configuration support */ if (_sg_cfg_init_status != 0) { /* Only create stuff if not already inited */ XBT_WARN("Call to sg_config_init() after initialization ignored"); @@ -244,24 +238,22 @@ void sg_config_init(int *argc, char **argv) } /* Plugins configuration */ - describe_model(description, descsize, surf_plugin_description, "plugin", "The plugins"); - simgrid::config::declare_flag("plugin", description, "", &_sg_cfg_cb__plugin); + declare_model_flag("plugin", "", &_sg_cfg_cb__plugin, *surf_plugin_description, "plugin", "The plugins"); - describe_model(description, descsize, surf_cpu_model_description, "model", "The model to use for the CPU"); - simgrid::config::declare_flag("cpu/model", description, "Cas01", &_sg_cfg_cb__cpu_model); + declare_model_flag("cpu/model", "Cas01", &_sg_cfg_cb__cpu_model, surf_cpu_model_description, "model", + "The model to use for the CPU"); - describe_model(description, descsize, surf_storage_model_description, "model", "The model to use for the storage"); - simgrid::config::declare_flag("storage/model", description, "default", &_sg_cfg_cb__storage_mode); + declare_model_flag("storage/model", "default", &_sg_cfg_cb__storage_mode, surf_storage_model_description, "model", + "The model to use for the storage"); - describe_model(description, descsize, surf_network_model_description, "model", "The model to use for the network"); - simgrid::config::declare_flag("network/model", description, "LV08", &_sg_cfg_cb__network_model); + declare_model_flag("network/model", "LV08", &_sg_cfg_cb__network_model, surf_network_model_description, "model", + "The model to use for the network"); - describe_model(description, descsize, surf_optimization_mode_description, "optimization mode", - "The optimization modes to use for the network"); - simgrid::config::declare_flag("network/optim", description, "Lazy", &_sg_cfg_cb__optimization_mode); + declare_model_flag("network/optim", "Lazy", &_sg_cfg_cb__optimization_mode, surf_optimization_mode_description, + "optimization mode", "The optimization modes to use for the network"); - describe_model(description, descsize, surf_host_model_description, "model", "The model to use for the host"); - simgrid::config::declare_flag("host/model", description, "default", &_sg_cfg_cb__host_model); + declare_model_flag("host/model", "default", &_sg_cfg_cb__host_model, surf_host_model_description, "model", + "The model to use for the host"); simgrid::config::bind_flag(sg_surf_precision, "surf/precision", "Numerical precision used when updating simulation times (in seconds)"); diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index d2f6966f03..21b718bcd8 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -36,29 +36,23 @@ std::vector surf_path; std::set watched_hosts; extern std::map storage_types; -s_surf_model_description_t* surf_plugin_description = nullptr; +std::vector* surf_plugin_description = nullptr; + static void XBT_ATTRIB_DESTRUCTOR(800) simgrid_free_plugin_description() { - xbt_free(surf_plugin_description); + delete surf_plugin_description; + surf_plugin_description = nullptr; } XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun) { - static int plugin_amount = 0; - - /* no need to check for plugin name conflict: the compiler already ensures that the generated - * simgrid_##id##_plugin_register() is unique */ - - plugin_amount++; - surf_plugin_description = static_cast( - xbt_realloc(surf_plugin_description, sizeof(s_surf_model_description_t) * (plugin_amount + 2))); - - surf_plugin_description[plugin_amount - 1] = {name, description, init_fun}; - surf_plugin_description[plugin_amount] = {nullptr, nullptr, nullptr}; // this array must be null terminated + if (not surf_plugin_description) + surf_plugin_description = new std::vector; + surf_plugin_description->emplace_back(surf_model_description_t{name, description, init_fun}); } /* Don't forget to update the option description in smx_config when you change this */ -s_surf_model_description_t surf_network_model_description[] = { +const std::vector surf_network_model_description = { {"LV08", "Realistic network analytic model (slow-start modeled by multiplying latency by 13.01, bandwidth by .97; " "bottleneck sharing uses a payload of S=20537 for evaluating RTT). ", &surf_network_model_init_LegrandVelho}, @@ -83,7 +77,6 @@ s_surf_model_description_t surf_network_model_description[] = { {"Vegas", "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).", &surf_network_model_init_Vegas}, - {nullptr, nullptr, nullptr} /* this array must be nullptr terminated */ }; #if ! HAVE_SMPI @@ -100,28 +93,29 @@ void surf_network_model_init_NS3() { } #endif -s_surf_model_description_t surf_cpu_model_description[] = { - {"Cas01", "Simplistic CPU model (time=size/power).", &surf_cpu_model_init_Cas01}, - {nullptr, nullptr, nullptr} /* this array must be nullptr terminated */ +const std::vector surf_cpu_model_description = { + {"Cas01", "Simplistic CPU model (time=size/power).", &surf_cpu_model_init_Cas01}, }; -s_surf_model_description_t surf_host_model_description[] = { - {"default", "Default host model. Currently, CPU:Cas01 and network:LV08 (with cross traffic enabled)", &surf_host_model_init_current_default}, - {"compound", "Host model that is automatically chosen if you change the network and CPU models", &surf_host_model_init_compound}, - {"ptask_L07", "Host model somehow similar to Cas01+CM02 but allowing parallel tasks", &surf_host_model_init_ptask_L07}, - {nullptr, nullptr, nullptr} /* this array must be nullptr terminated */ +const std::vector surf_host_model_description = { + {"default", "Default host model. Currently, CPU:Cas01 and network:LV08 (with cross traffic enabled)", + &surf_host_model_init_current_default}, + {"compound", "Host model that is automatically chosen if you change the network and CPU models", + &surf_host_model_init_compound}, + {"ptask_L07", "Host model somehow similar to Cas01+CM02 but allowing parallel tasks", + &surf_host_model_init_ptask_L07}, }; -s_surf_model_description_t surf_optimization_mode_description[] = { - {"Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining).", nullptr}, - {"TI", "Trace integration. Highly optimized mode when using availability traces (only available for the Cas01 CPU model for now).", nullptr}, - {"Full", "Full update of remaining and variables. Slow but may be useful when debugging.", nullptr}, - {nullptr, nullptr, nullptr} /* this array must be nullptr terminated */ +const std::vector surf_optimization_mode_description = { + {"Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining).", nullptr}, + {"TI", "Trace integration. Highly optimized mode when using availability traces (only available for the Cas01 CPU " + "model for now).", + nullptr}, + {"Full", "Full update of remaining and variables. Slow but may be useful when debugging.", nullptr}, }; -s_surf_model_description_t surf_storage_model_description[] = { - {"default", "Simplistic storage model.", &surf_storage_model_init_default}, - {nullptr, nullptr, nullptr} /* this array must be nullptr terminated */ +const std::vector surf_storage_model_description = { + {"default", "Simplistic storage model.", &surf_storage_model_init_default}, }; double NOW = 0; @@ -190,25 +184,29 @@ FILE* surf_fopen(const std::string& name, const char* mode) } /** Displays the long description of all registered models, and quit */ -void model_help(const char *category, s_surf_model_description_t * table) +void model_help(const char* category, const std::vector& table) { printf("Long description of the %s models accepted by this simulator:\n", category); - for (int i = 0; table[i].name; i++) - printf(" %s: %s\n", table[i].name, table[i].description); + for (auto const& item : table) + printf(" %s: %s\n", item.name, item.description); } -int find_model_description(s_surf_model_description_t* table, const std::string& name) +int find_model_description(const std::vector& table, const std::string& name) { - for (int i = 0; table[i].name; i++) - if (name == table[i].name) - return i; + auto pos = std::find_if(table.begin(), table.end(), + [&name](const surf_model_description_t& item) { return item.name == name; }); + if (pos != table.end()) + return std::distance(table.begin(), pos); - if (not table[0].name) + if (table.empty()) xbt_die("No model is valid! This is a bug."); - std::string name_list = std::string(table[0].name); - for (int i = 1; table[i].name; i++) - name_list = name_list + ", " + table[i].name; + std::string sep; + std::string name_list; + for (auto const& item : table) { + name_list += sep + item.name; + sep = ", "; + } xbt_die("Model '%s' is invalid! Valid models are: %s.", name.c_str(), name_list.c_str()); return -1; diff --git a/src/surf/surf_interface.hpp b/src/surf/surf_interface.hpp index 38fad6258e..82ca23eb82 100644 --- a/src/surf/surf_interface.hpp +++ b/src/surf/surf_interface.hpp @@ -206,15 +206,14 @@ XBT_PUBLIC void surf_storage_model_init_default(); * Model Descriptions * -------------------- */ /** @brief Resource model description */ -struct surf_model_description { +struct surf_model_description_t { const char* name; const char* description; void_f_void_t model_init_preparse; }; -typedef struct surf_model_description s_surf_model_description_t; -XBT_PUBLIC int find_model_description(s_surf_model_description_t* table, const std::string& name); -XBT_PUBLIC void model_help(const char* category, s_surf_model_description_t* table); +XBT_PUBLIC int find_model_description(const std::vector& table, const std::string& name); +XBT_PUBLIC void model_help(const char* category, const std::vector& table); #define SIMGRID_REGISTER_PLUGIN(id, desc, init) \ static void XBT_ATTRIB_CONSTRUCTOR(800) simgrid_##id##_plugin_register() \ @@ -225,18 +224,18 @@ XBT_PUBLIC void model_help(const char* category, s_surf_model_description_t* tab XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun); /** @brief The list of all available plugins */ -XBT_PUBLIC_DATA s_surf_model_description_t* surf_plugin_description; +XBT_PUBLIC_DATA std::vector* surf_plugin_description; /** @brief The list of all available optimization modes (both for cpu and networks). * These optimization modes can be set using --cfg=cpu/optim:... and --cfg=network/optim:... */ -XBT_PUBLIC_DATA s_surf_model_description_t surf_optimization_mode_description[]; +XBT_PUBLIC_DATA const std::vector surf_optimization_mode_description; /** @brief The list of all cpu models (pick one with --cfg=cpu/model) */ -XBT_PUBLIC_DATA s_surf_model_description_t surf_cpu_model_description[]; +XBT_PUBLIC_DATA const std::vector surf_cpu_model_description; /** @brief The list of all network models (pick one with --cfg=network/model) */ -XBT_PUBLIC_DATA s_surf_model_description_t surf_network_model_description[]; +XBT_PUBLIC_DATA const std::vector surf_network_model_description; /** @brief The list of all storage models (pick one with --cfg=storage/model) */ -XBT_PUBLIC_DATA s_surf_model_description_t surf_storage_model_description[]; +XBT_PUBLIC_DATA const std::vector surf_storage_model_description; /** @brief The list of all host models (pick one with --cfg=host/model:) */ -XBT_PUBLIC_DATA s_surf_model_description_t surf_host_model_description[]; +XBT_PUBLIC_DATA const std::vector surf_host_model_description; /********** * Action * -- 2.20.1