Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / surf / storage_n11.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "storage_n11.hpp"
8 #include "simgrid/s4u/engine.hpp"
9 #include "src/kernel/routing/NetPoint.hpp"
10 #include "surf_private.h"
11 #include <math.h> /*ceil*/
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_storage);
14
15 /*************
16  * CallBacks *
17  *************/
18
19 static inline void routing_storage_type_free(void *r)
20 {
21   storage_type_t stype = (storage_type_t) r;
22   free(stype->model);
23   free(stype->type_id);
24   free(stype->content);
25   free(stype->content_type);
26   xbt_dict_free(&(stype->properties));
27   xbt_dict_free(&(stype->model_properties));
28   free(stype);
29 }
30
31 static inline void routing_storage_host_free(void *r)
32 {
33   xbt_dynar_t dyn = (xbt_dynar_t) r;
34   xbt_dynar_free(&dyn);
35 }
36
37 static void check_disk_attachment()
38 {
39   xbt_lib_cursor_t cursor;
40   char* key;
41   void** data;
42   xbt_lib_foreach(storage_lib, cursor, key, data) {
43     if (xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != nullptr) {
44       simgrid::surf::Storage* storage =
45           static_cast<simgrid::surf::Storage*>(xbt_lib_get_or_null(storage_lib, key, SURF_STORAGE_LEVEL));
46       simgrid::kernel::routing::NetPoint* host_elm = sg_netpoint_by_name_or_null(storage->attach_);
47       if (!host_elm)
48         surf_parse_error("Unable to attach storage %s: host %s does not exist.", storage->cname(), storage->attach_);
49     }
50   }
51 }
52
53 void storage_register_callbacks()
54 {
55   simgrid::s4u::onPlatformCreated.connect(check_disk_attachment);
56   instr_routing_define_callbacks();
57
58   ROUTING_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, xbt_free_f);
59   ROUTING_STORAGE_HOST_LEVEL = xbt_lib_add_level(storage_lib, routing_storage_host_free);
60   ROUTING_STORAGE_TYPE_LEVEL = xbt_lib_add_level(storage_type_lib, routing_storage_type_free);
61   SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, [](void *self) {
62     delete static_cast<simgrid::surf::Storage*>(self);
63   });
64 }
65
66 /*********
67  * Model *
68  *********/
69
70 void surf_storage_model_init_default()
71 {
72   surf_storage_model = new simgrid::surf::StorageN11Model();
73   all_existing_models->push_back(surf_storage_model);
74 }
75
76 namespace simgrid {
77 namespace surf {
78
79 Storage* StorageN11Model::createStorage(const char* id, const char* type_id, const char* content_name,
80                                         const char* content_type, const char* attach)
81 {
82
83   xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
84       "Storage '%s' declared several times in the platform file", id);
85
86   storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
87
88   double Bread  = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->model_properties, "Bread"),
89       "property Bread, storage",type_id);
90   double Bwrite = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->model_properties, "Bwrite"),
91       "property Bwrite, storage",type_id);
92   double Bconnection   = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->model_properties, "Bconnection"),
93       "property Bconnection, storage",type_id);
94
95   Storage* storage = new StorageN11(this, id, maxminSystem_, Bread, Bwrite, Bconnection, type_id, (char*)content_name,
96                                     content_type, storage_type->size, (char*)attach);
97   storageCreatedCallbacks(storage);
98   xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
99
100   XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s'\n\t\tBread '%f'\n", id, type_id, Bread);
101
102   p_storageList.push_back(storage);
103
104   return storage;
105 }
106
107 double StorageN11Model::nextOccuringEvent(double now)
108 {
109   double min_completion = StorageModel::nextOccuringEventFull(now);
110
111   for(auto storage: p_storageList) {
112     double rate = 0;
113     // Foreach write action on that disk
114     for (auto write_action: storage->writeActions_) {
115       rate += lmm_variable_getvalue(write_action->getVariable());
116     }
117     if(rate > 0)
118       min_completion = MIN(min_completion, (storage->size_-storage->usedSize_)/rate);
119   }
120
121   return min_completion;
122 }
123
124 void StorageN11Model::updateActionsState(double /*now*/, double delta)
125 {
126
127   ActionList *actionSet = getRunningActionSet();
128   for(ActionList::iterator it(actionSet->begin()), itNext=it, itend(actionSet->end())
129       ; it != itend ; it=itNext) {
130     ++itNext;
131
132     StorageAction *action = static_cast<StorageAction*>(&*it);
133
134     if (action->type_ == WRITE) {
135       // Update the disk usage
136       // Update the file size
137       // For each action of type write
138       double current_progress = delta * lmm_variable_getvalue(action->getVariable());
139       long int incr = current_progress;
140
141       XBT_DEBUG("%s:\n\t progress =  %.2f, current_progress = %.2f, incr = %ld, lrint(1) = %ld, lrint(2) = %ld",
142                 action->file_->name, action->progress_, current_progress, incr,
143                 lrint(action->progress_ + current_progress), lrint(action->progress_) + incr);
144
145       /* take care of rounding error accumulation */
146       if (lrint(action->progress_ + current_progress) > lrint(action->progress_) + incr)
147         incr++;
148
149       action->progress_ += current_progress;
150
151       action->storage_->usedSize_ += incr;     // disk usage
152       action->file_->current_position += incr; // current_position
153       //  which becomes the new file size
154       action->file_->size = action->file_->current_position;
155
156       sg_size_t *psize = xbt_new(sg_size_t,1);
157       *psize                  = action->file_->size;
158       xbt_dict_t content_dict = action->storage_->content_;
159       xbt_dict_set(content_dict, action->file_->name, psize, nullptr);
160     }
161
162     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
163
164     if (action->getMaxDuration() > NO_MAX_DURATION)
165       action->updateMaxDuration(delta);
166
167     if (action->getRemainsNoUpdate() > 0 && lmm_get_variable_weight(action->getVariable()) > 0 &&
168         action->storage_->usedSize_ == action->storage_->size_) {
169       action->finish();
170       action->setState(Action::State::failed);
171     } else if (((action->getRemainsNoUpdate() <= 0) && (lmm_get_variable_weight(action->getVariable()) > 0)) ||
172                ((action->getMaxDuration() > NO_MAX_DURATION) && (action->getMaxDuration() <= 0))) {
173       action->finish();
174       action->setState(Action::State::done);
175     }
176   }
177 }
178
179 /************
180  * Resource *
181  ************/
182
183 StorageN11::StorageN11(StorageModel* model, const char* name, lmm_system_t maxminSystem, double bread, double bwrite,
184                        double bconnection, const char* type_id, char* content_name, const char* content_type,
185                        sg_size_t size, char* attach)
186     : Storage(model, name, maxminSystem, bread, bwrite, bconnection, type_id, content_name, content_type, size, attach)
187 {
188   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
189 }
190
191 StorageAction *StorageN11::open(const char* mount, const char* path)
192 {
193   XBT_DEBUG("\tOpen file '%s'",path);
194
195   sg_size_t size, *psize;
196   psize = (sg_size_t*) xbt_dict_get_or_null(content_, path);
197   // if file does not exist create an empty file
198   if(psize)
199     size = *psize;
200   else {
201     psize = xbt_new(sg_size_t,1);
202     size = 0;
203     *psize = size;
204     xbt_dict_set(content_, path, psize, nullptr);
205     XBT_DEBUG("File '%s' was not found, file created.",path);
206   }
207   surf_file_t file = xbt_new0(s_surf_file_t,1);
208   file->name = xbt_strdup(path);
209   file->size = size;
210   file->mount = xbt_strdup(mount);
211   file->current_position = 0;
212
213   StorageAction* action = new StorageN11Action(model(), 0, isOff(), this, OPEN);
214   action->file_         = file;
215
216   return action;
217 }
218
219 StorageAction *StorageN11::close(surf_file_t fd)
220 {
221   XBT_DEBUG("\tClose file '%s' size '%llu'", fd->name, fd->size);
222   // unref write actions from storage
223   for (std::vector<StorageAction*>::iterator it = writeActions_.begin(); it != writeActions_.end();) {
224     StorageAction *write_action = *it;
225     if ((write_action->file_) == fd) {
226       write_action->unref();
227       it = writeActions_.erase(it);
228     } else {
229       ++it;
230     }
231   }
232   free(fd->name);
233   free(fd->mount);
234   xbt_free(fd);
235   StorageAction* action = new StorageN11Action(model(), 0, isOff(), this, CLOSE);
236   return action;
237 }
238
239 StorageAction *StorageN11::read(surf_file_t fd, sg_size_t size)
240 {
241   if(fd->current_position + size > fd->size){
242     if (fd->current_position > fd->size){
243       size = 0;
244     } else {
245       size = fd->size - fd->current_position;
246     }
247     fd->current_position = fd->size;
248   }
249   else
250     fd->current_position += size;
251
252   StorageAction* action = new StorageN11Action(model(), size, isOff(), this, READ);
253   return action;
254 }
255
256 StorageAction *StorageN11::write(surf_file_t fd, sg_size_t size)
257 {
258   char *filename = fd->name;
259   XBT_DEBUG("\tWrite file '%s' size '%llu/%llu'",filename,size,fd->size);
260
261   StorageAction* action = new StorageN11Action(model(), size, isOff(), this, WRITE);
262   action->file_         = fd;
263   /* Substract the part of the file that might disappear from the used sized on the storage element */
264   usedSize_ -= (fd->size - fd->current_position);
265   // If the storage is full before even starting to write
266   if(usedSize_==size_) {
267     action->setState(Action::State::failed);
268   }
269   return action;
270 }
271
272 /**********
273  * Action *
274  **********/
275
276 StorageN11Action::StorageN11Action(Model *model, double cost, bool failed, Storage *storage, e_surf_action_storage_type_t type)
277 : StorageAction(model, cost, failed,
278     lmm_variable_new(model->getMaxminSystem(), this, 1.0, -1.0 , 3),
279     storage, type) {
280   XBT_IN("(%s,%g", storage->cname(), cost);
281
282   // Must be less than the max bandwidth for all actions
283   lmm_expand(model->getMaxminSystem(), storage->constraint(), getVariable(), 1.0);
284   switch(type) {
285   case OPEN:
286   case CLOSE:
287   case STAT:
288     break;
289   case READ:
290     lmm_expand(model->getMaxminSystem(), storage->constraintRead_, getVariable(), 1.0);
291     break;
292   case WRITE:
293     lmm_expand(model->getMaxminSystem(), storage->constraintWrite_, getVariable(), 1.0);
294
295     //TODO there is something annoying with what's below. Have to sort it out...
296     //    Action *action = this;
297     //    storage->p_writeActions->push_back(action);
298     //    ref();
299     break;
300   }
301   XBT_OUT();
302 }
303
304 int StorageN11Action::unref()
305 {
306   refcount_--;
307   if (!refcount_) {
308     if (action_hook.is_linked())
309       stateSet_->erase(stateSet_->iterator_to(*this));
310     if (getVariable())
311       lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
312     xbt_free(getCategory());
313     delete this;
314     return 1;
315   }
316   return 0;
317 }
318
319 void StorageN11Action::cancel()
320 {
321   setState(Action::State::failed);
322 }
323
324 void StorageN11Action::suspend()
325 {
326   XBT_IN("(%p)", this);
327   if (suspended_ != 2) {
328     lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 0.0);
329     suspended_ = 1;
330   }
331   XBT_OUT();
332 }
333
334 void StorageN11Action::resume()
335 {
336   THROW_UNIMPLEMENTED;
337 }
338
339 bool StorageN11Action::isSuspended()
340 {
341   return suspended_ == 1;
342 }
343
344 void StorageN11Action::setMaxDuration(double /*duration*/)
345 {
346   THROW_UNIMPLEMENTED;
347 }
348
349 void StorageN11Action::setPriority(double /*priority*/)
350 {
351   THROW_UNIMPLEMENTED;
352 }
353
354 }
355 }