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 static int storage_selective_update = 0;
22 static xbt_swag_t storage_running_action_set_that_does_not_need_being_checked = NULL;
28 static XBT_INLINE void routing_storage_type_free(void *r)
30 storage_type_t stype = (storage_type_t) r;
34 xbt_dict_free(&(stype->properties));
38 static XBT_INLINE void surf_storage_resource_free(void *r)
40 // specific to storage
41 StoragePtr storage = (StoragePtr) r;
42 xbt_dict_free(&storage->p_content);
43 xbt_dynar_free(&storage->p_writeActions);
48 static XBT_INLINE void routing_storage_host_free(void *r)
50 xbt_dynar_t dyn = (xbt_dynar_t) r;
54 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
56 void* stype = xbt_lib_get_or_null(storage_type_lib,
58 ROUTING_STORAGE_TYPE_LEVEL);
59 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
61 // if storage content is not specified use the content of storage_type if exist
62 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
63 storage->content = ((storage_type_t) stype)->content;
64 XBT_DEBUG("For disk '%s' content is empty, use the content of storage type '%s'",storage->id,((storage_type_t) stype)->type_id);
67 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",
69 ((storage_type_t) stype)->model,
70 ((storage_type_t) stype)->type_id,
72 ((storage_type_t) stype)->properties);
74 surf_storage_model->createResource(storage->id, ((storage_type_t) stype)->model,
75 ((storage_type_t) stype)->type_id,
79 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
81 XBT_DEBUG("parse_mstorage_init");
84 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
86 XBT_DEBUG("parse_storage_type_init");
89 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
91 XBT_DEBUG("parse_mount_init");
94 static void storage_parse_storage(sg_platf_storage_cbarg_t storage)
96 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
97 "Reading a storage, processing unit \"%s\" already exists", storage->id);
99 // Verification of an existing type_id
101 void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
103 xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
105 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
110 xbt_lib_set(storage_lib,
112 ROUTING_STORAGE_LEVEL,
113 (void *) xbt_strdup(storage->type_id));
116 static xbt_dict_t parse_storage_content(char *filename, size_t *used_size)
119 if ((!filename) || (strcmp(filename, "") == 0))
122 xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
125 file = surf_fopen(filename, "r");
126 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
127 xbt_str_join(surf_path, ":"));
136 while ((read = xbt_getline(&line, &len, file)) != -1) {
138 if(sscanf(line,"%s %zu",path, &size)==2) {
140 xbt_dict_set(parse_content,path,(void*) size,NULL);
142 xbt_die("Be sure of passing a good format for content file.\n");
148 return parse_content;
151 static void storage_parse_storage_type(sg_platf_storage_type_cbarg_t storage_type)
153 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
154 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
156 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
157 stype->model = xbt_strdup(storage_type->model);
158 stype->properties = storage_type->properties;
159 stype->content = xbt_strdup(storage_type->content);
160 stype->type_id = xbt_strdup(storage_type->id);
161 stype->size = storage_type->size * 1000000000; /* storage_type->size is in Gbytes and stype->sizeis in bytes */
163 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s' content '%s'",
166 storage_type->content);
168 xbt_lib_set(storage_type_lib,
170 ROUTING_STORAGE_TYPE_LEVEL,
173 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t mstorage)
176 // mount_t mnt = xbt_new0(s_mount_t, 1);
177 // mnt->id = xbt_strdup(mstorage->type_id);
178 // mnt->name = xbt_strdup(mstorage->name);
181 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
182 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
184 // xbt_dynar_push(mount_list,(void *) mnt);
188 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
191 static void mount_free(void *p)
193 mount_t mnt = (mount_t) p;
197 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
199 // Verification of an existing storage
201 void* storage = xbt_lib_get_or_null(storage_lib, mount->id,ROUTING_STORAGE_LEVEL);
203 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->id);
205 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->id, mount->name);
208 mnt.id = surf_storage_resource_priv(surf_storage_resource_by_name(mount->id));
209 mnt.name = xbt_strdup(mount->name);
212 //FIXME:XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
213 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
215 xbt_dynar_push(mount_list,&mnt);
218 static void storage_define_callbacks()
220 sg_platf_storage_add_cb(parse_storage_init);
221 sg_platf_storage_type_add_cb(parse_storage_type_init);
222 sg_platf_mstorage_add_cb(parse_mstorage_init);
223 sg_platf_mount_add_cb(parse_mount_init);
226 void storage_register_callbacks() {
228 ROUTING_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,xbt_free);
229 ROUTING_STORAGE_HOST_LEVEL = xbt_lib_add_level(storage_lib, routing_storage_host_free);
230 ROUTING_STORAGE_TYPE_LEVEL = xbt_lib_add_level(storage_type_lib, routing_storage_type_free);
231 SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, surf_storage_resource_free);
233 sg_platf_storage_add_cb(storage_parse_storage);
234 sg_platf_mstorage_add_cb(storage_parse_mstorage);
235 sg_platf_storage_type_add_cb(storage_parse_storage_type);
236 sg_platf_mount_add_cb(storage_parse_mount);
243 void surf_storage_model_init_default(void)
245 surf_storage_model = new StorageModel();
246 storage_define_callbacks();
247 xbt_dynar_push(model_list, &surf_storage_model);
250 StorageModel::StorageModel() : Model("Storage") {
251 StorageActionLmm action;
253 XBT_DEBUG("surf_storage_model_init_internal");
255 storage_running_action_set_that_does_not_need_being_checked =
256 xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
258 if (!p_maxminSystem) {
259 p_maxminSystem = lmm_system_new(storage_selective_update);
264 StorageModel::~StorageModel(){
265 lmm_system_free(p_maxminSystem);
267 surf_storage_model = NULL;
269 xbt_dynar_free(&storage_list);
271 xbt_swag_free(storage_running_action_set_that_does_not_need_being_checked);
272 storage_running_action_set_that_does_not_need_being_checked = NULL;
275 StoragePtr StorageModel::createResource(const char* id, const char* model, const char* type_id, const char* content_name)
278 xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
279 "Storage '%s' declared several times in the platform file",
282 storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
284 double Bread = atof((char*)xbt_dict_get(storage_type->properties, "Bread"));
285 double Bwrite = atof((char*)xbt_dict_get(storage_type->properties, "Bwrite"));
286 double Bconnection = atof((char*)xbt_dict_get(storage_type->properties, "Bconnection"));
288 StoragePtr storage = new StorageLmm(this, NULL, NULL, p_maxminSystem,
289 Bread, Bwrite, Bconnection, (char *)content_name, storage_type->size);
291 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
293 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",
297 storage_type->properties,
300 if(!storage_list) storage_list=xbt_dynar_new(sizeof(char *),NULL);
301 xbt_dynar_push(storage_list, &storage);
306 double StorageModel::shareResources(double now)
308 XBT_DEBUG("storage_share_resources %f", now);
311 StorageActionLmmPtr write_action;
313 double min_completion = shareResourcesMaxMin(p_runningActionSet,
314 p_maxminSystem, lmm_solve);
318 xbt_dynar_foreach(storage_list,i,storage)
321 // Foreach write action on disk
322 xbt_dynar_foreach(storage->p_writeActions, j, write_action)
324 rate += lmm_variable_getvalue(write_action->p_variable);
327 min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
330 return min_completion;
333 void StorageModel::updateActionsState(double now, double delta)
335 void *_action, *_next_action;
336 StorageActionLmmPtr action = NULL;
338 // Update the disk usage
339 // Update the file size
340 // For each action of type write
341 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
342 action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_action));
343 if(action->m_type == WRITE)
345 double rate = lmm_variable_getvalue(action->p_variable);
346 /* Hack to avoid rounding differences between x86 and x86_64
347 * (note that the next sizes are of type size_t). */
348 long incr = delta * rate + MAXMIN_PRECISION;
349 action->p_storage->m_usedSize += incr; // disk usage
350 action->p_file->size += incr; // file size
354 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
355 action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_action));
357 double_update(&action->m_remains,
358 lmm_variable_getvalue(action->p_variable) * delta);
360 if (action->m_maxDuration != NO_MAX_DURATION)
361 double_update(&action->m_maxDuration, delta);
363 if(action->m_remains > 0 &&
364 lmm_get_variable_weight(action->p_variable) > 0 &&
365 action->p_storage->m_usedSize == action->p_storage->m_size)
367 action->m_finish = surf_get_clock();
368 action->setState(SURF_ACTION_FAILED);
369 } else if ((action->m_remains <= 0) &&
370 (lmm_get_variable_weight(action->p_variable) > 0))
372 action->m_finish = surf_get_clock();
373 action->setState(SURF_ACTION_DONE);
374 } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
375 (action->m_maxDuration <= 0))
377 action->m_finish = surf_get_clock();
378 action->setState(SURF_ACTION_DONE);
385 xbt_dict_t Storage::parseContent(char *filename)
388 if ((!filename) || (strcmp(filename, "") == 0))
391 xbt_dict_t parse_content = xbt_dict_new_homogeneous(NULL);
394 file = surf_fopen(filename, "r");
395 xbt_assert(file != NULL, "Cannot open file '%s' (path=%s)", filename,
396 xbt_str_join(surf_path, ":"));
405 while ((read = xbt_getline(&line, &len, file)) != -1) {
407 if(sscanf(line,"%s %zu",path, &size)==2) {
409 xbt_dict_set(parse_content,path,(void*) size,NULL);
411 xbt_die("Be sure of passing a good format for content file.\n");
417 return parse_content;
424 Storage::Storage(StorageModelPtr model, const char* name, xbt_dict_t properties)
425 : Resource(model, name, properties)
427 p_writeActions = xbt_dynar_new(sizeof(char *),NULL);
430 StorageLmm::StorageLmm(StorageModelPtr model, const char* name, xbt_dict_t properties,
431 lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
432 char *content_name, size_t size)
433 : Resource(model, name, properties), ResourceLmm(), Storage(model, name, properties) {
434 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%lu'", bconnection, bread, bwrite, ((unsigned long)size));
436 p_stateCurrent = SURF_RESOURCE_ON;
440 p_content = parseContent(content_name);
441 p_constraint = lmm_constraint_new(maxminSystem, this, bconnection);
442 p_constraintRead = lmm_constraint_new(maxminSystem, this, bread);
443 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, size, 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, size, 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 : Action(model, cost, failed), ActionLmm(model, cost, failed), StorageAction(model, cost, failed, storage, type) {
569 XBT_IN("(%s,%zu", storage->m_name, cost);
570 p_variable = lmm_variable_new(p_model->p_maxminSystem, this, 1.0, -1.0 , 3);
572 // Must be less than the max bandwidth for all actions
573 lmm_expand(p_model->p_maxminSystem, storage->p_constraint, p_variable, 1.0);
581 lmm_expand(p_model->p_maxminSystem, storage->p_constraintRead,
585 lmm_expand(p_model->p_maxminSystem, storage->p_constraintWrite,
587 xbt_dynar_push(storage->p_writeActions, static_cast<ActionPtr>(this));
593 int StorageActionLmm::unref()
597 xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
599 lmm_variable_free(p_model->p_maxminSystem, p_variable);
601 xbt_free(p_category);
609 void StorageActionLmm::cancel()
611 setState(SURF_ACTION_FAILED);
615 void StorageActionLmm::suspend()
617 XBT_IN("(%p)", this);
618 if (m_suspended != 2) {
619 lmm_update_variable_weight(p_model->p_maxminSystem,
627 void StorageActionLmm::resume()
632 bool StorageActionLmm::isSuspended()
634 return m_suspended == 1;
637 void StorageActionLmm::setMaxDuration(double duration)
642 void StorageActionLmm::setPriority(double priority)