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);
35 static surf_action_t storage_action_open(void *storage, const char* path, const char* mode)
37 return storage_action_sleep(storage,1.0);
40 static surf_action_t storage_action_close(void *storage, surf_file_t fp)
42 return storage_action_sleep(storage,2.0);
45 static surf_action_t storage_action_read(void *storage, void* ptr, size_t size, size_t nmemb, surf_file_t stream)
47 return storage_action_sleep(storage,3.0);
50 static surf_action_t storage_action_write(void *storage, const void* ptr, size_t size, size_t nmemb, surf_file_t stream)
52 return storage_action_sleep(storage,4.0);
55 static surf_action_t storage_action_stat(void *storage, int fd, void* buf)
57 return storage_action_sleep(storage,5.0);
60 static surf_action_t storage_action_execute (void *storage, double size)
62 surf_action_storage_t action = NULL;
63 storage_t STORAGE = storage;
65 XBT_IN("(%s,%g)", surf_resource_name(STORAGE), size);
67 surf_action_new(sizeof(s_surf_action_storage_t), size, surf_storage_model,
68 STORAGE->state_current != SURF_RESOURCE_ON);
70 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
71 calloc but it seems to help valgrind... */
72 GENERIC_LMM_ACTION(action).variable =
73 lmm_variable_new(storage_maxmin_system, action, 1.0, -1.0 , 1);
75 return (surf_action_t) action;
78 static surf_action_t storage_action_sleep (void *storage, double duration)
80 surf_action_storage_t action = NULL;
83 duration = MAX(duration, MAXMIN_PRECISION);
85 XBT_IN("(%s,%g)", surf_resource_name(storage), duration);
86 action = (surf_action_storage_t) storage_action_execute(storage, 1.0);
87 GENERIC_ACTION(action).max_duration = duration;
88 GENERIC_LMM_ACTION(action).suspended = 2;
89 if (duration == NO_MAX_DURATION) {
90 /* Move to the *end* of the corresponding action set. This convention
91 is used to speed up update_resource_state */
92 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
93 ((surf_action_t) action)->state_set =
94 storage_running_action_set_that_does_not_need_being_checked;
95 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
98 return (surf_action_t) action;
101 static void* storage_create_resource(const char* id, const char* model,const char* type_id,
102 const char* content, xbt_dict_t storage_properties)
104 storage_t storage = NULL;
106 xbt_assert(!surf_storage_resource_by_name(id),
107 "Storage '%s' declared several times in the platform file",
109 storage = (storage_t) surf_resource_new(sizeof(s_storage_t),
110 surf_storage_model, id,storage_properties);
112 storage->state_current = SURF_RESOURCE_ON;
114 xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
116 XBT_DEBUG("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",
126 static void storage_finalize(void)
128 lmm_system_free(storage_maxmin_system);
129 storage_maxmin_system = NULL;
131 surf_model_exit(surf_storage_model);
132 surf_storage_model = NULL;
135 (storage_running_action_set_that_does_not_need_being_checked);
136 storage_running_action_set_that_does_not_need_being_checked = NULL;
139 static void storage_update_actions_state(double now, double delta)
141 surf_action_storage_t action = NULL;
142 surf_action_storage_t next_action = NULL;
143 xbt_swag_t running_actions = surf_storage_model->states.running_action_set;
144 xbt_swag_foreach_safe(action, next_action, running_actions) {
146 double_update(&(GENERIC_ACTION(action).remains),
147 lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * delta);
148 if (GENERIC_LMM_ACTION(action).generic_action.max_duration != NO_MAX_DURATION)
149 double_update(&(GENERIC_ACTION(action).max_duration), delta);
150 if ((GENERIC_ACTION(action).remains <= 0) &&
151 (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
152 GENERIC_ACTION(action).finish = surf_get_clock();
153 storage_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
154 } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
155 (GENERIC_ACTION(action).max_duration <= 0)) {
156 GENERIC_ACTION(action).finish = surf_get_clock();
157 storage_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
164 static double storage_share_resources(double NOW)
166 XBT_DEBUG("storage_share_resources %f",NOW);
167 s_surf_action_storage_t action;
168 return generic_maxmin_share_resources(surf_storage_model->states.running_action_set,
169 xbt_swag_offset(action, generic_lmm_action.variable),
170 storage_maxmin_system, lmm_solve);
173 static int storage_resource_used(void *resource_id)
179 static void storage_resources_state(void *id, tmgr_trace_event_t event_type,
180 double value, double time)
185 static int storage_action_unref(surf_action_t action)
188 if (!action->refcount) {
189 xbt_swag_remove(action, action->state_set);
190 if (((surf_action_lmm_t) action)->variable)
191 lmm_variable_free(storage_maxmin_system,
192 ((surf_action_lmm_t) action)->variable);
194 xbt_free(action->category);
196 surf_action_free(&action);
202 static void storage_action_cancel(surf_action_t action)
204 surf_action_state_set(action, SURF_ACTION_FAILED);
208 static void storage_action_state_set(surf_action_t action, e_surf_action_state_t state)
210 surf_action_state_set(action, state);
214 static void storage_action_suspend(surf_action_t action)
216 XBT_IN("(%p)", action);
217 if (((surf_action_lmm_t) action)->suspended != 2) {
218 lmm_update_variable_weight(storage_maxmin_system,
219 ((surf_action_lmm_t) action)->variable,
221 ((surf_action_lmm_t) action)->suspended = 1;
226 static void storage_action_resume(surf_action_t action)
231 static int storage_action_is_suspended(surf_action_t action)
233 return (((surf_action_lmm_t) action)->suspended == 1);
236 static void storage_action_set_max_duration(surf_action_t action, double duration)
241 static void storage_action_set_priority(surf_action_t action, double priority)
246 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
248 void* stype = xbt_lib_get_or_null(storage_type_lib,
250 ROUTING_STORAGE_TYPE_LEVEL);
251 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
252 // 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",
254 // ((storage_type_t) stype)->model,
255 // ((storage_type_t) stype)->type_id,
256 // ((storage_type_t) stype)->content,
257 // ((storage_type_t) stype)->properties);
259 storage_create_resource(storage->id,
260 ((storage_type_t) stype)->model,
261 ((storage_type_t) stype)->type_id,
262 ((storage_type_t) stype)->content,
266 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
268 XBT_DEBUG("parse_mstorage_init");
271 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
273 XBT_DEBUG("parse_storage_type_init");
276 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
278 XBT_DEBUG("parse_mount_init");
281 static void storage_define_callbacks()
283 sg_platf_storage_add_cb(parse_storage_init);
284 sg_platf_storage_type_add_cb(parse_storage_type_init);
285 sg_platf_mstorage_add_cb(parse_mstorage_init);
286 sg_platf_mount_add_cb(parse_mount_init);
289 static void surf_storage_model_init_internal(void)
291 s_surf_action_t action;
293 XBT_DEBUG("surf_storage_model_init_internal");
294 surf_storage_model = surf_model_init();
296 storage_running_action_set_that_does_not_need_being_checked =
297 xbt_swag_new(xbt_swag_offset(action, state_hookup));
299 surf_storage_model->name = "Storage";
300 surf_storage_model->action_unref = storage_action_unref;
301 surf_storage_model->action_cancel = storage_action_cancel;
302 surf_storage_model->action_state_set = storage_action_state_set;
304 surf_storage_model->model_private->finalize = storage_finalize;
305 surf_storage_model->model_private->update_actions_state = storage_update_actions_state;
306 surf_storage_model->model_private->share_resources = storage_share_resources;
307 surf_storage_model->model_private->resource_used = storage_resource_used;
308 surf_storage_model->model_private->update_resource_state = storage_resources_state;
310 surf_storage_model->suspend = storage_action_suspend;
311 surf_storage_model->resume = storage_action_resume;
312 surf_storage_model->is_suspended = storage_action_is_suspended;
313 surf_storage_model->set_max_duration = storage_action_set_max_duration;
314 surf_storage_model->set_priority = storage_action_set_priority;
316 surf_storage_model->extension.storage.open = storage_action_open;
317 surf_storage_model->extension.storage.close = storage_action_close;
318 surf_storage_model->extension.storage.read = storage_action_read;
319 surf_storage_model->extension.storage.write = storage_action_write;
320 surf_storage_model->extension.storage.stat = storage_action_stat;
321 surf_storage_model->extension.storage.create_resource = storage_create_resource;
323 if (!storage_maxmin_system) {
324 storage_maxmin_system = lmm_system_new(storage_selective_update);
329 void surf_storage_model_init_default(void)
331 surf_storage_model_init_internal();
332 storage_define_callbacks();
334 xbt_dynar_push(model_list, &surf_storage_model);