Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reorganize *LinkImpl stuff
[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 <simgrid/kernel/routing/NetZoneImpl.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/surf/network_constant.hpp"
11 #include "src/surf/surf_interface.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   auto* engine   = simgrid::kernel::EngineImpl::get_instance();
22   engine->add_model(net_model);
23   engine->get_netzone_root()->set_network_model(net_model);
24 }
25
26 namespace simgrid {
27 namespace kernel {
28 namespace resource {
29
30 StandardLinkImpl* NetworkConstantModel::create_link(const std::string& name, const std::vector<double>& /*bandwidth*/)
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 StandardLinkImpl* NetworkConstantModel::create_wifi_link(const std::string& name, const std::vector<double>& bandwidths)
39 {
40   return create_link(name, bandwidths);
41 }
42
43 double NetworkConstantModel::next_occurring_event(double /*now*/)
44 {
45   double min = -1.0;
46   for (Action const& action : *get_started_action_set()) {
47     const auto& net_action = static_cast<const NetworkConstantAction&>(action);
48     if (net_action.latency_ > 0 && (min < 0 || net_action.latency_ < min))
49       min = net_action.latency_;
50   }
51   return min;
52 }
53
54 void NetworkConstantModel::update_actions_state(double /*now*/, double delta)
55 {
56   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
57     auto& action = static_cast<NetworkConstantAction&>(*it);
58     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
59     if (action.latency_ > 0) {
60       if (action.latency_ > delta) {
61         double_update(&action.latency_, delta, sg_surf_precision);
62       } else {
63         action.latency_ = 0.0;
64       }
65     }
66     action.update_remains(action.get_cost() * delta / sg_latency_factor);
67     action.update_max_duration(delta);
68
69     if ((action.get_remains_no_update() <= 0) ||
70         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
71       action.finish(Action::State::FINISHED);
72     }
73   }
74 }
75
76 Action* NetworkConstantModel::communicate(s4u::Host* src, s4u::Host* dst, double size, double /*rate*/)
77 {
78   return (new NetworkConstantAction(this, *src, *dst, size));
79 }
80
81 /**********
82  * Action *
83  **********/
84 NetworkConstantAction::NetworkConstantAction(NetworkConstantModel* model_, s4u::Host& src, s4u::Host& dst, double size)
85     : NetworkAction(model_, src, dst, size, false)
86 {
87   latency_ = sg_latency_factor;
88   if (latency_ <= 0.0)
89     set_state(Action::State::FINISHED);
90 }
91
92 void NetworkConstantAction::update_remains_lazy(double /*now*/)
93 {
94   THROW_IMPOSSIBLE;
95 }
96
97 } // namespace resource
98 } // namespace kernel
99 } // namespace simgrid