Logo AND Algorithmique Numérique Distribuée

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