Logo AND Algorithmique Numérique Distribuée

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