Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
74627b925a86699c9006bd52d2272e73fb95f473
[simgrid.git] / src / surf / storage.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012. The SimGrid Team.
2  * All rights reserved.                                                                 */
3
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.             */
6
7 #include "xbt/ex.h"
8 #include "xbt/dict.h"
9 #include "portable.h"
10 #include "surf_private.h"
11 #include "storage_private.h"
12 #include "surf/surf_resource.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_storage, surf,
15                                 "Logging specific to the SURF storage module");
16
17 surf_model_t surf_storage_model = NULL;
18 lmm_system_t storage_maxmin_system = NULL;
19 static int storage_selective_update = 0;
20 static xbt_swag_t
21     storage_running_action_set_that_does_not_need_being_checked = NULL;
22
23 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
24 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
25
26 typedef struct surf_storage {
27   s_surf_resource_t generic_resource;
28   const char* type;
29   const char* content; /*should be a dict */
30 } s_surf_storage_t, *surf_storage_t;
31
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);
35
36 static surf_action_t storage_action_open(void *storage, const char* path, const char* mode)
37 {
38   char *storage_type_id = xbt_lib_get_or_null(
39       storage_lib,
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);
45
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);
49
50   surf_action_t action = storage_action_execute(storage,size);
51   return action;
52 }
53
54 static surf_action_t storage_action_close(void *storage, m_file_t fp)
55 {
56   return storage_action_sleep(storage,2.0);
57 }
58
59 static surf_action_t storage_action_read(void *storage, void* ptr, size_t size, size_t nmemb, m_file_t stream)
60 {
61   return storage_action_sleep(storage,3.0);
62 }
63
64 static surf_action_t storage_action_write(void *storage, const void* ptr, size_t size, size_t nmemb, m_file_t stream)
65 {
66   return storage_action_sleep(storage,4.0);
67 }
68
69 static surf_action_t storage_action_stat(void *storage, int fd, void* buf)
70 {
71   return storage_action_sleep(storage,5.0);
72 }
73
74 static surf_action_t storage_action_execute (void *storage, double size)
75 {
76   surf_action_storage_t action = NULL;
77   storage_t STORAGE = storage;
78
79   XBT_IN("(%s,%g)", surf_resource_name(STORAGE), size);
80   action =
81       surf_action_new(sizeof(s_surf_action_storage_t), size, surf_storage_model,
82           STORAGE->state_current != SURF_RESOURCE_ON);
83
84   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
85                                                    calloc but it seems to help valgrind... */
86
87   GENERIC_LMM_ACTION(action).variable =
88       lmm_variable_new(storage_maxmin_system, action, 1.0, -1.0 , 3);
89
90   lmm_expand(storage_maxmin_system, STORAGE->constraint,
91              GENERIC_LMM_ACTION(action).variable, 1.0);
92   XBT_OUT();
93   return (surf_action_t) action;
94 }
95
96 static surf_action_t storage_action_sleep (void *storage, double duration)
97 {
98   surf_action_storage_t action = NULL;
99
100   if (duration > 0)
101     duration = MAX(duration, MAXMIN_PRECISION);
102
103   XBT_IN("(%s,%g)", surf_resource_name(storage), duration);
104   action = (surf_action_storage_t) storage_action_execute(storage, 1.0);
105   GENERIC_ACTION(action).max_duration = duration;
106   GENERIC_LMM_ACTION(action).suspended = 2;
107   if (duration == NO_MAX_DURATION) {
108     /* Move to the *end* of the corresponding action set. This convention
109        is used to speed up update_resource_state  */
110     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
111     ((surf_action_t) action)->state_set =
112         storage_running_action_set_that_does_not_need_being_checked;
113     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
114   }
115
116   lmm_update_variable_weight(storage_maxmin_system,
117                              GENERIC_LMM_ACTION(action).variable, 0.0);
118
119   XBT_OUT();
120   return (surf_action_t) action;
121 }
122
123 static void* storage_create_resource(const char* id, const char* model,const char* type_id)
124 {
125   storage_t storage = NULL;
126
127   xbt_assert(!surf_storage_resource_by_name(id),
128               "Storage '%s' declared several times in the platform file",
129               id);
130   storage = (storage_t) surf_resource_new(sizeof(s_storage_t),
131           surf_storage_model, id,NULL);
132
133   storage->state_current = SURF_RESOURCE_ON;
134
135   storage_type_t storage_type = xbt_lib_get_or_null(storage_type_lib, type_id,ROUTING_STORAGE_TYPE_LEVEL);
136   double Bread  = atof(xbt_dict_get(storage_type->properties,"Bread"));
137   double Bwrite = atof(xbt_dict_get(storage_type->properties,"Bwrite"));
138   double Bconnexion   = atof(xbt_dict_get(storage_type->properties,"Bconnexion"));
139   storage->constraint       = lmm_constraint_new(storage_maxmin_system, storage, Bconnexion);
140   storage->constraint_read  = lmm_constraint_new(storage_maxmin_system, storage, Bread);
141   storage->constraint_write = lmm_constraint_new(storage_maxmin_system, storage, Bwrite);
142
143   xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
144
145   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 '%f'\n",
146       id,
147       model,
148       type_id,
149       storage_type->properties,
150       Bread);
151
152   return storage;
153 }
154
155 static void storage_finalize(void)
156 {
157   lmm_system_free(storage_maxmin_system);
158   storage_maxmin_system = NULL;
159
160   surf_model_exit(surf_storage_model);
161   surf_storage_model = NULL;
162
163   xbt_swag_free
164       (storage_running_action_set_that_does_not_need_being_checked);
165   storage_running_action_set_that_does_not_need_being_checked = NULL;
166 }
167
168 static void storage_update_actions_state(double now, double delta)
169 {
170   surf_action_storage_t action = NULL;
171   surf_action_storage_t next_action = NULL;
172   xbt_swag_t running_actions = surf_storage_model->states.running_action_set;
173   xbt_swag_foreach_safe(action, next_action, running_actions) {
174
175     double_update(&(GENERIC_ACTION(action).remains),
176                   lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * delta);
177     if (GENERIC_LMM_ACTION(action).generic_action.max_duration != NO_MAX_DURATION)
178       double_update(&(GENERIC_ACTION(action).max_duration), delta);
179     if ((GENERIC_ACTION(action).remains <= 0) &&
180         (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
181       GENERIC_ACTION(action).finish = surf_get_clock();
182       storage_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
183     } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
184                (GENERIC_ACTION(action).max_duration <= 0)) {
185       GENERIC_ACTION(action).finish = surf_get_clock();
186       storage_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
187     }
188   }
189
190   return;
191 }
192
193 static double storage_share_resources(double NOW)
194 {
195   XBT_DEBUG("storage_share_resources %f",NOW);
196   s_surf_action_storage_t action;
197   return generic_maxmin_share_resources(surf_storage_model->states.running_action_set,
198       xbt_swag_offset(action, generic_lmm_action.variable),
199       storage_maxmin_system, lmm_solve);
200 }
201
202 static int storage_resource_used(void *resource_id)
203 {
204   THROW_UNIMPLEMENTED;
205   return 0;
206 }
207
208 static void storage_resources_state(void *id, tmgr_trace_event_t event_type,
209                                  double value, double time)
210 {
211   THROW_UNIMPLEMENTED;
212 }
213
214 static int storage_action_unref(surf_action_t action)
215 {
216   action->refcount--;
217   if (!action->refcount) {
218     xbt_swag_remove(action, action->state_set);
219     if (((surf_action_lmm_t) action)->variable)
220       lmm_variable_free(storage_maxmin_system,
221                         ((surf_action_lmm_t) action)->variable);
222 #ifdef HAVE_TRACING
223     xbt_free(action->category);
224 #endif
225     surf_action_free(&action);
226     return 1;
227   }
228   return 0;
229 }
230
231 static void storage_action_cancel(surf_action_t action)
232 {
233   surf_action_state_set(action, SURF_ACTION_FAILED);
234   return;
235 }
236
237 static void storage_action_state_set(surf_action_t action, e_surf_action_state_t state)
238 {
239   surf_action_state_set(action, state);
240   return;
241 }
242
243 static void storage_action_suspend(surf_action_t action)
244 {
245   XBT_IN("(%p)", action);
246   if (((surf_action_lmm_t) action)->suspended != 2) {
247     lmm_update_variable_weight(storage_maxmin_system,
248                                ((surf_action_lmm_t) action)->variable,
249                                0.0);
250     ((surf_action_lmm_t) action)->suspended = 1;
251   }
252   XBT_OUT();
253 }
254
255 static void storage_action_resume(surf_action_t action)
256 {
257   THROW_UNIMPLEMENTED;
258 }
259
260 static int storage_action_is_suspended(surf_action_t action)
261 {
262   return (((surf_action_lmm_t) action)->suspended == 1);
263 }
264
265 static void storage_action_set_max_duration(surf_action_t action, double duration)
266 {
267   THROW_UNIMPLEMENTED;
268 }
269
270 static void storage_action_set_priority(surf_action_t action, double priority)
271 {
272   THROW_UNIMPLEMENTED;
273 }
274
275 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
276 {
277   void* stype = xbt_lib_get_or_null(storage_type_lib,
278                                     storage->type_id,
279                                     ROUTING_STORAGE_TYPE_LEVEL);
280   if(!stype) xbt_die("No storage type '%s'",storage->type_id);
281 //  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",
282 //      storage->id,
283 //      ((storage_type_t) stype)->model,
284 //      ((storage_type_t) stype)->type_id,
285 //      ((storage_type_t) stype)->content,
286 //      ((storage_type_t) stype)->properties);
287
288  storage_create_resource(storage->id,
289      ((storage_type_t) stype)->model,
290      ((storage_type_t) stype)->type_id);
291 }
292
293 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
294 {
295   XBT_DEBUG("parse_mstorage_init");
296 }
297
298 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
299 {
300   XBT_DEBUG("parse_storage_type_init");
301 }
302
303 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
304 {
305   XBT_DEBUG("parse_mount_init");
306 }
307
308 static void storage_define_callbacks()
309 {
310   sg_platf_storage_add_cb(parse_storage_init);
311   sg_platf_storage_type_add_cb(parse_storage_type_init);
312   sg_platf_mstorage_add_cb(parse_mstorage_init);
313   sg_platf_mount_add_cb(parse_mount_init);
314 }
315
316 static void surf_storage_model_init_internal(void)
317 {
318   s_surf_action_t action;
319
320   XBT_DEBUG("surf_storage_model_init_internal");
321   surf_storage_model = surf_model_init();
322
323   storage_running_action_set_that_does_not_need_being_checked =
324       xbt_swag_new(xbt_swag_offset(action, state_hookup));
325
326   surf_storage_model->name = "Storage";
327   surf_storage_model->action_unref = storage_action_unref;
328   surf_storage_model->action_cancel = storage_action_cancel;
329   surf_storage_model->action_state_set = storage_action_state_set;
330
331   surf_storage_model->model_private->finalize = storage_finalize;
332   surf_storage_model->model_private->update_actions_state = storage_update_actions_state;
333   surf_storage_model->model_private->share_resources = storage_share_resources;
334   surf_storage_model->model_private->resource_used = storage_resource_used;
335   surf_storage_model->model_private->update_resource_state = storage_resources_state;
336
337   surf_storage_model->suspend = storage_action_suspend;
338   surf_storage_model->resume = storage_action_resume;
339   surf_storage_model->is_suspended = storage_action_is_suspended;
340   surf_storage_model->set_max_duration = storage_action_set_max_duration;
341   surf_storage_model->set_priority = storage_action_set_priority;
342
343   surf_storage_model->extension.storage.open = storage_action_open;
344   surf_storage_model->extension.storage.close = storage_action_close;
345   surf_storage_model->extension.storage.read = storage_action_read;
346   surf_storage_model->extension.storage.write = storage_action_write;
347   surf_storage_model->extension.storage.stat = storage_action_stat;
348   surf_storage_model->extension.storage.create_resource = storage_create_resource;
349
350   if (!storage_maxmin_system) {
351     storage_maxmin_system = lmm_system_new(storage_selective_update);
352   }
353
354 }
355
356 void surf_storage_model_init_default(void)
357 {
358   surf_storage_model_init_internal();
359   storage_define_callbacks();
360
361   xbt_dynar_push(model_list, &surf_storage_model);
362 }