1 /* Copyright (c) 2013-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "storage_n11.hpp"
8 #include "surf_private.h"
9 #include <math.h> /*ceil*/
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_storage);
12 static int storage_selective_update = 0;
13 static xbt_swag_t storage_running_action_set_that_does_not_need_being_checked = NULL;
19 static XBT_INLINE void routing_storage_type_free(void *r)
21 storage_type_t stype = (storage_type_t) r;
25 free(stype->content_type);
26 xbt_dict_free(&(stype->properties));
27 xbt_dict_free(&(stype->model_properties));
31 static XBT_INLINE void surf_storage_resource_free(void *r)
33 // specific to storage
34 StoragePtr storage = static_cast<StoragePtr>(r);
39 static XBT_INLINE void routing_storage_host_free(void *r)
41 xbt_dynar_t dyn = (xbt_dynar_t) r;
45 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
47 void* stype = xbt_lib_get_or_null(storage_type_lib,
49 ROUTING_STORAGE_TYPE_LEVEL);
50 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
52 // if storage content is not specified use the content of storage_type if exist
53 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
54 storage->content = ((storage_type_t) stype)->content;
55 storage->content_type = ((storage_type_t) stype)->content_type;
56 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
57 storage->id,((storage_type_t) stype)->content_type,
58 ((storage_type_t) stype)->type_id);
61 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
62 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
63 "\n\t\tproperties '%p''\n",
65 ((storage_type_t) stype)->model,
66 ((storage_type_t) stype)->type_id,
68 storage->content_type,
71 surf_storage_model->createStorage(storage->id,
72 ((storage_type_t) stype)->type_id,
74 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;
128 stype->model_properties = storage_type->model_properties;
130 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
131 "content '%s', and content_type '%s'",
134 storage_type->content,
135 storage_type->content_type);
137 xbt_lib_set(storage_type_lib,
139 ROUTING_STORAGE_TYPE_LEVEL,
143 static void storage_parse_mstorage(sg_platf_mstorage_cbarg_t /*mstorage*/)
146 // mount_t mnt = xbt_new0(s_mount_t, 1);
147 // mnt->id = xbt_strdup(mstorage->type_id);
148 // mnt->name = xbt_strdup(mstorage->name);
151 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
152 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
154 // xbt_dynar_push(mount_list,(void *) mnt);
158 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
161 static void mount_free(void *p)
163 mount_t mnt = (mount_t) p;
167 static void storage_parse_mount(sg_platf_mount_cbarg_t mount)
169 // Verification of an existing storage
171 void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
173 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
175 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
178 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
179 mnt.name = xbt_strdup(mount->name);
182 XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
183 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
185 xbt_dynar_push(mount_list, &mnt);
188 static void storage_define_callbacks()
190 sg_platf_storage_add_cb(parse_storage_init);
191 sg_platf_storage_type_add_cb(parse_storage_type_init);
192 sg_platf_mstorage_add_cb(parse_mstorage_init);
193 sg_platf_mount_add_cb(parse_mount_init);
196 void storage_register_callbacks() {
198 ROUTING_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, xbt_free_f);
199 ROUTING_STORAGE_HOST_LEVEL = xbt_lib_add_level(storage_lib, routing_storage_host_free);
200 ROUTING_STORAGE_TYPE_LEVEL = xbt_lib_add_level(storage_type_lib, routing_storage_type_free);
201 SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, surf_storage_resource_free);
203 sg_platf_storage_add_cb(storage_parse_storage);
204 sg_platf_mstorage_add_cb(storage_parse_mstorage);
205 sg_platf_storage_type_add_cb(storage_parse_storage_type);
206 sg_platf_mount_add_cb(storage_parse_mount);
213 void surf_storage_model_init_default(void)
215 surf_storage_model = new StorageN11Model();
216 storage_define_callbacks();
217 xbt_dynar_push(model_list, &surf_storage_model);
220 StorageN11Model::StorageN11Model() : StorageModel() {
221 ActionPtr action = NULL;
223 XBT_DEBUG("surf_storage_model_init_internal");
225 storage_running_action_set_that_does_not_need_being_checked =
226 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::createStorage(const char* id, const char* type_id,
238 const char* content_name, const char* content_type, xbt_dict_t properties,
242 xbt_assert(!surf_storage_resource_priv(surf_storage_resource_by_name(id)),
243 "Storage '%s' declared several times in the platform file",
246 storage_type_t storage_type = (storage_type_t) xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
248 double Bread = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->model_properties, "Bread"));
249 double Bwrite = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->model_properties, "Bwrite"));
250 double Bconnection = surf_parse_get_bandwidth((char*)xbt_dict_get(storage_type->model_properties, "Bconnection"));
252 StoragePtr storage = new StorageN11(this, id, properties, p_maxminSystem,
253 Bread, Bwrite, Bconnection, type_id, (char *)content_name,
254 xbt_strdup(content_type), storage_type->size, (char *) attach);
256 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
258 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s'\n\t\tproperties '%p'\n\t\tBread '%f'\n",
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 StorageActionPtr write_action;
279 double min_completion = shareResourcesMaxMin(getRunningActionSet(),
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 = static_cast<StorageActionPtr>(_write_action);
291 rate += lmm_variable_getvalue(write_action->getVariable());
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 StorageActionPtr action = NULL;
304 ActionListPtr actionSet = getRunningActionSet();
305 for(ActionList::iterator it(actionSet->begin()), itNext=it, itend(actionSet->end())
306 ; it != itend ; it=itNext) {
308 action = static_cast<StorageActionPtr>(&*it);
310 if(action->m_type == WRITE)
312 // Update the disk usage
313 // Update the file size
314 // For each action of type write
315 volatile double current_progress =
316 delta * lmm_variable_getvalue(action->getVariable());
317 long int incr = current_progress;
319 XBT_DEBUG("%s:\n\t progress = %.2f, current_progress = %.2f, "
320 "incr = %ld, lrint(1) = %ld, lrint(2) = %ld",
321 action->p_file->name,
322 action->progress, current_progress, incr,
323 lrint(action->progress + current_progress),
324 lrint(action->progress)+ incr);
326 /* take care of rounding error accumulation */
327 if (lrint(action->progress + current_progress) >
328 lrint(action->progress)+ incr)
331 action->progress +=current_progress;
333 action->p_storage->m_usedSize += incr; // disk usage
334 action->p_file->current_position+= incr; // current_position
335 // which becomes the new file size
336 action->p_file->size = action->p_file->current_position ;
338 sg_size_t *psize = xbt_new(sg_size_t,1);
339 *psize = action->p_file->size;
340 xbt_dict_t content_dict = action->p_storage->p_content;
341 xbt_dict_set(content_dict, action->p_file->name, psize, NULL);
344 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
346 if (action->getMaxDuration() != NO_MAX_DURATION)
347 action->updateMaxDuration(delta);
349 if(action->getRemainsNoUpdate() > 0 &&
350 lmm_get_variable_weight(action->getVariable()) > 0 &&
351 action->p_storage->m_usedSize == action->p_storage->m_size)
354 action->setState(SURF_ACTION_FAILED);
355 } else if ((action->getRemainsNoUpdate() <= 0) &&
356 (lmm_get_variable_weight(action->getVariable()) > 0))
359 action->setState(SURF_ACTION_DONE);
360 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
361 (action->getMaxDuration() <= 0))
364 action->setState(SURF_ACTION_DONE);
374 StorageN11::StorageN11(StorageModelPtr model, const char* name,
375 xbt_dict_t properties, lmm_system_t maxminSystem, double bread,
376 double bwrite, double bconnection, const char* type_id, char *content_name,
377 char *content_type, sg_size_t size, char *attach)
378 : Storage(model, name, properties,
379 maxminSystem, bread, bwrite, bconnection, type_id, content_name, content_type, size, attach) {
380 XBT_DEBUG("Create resource with Bconnection '%f' Bread '%f' Bwrite '%f' and Size '%llu'", bconnection, bread, bwrite, size);
383 StorageActionPtr StorageN11::open(const char* mount, const char* path)
385 XBT_DEBUG("\tOpen file '%s'",path);
387 sg_size_t size, *psize;
388 psize = (sg_size_t*) xbt_dict_get_or_null(p_content, path);
389 // if file does not exist create an empty file
393 psize = xbt_new(sg_size_t,1);
396 xbt_dict_set(p_content, path, psize, NULL);
397 XBT_DEBUG("File '%s' was not found, file created.",path);
399 surf_file_t file = xbt_new0(s_surf_file_t,1);
400 file->name = xbt_strdup(path);
402 file->mount = xbt_strdup(mount);
403 file->current_position = 0;
405 StorageActionPtr action = new StorageN11Action(getModel(), 0, getState() != SURF_RESOURCE_ON, this, OPEN);
406 action->p_file = file;
411 StorageActionPtr StorageN11::close(surf_file_t fd)
413 char *filename = fd->name;
414 XBT_DEBUG("\tClose file '%s' size '%llu'", filename, fd->size);
415 // unref write actions from storage
417 StorageActionPtr write_action;
419 xbt_dynar_foreach(p_writeActions, i, _write_action) {
420 write_action = static_cast<StorageActionPtr>(_write_action);
421 if ((write_action->p_file) == fd) {
422 xbt_dynar_cursor_rm(p_writeActions, &i);
423 write_action->unref();
429 StorageActionPtr action = new StorageN11Action(getModel(), 0, getState() != SURF_RESOURCE_ON, this, CLOSE);
433 StorageActionPtr StorageN11::read(surf_file_t fd, sg_size_t size)
435 if(fd->current_position + size > fd->size){
436 size = fd->size - fd->current_position;
437 fd->current_position = fd->size;
440 fd->current_position += size;
442 StorageActionPtr action = new StorageN11Action(getModel(), size, getState() != SURF_RESOURCE_ON, this, READ);
446 StorageActionPtr StorageN11::write(surf_file_t fd, sg_size_t size)
448 char *filename = fd->name;
449 XBT_DEBUG("\tWrite file '%s' size '%llu/%llu'",filename,size,fd->size);
451 StorageActionPtr action = new StorageN11Action(getModel(), size, getState() != SURF_RESOURCE_ON, this, WRITE);
453 /* Substract the part of the file that might disappear from the used sized on
454 * the storage element */
455 m_usedSize -= (fd->size - fd->current_position);
456 // If the storage is full before even starting to write
457 if(m_usedSize==m_size) {
458 action->setState(SURF_ACTION_FAILED);
467 StorageN11Action::StorageN11Action(ModelPtr model, double cost, bool failed, StoragePtr storage, e_surf_action_storage_type_t type)
468 : StorageAction(model, cost, failed,
469 lmm_variable_new(model->getMaxminSystem(), this, 1.0, -1.0 , 3),
471 XBT_IN("(%s,%g", storage->getName(), cost);
473 // Must be less than the max bandwidth for all actions
474 lmm_expand(model->getMaxminSystem(), storage->getConstraint(), getVariable(), 1.0);
481 lmm_expand(model->getMaxminSystem(), storage->p_constraintRead,
485 lmm_expand(model->getMaxminSystem(), storage->p_constraintWrite,
488 //TODO there is something annoying with what's below. Have to sort it out...
489 // ActionPtr action = this;
490 // xbt_dynar_push(storage->p_writeActions, &action);
497 int StorageN11Action::unref()
501 if (actionHook::is_linked())
502 p_stateSet->erase(p_stateSet->iterator_to(*this));
504 lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
505 xbt_free(getCategory());
512 void StorageN11Action::cancel()
514 setState(SURF_ACTION_FAILED);
518 void StorageN11Action::suspend()
520 XBT_IN("(%p)", this);
521 if (m_suspended != 2) {
522 lmm_update_variable_weight(getModel()->getMaxminSystem(),
530 void StorageN11Action::resume()
535 bool StorageN11Action::isSuspended()
537 return m_suspended == 1;
540 void StorageN11Action::setMaxDuration(double /*duration*/)
545 void StorageN11Action::setPriority(double /*priority*/)