Logo AND Algorithmique Numérique Distribuée

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