Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stop loading private headers from now public NetZoneImpl.hpp
[simgrid.git] / src / surf / storage_n11.cpp
1 /* Copyright (c) 2013-2018. 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/surf/xml/platf.hpp"
11 #include "src/kernel/lmm/maxmin.hpp"
12 #include "xbt/utility.hpp"
13 #include <cmath> /*ceil*/
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_storage);
16
17 /*************
18  * CallBacks *
19  *************/
20 extern std::map<std::string, simgrid::surf::StorageType*> storage_types;
21
22 static void check_disk_attachment()
23 {
24   for (auto const& s : simgrid::s4u::Engine::getInstance()->getAllStorages()) {
25     simgrid::kernel::routing::NetPoint* host_elm = sg_netpoint_by_name_or_null(s->getImpl()->getHost().c_str());
26     if (not host_elm)
27       surf_parse_error(std::string("Unable to attach storage ") + s->getCname() + ": host " +
28                        s->getImpl()->getHost().c_str() + " does not exist.");
29     else
30       s->attached_to_ = sg_host_by_name(s->getImpl()->getHost().c_str());
31   }
32 }
33
34 void storage_register_callbacks()
35 {
36   simgrid::s4u::onPlatformCreated.connect(check_disk_attachment);
37   instr_routing_define_callbacks();
38 }
39
40 /*********
41  * Model *
42  *********/
43
44 void surf_storage_model_init_default()
45 {
46   surf_storage_model = new simgrid::surf::StorageN11Model();
47   all_existing_models->push_back(surf_storage_model);
48 }
49
50 namespace simgrid {
51 namespace surf {
52
53 StorageImpl* StorageN11Model::createStorage(std::string id, std::string type_id, std::string content_name,
54                                             std::string attach)
55 {
56   StorageType* storage_type = storage_types.at(type_id);
57
58   double Bread = surf_parse_get_bandwidth(storage_type->model_properties->at("Bread").c_str(),
59                                           "property Bread, storage", type_id.c_str());
60   double Bwrite = surf_parse_get_bandwidth(storage_type->model_properties->at("Bwrite").c_str(),
61                                            "property Bwrite, storage", type_id.c_str());
62
63   StorageImpl* storage =
64       new StorageN11(this, id, maxminSystem_, Bread, Bwrite, type_id, content_name, storage_type->size, attach);
65   storageCreatedCallbacks(storage);
66
67   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(),
68             Bread);
69
70   return storage;
71 }
72
73 double StorageN11Model::nextOccuringEvent(double now)
74 {
75   return StorageModel::nextOccuringEventFull(now);
76 }
77
78 void StorageN11Model::updateActionsState(double /*now*/, double delta)
79 {
80   for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
81     StorageAction& action = static_cast<StorageAction&>(*it);
82     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
83     action.updateRemains(lrint(action.getVariable()->get_value() * delta));
84
85     if (action.getMaxDuration() > NO_MAX_DURATION)
86       action.updateMaxDuration(delta);
87
88     if (((action.getRemainsNoUpdate() <= 0) && (action.getVariable()->get_weight() > 0)) ||
89         ((action.getMaxDuration() > NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
90       action.finish(kernel::resource::Action::State::done);
91     }
92   }
93 }
94
95 /************
96  * Resource *
97  ************/
98
99 StorageN11::StorageN11(StorageModel* model, std::string name, lmm_system_t maxminSystem, double bread, double bwrite,
100                        std::string type_id, std::string content_name, sg_size_t size, std::string attach)
101     : StorageImpl(model, name, maxminSystem, bread, bwrite, type_id, content_name, size, attach)
102 {
103   XBT_DEBUG("Create resource with Bread '%f' Bwrite '%f' and Size '%llu'", bread, bwrite, size);
104   simgrid::s4u::Storage::onCreation(this->piface_);
105 }
106
107 StorageAction* StorageN11::read(sg_size_t size)
108 {
109   return new StorageN11Action(model(), size, isOff(), this, READ);
110 }
111
112 StorageAction* StorageN11::write(sg_size_t size)
113 {
114   return new StorageN11Action(model(), size, isOff(), this, WRITE);
115 }
116
117 /**********
118  * Action *
119  **********/
120
121 StorageN11Action::StorageN11Action(kernel::resource::Model* model, double cost, bool failed, StorageImpl* storage,
122                                    e_surf_action_storage_type_t type)
123     : StorageAction(model, cost, failed, model->getMaxminSystem()->variable_new(this, 1.0, -1.0, 3), storage, type)
124 {
125   XBT_IN("(%s,%g", storage->getCname(), cost);
126
127   // Must be less than the max bandwidth for all actions
128   model->getMaxminSystem()->expand(storage->constraint(), getVariable(), 1.0);
129   switch(type) {
130   case READ:
131     model->getMaxminSystem()->expand(storage->constraintRead_, getVariable(), 1.0);
132     break;
133   case WRITE:
134     model->getMaxminSystem()->expand(storage->constraintWrite_, getVariable(), 1.0);
135     break;
136   default:
137     THROW_UNIMPLEMENTED;
138   }
139   XBT_OUT();
140 }
141
142 int StorageN11Action::unref()
143 {
144   refcount_--;
145   if (not refcount_) {
146     if (stateSetHook_.is_linked())
147       simgrid::xbt::intrusive_erase(*stateSet_, *this);
148     if (getVariable())
149       getModel()->getMaxminSystem()->variable_free(getVariable());
150     xbt_free(getCategory());
151     delete this;
152     return 1;
153   }
154   return 0;
155 }
156
157 void StorageN11Action::cancel()
158 {
159   setState(Action::State::failed);
160 }
161
162 void StorageN11Action::suspend()
163 {
164   XBT_IN("(%p)", this);
165   if (suspended_ != Action::SuspendStates::sleeping) {
166     getModel()->getMaxminSystem()->update_variable_weight(getVariable(), 0.0);
167     suspended_ = Action::SuspendStates::suspended;
168   }
169   XBT_OUT();
170 }
171
172 void StorageN11Action::resume()
173 {
174   THROW_UNIMPLEMENTED;
175 }
176
177 bool StorageN11Action::isSuspended()
178 {
179   return suspended_ == Action::SuspendStates::suspended;
180 }
181
182 void StorageN11Action::setMaxDuration(double /*duration*/)
183 {
184   THROW_UNIMPLEMENTED;
185 }
186
187 void StorageN11Action::setSharingWeight(double /*priority*/)
188 {
189   THROW_UNIMPLEMENTED;
190 }
191
192 }
193 }