1 #include "storage_n11.hpp"
2 #include "surf_private.h"
4 #define __STDC_FORMAT_MACROS
6 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_storage);
9 static int storage_selective_update = 0;
10 static xbt_swag_t storage_running_action_set_that_does_not_need_being_checked = NULL;
16 static XBT_INLINE void routing_storage_type_free(void *r)
18 storage_type_t stype = (storage_type_t) r;
22 free(stype->content_type);
23 xbt_dict_free(&(stype->properties));
24 xbt_dict_free(&(stype->properties));
28 static XBT_INLINE void surf_storage_resource_free(void *r)
30 // specific to storage
31 StoragePtr storage = dynamic_cast<StoragePtr>(static_cast<ResourcePtr>(r));
32 xbt_dict_free(&storage->p_content);
33 xbt_dynar_free(&storage->p_writeActions);
34 free(storage->p_typeId);
35 free(storage->p_contentType);
40 static XBT_INLINE void routing_storage_host_free(void *r)
42 xbt_dynar_t dyn = (xbt_dynar_t) r;
46 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
48 void* stype = xbt_lib_get_or_null(storage_type_lib,
50 ROUTING_STORAGE_TYPE_LEVEL);
51 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
53 // if storage content is not specified use the content of storage_type if exist
54 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
55 storage->content = ((storage_type_t) stype)->content;
56 storage->content_type = ((storage_type_t) stype)->content_type;
57 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
58 storage->id,((storage_type_t) stype)->content_type,
59 ((storage_type_t) stype)->type_id);
62 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
63 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
64 "\n\t\tproperties '%p'\n",
66 ((storage_type_t) stype)->model,
67 ((storage_type_t) stype)->type_id,
69 storage->content_type,
70 ((storage_type_t) stype)->properties);
72 surf_storage_model->createResource(storage->id,
73 ((storage_type_t) stype)->type_id,
75 storage->content_type,
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 void storage_parse_storage_type(sg_platf_storage_type_cbarg_t storage_type)
118 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
119 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
121 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
122 stype->model = xbt_strdup(storage_type->model);
123 stype->properties = storage_type->properties;
124 stype->content = xbt_strdup(storage_type->content);
125 stype->content_type = xbt_strdup(storage_type->content_type);
126 stype->type_id = xbt_strdup(storage_type->id);
127 stype->size = storage_type->size;
129 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
130 "content '%s', and content_type '%s'",
133 storage_type->content,
134 storage_type->content_type);
136 xbt_lib_set(storage_type_lib,
138 ROUTING_STORAGE_TYPE_LEVEL,
142 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t /*mstorage*/)
145 // mount_t mnt = xbt_new0(s_mount_t, 1);
146 // mnt->id = xbt_strdup(mstorage->type_id);
147 // mnt->name = xbt_strdup(mstorage->name);
150 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
151 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
153 // xbt_dynar_push(mount_list,(void *) mnt);
157 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
160 static void mount_free(void *p)
162 mount_t mnt = (mount_t) p;
166 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
168 // Verification of an existing storage
170 void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
172 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
174 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
177 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
178 mnt.name = xbt_strdup(mount->name);
181 XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
182 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
184 xbt_dynar_push(mount_list, &mnt);
187 static void storage_define_callbacks()
189 sg_platf_storage_add_cb(parse_storage_init);
190 sg_platf_storage_type_add_cb(parse_storage_type_init);
191 sg_platf_mstorage_add_cb(parse_mstorage_init);
192 sg_platf_mount_add_cb(parse_mount_init);
195 void storage_register_callbacks() {
197 ROUTING_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,xbt_free);
198 ROUTING_STORAGE_HOST_LEVEL = xbt_lib_add_level(storage_lib, routing_storage_host_free);
199 ROUTING_STORAGE_TYPE_LEVEL = xbt_lib_add_level(storage_type_lib, routing_storage_type_free);
200 SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, surf_storage_resource_free);
202 sg_platf_storage_add_cb(storage_parse_storage);
203 sg_platf_mstorage_add_cb(storage_parse_mstorage);
204 sg_platf_storage_type_add_cb(storage_parse_storage_type);
205 sg_platf_mount_add_cb(storage_parse_mount);
212 void surf_storage_model_init_default(void)
214 surf_storage_model = new StorageN11Model();
215 storage_define_callbacks();
216 xbt_dynar_push(model_list, &surf_storage_model);
219 StorageN11Model::StorageN11Model() : StorageModel() {
220 StorageN11ActionLmm action;
222 XBT_DEBUG("surf_storage_model_init_internal");
224 storage_running_action_set_that_does_not_need_being_checked =
225 xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
227 if (!p_maxminSystem) {
228 p_maxminSystem = lmm_system_new(storage_selective_update);
232 StorageN11Model::~StorageN11Model(){
233 xbt_swag_free(storage_running_action_set_that_does_not_need_being_checked);
234 storage_running_action_set_that_does_not_need_being_checked = NULL;
237 StoragePtr StorageN11Model::createResource(const char* id, const char* type_id,
238 const char* content_name, const char* content_type, xbt_dict_t properties)
241 xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
242 "Storage '%s' declared several times in the platform file",
245 storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
247 double Bread = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bread"));
248 double Bwrite = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bwrite"));
249 double Bconnection = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bconnection"));
251 StoragePtr storage = new StorageN11Lmm(this, id, properties, p_maxminSystem,
252 Bread, Bwrite, Bconnection,
253 type_id, (char *)content_name, xbt_strdup(content_type), storage_type->size);
255 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, static_cast<ResourcePtr>(storage));
257 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",
261 storage_type->properties,
265 p_storageList = xbt_dynar_new(sizeof(char *),NULL);
266 xbt_dynar_push(p_storageList, &storage);
271 double StorageN11Model::shareResources(double now)
273 XBT_DEBUG("storage_share_resources %f", now);
277 StorageActionLmmPtr write_action;
279 double min_completion = shareResourcesMaxMin(p_runningActionSet,
280 p_maxminSystem, lmm_solve);
284 xbt_dynar_foreach(p_storageList,i,storage)
287 // Foreach write action on disk
288 xbt_dynar_foreach(storage->p_writeActions, j, _write_action)
290 write_action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_write_action));
291 rate += lmm_variable_getvalue(write_action->p_variable);
294 min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
297 return min_completion;
300 void StorageN11Model::updateActionsState(double /*now*/, double delta)
302 void *_action, *_next_action;
303 StorageActionLmmPtr action = NULL;
305 xbt_swag_foreach_safe(_action, _next_action, p_runningActionSet) {
306 action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_action));
307 if(action->m_type == WRITE)
309 // Update the disk usage
310 // Update the file size
311 // For each action of type write
312 double rate = lmm_variable_getvalue(action->p_variable);
313 /* Hack to avoid rounding differences between x86 and x86_64
314 * (note that the next sizes are of type sg_size_t). */
315 long incr = delta * rate + MAXMIN_PRECISION;
316 action->p_storage->m_usedSize += (incr - action->p_file->size); // disk usage
317 action->p_file->size = incr; // file size
319 sg_size_t *psize = xbt_new(sg_size_t,1);
320 *psize = action->p_file->size;
322 xbt_dict_t content_dict = action->p_storage->p_content;
323 xbt_dict_set(content_dict, action->p_file->name, psize, NULL);
326 double_update(&action->m_remains,
327 lmm_variable_getvalue(action->p_variable) * delta);
329 if (action->m_maxDuration != NO_MAX_DURATION)
330 double_update(&action->m_maxDuration, delta);
332 if(action->m_remains > 0 &&
333 lmm_get_variable_weight(action->p_variable) > 0 &&
334 action->p_storage->m_usedSize == action->p_storage->m_size)
336 action->m_finish = surf_get_clock();
337 action->setState(SURF_ACTION_FAILED);
338 } else if ((action->m_remains <= 0) &&
339 (lmm_get_variable_weight(action->p_variable) > 0))
341 action->m_finish = surf_get_clock();
342 action->setState(SURF_ACTION_DONE);
343 } else if ((action->m_maxDuration != NO_MAX_DURATION) &&
344 (action->m_maxDuration <= 0))
346 action->m_finish = surf_get_clock();
347 action->setState(SURF_ACTION_DONE);
358 StorageN11Lmm::StorageN11Lmm(StorageModelPtr model, const char* name, xbt_dict_t properties,
359 lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
360 const char* type_id, char *content_name, char *content_type, sg_size_t size)
361 : Resource(model, name, properties),
362 StorageLmm(maxminSystem, bread, bwrite, bconnection, type_id, content_name, content_type, size) {
363 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
365 p_stateCurrent = SURF_RESOURCE_ON;
369 p_content = parseContent(content_name);
370 p_contentType = content_type;
371 p_constraint = lmm_constraint_new(maxminSystem, this, bconnection);
372 p_constraintRead = lmm_constraint_new(maxminSystem, this, bread);
373 p_constraintWrite = lmm_constraint_new(maxminSystem, this, bwrite);
375 p_typeId = xbt_strdup(type_id);
378 StorageActionPtr StorageN11Lmm::ls(const char* path)
380 StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, LS);
382 action->p_lsDict = NULL;
383 xbt_dict_t ls_dict = xbt_dict_new_homogeneous(xbt_free);
387 xbt_dict_cursor_t cursor = NULL;
389 xbt_dynar_t dyn = NULL;
392 // for each file in the storage content
393 xbt_dict_foreach(p_content,cursor,key,size){
394 // Search if file start with the prefix 'path'
395 if(xbt_str_start_with(key,path)){
396 file = &key[strlen(path)];
398 // Split file with '/'
399 dyn = xbt_str_split(file,"/");
400 file = xbt_dynar_get_as(dyn,0,char*);
403 if(xbt_dynar_length(dyn) == 1){
404 sg_size_t *psize = xbt_new(sg_size_t, 1);
406 xbt_dict_set(ls_dict, file, psize, NULL);
411 // if directory does not exist yet in the dictionary
412 if(!xbt_dict_get_or_null(ls_dict,file))
413 xbt_dict_set(ls_dict,file,NULL,NULL);
415 xbt_dynar_free(&dyn);
419 action->p_lsDict = ls_dict;
423 StorageActionPtr StorageN11Lmm::open(const char* mount, const char* path)
425 XBT_DEBUG("\tOpen file '%s'",path);
426 sg_size_t size, *psize;
427 psize = (sg_size_t*) xbt_dict_get_or_null(p_content, path);
428 // if file does not exist create an empty file
432 psize = xbt_new(sg_size_t,1);
435 xbt_dict_set(p_content, path, psize, NULL);
436 XBT_DEBUG("File '%s' was not found, file created.",path);
438 surf_file_t file = xbt_new0(s_surf_file_t,1);
439 file->name = xbt_strdup(path);
441 file->mount = xbt_strdup(mount);
442 file->current_position = 0;
444 StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, OPEN);
445 action->p_file = file;
449 StorageActionPtr StorageN11Lmm::close(surf_file_t fd)
451 char *filename = fd->name;
452 XBT_DEBUG("\tClose file '%s' size '%llu'", filename, fd->size);
453 // unref write actions from storage
455 StorageActionLmmPtr write_action;
457 xbt_dynar_foreach(p_writeActions, i, _write_action) {
458 write_action = dynamic_cast<StorageActionLmmPtr>(static_cast<ActionPtr>(_write_action));
459 if ((write_action->p_file) == fd) {
460 xbt_dynar_cursor_rm(p_writeActions, &i);
461 write_action->unref();
467 StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, 0, p_stateCurrent != SURF_RESOURCE_ON, this, CLOSE);
471 StorageActionPtr StorageN11Lmm::read(surf_file_t fd, sg_size_t size)
475 fd->current_position = fd->size;
478 fd->current_position += size;
480 StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, size, p_stateCurrent != SURF_RESOURCE_ON, this, READ);
484 StorageActionPtr StorageN11Lmm::write(surf_file_t fd, sg_size_t size)
486 char *filename = fd->name;
487 XBT_DEBUG("\tWrite file '%s' size '%llu/%llu'",filename,size,fd->size);
489 StorageActionLmmPtr action = new StorageN11ActionLmm(p_model, size, p_stateCurrent != SURF_RESOURCE_ON, this, WRITE);
491 fd->current_position += size;
492 // If the storage is full
493 if(m_usedSize==m_size) {
494 action->setState(SURF_ACTION_FAILED);
499 void StorageN11Lmm::rename(const char *src, const char *dest)
501 sg_size_t *psize, *new_psize;
502 psize = (sg_size_t*) xbt_dict_get_or_null(p_content,src);
503 new_psize = xbt_new(sg_size_t, 1);
505 if (psize){// src file exists
506 xbt_dict_remove(p_content, src);
507 xbt_dict_set(p_content, dest, new_psize,NULL);
508 XBT_DEBUG("Change file name from %s to %s, size '%llu'",src, dest, *psize);
511 XBT_DEBUG("File %s doesn't exist",src);
514 xbt_dict_t StorageN11Lmm::getContent()
516 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
517 /*surf_action_t action = storage_action_execute(storage,0, LS);*/
519 xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
520 xbt_dict_cursor_t cursor = NULL;
524 xbt_dict_foreach(p_content, cursor, file, psize){
525 xbt_dict_set(content_dict,file,psize,NULL);
530 sg_size_t StorageN11Lmm::getSize(){
538 StorageN11ActionLmm::StorageN11ActionLmm(ModelPtr model, double cost, bool failed, StorageLmmPtr storage, e_surf_action_storage_type_t type)
539 : Action(model, cost, failed),
540 StorageActionLmm(storage, type) {
541 XBT_IN("(%s,%g", storage->m_name, cost);
542 p_variable = lmm_variable_new(p_model->p_maxminSystem, this, 1.0, -1.0 , 3);
544 // Must be less than the max bandwidth for all actions
545 lmm_expand(p_model->p_maxminSystem, storage->p_constraint, p_variable, 1.0);
553 lmm_expand(p_model->p_maxminSystem, storage->p_constraintRead,
557 lmm_expand(p_model->p_maxminSystem, storage->p_constraintWrite,
559 ActionPtr action = this;
560 xbt_dynar_push(storage->p_writeActions, &action);
567 int StorageN11ActionLmm::unref()
571 xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
573 lmm_variable_free(p_model->p_maxminSystem, p_variable);
575 xbt_free(p_category);
583 void StorageN11ActionLmm::cancel()
585 setState(SURF_ACTION_FAILED);
589 void StorageN11ActionLmm::suspend()
591 XBT_IN("(%p)", this);
592 if (m_suspended != 2) {
593 lmm_update_variable_weight(p_model->p_maxminSystem,
601 void StorageN11ActionLmm::resume()
606 bool StorageN11ActionLmm::isSuspended()
608 return m_suspended == 1;
611 void StorageN11ActionLmm::setMaxDuration(double /*duration*/)
616 void StorageN11ActionLmm::setPriority(double /*priority*/)