Logo AND Algorithmique Numérique Distribuée

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