2 #include "surf_private.h"
4 #define __STDC_FORMAT_MACROS
8 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf,
9 "Logging specific to the SURF storage module");
12 xbt_lib_t storage_lib;
13 int ROUTING_STORAGE_LEVEL; //Routing for storagelevel
14 int ROUTING_STORAGE_HOST_LEVEL;
15 int SURF_STORAGE_LEVEL;
16 xbt_lib_t storage_type_lib;
17 int ROUTING_STORAGE_TYPE_LEVEL; //Routing for storage_type level
19 static xbt_dynar_t storage_list;
21 xbt_dynar_t mount_list = NULL; /* temporary store of current mount storage */
22 StorageModelPtr surf_storage_model = NULL;
24 static int storage_selective_update = 0;
25 static xbt_swag_t storage_running_action_set_that_does_not_need_being_checked = NULL;
31 static XBT_INLINE void routing_storage_type_free(void *r)
33 storage_type_t stype = (storage_type_t) r;
37 xbt_dict_free(&(stype->properties));
38 xbt_dict_free(&(stype->properties));
42 static XBT_INLINE void surf_storage_resource_free(void *r)
44 // specific to storage
45 StoragePtr storage = dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(r));
46 xbt_dict_free(&storage->p_content);
47 xbt_dynar_free(&storage->p_writeActions);
48 free(storage->p_typeId);
49 free(storage->p_contentType);
54 static XBT_INLINE void routing_storage_host_free(void *r)
56 xbt_dynar_t dyn = (xbt_dynar_t) r;
60 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
62 void* stype = xbt_lib_get_or_null(storage_type_lib,
64 ROUTING_STORAGE_TYPE_LEVEL);
65 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
67 // if storage content is not specified use the content of storage_type if exist
68 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
69 storage->content = ((storage_type_t) stype)->content;
70 storage->content_type = ((storage_type_t) stype)->content_type;
71 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
72 storage->id,((storage_type_t) stype)->content_type,
73 ((storage_type_t) stype)->type_id);
76 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
77 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
78 "\n\t\tproperties '%p'\n",
80 ((storage_type_t) stype)->model,
81 ((storage_type_t) stype)->type_id,
83 storage->content_type,
84 ((storage_type_t) stype)->properties);
86 surf_storage_model->createResource(storage->id, ((storage_type_t) stype)->model,
87 ((storage_type_t) stype)->type_id,
89 storage->content_type,
93 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
95 XBT_DEBUG("parse_mstorage_init");
98 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
100 XBT_DEBUG("parse_storage_type_init");
103 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
105 XBT_DEBUG("parse_mount_init");
108 static void storage_parse_storage(sg_platf_storage_cbarg_t storage)
110 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
111 "Reading a storage, processing unit \"%s\" already exists", storage->id);
113 // Verification of an existing type_id
115 void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
117 xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
119 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
124 xbt_lib_set(storage_lib,
126 ROUTING_STORAGE_LEVEL,
127 (void *) xbt_strdup(storage->type_id));
130 static xbt_dict_t parse_storage_content(char *filename, sg_storage_size_t *used_size)
133 if ((!filename) || (strcmp(filename, "") == 0))
136 xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free);
139 file = surf_fopen(filename, "r");
140 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
141 xbt_str_join(surf_path, ":"));
147 sg_storage_size_t size;
150 while ((read = xbt_getline(&line, &len, file)) != -1) {
152 if(sscanf(line,"%s %" SCNu64, path, &size) == 2) {
154 sg_storage_size_t *psize = xbt_new(sg_storage_size_t, 1);
156 xbt_dict_set(parse_content,path,psize,NULL);
158 xbt_die("Be sure of passing a good format for content file.\n");
164 return parse_content;
167 static void storage_parse_storage_type(sg_platf_storage_type_cbarg_t storage_type)
169 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
170 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
172 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
173 stype->model = xbt_strdup(storage_type->model);
174 stype->properties = storage_type->properties;
175 stype->content = xbt_strdup(storage_type->content);
176 stype->content_type = xbt_strdup(storage_type->content_type);
177 stype->type_id = xbt_strdup(storage_type->id);
178 stype->size = storage_type->size;
180 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
181 "content '%s', and content_type '%s'",
184 storage_type->content,
185 storage_type->content_type);
187 xbt_lib_set(storage_type_lib,
189 ROUTING_STORAGE_TYPE_LEVEL,
192 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t mstorage)
195 // mount_t mnt = xbt_new0(s_mount_t, 1);
196 // mnt->id = xbt_strdup(mstorage->type_id);
197 // mnt->name = xbt_strdup(mstorage->name);
200 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
201 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
203 // xbt_dynar_push(mount_list,(void *) mnt);
207 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
210 static void mount_free(void *p)
212 mount_t mnt = (mount_t) p;
216 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
218 // Verification of an existing storage
220 void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
222 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
224 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
227 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
228 mnt.name = xbt_strdup(mount->name);
231 //FIXME:XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
232 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
234 xbt_dynar_push(mount_list,&mnt);
237 static void storage_define_callbacks()
239 sg_platf_storage_add_cb(parse_storage_init);
240 sg_platf_storage_type_add_cb(parse_storage_type_init);
241 sg_platf_mstorage_add_cb(parse_mstorage_init);
242 sg_platf_mount_add_cb(parse_mount_init);
245 void storage_register_callbacks() {
247 ROUTING_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,xbt_free);
248 ROUTING_STORAGE_HOST_LEVEL = xbt_lib_add_level(storage_lib, routing_storage_host_free);
249 ROUTING_STORAGE_TYPE_LEVEL = xbt_lib_add_level(storage_type_lib, routing_storage_type_free);
250 SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, surf_storage_resource_free);
252 sg_platf_storage_add_cb(storage_parse_storage);
253 sg_platf_mstorage_add_cb(storage_parse_mstorage);
254 sg_platf_storage_type_add_cb(storage_parse_storage_type);
255 sg_platf_mount_add_cb(storage_parse_mount);
262 void surf_storage_model_init_default(void)
264 surf_storage_model = new StorageModel();
265 storage_define_callbacks();
266 xbt_dynar_push(model_list, &surf_storage_model);
269 StorageModel::StorageModel() : Model("Storage") {
270 StorageActionLmm action;
272 XBT_DEBUG("surf_storage_model_init_internal");
274 storage_running_action_set_that_does_not_need_being_checked =
275 xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
277 if (!p_maxminSystem) {
278 p_maxminSystem = lmm_system_new(storage_selective_update);
283 StorageModel::~StorageModel(){
284 lmm_system_free(p_maxminSystem);
286 surf_storage_model = NULL;
288 xbt_dynar_free(&storage_list);
290 xbt_swag_free(storage_running_action_set_that_does_not_need_being_checked);
291 storage_running_action_set_that_does_not_need_being_checked = NULL;
294 StoragePtr StorageModel::createResource(const char* id, const char* model, const char* type_id,
295 const char* content_name, const char* content_type, xbt_dict_t properties)
298 xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
299 "Storage '%s' declared several times in the platform file",
302 storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
304 double Bread = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bread"));
305 double Bwrite = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bwrite"));
306 double Bconnection = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bconnection"));
308 StoragePtr storage = new StorageLmm(this, id, properties, p_maxminSystem,
309 Bread, Bwrite, Bconnection,
310 type_id, (char *)content_name, xbt_strdup(content_type), storage_type->size);
312 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, static_cast<ResourcePtr>(storage));
314 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",
318 storage_type->properties,
322 storage_list = xbt_dynar_new(sizeof(char *),NULL);
323 xbt_dynar_push(storage_list, &storage);
328 double StorageModel::shareResources(double now)
330 XBT_DEBUG("storage_share_resources %f", now);
333 StorageActionLmmPtr write_action;
335 double min_completion = shareResourcesMaxMin(p_runningActionSet,
336 p_maxminSystem, lmm_solve);
340 xbt_dynar_foreach(storage_list,i,storage)
343 // Foreach write action on disk
344 xbt_dynar_foreach(storage->p_writeActions, j, write_action)
346 rate += lmm_variable_getvalue(write_action->p_variable);
349 min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
352 return min_completion;
355 void StorageModel::updateActionsState(double now, double delta)
357 void *_action, *_next_action;
358 StorageActionLmmPtr action = NULL;
360 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
361 action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_action));
362 if(action->m_type == WRITE)
364 // Update the disk usage
365 // Update the file size
366 // For each action of type write
367 double rate = lmm_variable_getvalue(action->p_variable);
368 /* Hack to avoid rounding differences between x86 and x86_64
369 * (note that the next sizes are of type sg_storage_size_t). */
370 long incr = delta * rate + MAXMIN_PRECISION;
371 action->p_storage->m_usedSize += incr; // disk usage
372 action->p_file->size += incr; // file size
374 sg_storage_size_t *psize = xbt_new(sg_storage_size_t,1);
375 *psize = action->p_file->size;
377 xbt_dict_t content_dict = action->p_storage->p_content;
378 xbt_dict_set(content_dict, action->p_file->name, psize, NULL);
381 double_update(&action->m_remains,
382 lmm_variable_getvalue(action->p_variable) * delta);
384 if (action->m_maxDuration != NO_MAX_DURATION)
385 double_update(&action->m_maxDuration, delta);
387 if(action->m_remains > 0 &&
388 lmm_get_variable_weight(action->p_variable) > 0 &&
389 action->p_storage->m_usedSize == action->p_storage->m_size)
391 action->m_finish = surf_get_clock();
392 action->setState(SURF_ACTION_FAILED);
393 } else if ((action->m_remains <= 0) &&
394 (lmm_get_variable_weight(action->p_variable) > 0))
396 action->m_finish = surf_get_clock();
397 action->setState(SURF_ACTION_DONE);
398 } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
399 (action->m_maxDuration <= 0))
401 action->m_finish = surf_get_clock();
402 action->setState(SURF_ACTION_DONE);
409 xbt_dict_t Storage::parseContent(char *filename)
412 if ((!filename) || (strcmp(filename, "") == 0))
415 xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free);
418 file = surf_fopen(filename, "r");
419 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
420 xbt_str_join(surf_path, ":"));
426 sg_storage_size_t size;
429 while ((read = xbt_getline(&line, &len, file)) != -1) {
431 if(sscanf(line,"%s %" SCNu64, path, &size) == 2) {
433 sg_storage_size_t *psize = xbt_new(sg_storage_size_t, 1);
435 xbt_dict_set(parse_content,path,psize,NULL);
437 xbt_die("Be sure of passing a good format for content file.\n");
443 return parse_content;
450 Storage::Storage(StorageModelPtr model, const char* name, xbt_dict_t properties)
451 : Resource(model, name, properties)
453 p_writeActions = xbt_dynar_new(sizeof(char *),NULL);
456 StorageLmm::StorageLmm(StorageModelPtr model, const char* name, xbt_dict_t properties,
457 lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
458 const char* type_id, char *content_name, char *content_type, size_t size)
459 : Resource(model, name, properties), ResourceLmm(), Storage(model, name, properties) {
460 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%lu'", bconnection, bread, bwrite, ((unsigned long)size));
462 p_stateCurrent = SURF_RESOURCE_ON;
466 p_content = parseContent(content_name);
467 p_contentType = content_type;
468 p_constraint = lmm_constraint_new(maxminSystem, this, bconnection);
469 p_constraintRead = lmm_constraint_new(maxminSystem, this, bread);
470 p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
472 p_typeId = xbt_strdup(type_id);
475 bool Storage::isUsed()
481 void Storage::updateState(tmgr_trace_event_t event_type, double value, double date)
486 StorageActionPtr StorageLmm::ls(const char* path)
488 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, LS);
490 action->p_lsDict = NULL;
491 xbt_dict_t ls_dict = xbt_dict_new_homogeneous(xbt_free);
494 sg_storage_size_t size = 0;
495 xbt_dict_cursor_t cursor = NULL;
497 xbt_dynar_t dyn = NULL;
500 // for each file in the storage content
501 xbt_dict_foreach(p_content,cursor,key,size){
502 // Search if file start with the prefix 'path'
503 if(xbt_str_start_with(key,path)){
504 file = &key[strlen(path)];
506 // Split file with '/'
507 dyn = xbt_str_split(file,"/");
508 file = xbt_dynar_get_as(dyn,0,char*);
511 if(xbt_dynar_length(dyn) == 1){
512 sg_storage_size_t *psize = xbt_new(sg_storage_size_t, 1);
514 xbt_dict_set(ls_dict, file, psize, NULL);
519 // if directory does not exist yet in the dictionary
520 if(!xbt_dict_get_or_null(ls_dict,file))
521 xbt_dict_set(ls_dict,file,NULL,NULL);
523 xbt_dynar_free(&dyn);
527 action->p_lsDict = ls_dict;
531 StorageActionPtr StorageLmm::open(const char* mount, const char* path)
533 XBT_DEBUG("\tOpen file '%s'",path);
534 sg_storage_size_t size, *psize;
535 psize = (sg_storage_size_t*) xbt_dict_get_or_null(p_content, path);
536 // if file does not exist create an empty file
540 psize = xbt_new(sg_storage_size_t,1);
543 xbt_dict_set(p_content, path, psize, NULL);
544 XBT_DEBUG("File '%s' was not found, file created.",path);
546 surf_file_t file = xbt_new0(s_surf_file_t,1);
547 file->name = xbt_strdup(path);
549 file->mount = xbt_strdup(mount);
551 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, OPEN);
552 action->p_file = file;
556 StorageActionPtr StorageLmm::close(surf_file_t fd)
558 char *filename = fd->name;
559 XBT_DEBUG("\tClose file '%s' size '%" PRIu64 "'", filename, fd->size);
560 // unref write actions from storage
561 StorageActionLmmPtr write_action;
563 xbt_dynar_foreach(p_writeActions, i, write_action) {
564 if ((write_action->p_file) == fd) {
565 xbt_dynar_cursor_rm(p_writeActions, &i);
566 write_action->unref();
572 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, CLOSE);
576 StorageActionPtr StorageLmm::read(surf_file_t fd, sg_storage_size_t size)
580 StorageActionLmmPtr action = new StorageActionLmm(p_model, size, p_stateCurrent != SURF_RESOURCE_ON, this, READ);
584 StorageActionPtr StorageLmm::write(surf_file_t fd, sg_storage_size_t size)
586 char *filename = fd->name;
587 XBT_DEBUG("\tWrite file '%s' size '%" PRIu64 "/%" PRIu64 "'",filename,size,fd->size);
589 StorageActionLmmPtr action = new StorageActionLmm(p_model, size, p_stateCurrent != SURF_RESOURCE_ON, this, WRITE);
592 // If the storage is full
593 if(m_usedSize==m_size) {
594 action->setState(SURF_ACTION_FAILED);
599 void StorageLmm::rename(const char *src, const char *dest)
601 sg_storage_size_t *psize, *new_psize;
602 psize = (sg_storage_size_t*) xbt_dict_get_or_null(p_content,src);
603 new_psize = xbt_new(sg_storage_size_t, 1);
605 if (psize){// src file exists
606 xbt_dict_remove(p_content, src);
607 xbt_dict_set(p_content, dest, new_psize,NULL);
608 XBT_DEBUG("Change file name from %s to %s, size '%" PRIu64 "'",src, dest, *psize);
611 XBT_DEBUG("File %s doesn't exist",src);
614 xbt_dict_t StorageLmm::getContent()
616 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
617 /*surf_action_t action = storage_action_execute(storage,0, LS);*/
619 xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
620 xbt_dict_cursor_t cursor = NULL;
622 sg_storage_size_t *psize;
624 xbt_dict_foreach(p_content, cursor, file, psize){
625 xbt_dict_set(content_dict,file,psize,NULL);
630 sg_storage_size_t StorageLmm::getSize(){
638 StorageActionLmm::StorageActionLmm(ModelPtr model, double cost, bool failed, StorageLmmPtr storage, e_surf_action_storage_type_t type)
639 : Action(model, cost, failed), ActionLmm(model, cost, failed), StorageAction(model, cost, failed, storage, type) {
640 XBT_IN("(%s,%" PRIu64, storage->m_name, cost);
641 p_variable = lmm_variable_new(p_model->p_maxminSystem, this, 1.0, -1.0 , 3);
643 // Must be less than the max bandwidth for all actions
644 lmm_expand(p_model->p_maxminSystem, storage->p_constraint, p_variable, 1.0);
652 lmm_expand(p_model->p_maxminSystem, storage->p_constraintRead,
656 lmm_expand(p_model->p_maxminSystem, storage->p_constraintWrite,
658 xbt_dynar_push(storage->p_writeActions, static_cast<ActionPtr>(this));
664 int StorageActionLmm::unref()
668 xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
670 lmm_variable_free(p_model->p_maxminSystem, p_variable);
672 xbt_free(p_category);
680 void StorageActionLmm::cancel()
682 setState(SURF_ACTION_FAILED);
686 void StorageActionLmm::suspend()
688 XBT_IN("(%p)", this);
689 if (m_suspended != 2) {
690 lmm_update_variable_weight(p_model->p_maxminSystem,
698 void StorageActionLmm::resume()
703 bool StorageActionLmm::isSuspended()
705 return m_suspended == 1;
708 void StorageActionLmm::setMaxDuration(double duration)
713 void StorageActionLmm::setPriority(double priority)