Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use const& for std::string parameter (sonar, again).
[simgrid.git] / src / surf / network_constant.cpp
1 /* Copyright (c) 2013-2021. 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 "network_constant.hpp"
7 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include "surf/surf.hpp"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
14
15 /*********
16  * Model *
17  *********/
18 void surf_network_model_init_Constant()
19 {
20   auto net_model = std::make_shared<simgrid::kernel::resource::NetworkConstantModel>("Network_Constant");
21   simgrid::kernel::EngineImpl::get_instance()->add_model(net_model);
22   simgrid::s4u::Engine::get_instance()->get_netzone_root()->get_impl()->set_network_model(net_model);
23 }
24
25 namespace simgrid {
26 namespace kernel {
27 namespace resource {
28
29 LinkImpl* NetworkConstantModel::create_link(const std::string& name, const std::vector<double>& /*bandwidth*/,
30                                             s4u::Link::SharingPolicy)
31 {
32   xbt_die("Refusing to create the link %s: there is no link in the Constant network model. "
33           "Please remove any link from your platform (and switch to routing='None')",
34           name.c_str());
35   return nullptr;
36 }
37
38 double NetworkConstantModel::next_occurring_event(double /*now*/)
39 {
40   double min = -1.0;
41   for (kernel::resource::Action const& action : *get_started_action_set()) {
42     const auto& net_action = static_cast<const NetworkConstantAction&>(action);
43     if (net_action.latency_ > 0 && (min < 0 || net_action.latency_ < min))
44       min = net_action.latency_;
45   }
46   return min;
47 }
48
49 void NetworkConstantModel::update_actions_state(double /*now*/, double delta)
50 {
51   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
52     auto& action = static_cast<NetworkConstantAction&>(*it);
53     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
54     if (action.latency_ > 0) {
55       if (action.latency_ > delta) {
56         double_update(&action.latency_, delta, sg_surf_precision);
57       } else {
58         action.latency_ = 0.0;
59       }
60     }
61     action.update_remains(action.get_cost() * delta / action.initial_latency_);
62     action.update_max_duration(delta);
63
64     if ((action.get_remains_no_update() <= 0) ||
65         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
66       action.finish(Action::State::FINISHED);
67     }
68   }
69 }
70
71 Action* NetworkConstantModel::communicate(s4u::Host* src, s4u::Host* dst, double size, double)
72 {
73   auto* action = new NetworkConstantAction(this, *src, *dst, size, sg_latency_factor);
74
75   s4u::Link::on_communicate(*action);
76   return action;
77 }
78
79 /**********
80  * Action *
81  **********/
82 NetworkConstantAction::NetworkConstantAction(NetworkConstantModel* model_, s4u::Host& src, s4u::Host& dst, double size,
83                                              double latency)
84     : NetworkAction(model_, src, dst, size, false), initial_latency_(latency)
85 {
86   latency_ = latency;
87   if (latency_ <= 0.0)
88     NetworkConstantAction::set_state(Action::State::FINISHED);
89 }
90
91 NetworkConstantAction::~NetworkConstantAction() = default;
92
93 void NetworkConstantAction::update_remains_lazy(double /*now*/)
94 {
95   THROW_IMPOSSIBLE;
96 }
97
98 } // namespace resource
99 } // namespace kernel
100 } // namespace simgrid