Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / surf / storage_n11.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 "storage_n11.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/kernel/lmm/maxmin.hpp"
11 #include "src/surf/xml/platf.hpp"
12 #include "surf/surf.hpp"
13 #include "xbt/parse_units.hpp"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_storage);
16
17 /*************
18  * CallBacks *
19  *************/
20 extern std::map<std::string, simgrid::kernel::resource::StorageType*, std::less<>> storage_types;
21
22 void check_disk_attachment()
23 {
24   for (auto const& s : simgrid::s4u::Engine::get_instance()->get_all_storages()) {
25     const simgrid::kernel::routing::NetPoint* host_elm =
26         simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(s->get_impl()->get_host());
27     if (not host_elm)
28       surf_parse_error(std::string("Unable to attach storage ") + s->get_cname() + ": host " +
29                        s->get_impl()->get_host() + " does not exist.");
30     else
31       s->set_host(simgrid::s4u::Host::by_name(s->get_impl()->get_host()));
32   }
33 }
34
35 /*********
36  * Model *
37  *********/
38
39 void surf_storage_model_init_default()
40 {
41   surf_storage_model = new simgrid::kernel::resource::StorageN11Model();
42 }
43
44 namespace simgrid {
45 namespace kernel {
46 namespace resource {
47
48 StorageN11Model::StorageN11Model()
49 {
50   all_existing_models.push_back(this);
51 }
52
53 StorageImpl* StorageN11Model::createStorage(std::string& filename, int lineno, const std::string& id,
54                                             const std::string& type_id, const std::string& content_name,
55                                             const std::string& attach)
56 {
57   const StorageType* storage_type = storage_types.at(type_id);
58
59   double Bread  = xbt_parse_get_bandwidth(filename, lineno, storage_type->model_properties->at("Bread").c_str(),
60                                          "property Bread, storage", type_id);
61   double Bwrite = xbt_parse_get_bandwidth(filename, lineno, storage_type->model_properties->at("Bwrite").c_str(),
62                                           "property Bwrite, storage", type_id);
63
64   XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s'\n\t\tBread '%f'\n", id.c_str(), type_id.c_str(),
65             Bread);
66
67   return new StorageN11(this, id, get_maxmin_system(), Bread, Bwrite, type_id, content_name, storage_type->size,
68                         attach);
69 }
70
71 double StorageN11Model::next_occurring_event(double now)
72 {
73   return StorageModel::next_occurring_event_full(now);
74 }
75
76 void StorageN11Model::update_actions_state(double /*now*/, double delta)
77 {
78   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
79     auto& action = *it;
80     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
81     action.update_remains(rint(action.get_variable()->get_value() * delta));
82     action.update_max_duration(delta);
83
84     if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
85         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
86       action.finish(Action::State::FINISHED);
87     }
88   }
89 }
90
91 /************
92  * Resource *
93  ************/
94
95 StorageN11::StorageN11(StorageModel* model, const std::string& name, lmm::System* maxminSystem, double bread,
96                        double bwrite, const std::string& type_id, const std::string& content_name, sg_size_t size,
97                        const std::string& attach)
98     : StorageImpl(model, name, maxminSystem, bread, bwrite, type_id, content_name, size, attach)
99 {
100   XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
101   s4u::Storage::on_creation(*get_iface());
102 }
103
104 StorageAction* StorageN11::io_start(sg_size_t size, s4u::Io::OpType type)
105 {
106   return new StorageN11Action(get_model(), static_cast<double>(size), not is_on(), this, type);
107 }
108
109 StorageAction* StorageN11::read(sg_size_t size)
110 {
111   return new StorageN11Action(get_model(), static_cast<double>(size), not is_on(), this, s4u::Io::OpType::READ);
112 }
113
114 StorageAction* StorageN11::write(sg_size_t size)
115 {
116   return new StorageN11Action(get_model(), static_cast<double>(size), not is_on(), this, s4u::Io::OpType::WRITE);
117 }
118
119 /**********
120  * Action *
121  **********/
122
123 StorageN11Action::StorageN11Action(Model* model, double cost, bool failed, StorageImpl* storage, s4u::Io::OpType type)
124     : StorageAction(model, cost, failed, model->get_maxmin_system()->variable_new(this, 1.0, -1.0, 3), storage, type)
125 {
126   XBT_IN("(%s,%g", storage->get_cname(), cost);
127
128   // Must be less than the max bandwidth for all actions
129   model->get_maxmin_system()->expand(storage->get_constraint(), get_variable(), 1.0);
130   switch(type) {
131     case s4u::Io::OpType::READ:
132       model->get_maxmin_system()->expand(storage->get_read_constraint(), get_variable(), 1.0);
133       break;
134     case s4u::Io::OpType::WRITE:
135       model->get_maxmin_system()->expand(storage->get_write_constraint(), get_variable(), 1.0);
136       break;
137     default:
138       THROW_UNIMPLEMENTED;
139   }
140   XBT_OUT();
141 }
142
143 void StorageN11Action::cancel()
144 {
145   set_state(Action::State::FAILED);
146 }
147
148 void StorageN11Action::suspend()
149 {
150   XBT_IN("(%p)", this);
151   if (is_running()) {
152     get_model()->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
153     set_suspend_state(Action::SuspendStates::SUSPENDED);
154   }
155   XBT_OUT();
156 }
157
158 void StorageN11Action::resume()
159 {
160   THROW_UNIMPLEMENTED;
161 }
162
163 void StorageN11Action::set_max_duration(double /*duration*/)
164 {
165   THROW_UNIMPLEMENTED;
166 }
167
168 void StorageN11Action::set_sharing_penalty(double)
169 {
170   THROW_UNIMPLEMENTED;
171 }
172 void StorageN11Action::update_remains_lazy(double /*now*/)
173 {
174   THROW_IMPOSSIBLE;
175 }
176 } // namespace resource
177 } // namespace kernel
178 } // namespace simgrid