Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This is already destroyed by Storage::~Storage.
[simgrid.git] / src / surf / storage_n11.cpp
1 #include "storage_n11.hpp"
2 #include "surf_private.h"
3
4 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_storage);
5
6 static int storage_selective_update = 0;
7 static xbt_swag_t storage_running_action_set_that_does_not_need_being_checked = NULL;
8
9 /*************
10  * CallBacks *
11  *************/
12
13 static XBT_INLINE void routing_storage_type_free(void *r)
14 {
15   storage_type_t stype = (storage_type_t) r;
16   free(stype->model);
17   free(stype->type_id);
18   free(stype->content);
19   free(stype->content_type);
20   xbt_dict_free(&(stype->properties));
21   xbt_dict_free(&(stype->properties));
22   free(stype);
23 }
24
25 static XBT_INLINE void surf_storage_resource_free(void *r)
26 {
27   // specific to storage
28   StoragePtr storage = dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(r));
29   // generic resource
30   delete storage;
31 }
32
33 static XBT_INLINE void routing_storage_host_free(void *r)
34 {
35   xbt_dynar_t dyn = (xbt_dynar_t) r;
36   xbt_dynar_free(&dyn);
37 }
38
39 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
40 {
41   void* stype = xbt_lib_get_or_null(storage_type_lib,
42                                     storage->type_id,
43                                     ROUTING_STORAGE_TYPE_LEVEL);
44   if(!stype) xbt_die("No storage type '%s'",storage->type_id);
45
46   // if storage content is not specified use the content of storage_type if exist
47   if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
48     storage->content = ((storage_type_t) stype)->content;
49     storage->content_type = ((storage_type_t) stype)->content_type;
50     XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
51         storage->id,((storage_type_t) stype)->content_type,
52         ((storage_type_t) stype)->type_id);
53   }
54
55   XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
56       "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
57       "\n\t\tproperties '%p'\n",
58       storage->id,
59       ((storage_type_t) stype)->model,
60       ((storage_type_t) stype)->type_id,
61       storage->content,
62       storage->content_type,
63       ((storage_type_t) stype)->properties);
64
65   surf_storage_model->createResource(storage->id,
66                                      ((storage_type_t) stype)->type_id,
67                                      storage->content,
68                                      storage->content_type,
69                                      storage->properties);
70 }
71
72 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t /*mstorage*/)
73 {
74   XBT_DEBUG("parse_mstorage_init");
75 }
76
77 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t /*storagetype_*/)
78 {
79   XBT_DEBUG("parse_storage_type_init");
80 }
81
82 static void parse_mount_init(sg_platf_mount_cbarg_t /*mount*/)
83 {
84   XBT_DEBUG("parse_mount_init");
85 }
86
87 static void storage_parse_storage(sg_platf_storage_cbarg_t storage)
88 {
89   xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
90                "Reading a storage, processing unit \"%s\" already exists", storage->id);
91
92   // Verification of an existing type_id
93 #ifndef NDEBUG
94   void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
95 #endif
96   xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
97
98   XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
99       storage->id,
100       storage->type_id,
101       storage->content);
102
103   xbt_lib_set(storage_lib,
104       storage->id,
105       ROUTING_STORAGE_LEVEL,
106       (void *) xbt_strdup(storage->type_id));
107 }
108
109 static void storage_parse_storage_type(sg_platf_storage_type_cbarg_t storage_type)
110 {
111   xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
112                "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
113
114   storage_type_t stype = xbt_new0(s_storage_type_t, 1);
115   stype->model = xbt_strdup(storage_type->model);
116   stype->properties = storage_type->properties;
117   stype->content = xbt_strdup(storage_type->content);
118   stype->content_type = xbt_strdup(storage_type->content_type);
119   stype->type_id = xbt_strdup(storage_type->id);
120   stype->size = storage_type->size;
121
122   XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
123       "content '%s', and content_type '%s'",
124       stype->type_id,
125       stype->model,
126       storage_type->content,
127       storage_type->content_type);
128
129   xbt_lib_set(storage_type_lib,
130       stype->type_id,
131       ROUTING_STORAGE_TYPE_LEVEL,
132       (void *) stype);
133 }
134
135 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t /*mstorage*/)
136 {
137   THROW_UNIMPLEMENTED;
138 //  mount_t mnt = xbt_new0(s_mount_t, 1);
139 //  mnt->id = xbt_strdup(mstorage->type_id);
140 //  mnt->name = xbt_strdup(mstorage->name);
141 //
142 //  if(!mount_list){
143 //    XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
144 //    mount_list = xbt_dynar_new(sizeof(char *), NULL);
145 //  }
146 //  xbt_dynar_push(mount_list,(void *) mnt);
147 //  free(mnt->id);
148 //  free(mnt->name);
149 //  xbt_free(mnt);
150 //  XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
151 }
152
153 static void mount_free(void *p)
154 {
155   mount_t mnt = (mount_t) p;
156   xbt_free(mnt->name);
157 }
158
159 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
160 {
161   // Verification of an existing storage
162 #ifndef NDEBUG
163   void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
164 #endif
165   xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
166
167   XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
168
169   s_mount_t mnt;
170   mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
171   mnt.name = xbt_strdup(mount->name);
172
173   if(!mount_list){
174     XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
175     mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
176   }
177   xbt_dynar_push(mount_list, &mnt);
178 }
179
180 static void storage_define_callbacks()
181 {
182   sg_platf_storage_add_cb(parse_storage_init);
183   sg_platf_storage_type_add_cb(parse_storage_type_init);
184   sg_platf_mstorage_add_cb(parse_mstorage_init);
185   sg_platf_mount_add_cb(parse_mount_init);
186 }
187
188 void storage_register_callbacks() {
189
190   ROUTING_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,xbt_free);
191   ROUTING_STORAGE_HOST_LEVEL = xbt_lib_add_level(storage_lib, routing_storage_host_free);
192   ROUTING_STORAGE_TYPE_LEVEL = xbt_lib_add_level(storage_type_lib, routing_storage_type_free);
193   SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, surf_storage_resource_free);
194
195   sg_platf_storage_add_cb(storage_parse_storage);
196   sg_platf_mstorage_add_cb(storage_parse_mstorage);
197   sg_platf_storage_type_add_cb(storage_parse_storage_type);
198   sg_platf_mount_add_cb(storage_parse_mount);
199 }
200
201 /*********
202  * Model *
203  *********/
204
205 void surf_storage_model_init_default(void)
206 {
207   surf_storage_model = new StorageN11Model();
208   storage_define_callbacks();
209   xbt_dynar_push(model_list, &surf_storage_model);
210 }
211
212 StorageN11Model::StorageN11Model() : StorageModel() {
213   StorageN11ActionLmm action;
214
215   XBT_DEBUG("surf_storage_model_init_internal");
216
217   storage_running_action_set_that_does_not_need_being_checked =
218       xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
219
220   if (!p_maxminSystem) {
221     p_maxminSystem = lmm_system_new(storage_selective_update);
222   }
223 }
224
225 StorageN11Model::~StorageN11Model(){
226   xbt_swag_free(storage_running_action_set_that_does_not_need_being_checked);
227   storage_running_action_set_that_does_not_need_being_checked = NULL;
228 }
229
230 StoragePtr StorageN11Model::createResource(const char* id, const char* type_id,
231                 const char* content_name, const char* content_type, xbt_dict_t properties)
232 {
233
234   xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
235               "Storage '%s' declared several times in the platform file",
236               id);
237
238   storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
239
240   double Bread  = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bread"));
241   double Bwrite = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bwrite"));
242   double Bconnection   = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bconnection"));
243
244   StoragePtr storage = new StorageN11Lmm(this, id, properties, p_maxminSystem,
245                   Bread, Bwrite, Bconnection,
246                   type_id, (char *)content_name, xbt_strdup(content_type), storage_type->size);
247
248   xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, static_cast<ResourcePtr>(storage));
249
250   XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s'\n\t\tproperties '%p'\n\t\tBread '%f'\n",
251       id,
252       type_id,
253       storage_type->properties,
254       Bread);
255
256   if(!p_storageList)
257         p_storageList = xbt_dynar_new(sizeof(char *),NULL);
258   xbt_dynar_push(p_storageList, &storage);
259
260   return storage;
261 }
262
263 double StorageN11Model::shareResources(double now)
264 {
265   XBT_DEBUG("storage_share_resources %f", now);
266   unsigned int i, j;
267   StoragePtr storage;
268   void *_write_action;
269   StorageActionLmmPtr write_action;
270
271   double min_completion = shareResourcesMaxMin(p_runningActionSet,
272       p_maxminSystem, lmm_solve);
273
274   double rate;
275   // Foreach disk
276   xbt_dynar_foreach(p_storageList,i,storage)
277   {
278     rate = 0;
279     // Foreach write action on disk
280     xbt_dynar_foreach(storage->p_writeActions, j, _write_action)
281     {
282       write_action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_write_action));
283       rate += lmm_variable_getvalue(write_action->p_variable);
284     }
285     if(rate > 0)
286       min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
287   }
288
289   return min_completion;
290 }
291
292 void StorageN11Model::updateActionsState(double /*now*/, double delta)
293 {
294   void *_action, *_next_action;
295   StorageActionLmmPtr action = NULL;
296
297   xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
298         action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_action));
299     if(action->m_type == WRITE)
300     {
301       // Update the disk usage
302      // Update the file size
303      // For each action of type write
304       double rate = lmm_variable_getvalue(action->p_variable);
305       /* Hack to avoid rounding differences between x86 and x86_64
306        * (note that the next sizes are of type sg_size_t). */
307       long incr = delta * rate + MAXMIN_PRECISION;
308       action->p_storage->m_usedSize += (incr - action->p_file->size); // disk usage
309       action->p_file->size = incr; // file size
310
311       sg_size_t *psize = xbt_new(sg_size_t,1);
312       *psize = action->p_file->size;
313
314       xbt_dict_t content_dict = action->p_storage->p_content;
315       xbt_dict_set(content_dict, action->p_file->name, psize, NULL);
316     }
317
318     double_update(&action->m_remains,
319                   lmm_variable_getvalue(action->p_variable) * delta);
320
321     if (action->m_maxDuration != NO_MAX_DURATION)
322       double_update(&action->m_maxDuration, delta);
323
324     if(action->m_remains > 0 &&
325         lmm_get_variable_weight(action->p_variable) > 0 &&
326         action->p_storage->m_usedSize == action->p_storage->m_size)
327     {
328       action->m_finish = surf_get_clock();
329       action->setState(SURF_ACTION_FAILED);
330     } else if ((action->m_remains <= 0) &&
331         (lmm_get_variable_weight(action->p_variable) > 0))
332     {
333       action->m_finish = surf_get_clock();
334       action->setState(SURF_ACTION_DONE);
335     } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
336                (action->m_maxDuration <= 0))
337     {
338       action->m_finish = surf_get_clock();
339       action->setState(SURF_ACTION_DONE);
340     }
341   }
342
343   return;
344 }
345
346 /************
347  * Resource *
348  ************/
349
350 StorageN11Lmm::StorageN11Lmm(StorageModelPtr model, const char* name, xbt_dict_t properties,
351              lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
352              const char* type_id, char *content_name, char *content_type, sg_size_t size)
353  :  Resource(model, name, properties),
354     StorageLmm(maxminSystem, bread, bwrite, bconnection, type_id, content_name, content_type, size) {
355   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
356
357   p_stateCurrent = SURF_RESOURCE_ON;
358   m_usedSize = 0;
359   m_size = 0;
360
361   p_content = parseContent(content_name);
362   p_contentType = content_type;
363   p_constraint = lmm_constraint_new(maxminSystem, this, bconnection);
364   p_constraintRead  = lmm_constraint_new(maxminSystem, this, bread);
365   p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
366   m_size = size;
367   p_typeId = xbt_strdup(type_id);
368 }
369
370 StorageActionPtr StorageN11Lmm::ls(const char* path)
371 {
372   StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, LS);
373
374   action->p_lsDict = NULL;
375   xbt_dict_t ls_dict = xbt_dict_new_homogeneous(xbt_free);
376
377   char* key;
378   sg_size_t size = 0;
379   xbt_dict_cursor_t cursor = NULL;
380
381   xbt_dynar_t dyn = NULL;
382   char* file = NULL;
383
384   // for each file in the storage content
385   xbt_dict_foreach(p_content,cursor,key,size){
386     // Search if file start with the prefix 'path'
387     if(xbt_str_start_with(key,path)){
388       file = &key[strlen(path)];
389
390       // Split file with '/'
391       dyn = xbt_str_split(file,"/");
392       file = xbt_dynar_get_as(dyn,0,char*);
393
394       // file
395       if(xbt_dynar_length(dyn) == 1){
396         sg_size_t *psize = xbt_new(sg_size_t, 1);
397         *psize=size;
398         xbt_dict_set(ls_dict, file, psize, NULL);
399       }
400       // Directory
401       else
402       {
403         // if directory does not exist yet in the dictionary
404         if(!xbt_dict_get_or_null(ls_dict,file))
405           xbt_dict_set(ls_dict,file,NULL,NULL);
406       }
407       xbt_dynar_free(&dyn);
408     }
409   }
410
411   action->p_lsDict = ls_dict;
412   return action;
413 }
414
415 StorageActionPtr StorageN11Lmm::open(const char* mount, const char* path)
416 {
417   XBT_DEBUG("\tOpen file '%s'",path);
418   sg_size_t size, *psize;
419   psize = (sg_size_t*) xbt_dict_get_or_null(p_content, path);
420   // if file does not exist create an empty file
421   if(psize)
422     size = *psize;
423   else {
424         psize = xbt_new(sg_size_t,1);
425     size = 0;
426     *psize = size;
427     xbt_dict_set(p_content, path, psize, NULL);
428     XBT_DEBUG("File '%s' was not found, file created.",path);
429   }
430   surf_file_t file = xbt_new0(s_surf_file_t,1);
431   file->name = xbt_strdup(path);
432   file->size = size;
433   file->mount = xbt_strdup(mount);
434   file->current_position = 0;
435
436   StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, OPEN);
437   action->p_file = file;
438   return action;
439 }
440
441 StorageActionPtr StorageN11Lmm::close(surf_file_t fd)
442 {
443   char *filename = fd->name;
444   XBT_DEBUG("\tClose file '%s' size '%llu'", filename, fd->size);
445   // unref write actions from storage
446   void *_write_action;
447   StorageActionLmmPtr write_action;
448   unsigned int i;
449   xbt_dynar_foreach(p_writeActions, i, _write_action) {
450         write_action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_write_action));
451     if ((write_action->p_file) == fd) {
452       xbt_dynar_cursor_rm(p_writeActions, &i);
453       write_action->unref();
454     }
455   }
456   free(fd->name);
457   free(fd->mount);
458   xbt_free(fd);
459   StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, CLOSE);
460   return action;
461 }
462
463 StorageActionPtr StorageN11Lmm::read(surf_file_t fd, sg_size_t size)
464 {
465   if(size > fd->size){
466     size = fd->size;
467     fd->current_position = fd->size;
468   }
469   else
470         fd->current_position += size;
471
472   StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, size, p_stateCurrent != SURF_RESOURCE_ON, this, READ);
473   return action;
474 }
475
476 StorageActionPtr StorageN11Lmm::write(surf_file_t fd, sg_size_t size)
477 {
478   char *filename = fd->name;
479   XBT_DEBUG("\tWrite file '%s' size '%llu/%llu'",filename,size,fd->size);
480
481   StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, size, p_stateCurrent != SURF_RESOURCE_ON, this, WRITE);
482   action->p_file = fd;
483   fd->current_position += size;
484   // If the storage is full
485   if(m_usedSize==m_size) {
486     action->setState(SURF_ACTION_FAILED);
487   }
488   return action;
489 }
490
491 void StorageN11Lmm::rename(const char *src, const char *dest)
492 {
493   sg_size_t *psize, *new_psize;
494   psize = (sg_size_t*) xbt_dict_get_or_null(p_content,src);
495   new_psize = xbt_new(sg_size_t, 1);
496   *new_psize = *psize;
497   if (psize){// src file exists
498     xbt_dict_remove(p_content, src);
499     xbt_dict_set(p_content, dest, new_psize,NULL);
500     XBT_DEBUG("Change file name from %s to %s, size '%llu'",src, dest, *psize);
501   }
502   else
503     XBT_DEBUG("File %s doesn't exist",src);
504 }
505
506 xbt_dict_t StorageN11Lmm::getContent()
507 {
508   /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
509   /*surf_action_t action = storage_action_execute(storage,0, LS);*/
510
511   xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
512   xbt_dict_cursor_t cursor = NULL;
513   char *file;
514   sg_size_t *psize;
515
516   xbt_dict_foreach(p_content, cursor, file, psize){
517     xbt_dict_set(content_dict,file,psize,NULL);
518   }
519   return content_dict;
520 }
521
522 sg_size_t StorageN11Lmm::getSize(){
523   return m_size;
524 }
525
526 /**********
527  * Action *
528  **********/
529
530 StorageN11ActionLmm::StorageN11ActionLmm(ModelPtr model, double cost, bool failed, StorageLmmPtr storage, e_surf_action_storage_type_t type)
531   : Action(model, cost, failed),
532     StorageActionLmm(storage, type) {
533   XBT_IN("(%s,%g", storage->m_name, cost);
534   p_variable = lmm_variable_new(p_model->p_maxminSystem, this, 1.0, -1.0 , 3);
535
536   // Must be less than the max bandwidth for all actions
537   lmm_expand(p_model->p_maxminSystem, storage->p_constraint, p_variable, 1.0);
538   switch(type) {
539   case OPEN:
540   case CLOSE:
541   case STAT:
542   case LS:
543     break;
544   case READ:
545     lmm_expand(p_model->p_maxminSystem, storage->p_constraintRead,
546                p_variable, 1.0);
547     break;
548   case WRITE:
549     lmm_expand(p_model->p_maxminSystem, storage->p_constraintWrite,
550                p_variable, 1.0);
551     ActionPtr action = this;
552     xbt_dynar_push(storage->p_writeActions, &action);
553     ref();
554     break;
555   }
556   XBT_OUT();
557 }
558
559 int StorageN11ActionLmm::unref()
560 {
561   m_refcount--;
562   if (!m_refcount) {
563     xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
564     if (p_variable)
565       lmm_variable_free(p_model->p_maxminSystem, p_variable);
566 #ifdef HAVE_TRACING
567     xbt_free(p_category);
568 #endif
569     delete this;
570     return 1;
571   }
572   return 0;
573 }
574
575 void StorageN11ActionLmm::cancel()
576 {
577   setState(SURF_ACTION_FAILED);
578   return;
579 }
580
581 void StorageN11ActionLmm::suspend()
582 {
583   XBT_IN("(%p)", this);
584   if (m_suspended != 2) {
585     lmm_update_variable_weight(p_model->p_maxminSystem,
586                                p_variable,
587                                0.0);
588     m_suspended = 1;
589   }
590   XBT_OUT();
591 }
592
593 void StorageN11ActionLmm::resume()
594 {
595   THROW_UNIMPLEMENTED;
596 }
597
598 bool StorageN11ActionLmm::isSuspended()
599 {
600   return m_suspended == 1;
601 }
602
603 void StorageN11ActionLmm::setMaxDuration(double /*duration*/)
604 {
605   THROW_UNIMPLEMENTED;
606 }
607
608 void StorageN11ActionLmm::setPriority(double /*priority*/)
609 {
610   THROW_UNIMPLEMENTED;
611 }
612