Logo AND Algorithmique Numérique Distribuée

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