Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Objectif the disk model
[simgrid.git] / src / simgrid / module.cpp
1 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <xbt/asserts.h>
7 #include <xbt/log.h>
8
9 #include "simgrid/sg_config.hpp"
10 #include "src/simgrid/module.hpp"
11 #include "src/surf/surf_interface.hpp"
12
13 #include <sstream>
14
15 XBT_LOG_NEW_CATEGORY(plugin, "Common category for the logging of all plugins");
16 XBT_LOG_EXTERNAL_CATEGORY(xbt_help);
17
18 using namespace simgrid;
19
20 void ModuleGroup::create_flag(const std::string& opt_name, const std::string& descr, const std::string& default_value,
21                               bool init_now)
22 {
23   std::string description = descr + ". Possible values (other compilation flags may activate more " + get_kind() +
24                             "s): " + existing_values() +
25                             ".\n       (use 'help' as a value to see the long description of each one)";
26
27   simgrid::config::declare_flag<std::string>(
28       opt_name, description, default_value, [this, default_value, init_now](const std::string& value) {
29         xbt_assert(_sg_cfg_init_status < 2, "Cannot load a %s after the initialization", kind_.c_str());
30
31         if (value == default_value)
32           return;
33
34         if (value == "help") {
35           help();
36           exit(0);
37         }
38
39         if (init_now)
40           by_name(value).init();
41         else
42           by_name(value); // Simply ensure that this value exists, it will be picked up later
43       });
44 }
45
46 ModuleGroup& ModuleGroup::add(const char* id, const char* desc, std::function<void()> init)
47 {
48   table_.emplace_back(Module(id, desc, init));
49   return *this;
50 }
51
52 Module const& ModuleGroup::by_name(const std::string& name) const
53 {
54   if (auto pos = std::find_if(table_.begin(), table_.end(), [&name](const Module& item) { return item.name_ == name; });
55       pos != table_.end())
56     return *pos;
57
58   xbt_die("Unable to find %s '%s'. Valid values are: %s.", kind_.c_str(), name.c_str(), existing_values().c_str());
59 }
60 /** Displays the long description of all registered models, and quit */
61 void ModuleGroup::help() const
62 {
63   XBT_HELP("Long description of the %s accepted by this simulator:", kind_.c_str());
64   for (auto const& item : table_)
65     XBT_HELP("  %s: %s", item.name_, item.description_);
66 }
67 std::string ModuleGroup::existing_values() const
68 {
69   std::stringstream ss;
70   std::string sep;
71   for (auto const& item : table_) {
72     ss << sep + item.name_;
73     sep = ", ";
74   }
75   return ss.str();
76 }
77
78 /* -------------------------------------------------------------------------------------------------------------- */
79 simgrid::ModuleGroup surf_optimization_mode_description("optimization mode");
80
81 void simgrid_create_models()
82 {
83   surf_optimization_mode_description
84       .add("Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining).", nullptr)
85       .add("TI",
86            "Trace integration. Highly optimized mode when using availability traces (only available for the Cas01 CPU "
87            "model for now).",
88            nullptr)
89       .add("Full", "Full update of remaining and variables. Slow but may be useful when debugging.", nullptr);
90 }