Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove c surf files
[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 static int storage_selective_update = 0;
22 static xbt_swag_t storage_running_action_set_that_does_not_need_being_checked = NULL;
23
24 /*************
25  * CallBacks *
26  *************/
27
28 static XBT_INLINE void routing_storage_type_free(void *r)
29 {
30   storage_type_t stype = (storage_type_t) r;
31   free(stype->model);
32   free(stype->type_id);
33   free(stype->content);
34   xbt_dict_free(&(stype->properties));
35   free(stype);
36 }
37
38 static XBT_INLINE void surf_storage_resource_free(void *r)
39 {
40   // specific to storage
41   StoragePtr storage = (StoragePtr) r;
42   xbt_dict_free(&storage->p_content);
43   xbt_dynar_free(&storage->p_writeActions);
44   // generic resource
45   delete storage;
46 }
47
48 static XBT_INLINE void routing_storage_host_free(void *r)
49 {
50   xbt_dynar_t dyn = (xbt_dynar_t) r;
51   xbt_dynar_free(&dyn);
52 }
53
54 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
55 {
56   void* stype = xbt_lib_get_or_null(storage_type_lib,
57                                     storage->type_id,
58                                     ROUTING_STORAGE_TYPE_LEVEL);
59   if(!stype) xbt_die("No storage type '%s'",storage->type_id);
60
61   // if storage content is not specified use the content of storage_type if exist
62   if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
63     storage->content = ((storage_type_t) stype)->content;
64     XBT_DEBUG("For disk '%s' content is empty, use the content of storage type '%s'",storage->id,((storage_type_t) stype)->type_id);
65   }
66
67   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",
68       storage->id,
69       ((storage_type_t) stype)->model,
70       ((storage_type_t) stype)->type_id,
71       storage->content,
72       ((storage_type_t) stype)->properties);
73
74   surf_storage_model->createResource(storage->id, ((storage_type_t) stype)->model,
75                                      ((storage_type_t) stype)->type_id,
76                                      storage->content);
77 }
78
79 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
80 {
81   XBT_DEBUG("parse_mstorage_init");
82 }
83
84 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
85 {
86   XBT_DEBUG("parse_storage_type_init");
87 }
88
89 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
90 {
91   XBT_DEBUG("parse_mount_init");
92 }
93
94 static void storage_parse_storage(sg_platf_storage_cbarg_t storage)
95 {
96   xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
97                "Reading a storage, processing unit \"%s\" already exists", storage->id);
98
99   // Verification of an existing type_id
100 #ifndef NDEBUG
101   void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
102 #endif
103   xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
104
105   XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
106       storage->id,
107       storage->type_id,
108       storage->content);
109
110   xbt_lib_set(storage_lib,
111       storage->id,
112       ROUTING_STORAGE_LEVEL,
113       (void *) xbt_strdup(storage->type_id));
114 }
115
116 static xbt_dict_t parse_storage_content(char *filename, size_t *used_size)
117 {
118   *used_size = 0;
119   if ((!filename) || (strcmp(filename, "") == 0))
120     return NULL;
121
122   xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
123   FILE *file = NULL;
124
125   file = surf_fopen(filename, "r");
126   xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
127               xbt_str_join(surf_path, ":"));
128
129   char *line = NULL;
130   size_t len = 0;
131   ssize_t read;
132   char path[1024];
133   size_t size;
134
135
136   while ((read = xbt_getline(&line, &len, file)) != -1) {
137     if (read){
138     if(sscanf(line,"%s %zu",path, &size)==2) {
139         *used_size += size;
140         xbt_dict_set(parse_content,path,(void*) size,NULL);
141       } else {
142         xbt_die("Be sure of passing a good format for content file.\n");
143       }
144     }
145   }
146   free(line);
147   fclose(file);
148   return parse_content;
149 }
150
151 static void storage_parse_storage_type(sg_platf_storage_type_cbarg_t storage_type)
152 {
153   xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
154                "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
155
156   storage_type_t stype = xbt_new0(s_storage_type_t, 1);
157   stype->model = xbt_strdup(storage_type->model);
158   stype->properties = storage_type->properties;
159   stype->content = xbt_strdup(storage_type->content);
160   stype->type_id = xbt_strdup(storage_type->id);
161   stype->size = storage_type->size * 1000000000; /* storage_type->size is in Gbytes and stype->sizeis in bytes */
162
163   XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s' content '%s'",
164       stype->type_id,
165       stype->model,
166       storage_type->content);
167
168   xbt_lib_set(storage_type_lib,
169       stype->type_id,
170       ROUTING_STORAGE_TYPE_LEVEL,
171       (void *) stype);
172 }
173 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t mstorage)
174 {
175   THROW_UNIMPLEMENTED;
176 //  mount_t mnt = xbt_new0(s_mount_t, 1);
177 //  mnt->id = xbt_strdup(mstorage->type_id);
178 //  mnt->name = xbt_strdup(mstorage->name);
179 //
180 //  if(!mount_list){
181 //    XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
182 //    mount_list = xbt_dynar_new(sizeof(char *), NULL);
183 //  }
184 //  xbt_dynar_push(mount_list,(void *) mnt);
185 //  free(mnt->id);
186 //  free(mnt->name);
187 //  xbt_free(mnt);
188 //  XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
189 }
190
191 static void mount_free(void *p)
192 {
193   mount_t mnt = (mount_t) p;
194   xbt_free(mnt->name);
195 }
196
197 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
198 {
199   // Verification of an existing storage
200 #ifndef NDEBUG
201   void* storage = xbt_lib_get_or_null(storage_lib, mount->id,ROUTING_STORAGE_LEVEL);
202 #endif
203   xbt_assert(storage,"Disk id \"%s\" does not exists", mount->id);
204
205   XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->id, mount->name);
206
207   s_mount_t mnt;
208   mnt.id = surf_storage_resource_priv(surf_storage_resource_by_name(mount->id));
209   mnt.name = xbt_strdup(mount->name);
210
211   if(!mount_list){
212     //FIXME:XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
213     mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
214   }
215   xbt_dynar_push(mount_list,&mnt);
216 }
217
218 static void storage_define_callbacks()
219 {
220   sg_platf_storage_add_cb(parse_storage_init);
221   sg_platf_storage_type_add_cb(parse_storage_type_init);
222   sg_platf_mstorage_add_cb(parse_mstorage_init);
223   sg_platf_mount_add_cb(parse_mount_init);
224 }
225
226 void storage_register_callbacks() {
227
228   ROUTING_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,xbt_free);
229   ROUTING_STORAGE_HOST_LEVEL = xbt_lib_add_level(storage_lib, routing_storage_host_free);
230   ROUTING_STORAGE_TYPE_LEVEL = xbt_lib_add_level(storage_type_lib, routing_storage_type_free);
231   SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, surf_storage_resource_free);
232
233   sg_platf_storage_add_cb(storage_parse_storage);
234   sg_platf_mstorage_add_cb(storage_parse_mstorage);
235   sg_platf_storage_type_add_cb(storage_parse_storage_type);
236   sg_platf_mount_add_cb(storage_parse_mount);
237 }
238
239 /*********
240  * Model *
241  *********/
242
243 void surf_storage_model_init_default(void)
244 {
245   surf_storage_model = new StorageModel();
246   storage_define_callbacks();
247   xbt_dynar_push(model_list, &surf_storage_model);
248 }
249
250 StorageModel::StorageModel() : Model("Storage") {
251   StorageActionLmm action;
252
253   XBT_DEBUG("surf_storage_model_init_internal");
254
255   storage_running_action_set_that_does_not_need_being_checked =
256       xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
257
258   if (!p_maxminSystem) {
259     p_maxminSystem = lmm_system_new(storage_selective_update);
260   }
261 }
262
263
264 StorageModel::~StorageModel(){
265   lmm_system_free(p_maxminSystem);
266
267   surf_storage_model = NULL;
268
269   xbt_dynar_free(&storage_list);
270
271   xbt_swag_free(storage_running_action_set_that_does_not_need_being_checked);
272   storage_running_action_set_that_does_not_need_being_checked = NULL;
273 }
274
275 StoragePtr StorageModel::createResource(const char* id, const char* model, const char* type_id, const char* content_name)
276 {
277
278   xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
279               "Storage '%s' declared several times in the platform file",
280               id);
281
282   storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
283
284   double Bread  = atof((char*)xbt_dict_get(storage_type->properties, "Bread"));
285   double Bwrite = atof((char*)xbt_dict_get(storage_type->properties, "Bwrite"));
286   double Bconnection   = atof((char*)xbt_dict_get(storage_type->properties, "Bconnection"));
287
288   StoragePtr storage = new StorageLmm(this, NULL, NULL, p_maxminSystem,
289                   Bread, Bwrite, Bconnection, (char *)content_name, storage_type->size);
290
291   xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
292
293   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",
294       id,
295       model,
296       type_id,
297       storage_type->properties,
298       Bread);
299
300   if(!storage_list) storage_list=xbt_dynar_new(sizeof(char *),NULL);
301   xbt_dynar_push(storage_list, &storage);
302
303   return storage;
304 }
305
306 double StorageModel::shareResources(double now)
307 {
308   XBT_DEBUG("storage_share_resources %f", now);
309   unsigned int i, j;
310   StoragePtr storage;
311   StorageActionLmmPtr write_action;
312
313   double min_completion = shareResourcesMaxMin(p_runningActionSet,
314       p_maxminSystem, lmm_solve);
315
316   double rate;
317   // Foreach disk
318   xbt_dynar_foreach(storage_list,i,storage)
319   {
320     rate = 0;
321     // Foreach write action on disk
322     xbt_dynar_foreach(storage->p_writeActions, j, write_action)
323     {
324       rate += lmm_variable_getvalue(write_action->p_variable);
325     }
326     if(rate > 0)
327       min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
328   }
329
330   return min_completion;
331 }
332
333 void StorageModel::updateActionsState(double now, double delta)
334 {
335   void *_action, *_next_action;
336   StorageActionLmmPtr action = NULL;
337
338   // Update the disk usage
339   // Update the file size
340   // For each action of type write
341   xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
342         action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_action));
343     if(action->m_type == WRITE)
344     {
345       double rate = lmm_variable_getvalue(action->p_variable);
346       /* Hack to avoid rounding differences between x86 and x86_64
347        * (note that the next sizes are of type size_t). */
348       long incr = delta * rate + MAXMIN_PRECISION;
349       action->p_storage->m_usedSize += incr; // disk usage
350       action->p_file->size += incr; // file size
351     }
352   }
353
354   xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
355         action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_action));
356
357     double_update(&action->m_remains,
358                   lmm_variable_getvalue(action->p_variable) * delta);
359
360     if (action->m_maxDuration != NO_MAX_DURATION)
361       double_update(&action->m_maxDuration, delta);
362
363     if(action->m_remains > 0 &&
364         lmm_get_variable_weight(action->p_variable) > 0 &&
365         action->p_storage->m_usedSize == action->p_storage->m_size)
366     {
367       action->m_finish = surf_get_clock();
368       action->setState(SURF_ACTION_FAILED);
369     } else if ((action->m_remains <= 0) &&
370         (lmm_get_variable_weight(action->p_variable) > 0))
371     {
372       action->m_finish = surf_get_clock();
373       action->setState(SURF_ACTION_DONE);
374     } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
375                (action->m_maxDuration <= 0))
376     {
377       action->m_finish = surf_get_clock();
378       action->setState(SURF_ACTION_DONE);
379     }
380   }
381
382   return;
383 }
384
385 xbt_dict_t Storage::parseContent(char *filename)
386 {
387   m_usedSize = 0;
388   if ((!filename) || (strcmp(filename, "") == 0))
389     return NULL;
390
391   xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
392   FILE *file = NULL;
393
394   file = surf_fopen(filename, "r");
395   xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
396               xbt_str_join(surf_path, ":"));
397
398   char *line = NULL;
399   size_t len = 0;
400   ssize_t read;
401   char path[1024];
402   size_t size;
403
404
405   while ((read = xbt_getline(&line, &len, file)) != -1) {
406     if (read){
407     if(sscanf(line,"%s %zu",path, &size)==2) {
408         m_usedSize += size;
409         xbt_dict_set(parse_content,path,(void*) size,NULL);
410       } else {
411         xbt_die("Be sure of passing a good format for content file.\n");
412       }
413     }
414   }
415   free(line);
416   fclose(file);
417   return parse_content;
418 }
419
420 /************
421  * Resource *
422  ************/
423
424 Storage::Storage(StorageModelPtr model, const char* name, xbt_dict_t properties)
425 :  Resource(model, name, properties)
426 {
427   p_writeActions = xbt_dynar_new(sizeof(char *),NULL);
428 }
429
430 StorageLmm::StorageLmm(StorageModelPtr model, const char* name, xbt_dict_t properties,
431              lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
432              char *content_name, size_t size)
433  :  Resource(model, name, properties), ResourceLmm(), Storage(model, name, properties) {
434   XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%lu'", bconnection, bread, bwrite, ((unsigned long)size));
435
436   p_stateCurrent = SURF_RESOURCE_ON;
437   m_usedSize = 0;
438   m_size = 0;
439
440   p_content = parseContent(content_name);
441   p_constraint = lmm_constraint_new(maxminSystem, this, bconnection);
442   p_constraintRead  = lmm_constraint_new(maxminSystem, this, bread);
443   p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
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, size, 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, size, 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   : Action(model, cost, failed), ActionLmm(model, cost, failed), StorageAction(model, cost, failed, storage, type) {
569   XBT_IN("(%s,%zu", storage->m_name, cost);
570   p_variable = lmm_variable_new(p_model->p_maxminSystem, this, 1.0, -1.0 , 3);
571
572   // Must be less than the max bandwidth for all actions
573   lmm_expand(p_model->p_maxminSystem, storage->p_constraint, p_variable, 1.0);
574   switch(type) {
575   case OPEN:
576   case CLOSE:
577   case STAT:
578   case LS:
579     break;
580   case READ:
581     lmm_expand(p_model->p_maxminSystem, storage->p_constraintRead,
582                p_variable, 1.0);
583     break;
584   case WRITE:
585     lmm_expand(p_model->p_maxminSystem, storage->p_constraintWrite,
586                p_variable, 1.0);
587     xbt_dynar_push(storage->p_writeActions, static_cast<ActionPtr>(this));
588     break;
589   }
590   XBT_OUT();
591 }
592
593 int StorageActionLmm::unref()
594 {
595   m_refcount--;
596   if (!m_refcount) {
597     xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
598     if (p_variable)
599       lmm_variable_free(p_model->p_maxminSystem, p_variable);
600 #ifdef HAVE_TRACING
601     xbt_free(p_category);
602 #endif
603     delete this;
604     return 1;
605   }
606   return 0;
607 }
608
609 void StorageActionLmm::cancel()
610 {
611   setState(SURF_ACTION_FAILED);
612   return;
613 }
614
615 void StorageActionLmm::suspend()
616 {
617   XBT_IN("(%p)", this);
618   if (m_suspended != 2) {
619     lmm_update_variable_weight(p_model->p_maxminSystem,
620                                p_variable,
621                                0.0);
622     m_suspended = 1;
623   }
624   XBT_OUT();
625 }
626
627 void StorageActionLmm::resume()
628 {
629   THROW_UNIMPLEMENTED;
630 }
631
632 bool StorageActionLmm::isSuspended()
633 {
634   return m_suspended == 1;
635 }
636
637 void StorageActionLmm::setMaxDuration(double duration)
638 {
639   THROW_UNIMPLEMENTED;
640 }
641
642 void StorageActionLmm::setPriority(double priority)
643 {
644   THROW_UNIMPLEMENTED;
645 }
646