2 #include "surf_private.h"
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf,
6 "Logging specific to the SURF storage module");
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
16 static xbt_dynar_t storage_list;
18 xbt_dynar_t mount_list = NULL; /* temporary store of current mount storage */
19 StorageModelPtr surf_storage_model = NULL;
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;
25 void storage_register_callbacks() {
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);
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);*/
42 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
44 void* stype = xbt_lib_get_or_null(storage_type_lib,
46 ROUTING_STORAGE_TYPE_LEVEL);
47 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
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);
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",
57 ((storage_type_t) stype)->model,
58 ((storage_type_t) stype)->type_id,
60 ((storage_type_t) stype)->properties);
62 surf_storage_model->createResource(storage->id, ((storage_type_t) stype)->model,
63 ((storage_type_t) stype)->type_id,
67 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
69 XBT_DEBUG("parse_mstorage_init");
72 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
74 XBT_DEBUG("parse_storage_type_init");
77 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
79 XBT_DEBUG("parse_mount_init");
82 static void storage_parse_storage(sg_platf_storage_cbarg_t storage)
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);
87 // Verification of an existing type_id
89 void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
91 xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
93 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
98 xbt_lib_set(storage_lib,
100 ROUTING_STORAGE_LEVEL,
101 (void *) xbt_strdup(storage->type_id));
104 static xbt_dict_t parse_storage_content(char *filename, size_t *used_size)
107 if ((!filename) || (strcmp(filename, "") == 0))
110 xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
113 file = surf_fopen(filename, "r");
114 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
115 xbt_str_join(surf_path, ":"));
124 while ((read = xbt_getline(&line, &len, file)) != -1) {
126 if(sscanf(line,"%s %zu",path, &size)==2) {
128 xbt_dict_set(parse_content,path,(void*) size,NULL);
130 xbt_die("Be sure of passing a good format for content file.\n");
136 return parse_content;
139 static void storage_parse_storage_type(sg_platf_storage_type_cbarg_t storage_type)
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);
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 */
151 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s' content '%s'",
154 storage_type->content);
156 xbt_lib_set(storage_type_lib,
158 ROUTING_STORAGE_TYPE_LEVEL,
161 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t mstorage)
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);
169 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
170 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
172 // xbt_dynar_push(mount_list,(void *) mnt);
176 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
179 static void mount_free(void *p)
181 mount_t mnt = (mount_t) p;
185 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
187 // Verification of an existing storage
189 void* storage = xbt_lib_get_or_null(storage_lib, mount->id,ROUTING_STORAGE_LEVEL);
191 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->id);
193 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->id, mount->name);
196 mnt.id = surf_storage_resource_priv(surf_storage_resource_by_name(mount->id));
197 mnt.name = xbt_strdup(mount->name);
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);
203 xbt_dynar_push(mount_list,&mnt);
206 static XBT_INLINE void routing_storage_type_free(void *r)
208 storage_type_t stype = (storage_type_t) r;
210 free(stype->type_id);
211 free(stype->content);
212 xbt_dict_free(&(stype->properties));
216 static XBT_INLINE void surf_storage_resource_free(void *r)
218 // specific to storage
219 StoragePtr storage = (StoragePtr) r;
220 xbt_dict_free(&storage->p_content);
221 xbt_dynar_free(&storage->p_writeActions);
226 static XBT_INLINE void routing_storage_host_free(void *r)
228 xbt_dynar_t dyn = (xbt_dynar_t) r;
229 xbt_dynar_free(&dyn);
236 void surf_storage_model_init_default(void)
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));
246 if (!storage_maxmin_system) {
247 storage_maxmin_system = lmm_system_new(storage_selective_update);
249 xbt_dynar_push(model_list, this);
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);
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);
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);
268 StorageModel::~StorageModel(){
269 lmm_system_free(storage_maxmin_system);
270 storage_maxmin_system = NULL;
272 surf_storage_model = NULL;
274 xbt_dynar_free(&storage_list);
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;
280 StoragePtr StorageModel::createResource(const char* id, const char* model, const char* type_id, const char* content_name)
283 xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
284 "Storage '%s' declared several times in the platform file",
287 StoragePtr storage ;//= new Storage();
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;
301 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
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",
307 storage_type->properties,
310 if(!storage_list) storage_list=xbt_dynar_new(sizeof(char *),NULL);
311 xbt_dynar_push(storage_list, &storage);
316 double StorageModel::shareResources(double now)
318 XBT_DEBUG("storage_share_resources %f", now);
319 StorageActionLmm action;
322 StorageActionLmmPtr write_action;
324 double min_completion = shareResourcesMaxMin(p_runningActionSet,
325 xbt_swag_offset(action, p_variable),
326 storage_maxmin_system, lmm_solve);
330 xbt_dynar_foreach(storage_list,i,storage)
333 // Foreach write action on disk
334 xbt_dynar_foreach(storage->p_writeActions, j, write_action)
336 rate += lmm_variable_getvalue(write_action->p_variable);
339 min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
342 return min_completion;
345 void StorageModel::updateActionsState(double now, double delta)
347 void *_action, *_next_action;
348 StorageActionLmmPtr action = NULL;
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)
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
366 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
367 action = (StorageActionLmmPtr) _action;
369 double_update(&action->m_remains,
370 lmm_variable_getvalue(action->p_variable) * delta);
372 if (action->m_maxDuration != NO_MAX_DURATION)
373 double_update(&action->m_maxDuration, delta);
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)
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))
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))
389 action->m_finish = surf_get_clock();
390 action->setState(SURF_ACTION_DONE);
397 xbt_dict_t StorageModel::parseContent(char *filename, size_t *used_size)
400 if ((!filename) || (strcmp(filename, "") == 0))
403 xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
406 file = surf_fopen(filename, "r");
407 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
408 xbt_str_join(surf_path, ":"));
417 while ((read = xbt_getline(&line, &len, file)) != -1) {
419 if(sscanf(line,"%s %zu",path, &size)==2) {
421 xbt_dict_set(parse_content,path,(void*) size,NULL);
423 xbt_die("Be sure of passing a good format for content file.\n");
429 return parse_content;
436 Storage::Storage(StorageModelPtr model, const char* name, xbt_dict_t properties) {
437 p_writeActions = xbt_dynar_new(sizeof(char *),NULL);
440 bool Storage::isUsed()
446 void Storage::updateState(tmgr_trace_event_t event_type, double value, double date)
451 StorageActionLmmPtr Storage::ls(const char* path)
453 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, m_stateCurrent != SURF_RESOURCE_ON, this, LS);
455 action->p_lsDict = NULL;
456 xbt_dict_t ls_dict = xbt_dict_new();
460 xbt_dict_cursor_t cursor = NULL;
462 xbt_dynar_t dyn = NULL;
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)];
471 // Split file with '/'
472 dyn = xbt_str_split(file,"/");
473 file = xbt_dynar_get_as(dyn,0,char*);
476 if(xbt_dynar_length(dyn) == 1){
477 xbt_dict_set(ls_dict,file,&size,NULL);
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);
486 xbt_dynar_free(&dyn);
490 action->p_lsDict = ls_dict;
494 StorageActionLmmPtr Storage::open(const char* mount, const char* path)
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
500 xbt_dict_set(p_content, path, &size, NULL);
501 XBT_DEBUG("File '%s' was not found, file created.",path);
503 surf_file_t file = xbt_new0(s_surf_file_t,1);
504 file->name = xbt_strdup(path);
506 file->storage = xbt_strdup(mount);
508 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, m_stateCurrent != SURF_RESOURCE_ON, this, OPEN);
509 action->p_file = file;
513 StorageActionLmmPtr Storage::close(surf_file_t fd)
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;
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();
529 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, m_stateCurrent != SURF_RESOURCE_ON, this, CLOSE);
533 StorageActionLmmPtr Storage::read(void* ptr, size_t size, surf_file_t fd)
537 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, m_stateCurrent != SURF_RESOURCE_ON, this, READ);
541 StorageActionLmmPtr Storage::write(const void* ptr, size_t size, surf_file_t fd)
543 char *filename = fd->name;
544 XBT_DEBUG("\tWrite file '%s' size '%zu/%zu'",filename,size,fd->size);
546 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, m_stateCurrent != SURF_RESOURCE_ON, this, WRITE);
549 // If the storage is full
550 if(m_usedSize==m_size) {
551 action->setState(SURF_ACTION_FAILED);
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);
572 lmm_expand(storage_maxmin_system, storage->p_constraintRead,
576 lmm_expand(storage_maxmin_system, storage->p_constraintWrite,
578 xbt_dynar_push(storage->p_writeActions,this);
584 int StorageActionLmm::unref()
588 xbt_swag_remove(this, p_stateSet);
590 lmm_variable_free(storage_maxmin_system, p_variable);
592 xbt_free(p_category);
600 void StorageActionLmm::cancel()
602 setState(SURF_ACTION_FAILED);
608 void StorageActionLmm::suspend()
610 XBT_IN("(%p)", this);
611 if (m_suspended != 2) {
612 lmm_update_variable_weight(storage_maxmin_system,
620 void StorageActionLmm::resume()
625 bool StorageActionLmm::isSuspended()
627 return m_suspended == 1;
630 void StorageActionLmm::setMaxDuration(double duration)
635 void StorageActionLmm::setPriority(double priority)