Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d4f35412fabc9252c7d1443269d0ef16f18a22c8
[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   unsigned int i, j;
314   StoragePtr storage;
315   StorageActionLmmPtr write_action;
316
317   double min_completion = shareResourcesMaxMin(p_runningActionSet,
318       storage_maxmin_system, lmm_solve);
319
320   double rate;
321   // Foreach disk
322   xbt_dynar_foreach(storage_list,i,storage)
323   {
324     rate = 0;
325     // Foreach write action on disk
326     xbt_dynar_foreach(storage->p_writeActions, j, write_action)
327     {
328       rate += lmm_variable_getvalue(write_action->p_variable);
329     }
330     if(rate > 0)
331       min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
332   }
333
334   return min_completion;
335 }
336
337 void StorageModel::updateActionsState(double now, double delta)
338 {
339   void *_action, *_next_action;
340   StorageActionLmmPtr action = NULL;
341
342   // Update the disk usage
343   // Update the file size
344   // For each action of type write
345   xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
346         action = (StorageActionLmmPtr) _action;
347     if(action->m_type == WRITE)
348     {
349       double rate = lmm_variable_getvalue(action->p_variable);
350       /* Hack to avoid rounding differences between x86 and x86_64
351        * (note that the next sizes are of type size_t). */
352       long incr = delta * rate + MAXMIN_PRECISION;
353       action->p_storage->m_usedSize += incr; // disk usage
354       action->p_file->size += incr; // file size
355     }
356   }
357
358   xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
359         action = (StorageActionLmmPtr) _action;
360
361     double_update(&action->m_remains,
362                   lmm_variable_getvalue(action->p_variable) * delta);
363
364     if (action->m_maxDuration != NO_MAX_DURATION)
365       double_update(&action->m_maxDuration, delta);
366
367     if(action->m_remains > 0 &&
368         lmm_get_variable_weight(action->p_variable) > 0 &&
369         action->p_storage->m_usedSize == action->p_storage->m_size)
370     {
371       action->m_finish = surf_get_clock();
372       action->setState(SURF_ACTION_FAILED);
373     } else if ((action->m_remains <= 0) &&
374         (lmm_get_variable_weight(action->p_variable) > 0))
375     {
376       action->m_finish = surf_get_clock();
377       action->setState(SURF_ACTION_DONE);
378     } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
379                (action->m_maxDuration <= 0))
380     {
381       action->m_finish = surf_get_clock();
382       action->setState(SURF_ACTION_DONE);
383     }
384   }
385
386   return;
387 }
388
389 xbt_dict_t StorageModel::parseContent(char *filename, size_t *used_size)
390 {
391   *used_size = 0;
392   if ((!filename) || (strcmp(filename, "") == 0))
393     return NULL;
394
395   xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
396   FILE *file = NULL;
397
398   file = surf_fopen(filename, "r");
399   xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
400               xbt_str_join(surf_path, ":"));
401
402   char *line = NULL;
403   size_t len = 0;
404   ssize_t read;
405   char path[1024];
406   size_t size;
407
408
409   while ((read = xbt_getline(&line, &len, file)) != -1) {
410     if (read){
411     if(sscanf(line,"%s %zu",path, &size)==2) {
412         *used_size += size;
413         xbt_dict_set(parse_content,path,(void*) size,NULL);
414       } else {
415         xbt_die("Be sure of passing a good format for content file.\n");
416       }
417     }
418   }
419   free(line);
420   fclose(file);
421   return parse_content;
422 }
423
424 /************
425  * Resource *
426  ************/
427
428 Storage::Storage(StorageModelPtr model, const char* name, xbt_dict_t properties) {
429   p_writeActions = xbt_dynar_new(sizeof(char *),NULL);
430 }
431
432 StorageLmm::StorageLmm(StorageModelPtr model, const char* name, xbt_dict_t properties,
433              lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
434              xbt_dict_t content, size_t size)
435  :    ResourceLmm(), Storage(model, name, properties) {
436   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%lu'", bconnection, bread, bwrite, ((unsigned long)size));
437
438   p_constraint = lmm_constraint_new(maxminSystem, this, bconnection);
439   p_constraintRead  = lmm_constraint_new(maxminSystem, this, bread);
440   p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
441   p_content = content;
442   m_size = size;
443 }
444
445 bool Storage::isUsed()
446 {
447   THROW_UNIMPLEMENTED;
448   return false;
449 }
450
451 void Storage::updateState(tmgr_trace_event_t event_type, double value, double date)
452 {
453   THROW_UNIMPLEMENTED;
454 }
455
456 StorageActionPtr StorageLmm::ls(const char* path)
457 {
458   StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, LS);
459
460   action->p_lsDict = NULL;
461   xbt_dict_t ls_dict = xbt_dict_new();
462
463   char* key;
464   size_t size = 0;
465   xbt_dict_cursor_t cursor = NULL;
466
467   xbt_dynar_t dyn = NULL;
468   char* file = NULL;
469
470   // for each file in the storage content
471   xbt_dict_foreach(p_content,cursor,key,size){
472     // Search if file start with the prefix 'path'
473     if(xbt_str_start_with(key,path)){
474       file = &key[strlen(path)];
475
476       // Split file with '/'
477       dyn = xbt_str_split(file,"/");
478       file = xbt_dynar_get_as(dyn,0,char*);
479
480       // file
481       if(xbt_dynar_length(dyn) == 1){
482         xbt_dict_set(ls_dict,file,&size,NULL);
483       }
484       // Directory
485       else
486       {
487         // if directory does not exist yet in the dictionary
488         if(!xbt_dict_get_or_null(ls_dict,file))
489           xbt_dict_set(ls_dict,file,NULL,NULL);
490       }
491       xbt_dynar_free(&dyn);
492     }
493   }
494
495   action->p_lsDict = ls_dict;
496   return action;
497 }
498
499 StorageActionPtr StorageLmm::open(const char* mount, const char* path)
500 {
501   XBT_DEBUG("\tOpen file '%s'",path);
502   size_t size = (size_t) xbt_dict_get_or_null(p_content, path);
503   // if file does not exist create an empty file
504   if(!size){
505     xbt_dict_set(p_content, path, &size, NULL);
506     XBT_DEBUG("File '%s' was not found, file created.",path);
507   }
508   surf_file_t file = xbt_new0(s_surf_file_t,1);
509   file->name = xbt_strdup(path);
510   file->size = size;
511   file->storage = xbt_strdup(mount);
512
513   StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, OPEN);
514   action->p_file = file;
515   return action;
516 }
517
518 StorageActionPtr StorageLmm::close(surf_file_t fd)
519 {
520   char *filename = fd->name;
521   XBT_DEBUG("\tClose file '%s' size '%zu'", filename, fd->size);
522   // unref write actions from storage
523   StorageActionLmmPtr write_action;
524   unsigned int i;
525   xbt_dynar_foreach(p_writeActions, i, write_action) {
526     if ((write_action->p_file) == fd) {
527       xbt_dynar_cursor_rm(p_writeActions, &i);
528       write_action->unref();
529     }
530   }
531   free(fd->name);
532   free(fd->storage);
533   xbt_free(fd);
534   StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, CLOSE);
535   return action;
536 }
537
538 StorageActionPtr StorageLmm::read(void* ptr, size_t size, surf_file_t fd)
539 {
540   if(size > fd->size)
541     size = fd->size;
542   StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, READ);
543   return action;
544 }
545
546 StorageActionPtr StorageLmm::write(const void* ptr, size_t size, surf_file_t fd)
547 {
548   char *filename = fd->name;
549   XBT_DEBUG("\tWrite file '%s' size '%zu/%zu'",filename,size,fd->size);
550
551   StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, WRITE);
552   action->p_file = fd;
553
554   // If the storage is full
555   if(m_usedSize==m_size) {
556     action->setState(SURF_ACTION_FAILED);
557   }
558   return action;
559 }
560
561 /**********
562  * Action *
563  **********/
564
565 StorageActionLmm::StorageActionLmm(ModelPtr model, double cost, bool failed, StorageLmmPtr storage, e_surf_action_storage_type_t type)
566   : ActionLmm(model, cost, failed), StorageAction(model, cost, failed, storage, type) {
567   XBT_IN("(%s,%zu", storage->m_name, cost);
568   // Must be less than the max bandwidth for all actions
569   lmm_expand(storage_maxmin_system, storage->p_constraint, p_variable, 1.0);
570   switch(type) {
571   case OPEN:
572   case CLOSE:
573   case STAT:
574   case LS:
575     break;
576   case READ:
577     lmm_expand(storage_maxmin_system, storage->p_constraintRead,
578                p_variable, 1.0);
579     break;
580   case WRITE:
581     lmm_expand(storage_maxmin_system, storage->p_constraintWrite,
582                p_variable, 1.0);
583     xbt_dynar_push(storage->p_writeActions,this);
584     break;
585   }
586   XBT_OUT();
587 }
588
589 int StorageActionLmm::unref()
590 {
591   m_refcount--;
592   if (!m_refcount) {
593     xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
594     if (p_variable)
595       lmm_variable_free(storage_maxmin_system, p_variable);
596 #ifdef HAVE_TRACING
597     xbt_free(p_category);
598 #endif
599     delete this;
600     return 1;
601   }
602   return 0;
603 }
604
605 void StorageActionLmm::cancel()
606 {
607   setState(SURF_ACTION_FAILED);
608   return;
609 }
610
611 void StorageActionLmm::suspend()
612 {
613   XBT_IN("(%p)", this);
614   if (m_suspended != 2) {
615     lmm_update_variable_weight(storage_maxmin_system,
616                                p_variable,
617                                0.0);
618     m_suspended = 1;
619   }
620   XBT_OUT();
621 }
622
623 void StorageActionLmm::resume()
624 {
625   THROW_UNIMPLEMENTED;
626 }
627
628 bool StorageActionLmm::isSuspended()
629 {
630   return m_suspended == 1;
631 }
632
633 void StorageActionLmm::setMaxDuration(double duration)
634 {
635   THROW_UNIMPLEMENTED;
636 }
637
638 void StorageActionLmm::setPriority(double priority)
639 {
640   THROW_UNIMPLEMENTED;
641 }
642