Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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
35 static surf_action_t storage_action_open(void *storage, const char* path, const char* mode)
36 {
37   return storage_action_sleep(storage,1.0);
38 }
39
40 static surf_action_t storage_action_close(void *storage, surf_file_t fp)
41 {
42   return storage_action_sleep(storage,2.0);
43 }
44
45 static surf_action_t storage_action_read(void *storage, void* ptr, size_t size, size_t nmemb, surf_file_t stream)
46 {
47   return storage_action_sleep(storage,3.0);
48 }
49
50 static surf_action_t storage_action_write(void *storage, const void* ptr, size_t size, size_t nmemb, surf_file_t stream)
51 {
52   return storage_action_sleep(storage,4.0);
53 }
54
55 static surf_action_t storage_action_stat(void *storage, int fd, void* buf)
56 {
57   return storage_action_sleep(storage,5.0);
58 }
59
60 static surf_action_t storage_action_execute (void *storage, double size)
61 {
62   surf_action_storage_t action = NULL;
63   storage_t STORAGE = storage;
64
65   XBT_IN("(%s,%g)", surf_resource_name(STORAGE), size);
66   action =
67       surf_action_new(sizeof(s_surf_action_storage_t), size, surf_storage_model,
68           STORAGE->state_current != SURF_RESOURCE_ON);
69
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);
74   XBT_OUT();
75   return (surf_action_t) action;
76 }
77
78 static surf_action_t storage_action_sleep (void *storage, double duration)
79 {
80   surf_action_storage_t action = NULL;
81
82   if (duration > 0)
83     duration = MAX(duration, MAXMIN_PRECISION);
84
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);
96   }
97   XBT_OUT();
98   return (surf_action_t) action;
99 }
100
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)
103 {
104   storage_t storage = NULL;
105
106   xbt_assert(!surf_storage_resource_by_name(id),
107               "Storage '%s' declared several times in the platform file",
108               id);
109   storage = (storage_t) surf_resource_new(sizeof(s_storage_t),
110           surf_storage_model, id,storage_properties);
111
112   storage->state_current = SURF_RESOURCE_ON;
113
114   xbt_lib_set(storage_lib, id, SURF_STORAGE_LEVEL, storage);
115
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",
117       id,
118       model,
119       type_id,
120       content,
121       storage_properties);
122
123   return storage;
124 }
125
126 static void storage_finalize(void)
127 {
128   lmm_system_free(storage_maxmin_system);
129   storage_maxmin_system = NULL;
130
131   surf_model_exit(surf_storage_model);
132   surf_storage_model = NULL;
133
134   xbt_swag_free
135       (storage_running_action_set_that_does_not_need_being_checked);
136   storage_running_action_set_that_does_not_need_being_checked = NULL;
137 }
138
139 static void storage_update_actions_state(double now, double delta)
140 {
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) {
145
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);
158     }
159   }
160
161   return;
162 }
163
164 static double storage_share_resources(double NOW)
165 {
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);
171 }
172
173 static int storage_resource_used(void *resource_id)
174 {
175   THROW_UNIMPLEMENTED;
176   return 0;
177 }
178
179 static void storage_resources_state(void *id, tmgr_trace_event_t event_type,
180                                  double value, double time)
181 {
182   THROW_UNIMPLEMENTED;
183 }
184
185 static int storage_action_unref(surf_action_t action)
186 {
187   action->refcount--;
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);
193 #ifdef HAVE_TRACING
194     xbt_free(action->category);
195 #endif
196     surf_action_free(&action);
197     return 1;
198   }
199   return 0;
200 }
201
202 static void storage_action_cancel(surf_action_t action)
203 {
204   surf_action_state_set(action, SURF_ACTION_FAILED);
205   return;
206 }
207
208 static void storage_action_state_set(surf_action_t action, e_surf_action_state_t state)
209 {
210   surf_action_state_set(action, state);
211   return;
212 }
213
214 static void storage_action_suspend(surf_action_t action)
215 {
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,
220                                0.0);
221     ((surf_action_lmm_t) action)->suspended = 1;
222   }
223   XBT_OUT();
224 }
225
226 static void storage_action_resume(surf_action_t action)
227 {
228   THROW_UNIMPLEMENTED;
229 }
230
231 static int storage_action_is_suspended(surf_action_t action)
232 {
233   return (((surf_action_lmm_t) action)->suspended == 1);
234 }
235
236 static void storage_action_set_max_duration(surf_action_t action, double duration)
237 {
238   THROW_UNIMPLEMENTED;
239 }
240
241 static void storage_action_set_priority(surf_action_t action, double priority)
242 {
243   THROW_UNIMPLEMENTED;
244 }
245
246 static void parse_storage_init(sg_platf_storage_cbarg_t storage)
247 {
248   void* stype = xbt_lib_get_or_null(storage_type_lib,
249                                     storage->type_id,
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",
253 //      storage->id,
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);
258
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,
263      NULL);
264 }
265
266 static void parse_mstorage_init(sg_platf_mstorage_cbarg_t mstorage)
267 {
268   XBT_DEBUG("parse_mstorage_init");
269 }
270
271 static void parse_storage_type_init(sg_platf_storage_type_cbarg_t storagetype_)
272 {
273   XBT_DEBUG("parse_storage_type_init");
274 }
275
276 static void parse_mount_init(sg_platf_mount_cbarg_t mount)
277 {
278   XBT_DEBUG("parse_mount_init");
279 }
280
281 static void storage_define_callbacks()
282 {
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);
287 }
288
289 static void surf_storage_model_init_internal(void)
290 {
291   s_surf_action_t action;
292
293   XBT_DEBUG("surf_storage_model_init_internal");
294   surf_storage_model = surf_model_init();
295
296   storage_running_action_set_that_does_not_need_being_checked =
297       xbt_swag_new(xbt_swag_offset(action, state_hookup));
298
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;
303
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;
309
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;
315
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;
322
323   if (!storage_maxmin_system) {
324     storage_maxmin_system = lmm_system_new(storage_selective_update);
325   }
326
327 }
328
329 void surf_storage_model_init_default(void)
330 {
331   surf_storage_model_init_internal();
332   storage_define_callbacks();
333
334   xbt_dynar_push(model_list, &surf_storage_model);
335 }