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;
29 static XBT_INLINE void routing_storage_type_free(void *r)
31 storage_type_t stype = (storage_type_t) r;
35 xbt_dict_free(&(stype->properties));
39 static XBT_INLINE void surf_storage_resource_free(void *r)
41 // specific to storage
42 StoragePtr storage = (StoragePtr) r;
43 xbt_dict_free(&storage->p_content);
44 xbt_dynar_free(&storage->p_writeActions);
49 static XBT_INLINE void routing_storage_host_free(void *r)
51 xbt_dynar_t dyn = (xbt_dynar_t) r;
55 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
57 void* stype = xbt_lib_get_or_null(storage_type_lib,
59 ROUTING_STORAGE_TYPE_LEVEL);
60 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
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);
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",
70 ((storage_type_t) stype)->model,
71 ((storage_type_t) stype)->type_id,
73 ((storage_type_t) stype)->properties);
75 surf_storage_model->createResource(storage->id, ((storage_type_t) stype)->model,
76 ((storage_type_t) stype)->type_id,
80 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
82 XBT_DEBUG("parse_mstorage_init");
85 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
87 XBT_DEBUG("parse_storage_type_init");
90 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
92 XBT_DEBUG("parse_mount_init");
95 static void storage_parse_storage(sg_platf_storage_cbarg_t storage)
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);
100 // Verification of an existing type_id
102 void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
104 xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
106 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
111 xbt_lib_set(storage_lib,
113 ROUTING_STORAGE_LEVEL,
114 (void *) xbt_strdup(storage->type_id));
117 static xbt_dict_t parse_storage_content(char *filename, size_t *used_size)
120 if ((!filename) || (strcmp(filename, "") == 0))
123 xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
126 file = surf_fopen(filename, "r");
127 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
128 xbt_str_join(surf_path, ":"));
137 while ((read = xbt_getline(&line, &len, file)) != -1) {
139 if(sscanf(line,"%s %zu",path, &size)==2) {
141 xbt_dict_set(parse_content,path,(void*) size,NULL);
143 xbt_die("Be sure of passing a good format for content file.\n");
149 return parse_content;
152 static void storage_parse_storage_type(sg_platf_storage_type_cbarg_t storage_type)
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);
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 */
164 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s' content '%s'",
167 storage_type->content);
169 xbt_lib_set(storage_type_lib,
171 ROUTING_STORAGE_TYPE_LEVEL,
174 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t mstorage)
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);
182 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
183 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
185 // xbt_dynar_push(mount_list,(void *) mnt);
189 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
192 static void mount_free(void *p)
194 mount_t mnt = (mount_t) p;
198 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
200 // Verification of an existing storage
202 void* storage = xbt_lib_get_or_null(storage_lib, mount->id,ROUTING_STORAGE_LEVEL);
204 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->id);
206 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->id, mount->name);
209 mnt.id = surf_storage_resource_priv(surf_storage_resource_by_name(mount->id));
210 mnt.name = xbt_strdup(mount->name);
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);
216 xbt_dynar_push(mount_list,&mnt);
219 static void storage_define_callbacks()
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);
227 void storage_register_callbacks() {
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);
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);
244 void surf_storage_model_init_default(void)
246 surf_storage_model = new StorageModel();
247 storage_define_callbacks();
248 xbt_dynar_push(model_list, &surf_storage_model);
251 StorageModel::StorageModel() : Model("Storage"){
252 StorageActionLmm action;
254 XBT_DEBUG("surf_storage_model_init_internal");
256 storage_running_action_set_that_does_not_need_being_checked =
257 xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
259 if (!storage_maxmin_system) {
260 storage_maxmin_system = lmm_system_new(storage_selective_update);
265 StorageModel::~StorageModel(){
266 lmm_system_free(storage_maxmin_system);
267 storage_maxmin_system = NULL;
269 surf_storage_model = NULL;
271 xbt_dynar_free(&storage_list);
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;
277 StoragePtr StorageModel::createResource(const char* id, const char* model, const char* type_id, const char* content_name)
280 xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
281 "Storage '%s' declared several times in the platform file",
284 storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
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"));
290 StoragePtr storage = new StorageLmm(this, NULL, NULL, p_maxminSystem,
291 Bread, Bwrite, Bconnection,
292 parseContent((char*)content_name, &(storage->m_usedSize)),
295 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
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",
301 storage_type->properties,
304 if(!storage_list) storage_list=xbt_dynar_new(sizeof(char *),NULL);
305 xbt_dynar_push(storage_list, &storage);
310 double StorageModel::shareResources(double now)
312 XBT_DEBUG("storage_share_resources %f", now);
313 StorageActionLmm action;
316 StorageActionLmmPtr write_action;
318 double min_completion = shareResourcesMaxMin(p_runningActionSet,
319 xbt_swag_offset(action, p_variable),
320 storage_maxmin_system, lmm_solve);
324 xbt_dynar_foreach(storage_list,i,storage)
327 // Foreach write action on disk
328 xbt_dynar_foreach(storage->p_writeActions, j, write_action)
330 rate += lmm_variable_getvalue(write_action->p_variable);
333 min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
336 return min_completion;
339 void StorageModel::updateActionsState(double now, double delta)
341 void *_action, *_next_action;
342 StorageActionLmmPtr action = NULL;
344 // Update the disk usage
345 // Update the file size
346 // For each action of type write
347 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
348 action = (StorageActionLmmPtr) _action;
349 if(action->m_type == WRITE)
351 double rate = lmm_variable_getvalue(action->p_variable);
352 /* Hack to avoid rounding differences between x86 and x86_64
353 * (note that the next sizes are of type size_t). */
354 long incr = delta * rate + MAXMIN_PRECISION;
355 action->p_storage->m_usedSize += incr; // disk usage
356 action->p_file->size += incr; // file size
360 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
361 action = (StorageActionLmmPtr) _action;
363 double_update(&action->m_remains,
364 lmm_variable_getvalue(action->p_variable) * delta);
366 if (action->m_maxDuration != NO_MAX_DURATION)
367 double_update(&action->m_maxDuration, delta);
369 if(action->m_remains > 0 &&
370 lmm_get_variable_weight(action->p_variable) > 0 &&
371 action->p_storage->m_usedSize == action->p_storage->m_size)
373 action->m_finish = surf_get_clock();
374 action->setState(SURF_ACTION_FAILED);
375 } else if ((action->m_remains <= 0) &&
376 (lmm_get_variable_weight(action->p_variable) > 0))
378 action->m_finish = surf_get_clock();
379 action->setState(SURF_ACTION_DONE);
380 } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
381 (action->m_maxDuration <= 0))
383 action->m_finish = surf_get_clock();
384 action->setState(SURF_ACTION_DONE);
391 xbt_dict_t StorageModel::parseContent(char *filename, size_t *used_size)
394 if ((!filename) || (strcmp(filename, "") == 0))
397 xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
400 file = surf_fopen(filename, "r");
401 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
402 xbt_str_join(surf_path, ":"));
411 while ((read = xbt_getline(&line, &len, file)) != -1) {
413 if(sscanf(line,"%s %zu",path, &size)==2) {
415 xbt_dict_set(parse_content,path,(void*) size,NULL);
417 xbt_die("Be sure of passing a good format for content file.\n");
423 return parse_content;
430 Storage::Storage(StorageModelPtr model, const char* name, xbt_dict_t properties) {
431 p_writeActions = xbt_dynar_new(sizeof(char *),NULL);
434 StorageLmm::StorageLmm(StorageModelPtr model, const char* name, xbt_dict_t properties,
435 lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
436 xbt_dict_t content, size_t size)
437 : ResourceLmm(), Storage(model, name, properties) {
438 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%lu'", bconnection, bread, bwrite, ((unsigned long)size));
440 p_constraint = lmm_constraint_new(maxminSystem, this, bconnection);
441 p_constraintRead = lmm_constraint_new(maxminSystem, this, bread);
442 p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
447 bool Storage::isUsed()
453 void Storage::updateState(tmgr_trace_event_t event_type, double value, double date)
458 StorageActionPtr StorageLmm::ls(const char* path)
460 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, LS);
462 action->p_lsDict = NULL;
463 xbt_dict_t ls_dict = xbt_dict_new();
467 xbt_dict_cursor_t cursor = NULL;
469 xbt_dynar_t dyn = NULL;
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)];
478 // Split file with '/'
479 dyn = xbt_str_split(file,"/");
480 file = xbt_dynar_get_as(dyn,0,char*);
483 if(xbt_dynar_length(dyn) == 1){
484 xbt_dict_set(ls_dict,file,&size,NULL);
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);
493 xbt_dynar_free(&dyn);
497 action->p_lsDict = ls_dict;
501 StorageActionPtr StorageLmm::open(const char* mount, const char* path)
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
507 xbt_dict_set(p_content, path, &size, NULL);
508 XBT_DEBUG("File '%s' was not found, file created.",path);
510 surf_file_t file = xbt_new0(s_surf_file_t,1);
511 file->name = xbt_strdup(path);
513 file->storage = xbt_strdup(mount);
515 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, OPEN);
516 action->p_file = file;
520 StorageActionPtr StorageLmm::close(surf_file_t fd)
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;
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();
536 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, CLOSE);
540 StorageActionPtr StorageLmm::read(void* ptr, size_t size, surf_file_t fd)
544 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, READ);
548 StorageActionPtr StorageLmm::write(const void* ptr, size_t size, surf_file_t fd)
550 char *filename = fd->name;
551 XBT_DEBUG("\tWrite file '%s' size '%zu/%zu'",filename,size,fd->size);
553 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, WRITE);
556 // If the storage is full
557 if(m_usedSize==m_size) {
558 action->setState(SURF_ACTION_FAILED);
567 StorageActionLmm::StorageActionLmm(ModelPtr model, double cost, bool failed, StorageLmmPtr storage, e_surf_action_storage_type_t type)
568 : ActionLmm(model, cost, failed), StorageAction(model, cost, failed, storage, type) {
569 XBT_IN("(%s,%zu", storage->m_name, cost);
570 // Must be less than the max bandwidth for all actions
571 lmm_expand(storage_maxmin_system, storage->p_constraint, p_variable, 1.0);
579 lmm_expand(storage_maxmin_system, storage->p_constraintRead,
583 lmm_expand(storage_maxmin_system, storage->p_constraintWrite,
585 xbt_dynar_push(storage->p_writeActions,this);
591 int StorageActionLmm::unref()
595 xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
597 lmm_variable_free(storage_maxmin_system, p_variable);
599 xbt_free(p_category);
607 void StorageActionLmm::cancel()
609 setState(SURF_ACTION_FAILED);
613 void StorageActionLmm::suspend()
615 XBT_IN("(%p)", this);
616 if (m_suspended != 2) {
617 lmm_update_variable_weight(storage_maxmin_system,
625 void StorageActionLmm::resume()
630 bool StorageActionLmm::isSuspended()
632 return m_suspended == 1;
635 void StorageActionLmm::setMaxDuration(double duration)
640 void StorageActionLmm::setPriority(double priority)