1 #include "storage_n11.hpp"
2 #include "surf_private.h"
4 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_storage);
6 static int storage_selective_update = 0;
7 static xbt_swag_t storage_running_action_set_that_does_not_need_being_checked = NULL;
13 static XBT_INLINE void routing_storage_type_free(void *r)
15 storage_type_t stype = (storage_type_t) r;
19 free(stype->content_type);
20 xbt_dict_free(&(stype->properties));
21 xbt_dict_free(&(stype->properties));
25 static XBT_INLINE void surf_storage_resource_free(void *r)
27 // specific to storage
28 StoragePtr storage = static_cast<StoragePtr>(r);
33 static XBT_INLINE void routing_storage_host_free(void *r)
35 xbt_dynar_t dyn = (xbt_dynar_t) r;
39 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
41 void* stype = xbt_lib_get_or_null(storage_type_lib,
43 ROUTING_STORAGE_TYPE_LEVEL);
44 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
46 // if storage content is not specified use the content of storage_type if exist
47 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
48 storage->content = ((storage_type_t) stype)->content;
49 storage->content_type = ((storage_type_t) stype)->content_type;
50 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
51 storage->id,((storage_type_t) stype)->content_type,
52 ((storage_type_t) stype)->type_id);
55 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
56 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
57 "\n\t\tproperties '%p'\n",
59 ((storage_type_t) stype)->model,
60 ((storage_type_t) stype)->type_id,
62 storage->content_type,
63 ((storage_type_t) stype)->properties);
65 surf_storage_model->createResource(storage->id,
66 ((storage_type_t) stype)->type_id,
68 storage->content_type,
72 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t /*mstorage*/)
74 XBT_DEBUG("parse_mstorage_init");
77 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t /*storagetype_*/)
79 XBT_DEBUG("parse_storage_type_init");
82 static void parse_mount_init(sg_platf_mount_cbarg_t /*mount*/)
84 XBT_DEBUG("parse_mount_init");
87 static void storage_parse_storage(sg_platf_storage_cbarg_t storage)
89 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
90 "Reading a storage, processing unit \"%s\" already exists", storage->id);
92 // Verification of an existing type_id
94 void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
96 xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
98 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
103 xbt_lib_set(storage_lib,
105 ROUTING_STORAGE_LEVEL,
106 (void *) xbt_strdup(storage->type_id));
109 static void storage_parse_storage_type(sg_platf_storage_type_cbarg_t storage_type)
111 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
112 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
114 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
115 stype->model = xbt_strdup(storage_type->model);
116 stype->properties = storage_type->properties;
117 stype->content = xbt_strdup(storage_type->content);
118 stype->content_type = xbt_strdup(storage_type->content_type);
119 stype->type_id = xbt_strdup(storage_type->id);
120 stype->size = storage_type->size;
122 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
123 "content '%s', and content_type '%s'",
126 storage_type->content,
127 storage_type->content_type);
129 xbt_lib_set(storage_type_lib,
131 ROUTING_STORAGE_TYPE_LEVEL,
135 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t /*mstorage*/)
138 // mount_t mnt = xbt_new0(s_mount_t, 1);
139 // mnt->id = xbt_strdup(mstorage->type_id);
140 // mnt->name = xbt_strdup(mstorage->name);
143 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
144 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
146 // xbt_dynar_push(mount_list,(void *) mnt);
150 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
153 static void mount_free(void *p)
155 mount_t mnt = (mount_t) p;
159 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
161 // Verification of an existing storage
163 void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
165 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
167 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
170 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
171 mnt.name = xbt_strdup(mount->name);
174 XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
175 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
177 xbt_dynar_push(mount_list, &mnt);
180 static void storage_define_callbacks()
182 sg_platf_storage_add_cb(parse_storage_init);
183 sg_platf_storage_type_add_cb(parse_storage_type_init);
184 sg_platf_mstorage_add_cb(parse_mstorage_init);
185 sg_platf_mount_add_cb(parse_mount_init);
188 void storage_register_callbacks() {
190 ROUTING_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,xbt_free);
191 ROUTING_STORAGE_HOST_LEVEL = xbt_lib_add_level(storage_lib, routing_storage_host_free);
192 ROUTING_STORAGE_TYPE_LEVEL = xbt_lib_add_level(storage_type_lib, routing_storage_type_free);
193 SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, surf_storage_resource_free);
195 sg_platf_storage_add_cb(storage_parse_storage);
196 sg_platf_mstorage_add_cb(storage_parse_mstorage);
197 sg_platf_storage_type_add_cb(storage_parse_storage_type);
198 sg_platf_mount_add_cb(storage_parse_mount);
205 void surf_storage_model_init_default(void)
207 surf_storage_model = new StorageN11Model();
208 storage_define_callbacks();
209 xbt_dynar_push(model_list, &surf_storage_model);
212 StorageN11Model::StorageN11Model() : StorageModel() {
213 ActionPtr action = NULL;
215 XBT_DEBUG("surf_storage_model_init_internal");
217 storage_running_action_set_that_does_not_need_being_checked =
218 xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
219 if (!p_maxminSystem) {
220 p_maxminSystem = lmm_system_new(storage_selective_update);
224 StorageN11Model::~StorageN11Model(){
225 xbt_swag_free(storage_running_action_set_that_does_not_need_being_checked);
226 storage_running_action_set_that_does_not_need_being_checked = NULL;
229 StoragePtr StorageN11Model::createResource(const char* id, const char* type_id,
230 const char* content_name, const char* content_type, xbt_dict_t properties)
233 xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
234 "Storage '%s' declared several times in the platform file",
237 storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
239 double Bread = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bread"));
240 double Bwrite = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bwrite"));
241 double Bconnection = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->properties, "Bconnection"));
243 StoragePtr storage = new StorageN11(this, id, properties, p_maxminSystem,
244 Bread, Bwrite, Bconnection,
245 type_id, (char *)content_name, xbt_strdup(content_type), storage_type->size);
247 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, static_cast<ResourcePtr>(storage));
249 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s'\n\t\tproperties '%p'\n\t\tBread '%f'\n",
252 storage_type->properties,
256 p_storageList = xbt_dynar_new(sizeof(char *),NULL);
257 xbt_dynar_push(p_storageList, &storage);
262 double StorageN11Model::shareResources(double now)
264 XBT_DEBUG("storage_share_resources %f", now);
268 StorageActionPtr write_action;
270 double min_completion = shareResourcesMaxMin(getRunningActionSet(),
271 p_maxminSystem, lmm_solve);
275 xbt_dynar_foreach(p_storageList,i,storage)
278 // Foreach write action on disk
279 xbt_dynar_foreach(storage->p_writeActions, j, _write_action)
281 write_action = static_cast<StorageActionPtr>(_write_action);
282 rate += lmm_variable_getvalue(write_action->getVariable());
285 min_completion = MIN(min_completion, (storage->m_size-storage->m_usedSize)/rate);
288 return min_completion;
291 void StorageN11Model::updateActionsState(double /*now*/, double delta)
293 StorageActionPtr action = NULL;
295 ActionListPtr actionSet = getRunningActionSet();
296 for(ActionList::iterator it(actionSet->begin()), itNext=it, itend(actionSet->end())
297 ; it != itend ; it=itNext) {
299 action = static_cast<StorageActionPtr>(&*it);
300 if(action->m_type == WRITE)
302 // Update the disk usage
303 // Update the file size
304 // For each action of type write
305 double rate = lmm_variable_getvalue(action->getVariable());
306 /* Hack to avoid rounding differences between x86 and x86_64
307 * (note that the next sizes are of type sg_size_t). */
308 long incr = delta * rate + MAXMIN_PRECISION;
309 action->p_storage->m_usedSize += (incr - action->p_file->size); // disk usage
310 action->p_file->size = incr; // file size
312 sg_size_t *psize = xbt_new(sg_size_t,1);
313 *psize = action->p_file->size;
315 xbt_dict_t content_dict = action->p_storage->p_content;
316 xbt_dict_set(content_dict, action->p_file->name, psize, NULL);
319 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
321 if (action->getMaxDuration() != NO_MAX_DURATION)
322 action->updateMaxDuration(delta);
324 if(action->getRemains() > 0 &&
325 lmm_get_variable_weight(action->getVariable()) > 0 &&
326 action->p_storage->m_usedSize == action->p_storage->m_size)
329 action->setState(SURF_ACTION_FAILED);
330 } else if ((action->getRemains() <= 0) &&
331 (lmm_get_variable_weight(action->getVariable()) > 0))
334 action->setState(SURF_ACTION_DONE);
335 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
336 (action->getMaxDuration() <= 0))
339 action->setState(SURF_ACTION_DONE);
350 StorageN11::StorageN11(StorageModelPtr model, const char* name, xbt_dict_t properties,
351 lmm_system_t maxminSystem, double bread, double bwrite, double bconnection,
352 const char* type_id, char *content_name, char *content_type, sg_size_t size)
353 : Storage(model, name, properties,
354 maxminSystem, bread, bwrite, bconnection, type_id, content_name, content_type, size) {
355 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
358 StorageActionPtr StorageN11::ls(const char* path)
360 StorageActionPtr action = new StorageN11Action(getModel(), 0, m_stateCurrent != SURF_RESOURCE_ON, this, LS);
362 action->p_lsDict = NULL;
363 xbt_dict_t ls_dict = xbt_dict_new_homogeneous(xbt_free);
367 xbt_dict_cursor_t cursor = NULL;
369 xbt_dynar_t dyn = NULL;
372 // for each file in the storage content
373 xbt_dict_foreach(p_content,cursor,key,size){
374 // Search if file start with the prefix 'path'
375 if(xbt_str_start_with(key,path)){
376 file = &key[strlen(path)];
378 // Split file with '/'
379 dyn = xbt_str_split(file,"/");
380 file = xbt_dynar_get_as(dyn,0,char*);
383 if(xbt_dynar_length(dyn) == 1){
384 sg_size_t *psize = xbt_new(sg_size_t, 1);
386 xbt_dict_set(ls_dict, file, psize, NULL);
391 // if directory does not exist yet in the dictionary
392 if(!xbt_dict_get_or_null(ls_dict,file))
393 xbt_dict_set(ls_dict,file,NULL,NULL);
395 xbt_dynar_free(&dyn);
399 action->p_lsDict = ls_dict;
403 StorageActionPtr StorageN11::open(const char* mount, const char* path)
405 XBT_DEBUG("\tOpen file '%s'",path);
406 sg_size_t size, *psize;
407 psize = (sg_size_t*) xbt_dict_get_or_null(p_content, path);
408 // if file does not exist create an empty file
412 psize = xbt_new(sg_size_t,1);
415 xbt_dict_set(p_content, path, psize, NULL);
416 XBT_DEBUG("File '%s' was not found, file created.",path);
418 surf_file_t file = xbt_new0(s_surf_file_t,1);
419 file->name = xbt_strdup(path);
421 file->mount = xbt_strdup(mount);
422 file->current_position = 0;
424 StorageActionPtr action = new StorageN11Action(getModel(), 0, m_stateCurrent != SURF_RESOURCE_ON, this, OPEN);
425 action->p_file = file;
429 StorageActionPtr StorageN11::close(surf_file_t fd)
431 char *filename = fd->name;
432 XBT_DEBUG("\tClose file '%s' size '%llu'", filename, fd->size);
433 // unref write actions from storage
435 StorageActionPtr write_action;
437 xbt_dynar_foreach(p_writeActions, i, _write_action) {
438 write_action = static_cast<StorageActionPtr>(static_cast<ActionPtr>(_write_action));
439 if ((write_action->p_file) == fd) {
440 xbt_dynar_cursor_rm(p_writeActions, &i);
441 write_action->unref();
447 StorageActionPtr action = new StorageN11Action(getModel(), 0, m_stateCurrent != SURF_RESOURCE_ON, this, CLOSE);
451 StorageActionPtr StorageN11::read(surf_file_t fd, sg_size_t size)
455 fd->current_position = fd->size;
458 fd->current_position += size;
460 StorageActionPtr action = new StorageN11Action(getModel(), size, m_stateCurrent != SURF_RESOURCE_ON, this, READ);
464 StorageActionPtr StorageN11::write(surf_file_t fd, sg_size_t size)
466 char *filename = fd->name;
467 XBT_DEBUG("\tWrite file '%s' size '%llu/%llu'",filename,size,fd->size);
469 StorageActionPtr action = new StorageN11Action(getModel(), size, m_stateCurrent != SURF_RESOURCE_ON, this, WRITE);
471 fd->current_position += size;
472 // If the storage is full
473 if(m_usedSize==m_size) {
474 action->setState(SURF_ACTION_FAILED);
479 void StorageN11::rename(const char *src, const char *dest)
481 sg_size_t *psize, *new_psize;
482 psize = (sg_size_t*) xbt_dict_get_or_null(p_content,src);
483 new_psize = xbt_new(sg_size_t, 1);
485 if (psize){// src file exists
486 xbt_dict_remove(p_content, src);
487 xbt_dict_set(p_content, dest, new_psize,NULL);
488 XBT_DEBUG("Change file name from %s to %s, size '%llu'",src, dest, *psize);
491 XBT_DEBUG("File %s doesn't exist",src);
494 xbt_dict_t StorageN11::getContent()
496 /* For the moment this action has no cost, but in the future we could take in account access latency of the disk */
497 /*surf_action_t action = storage_action_execute(storage,0, LS);*/
499 xbt_dict_t content_dict = xbt_dict_new_homogeneous(NULL);
500 xbt_dict_cursor_t cursor = NULL;
504 xbt_dict_foreach(p_content, cursor, file, psize){
505 xbt_dict_set(content_dict,file,psize,NULL);
510 sg_size_t StorageN11::getSize(){
518 StorageN11Action::StorageN11Action(ModelPtr model, double cost, bool failed, StoragePtr storage, e_surf_action_storage_type_t type)
519 : StorageAction(model, cost, failed,
520 lmm_variable_new(model->getMaxminSystem(), this, 1.0, -1.0 , 3),
522 XBT_IN("(%s,%g", storage->getName(), cost);
524 // Must be less than the max bandwidth for all actions
525 lmm_expand(model->getMaxminSystem(), storage->getConstraint(), getVariable(), 1.0);
533 lmm_expand(model->getMaxminSystem(), storage->p_constraintRead,
537 lmm_expand(model->getMaxminSystem(), storage->p_constraintWrite,
539 ActionPtr action = this;
540 xbt_dynar_push(storage->p_writeActions, &action);
547 int StorageN11Action::unref()
551 if (actionHook::is_linked())
552 p_stateSet->erase(p_stateSet->iterator_to(*this));
554 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
556 xbt_free(getCategory());
564 void StorageN11Action::cancel()
566 setState(SURF_ACTION_FAILED);
570 void StorageN11Action::suspend()
572 XBT_IN("(%p)", this);
573 if (m_suspended != 2) {
574 lmm_update_variable_weight(getModel()->getMaxminSystem(),
582 void StorageN11Action::resume()
587 bool StorageN11Action::isSuspended()
589 return m_suspended == 1;
592 void StorageN11Action::setMaxDuration(double /*duration*/)
597 void StorageN11Action::setPriority(double /*priority*/)