1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012. 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. */
10 #include "surf_private.h"
11 #include "storage_private.h"
12 #include "surf/surf_resource.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf,
15 "Logging specific to the SURF storage module");
17 surf_model_t surf_storage_model = NULL;
18 lmm_system_t storage_maxmin_system = NULL;
19 static int storage_selective_update = 0;
21 storage_running_action_set_that_does_not_need_being_checked = NULL;
23 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
24 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
30 } STORAGE_execute_type_t;
32 typedef struct surf_storage {
33 s_surf_resource_t generic_resource;
35 const char* content; /*should be a dict */
36 } s_surf_storage_t, *surf_storage_t;
38 static void storage_action_state_set(surf_action_t action, e_surf_action_state_t state);
39 static surf_action_t storage_action_sleep (void *storage, double duration);
40 static surf_action_t storage_action_execute (void *storage, size_t size, STORAGE_execute_type_t type);
42 static surf_action_t storage_action_open(void *storage, const char* path, const char* mode)
44 XBT_DEBUG("\tOpen file '%s'",path);
45 char *storage_type_id = xbt_lib_get_or_null(
47 ((storage_t)storage)->generic_resource.name,
48 ROUTING_STORAGE_LEVEL);
49 storage_type_t storage_type = xbt_lib_get_or_null(storage_type_lib, storage_type_id,ROUTING_STORAGE_TYPE_LEVEL);
50 xbt_dict_t content_dict = storage_type->content;
51 content_t content = xbt_dict_get(content_dict,path);
53 m_file_t file = xbt_new0(s_m_file_t,1);
54 file->name = xbt_strdup(path);
55 file->content = content;
57 surf_action_t action = storage_action_execute(storage,0, DEFAULT);
61 static surf_action_t storage_action_close(void *storage, m_file_t fp)
63 char *filename = fp->name;
67 surf_action_t action = storage_action_execute(storage,0, DEFAULT);
68 XBT_DEBUG("\tClose file '%s'",filename);
72 static surf_action_t storage_action_read(void *storage, void* ptr, size_t size, size_t nmemb, m_file_t stream)
74 char *filename = stream->name;
75 content_t content = (content_t)(stream->content);
76 XBT_DEBUG("\tRead file '%s' size '%Zu/%Zu'",filename,size,content->size);
77 if(size > content->size)
79 surf_action_t action = storage_action_execute(storage,size,READ);
83 static surf_action_t storage_action_write(void *storage, const void* ptr, size_t size, size_t nmemb, m_file_t stream)
85 char *filename = stream->name;
86 content_t content = (content_t)(stream->content);
87 XBT_DEBUG("\tWrite file '%s' size '%Zu/%Zu'",filename,size,content->size);
89 surf_action_t action = storage_action_execute(storage,size,WRITE);
90 content->size += size;
94 static surf_action_t storage_action_stat(void *storage, int fd, void* buf)
100 static surf_action_t storage_action_execute (void *storage, size_t size, STORAGE_execute_type_t type)
102 surf_action_storage_t action = NULL;
103 storage_t STORAGE = storage;
105 XBT_IN("(%s,%Zu)", surf_resource_name(STORAGE), size);
107 surf_action_new(sizeof(s_surf_action_storage_t), size, surf_storage_model,
108 STORAGE->state_current != SURF_RESOURCE_ON);
110 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
111 calloc but it seems to help valgrind... */
113 GENERIC_LMM_ACTION(action).variable =
114 lmm_variable_new(storage_maxmin_system, action, 1.0, -1.0 , 3);
117 lmm_expand(storage_maxmin_system, STORAGE->constraint,
118 GENERIC_LMM_ACTION(action).variable, 1.0);
119 else if(type == READ)
120 lmm_expand(storage_maxmin_system, STORAGE->constraint_read,
121 GENERIC_LMM_ACTION(action).variable, 1.0);
122 else if(type == WRITE)
123 lmm_expand(storage_maxmin_system, STORAGE->constraint_write,
124 GENERIC_LMM_ACTION(action).variable, 1.0);
125 else xbt_die("storage_action_execute with type '%d'",(int) type);
128 return (surf_action_t) action;
131 static surf_action_t storage_action_sleep (void *storage, double duration)
133 surf_action_storage_t action = NULL;
136 duration = MAX(duration, MAXMIN_PRECISION);
138 XBT_IN("(%s,%g)", surf_resource_name(storage), duration);
139 action = (surf_action_storage_t) storage_action_execute(storage, 1.0, DEFAULT);
140 GENERIC_ACTION(action).max_duration = duration;
141 GENERIC_LMM_ACTION(action).suspended = 2;
142 if (duration == NO_MAX_DURATION) {
143 /* Move to the *end* of the corresponding action set. This convention
144 is used to speed up update_resource_state */
145 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
146 ((surf_action_t) action)->state_set =
147 storage_running_action_set_that_does_not_need_being_checked;
148 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
151 lmm_update_variable_weight(storage_maxmin_system,
152 GENERIC_LMM_ACTION(action).variable, 0.0);
155 return (surf_action_t) action;
158 static void* storage_create_resource(const char* id, const char* model,const char* type_id)
160 storage_t storage = NULL;
162 xbt_assert(!surf_storage_resource_by_name(id),
163 "Storage '%s' declared several times in the platform file",
165 storage = (storage_t) surf_resource_new(sizeof(s_storage_t),
166 surf_storage_model, id,NULL);
168 storage->state_current = SURF_RESOURCE_ON;
170 storage_type_t storage_type = xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
171 size_t Bread = atof(xbt_dict_get(storage_type->properties,"Bread"));
172 size_t Bwrite = atof(xbt_dict_get(storage_type->properties,"Bwrite"));
173 size_t Bconnexion = atof(xbt_dict_get(storage_type->properties,"Bconnexion"));
174 XBT_INFO("Create resource with Bconnexion '%Zu' Bread '%Zu' Bwrite '%Zu'",Bconnexion,Bread,Bwrite);
175 storage->constraint = lmm_constraint_new(storage_maxmin_system, storage, Bconnexion);
176 storage->constraint_read = lmm_constraint_new(storage_maxmin_system, storage, Bread);
177 storage->constraint_write = lmm_constraint_new(storage_maxmin_system, storage, Bwrite);
179 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
181 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 '%Zu'\n",
185 storage_type->properties,
191 static void storage_finalize(void)
193 lmm_system_free(storage_maxmin_system);
194 storage_maxmin_system = NULL;
196 surf_model_exit(surf_storage_model);
197 surf_storage_model = NULL;
200 (storage_running_action_set_that_does_not_need_being_checked);
201 storage_running_action_set_that_does_not_need_being_checked = NULL;
204 static void storage_update_actions_state(double now, double delta)
206 surf_action_storage_t action = NULL;
207 surf_action_storage_t next_action = NULL;
208 xbt_swag_t running_actions = surf_storage_model->states.running_action_set;
209 xbt_swag_foreach_safe(action, next_action, running_actions) {
211 double_update(&(GENERIC_ACTION(action).remains),
212 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * delta);
213 if (GENERIC_LMM_ACTION(action).generic_action.max_duration != NO_MAX_DURATION)
214 double_update(&(GENERIC_ACTION(action).max_duration), delta);
215 if ((GENERIC_ACTION(action).remains <= 0) &&
216 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
217 GENERIC_ACTION(action).finish = surf_get_clock();
218 storage_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
219 } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
220 (GENERIC_ACTION(action).max_duration <= 0)) {
221 GENERIC_ACTION(action).finish = surf_get_clock();
222 storage_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
229 static double storage_share_resources(double NOW)
231 XBT_DEBUG("storage_share_resources %f",NOW);
232 s_surf_action_storage_t action;
233 return generic_maxmin_share_resources(surf_storage_model->states.running_action_set,
234 xbt_swag_offset(action, generic_lmm_action.variable),
235 storage_maxmin_system, lmm_solve);
238 static int storage_resource_used(void *resource_id)
244 static void storage_resources_state(void *id, tmgr_trace_event_t event_type,
245 double value, double time)
250 static int storage_action_unref(surf_action_t action)
253 if (!action->refcount) {
254 xbt_swag_remove(action, action->state_set);
255 if (((surf_action_lmm_t) action)->variable)
256 lmm_variable_free(storage_maxmin_system,
257 ((surf_action_lmm_t) action)->variable);
259 xbt_free(action->category);
261 surf_action_free(&action);
267 static void storage_action_cancel(surf_action_t action)
269 surf_action_state_set(action, SURF_ACTION_FAILED);
273 static void storage_action_state_set(surf_action_t action, e_surf_action_state_t state)
275 surf_action_state_set(action, state);
279 static void storage_action_suspend(surf_action_t action)
281 XBT_IN("(%p)", action);
282 if (((surf_action_lmm_t) action)->suspended != 2) {
283 lmm_update_variable_weight(storage_maxmin_system,
284 ((surf_action_lmm_t) action)->variable,
286 ((surf_action_lmm_t) action)->suspended = 1;
291 static void storage_action_resume(surf_action_t action)
296 static int storage_action_is_suspended(surf_action_t action)
298 return (((surf_action_lmm_t) action)->suspended == 1);
301 static void storage_action_set_max_duration(surf_action_t action, double duration)
306 static void storage_action_set_priority(surf_action_t action, double priority)
311 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
313 void* stype = xbt_lib_get_or_null(storage_type_lib,
315 ROUTING_STORAGE_TYPE_LEVEL);
316 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
317 // XBT_INFO("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",
319 // ((storage_type_t) stype)->model,
320 // ((storage_type_t) stype)->type_id,
321 // ((storage_type_t) stype)->content,
322 // ((storage_type_t) stype)->properties);
324 storage_create_resource(storage->id,
325 ((storage_type_t) stype)->model,
326 ((storage_type_t) stype)->type_id);
329 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
331 XBT_DEBUG("parse_mstorage_init");
334 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
336 XBT_DEBUG("parse_storage_type_init");
339 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
341 XBT_DEBUG("parse_mount_init");
344 static void storage_define_callbacks()
346 sg_platf_storage_add_cb(parse_storage_init);
347 sg_platf_storage_type_add_cb(parse_storage_type_init);
348 sg_platf_mstorage_add_cb(parse_mstorage_init);
349 sg_platf_mount_add_cb(parse_mount_init);
352 static void surf_storage_model_init_internal(void)
354 s_surf_action_t action;
356 XBT_DEBUG("surf_storage_model_init_internal");
357 surf_storage_model = surf_model_init();
359 storage_running_action_set_that_does_not_need_being_checked =
360 xbt_swag_new(xbt_swag_offset(action, state_hookup));
362 surf_storage_model->name = "Storage";
363 surf_storage_model->action_unref = storage_action_unref;
364 surf_storage_model->action_cancel = storage_action_cancel;
365 surf_storage_model->action_state_set = storage_action_state_set;
367 surf_storage_model->model_private->finalize = storage_finalize;
368 surf_storage_model->model_private->update_actions_state = storage_update_actions_state;
369 surf_storage_model->model_private->share_resources = storage_share_resources;
370 surf_storage_model->model_private->resource_used = storage_resource_used;
371 surf_storage_model->model_private->update_resource_state = storage_resources_state;
373 surf_storage_model->suspend = storage_action_suspend;
374 surf_storage_model->resume = storage_action_resume;
375 surf_storage_model->is_suspended = storage_action_is_suspended;
376 surf_storage_model->set_max_duration = storage_action_set_max_duration;
377 surf_storage_model->set_priority = storage_action_set_priority;
379 surf_storage_model->extension.storage.open = storage_action_open;
380 surf_storage_model->extension.storage.close = storage_action_close;
381 surf_storage_model->extension.storage.read = storage_action_read;
382 surf_storage_model->extension.storage.write = storage_action_write;
383 surf_storage_model->extension.storage.stat = storage_action_stat;
384 surf_storage_model->extension.storage.create_resource = storage_create_resource;
386 if (!storage_maxmin_system) {
387 storage_maxmin_system = lmm_system_new(storage_selective_update);
392 void surf_storage_model_init_default(void)
394 surf_storage_model_init_internal();
395 storage_define_callbacks();
397 xbt_dynar_push(model_list, &surf_storage_model);