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);
315 StorageActionLmmPtr write_action;
317 double min_completion = shareResourcesMaxMin(p_runningActionSet,
318 storage_maxmin_system, lmm_solve);
322 xbt_dynar_foreach(storage_list,i,storage)
325 // Foreach write action on disk
326 xbt_dynar_foreach(storage->p_writeActions, j, write_action)
328 rate += lmm_variable_getvalue(write_action->p_variable);
331 min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
334 return min_completion;
337 void StorageModel::updateActionsState(double now, double delta)
339 void *_action, *_next_action;
340 StorageActionLmmPtr action = NULL;
342 // Update the disk usage
343 // Update the file size
344 // For each action of type write
345 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
346 action = (StorageActionLmmPtr) _action;
347 if(action->m_type == WRITE)
349 double rate = lmm_variable_getvalue(action->p_variable);
350 /* Hack to avoid rounding differences between x86 and x86_64
351 * (note that the next sizes are of type size_t). */
352 long incr = delta * rate + MAXMIN_PRECISION;
353 action->p_storage->m_usedSize += incr; // disk usage
354 action->p_file->size += incr; // file size
358 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
359 action = (StorageActionLmmPtr) _action;
361 double_update(&action->m_remains,
362 lmm_variable_getvalue(action->p_variable) * delta);
364 if (action->m_maxDuration != NO_MAX_DURATION)
365 double_update(&action->m_maxDuration, delta);
367 if(action->m_remains > 0 &&
368 lmm_get_variable_weight(action->p_variable) > 0 &&
369 action->p_storage->m_usedSize == action->p_storage->m_size)
371 action->m_finish = surf_get_clock();
372 action->setState(SURF_ACTION_FAILED);
373 } else if ((action->m_remains <= 0) &&
374 (lmm_get_variable_weight(action->p_variable) > 0))
376 action->m_finish = surf_get_clock();
377 action->setState(SURF_ACTION_DONE);
378 } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
379 (action->m_maxDuration <= 0))
381 action->m_finish = surf_get_clock();
382 action->setState(SURF_ACTION_DONE);
389 xbt_dict_t StorageModel::parseContent(char *filename, size_t *used_size)
392 if ((!filename) || (strcmp(filename, "") == 0))
395 xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
398 file = surf_fopen(filename, "r");
399 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
400 xbt_str_join(surf_path, ":"));
409 while ((read = xbt_getline(&line, &len, file)) != -1) {
411 if(sscanf(line,"%s %zu",path, &size)==2) {
413 xbt_dict_set(parse_content,path,(void*) size,NULL);
415 xbt_die("Be sure of passing a good format for content file.\n");
421 return parse_content;
428 Storage::Storage(StorageModelPtr model, const char* name, xbt_dict_t properties) {
429 p_writeActions = xbt_dynar_new(sizeof(char *),NULL);
432 StorageLmm::StorageLmm(StorageModelPtr model, const char* name, xbt_dict_t properties,
433 lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
434 xbt_dict_t content, size_t size)
435 : ResourceLmm(), Storage(model, name, properties) {
436 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%lu'", bconnection, bread, bwrite, ((unsigned long)size));
438 p_constraint = lmm_constraint_new(maxminSystem, this, bconnection);
439 p_constraintRead = lmm_constraint_new(maxminSystem, this, bread);
440 p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
445 bool Storage::isUsed()
451 void Storage::updateState(tmgr_trace_event_t event_type, double value, double date)
456 StorageActionPtr StorageLmm::ls(const char* path)
458 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, LS);
460 action->p_lsDict = NULL;
461 xbt_dict_t ls_dict = xbt_dict_new();
465 xbt_dict_cursor_t cursor = NULL;
467 xbt_dynar_t dyn = NULL;
470 // for each file in the storage content
471 xbt_dict_foreach(p_content,cursor,key,size){
472 // Search if file start with the prefix 'path'
473 if(xbt_str_start_with(key,path)){
474 file = &key[strlen(path)];
476 // Split file with '/'
477 dyn = xbt_str_split(file,"/");
478 file = xbt_dynar_get_as(dyn,0,char*);
481 if(xbt_dynar_length(dyn) == 1){
482 xbt_dict_set(ls_dict,file,&size,NULL);
487 // if directory does not exist yet in the dictionary
488 if(!xbt_dict_get_or_null(ls_dict,file))
489 xbt_dict_set(ls_dict,file,NULL,NULL);
491 xbt_dynar_free(&dyn);
495 action->p_lsDict = ls_dict;
499 StorageActionPtr StorageLmm::open(const char* mount, const char* path)
501 XBT_DEBUG("\tOpen file '%s'",path);
502 size_t size = (size_t) xbt_dict_get_or_null(p_content, path);
503 // if file does not exist create an empty file
505 xbt_dict_set(p_content, path, &size, NULL);
506 XBT_DEBUG("File '%s' was not found, file created.",path);
508 surf_file_t file = xbt_new0(s_surf_file_t,1);
509 file->name = xbt_strdup(path);
511 file->storage = xbt_strdup(mount);
513 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, OPEN);
514 action->p_file = file;
518 StorageActionPtr StorageLmm::close(surf_file_t fd)
520 char *filename = fd->name;
521 XBT_DEBUG("\tClose file '%s' size '%zu'", filename, fd->size);
522 // unref write actions from storage
523 StorageActionLmmPtr write_action;
525 xbt_dynar_foreach(p_writeActions, i, write_action) {
526 if ((write_action->p_file) == fd) {
527 xbt_dynar_cursor_rm(p_writeActions, &i);
528 write_action->unref();
534 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, CLOSE);
538 StorageActionPtr StorageLmm::read(void* ptr, size_t size, surf_file_t fd)
542 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, READ);
546 StorageActionPtr StorageLmm::write(const void* ptr, size_t size, surf_file_t fd)
548 char *filename = fd->name;
549 XBT_DEBUG("\tWrite file '%s' size '%zu/%zu'",filename,size,fd->size);
551 StorageActionLmmPtr action = new StorageActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, WRITE);
554 // If the storage is full
555 if(m_usedSize==m_size) {
556 action->setState(SURF_ACTION_FAILED);
565 StorageActionLmm::StorageActionLmm(ModelPtr model, double cost, bool failed, StorageLmmPtr storage, e_surf_action_storage_type_t type)
566 : ActionLmm(model, cost, failed), StorageAction(model, cost, failed, storage, type) {
567 XBT_IN("(%s,%zu", storage->m_name, cost);
568 // Must be less than the max bandwidth for all actions
569 lmm_expand(storage_maxmin_system, storage->p_constraint, p_variable, 1.0);
577 lmm_expand(storage_maxmin_system, storage->p_constraintRead,
581 lmm_expand(storage_maxmin_system, storage->p_constraintWrite,
583 xbt_dynar_push(storage->p_writeActions,this);
589 int StorageActionLmm::unref()
593 xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
595 lmm_variable_free(storage_maxmin_system, p_variable);
597 xbt_free(p_category);
605 void StorageActionLmm::cancel()
607 setState(SURF_ACTION_FAILED);
611 void StorageActionLmm::suspend()
613 XBT_IN("(%p)", this);
614 if (m_suspended != 2) {
615 lmm_update_variable_weight(storage_maxmin_system,
623 void StorageActionLmm::resume()
628 bool StorageActionLmm::isSuspended()
630 return m_suspended == 1;
633 void StorageActionLmm::setMaxDuration(double duration)
638 void StorageActionLmm::setPriority(double priority)