Logo AND Algorithmique Numérique Distribuée

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