Logo AND Algorithmique Numérique Distribuée

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