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
26 typedef struct surf_storage {
27 s_surf_resource_t generic_resource;
29 const char* content; /*should be a dict */
30 } s_surf_storage_t, *surf_storage_t;
32 static void storage_action_state_set(surf_action_t action, e_surf_action_state_t state);
33 static surf_action_t storage_action_sleep (void *storage, double duration);
34 static surf_action_t storage_action_execute (void *storage, double size);
36 static surf_action_t storage_action_open(void *storage, const char* path, const char* mode)
38 char *storage_type_id = xbt_lib_get_or_null(
40 ((storage_t)storage)->generic_resource.name,
41 ROUTING_STORAGE_LEVEL);
42 storage_type_t storage_type = xbt_lib_get_or_null(storage_type_lib, storage_type_id,ROUTING_STORAGE_TYPE_LEVEL);
43 xbt_dict_t content_dict = storage_type->content;
44 content_t content = xbt_dict_get(content_dict,path);
46 double size = content->size;
47 XBT_DEBUG("Disk '%s' with type_d '%s'",((storage_t)storage)->generic_resource.name,storage_type_id);
48 XBT_INFO("\tFile '%s' size '%f'",path,size);
50 surf_action_t action = storage_action_execute(storage,size);
54 static surf_action_t storage_action_close(void *storage, surf_file_t fp)
56 return storage_action_sleep(storage,2.0);
59 static surf_action_t storage_action_read(void *storage, void* ptr, size_t size, size_t nmemb, surf_file_t stream)
61 return storage_action_sleep(storage,3.0);
64 static surf_action_t storage_action_write(void *storage, const void* ptr, size_t size, size_t nmemb, surf_file_t stream)
66 return storage_action_sleep(storage,4.0);
69 static surf_action_t storage_action_stat(void *storage, int fd, void* buf)
71 return storage_action_sleep(storage,5.0);
74 static surf_action_t storage_action_execute (void *storage, double size)
76 surf_action_storage_t action = NULL;
77 storage_t STORAGE = storage;
79 XBT_IN("(%s,%g)", surf_resource_name(STORAGE), size);
81 surf_action_new(sizeof(s_surf_action_storage_t), size, surf_storage_model,
82 STORAGE->state_current != SURF_RESOURCE_ON);
84 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
85 calloc but it seems to help valgrind... */
86 GENERIC_LMM_ACTION(action).variable =
87 lmm_variable_new(storage_maxmin_system, action, 1.0, -1.0 , 1);
89 lmm_expand(storage_maxmin_system, STORAGE->constraint,
90 GENERIC_LMM_ACTION(action).variable, 1.0);
92 return (surf_action_t) action;
95 static surf_action_t storage_action_sleep (void *storage, double duration)
97 surf_action_storage_t action = NULL;
100 duration = MAX(duration, MAXMIN_PRECISION);
102 XBT_IN("(%s,%g)", surf_resource_name(storage), duration);
103 action = (surf_action_storage_t) storage_action_execute(storage, 1.0);
104 GENERIC_ACTION(action).max_duration = duration;
105 GENERIC_LMM_ACTION(action).suspended = 2;
106 if (duration == NO_MAX_DURATION) {
107 /* Move to the *end* of the corresponding action set. This convention
108 is used to speed up update_resource_state */
109 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
110 ((surf_action_t) action)->state_set =
111 storage_running_action_set_that_does_not_need_being_checked;
112 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
115 return (surf_action_t) action;
118 static void* storage_create_resource(const char* id, const char* model,const char* type_id)
120 storage_t storage = NULL;
122 xbt_assert(!surf_storage_resource_by_name(id),
123 "Storage '%s' declared several times in the platform file",
125 storage = (storage_t) surf_resource_new(sizeof(s_storage_t),
126 surf_storage_model, id,NULL);
128 storage->state_current = SURF_RESOURCE_ON;
130 storage_type_t storage_type = xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
131 int Bread = atoi(xbt_dict_get(storage_type->properties,"Bread"));
133 storage->constraint =
134 lmm_constraint_new(storage_maxmin_system, storage,
137 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
139 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 '%d'\n",
143 storage_type->properties,
149 static void storage_finalize(void)
151 lmm_system_free(storage_maxmin_system);
152 storage_maxmin_system = NULL;
154 surf_model_exit(surf_storage_model);
155 surf_storage_model = NULL;
158 (storage_running_action_set_that_does_not_need_being_checked);
159 storage_running_action_set_that_does_not_need_being_checked = NULL;
162 static void storage_update_actions_state(double now, double delta)
164 surf_action_storage_t action = NULL;
165 surf_action_storage_t next_action = NULL;
166 xbt_swag_t running_actions = surf_storage_model->states.running_action_set;
167 xbt_swag_foreach_safe(action, next_action, running_actions) {
169 double_update(&(GENERIC_ACTION(action).remains),
170 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * delta);
171 if (GENERIC_LMM_ACTION(action).generic_action.max_duration != NO_MAX_DURATION)
172 double_update(&(GENERIC_ACTION(action).max_duration), delta);
173 if ((GENERIC_ACTION(action).remains <= 0) &&
174 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
175 GENERIC_ACTION(action).finish = surf_get_clock();
176 storage_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
177 } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
178 (GENERIC_ACTION(action).max_duration <= 0)) {
179 GENERIC_ACTION(action).finish = surf_get_clock();
180 storage_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
187 static double storage_share_resources(double NOW)
189 XBT_DEBUG("storage_share_resources %f",NOW);
190 s_surf_action_storage_t action;
191 return generic_maxmin_share_resources(surf_storage_model->states.running_action_set,
192 xbt_swag_offset(action, generic_lmm_action.variable),
193 storage_maxmin_system, lmm_solve);
196 static int storage_resource_used(void *resource_id)
202 static void storage_resources_state(void *id, tmgr_trace_event_t event_type,
203 double value, double time)
208 static int storage_action_unref(surf_action_t action)
211 if (!action->refcount) {
212 xbt_swag_remove(action, action->state_set);
213 if (((surf_action_lmm_t) action)->variable)
214 lmm_variable_free(storage_maxmin_system,
215 ((surf_action_lmm_t) action)->variable);
217 xbt_free(action->category);
219 surf_action_free(&action);
225 static void storage_action_cancel(surf_action_t action)
227 surf_action_state_set(action, SURF_ACTION_FAILED);
231 static void storage_action_state_set(surf_action_t action, e_surf_action_state_t state)
233 surf_action_state_set(action, state);
237 static void storage_action_suspend(surf_action_t action)
239 XBT_IN("(%p)", action);
240 if (((surf_action_lmm_t) action)->suspended != 2) {
241 lmm_update_variable_weight(storage_maxmin_system,
242 ((surf_action_lmm_t) action)->variable,
244 ((surf_action_lmm_t) action)->suspended = 1;
249 static void storage_action_resume(surf_action_t action)
254 static int storage_action_is_suspended(surf_action_t action)
256 return (((surf_action_lmm_t) action)->suspended == 1);
259 static void storage_action_set_max_duration(surf_action_t action, double duration)
264 static void storage_action_set_priority(surf_action_t action, double priority)
269 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
271 void* stype = xbt_lib_get_or_null(storage_type_lib,
273 ROUTING_STORAGE_TYPE_LEVEL);
274 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
275 // 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",
277 // ((storage_type_t) stype)->model,
278 // ((storage_type_t) stype)->type_id,
279 // ((storage_type_t) stype)->content,
280 // ((storage_type_t) stype)->properties);
282 storage_create_resource(storage->id,
283 ((storage_type_t) stype)->model,
284 ((storage_type_t) stype)->type_id);
287 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
289 XBT_DEBUG("parse_mstorage_init");
292 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
294 XBT_DEBUG("parse_storage_type_init");
297 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
299 XBT_DEBUG("parse_mount_init");
302 static void storage_define_callbacks()
304 sg_platf_storage_add_cb(parse_storage_init);
305 sg_platf_storage_type_add_cb(parse_storage_type_init);
306 sg_platf_mstorage_add_cb(parse_mstorage_init);
307 sg_platf_mount_add_cb(parse_mount_init);
310 static void surf_storage_model_init_internal(void)
312 s_surf_action_t action;
314 XBT_DEBUG("surf_storage_model_init_internal");
315 surf_storage_model = surf_model_init();
317 storage_running_action_set_that_does_not_need_being_checked =
318 xbt_swag_new(xbt_swag_offset(action, state_hookup));
320 surf_storage_model->name = "Storage";
321 surf_storage_model->action_unref = storage_action_unref;
322 surf_storage_model->action_cancel = storage_action_cancel;
323 surf_storage_model->action_state_set = storage_action_state_set;
325 surf_storage_model->model_private->finalize = storage_finalize;
326 surf_storage_model->model_private->update_actions_state = storage_update_actions_state;
327 surf_storage_model->model_private->share_resources = storage_share_resources;
328 surf_storage_model->model_private->resource_used = storage_resource_used;
329 surf_storage_model->model_private->update_resource_state = storage_resources_state;
331 surf_storage_model->suspend = storage_action_suspend;
332 surf_storage_model->resume = storage_action_resume;
333 surf_storage_model->is_suspended = storage_action_is_suspended;
334 surf_storage_model->set_max_duration = storage_action_set_max_duration;
335 surf_storage_model->set_priority = storage_action_set_priority;
337 surf_storage_model->extension.storage.open = storage_action_open;
338 surf_storage_model->extension.storage.close = storage_action_close;
339 surf_storage_model->extension.storage.read = storage_action_read;
340 surf_storage_model->extension.storage.write = storage_action_write;
341 surf_storage_model->extension.storage.stat = storage_action_stat;
342 surf_storage_model->extension.storage.create_resource = storage_create_resource;
344 if (!storage_maxmin_system) {
345 storage_maxmin_system = lmm_system_new(storage_selective_update);
350 void surf_storage_model_init_default(void)
352 surf_storage_model_init_internal();
353 storage_define_callbacks();
355 xbt_dynar_push(model_list, &surf_storage_model);