Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3334936f777134a6e5367f5e235564e8f4bdb06c
[simgrid.git] / src / simdag / sd_global.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. 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 "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/dynar.h"
10 #include "surf/surf.h"
11 #include "xbt/ex.h"
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14 #include "xbt/config.h"
15 #include "instr/instr_private.h"
16 #include "surf/surfxml_parse.h"
17 #ifdef HAVE_LUA
18 #include <lua.h>
19 #include <lauxlib.h>
20 #include <lualib.h>
21 #endif
22
23 #ifdef HAVE_JEDULE
24 #include "instr/jedule/jedule_sd_binding.h"
25 #endif
26
27 XBT_LOG_NEW_CATEGORY(sd, "Logging specific to SimDag");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel, sd,
29                                 "Logging specific to SimDag (kernel)");
30
31 SD_global_t sd_global = NULL;
32
33 /**
34  * \brief Initialises SD internal data
35  *
36  * This function must be called before any other SD function. Then you
37  * should call SD_create_environment().
38  *
39  * \param argc argument number
40  * \param argv argument list
41  * \see SD_create_environment(), SD_exit()
42  */
43 void SD_init(int *argc, char **argv)
44 {
45 #ifdef HAVE_TRACING
46   TRACE_global_init(argc, argv);
47 #endif
48
49   s_SD_task_t task;
50
51   xbt_assert(sd_global == NULL, "SD_init() already called");
52
53   /* Connect our log channels: that must be done manually under windows */
54   XBT_LOG_CONNECT(sd_kernel, sd);
55   XBT_LOG_CONNECT(sd_task, sd);
56   XBT_LOG_CONNECT(sd_workstation, sd);
57
58
59   sd_global = xbt_new(s_SD_global_t, 1);
60   sd_global->workstation_list = NULL;
61   sd_global->link_list = NULL;
62   sd_global->recyclable_route = NULL;
63   sd_global->watch_point_reached = 0;
64
65   sd_global->task_mallocator=xbt_mallocator_new(65536, SD_task_new_f,SD_task_free_f,SD_task_recycle_f);
66
67   sd_global->not_scheduled_task_set =
68       xbt_swag_new(xbt_swag_offset(task, state_hookup));
69   sd_global->schedulable_task_set =
70       xbt_swag_new(xbt_swag_offset(task, state_hookup));
71   sd_global->scheduled_task_set =
72       xbt_swag_new(xbt_swag_offset(task, state_hookup));
73   sd_global->runnable_task_set =
74       xbt_swag_new(xbt_swag_offset(task, state_hookup));
75   sd_global->in_fifo_task_set =
76       xbt_swag_new(xbt_swag_offset(task, state_hookup));
77   sd_global->running_task_set =
78       xbt_swag_new(xbt_swag_offset(task, state_hookup));
79   sd_global->done_task_set =
80       xbt_swag_new(xbt_swag_offset(task, state_hookup));
81   sd_global->failed_task_set =
82       xbt_swag_new(xbt_swag_offset(task, state_hookup));
83   sd_global->return_set =
84       xbt_swag_new(xbt_swag_offset(task, return_hookup));
85   sd_global->task_number = 0;
86
87   surf_init(argc, argv);
88
89   xbt_cfg_setdefault_string(_surf_cfg_set, "workstation/model",
90                             "ptask_L07");
91
92 #ifdef HAVE_TRACING
93   TRACE_start ();
94 #endif
95
96 #ifdef HAVE_JEDULE
97   jedule_sd_init();
98 #endif
99
100   XBT_DEBUG("ADD SD LEVELS");
101   SD_HOST_LEVEL = xbt_lib_add_level(host_lib,__SD_workstation_destroy);
102   SD_LINK_LEVEL = xbt_lib_add_level(link_lib,__SD_link_destroy);
103 }
104
105 /**
106  * \brief Reinits the application part of the simulation (experimental feature)
107  *
108  * This function allows you to run several simulations on the same platform
109  * by resetting the part describing the application.
110  *
111  * @warning: this function is still experimental and not perfect. For example,
112  * the simulation clock (and traces usage) is not reset. So, do not use it if
113  * you use traces in your simulation, and do not use absolute timing after using it.
114  * That being said, this function is still precious if you want to compare a bunch of
115  * heuristics on the same platforms.
116  */
117 void SD_application_reinit(void)
118 {
119
120   s_SD_task_t task;
121
122   XBT_DEBUG("Recreating the swags...");
123   xbt_swag_free(sd_global->not_scheduled_task_set);
124   xbt_swag_free(sd_global->schedulable_task_set);
125   xbt_swag_free(sd_global->scheduled_task_set);
126   xbt_swag_free(sd_global->runnable_task_set);
127   xbt_swag_free(sd_global->in_fifo_task_set);
128   xbt_swag_free(sd_global->running_task_set);
129   xbt_swag_free(sd_global->done_task_set);
130   xbt_swag_free(sd_global->failed_task_set);
131
132   sd_global->not_scheduled_task_set =
133       xbt_swag_new(xbt_swag_offset(task, state_hookup));
134   sd_global->schedulable_task_set =
135       xbt_swag_new(xbt_swag_offset(task, state_hookup));
136   sd_global->scheduled_task_set =
137       xbt_swag_new(xbt_swag_offset(task, state_hookup));
138   sd_global->runnable_task_set =
139       xbt_swag_new(xbt_swag_offset(task, state_hookup));
140   sd_global->in_fifo_task_set =
141       xbt_swag_new(xbt_swag_offset(task, state_hookup));
142   sd_global->running_task_set =
143       xbt_swag_new(xbt_swag_offset(task, state_hookup));
144   sd_global->done_task_set =
145       xbt_swag_new(xbt_swag_offset(task, state_hookup));
146   sd_global->failed_task_set =
147       xbt_swag_new(xbt_swag_offset(task, state_hookup));
148   sd_global->task_number = 0;
149
150 #ifdef HAVE_JEDULE
151   jedule_sd_cleanup();
152   jedule_sd_init();
153 #endif
154 }
155
156 /**
157  * \brief Creates the environment
158  *
159  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
160  * \ref SD_link_management "links") is created with the data stored in the given XML
161  * platform file.
162  *
163  * \param platform_file name of an XML file describing the environment to create
164  * \see SD_workstation_management, SD_link_management
165  *
166  * The XML file follows this DTD:
167  *
168  *     \include simgrid.dtd
169  *
170  * Here is a small example of such a platform:
171  *
172  *     \include small_platform.xml
173  */
174 void SD_create_environment(const char *platform_file)
175 {
176   xbt_lib_cursor_t cursor = NULL;
177   char *name = NULL;
178   void **surf_workstation = NULL;
179   void **surf_link = NULL;
180
181   parse_platform_file(platform_file);
182
183   /* now let's create the SD wrappers for workstations and links */
184   xbt_lib_foreach(host_lib, cursor, name, surf_workstation){
185           if(surf_workstation[SURF_WKS_LEVEL])
186                   __SD_workstation_create(surf_workstation[SURF_WKS_LEVEL], NULL);
187   }
188
189   xbt_lib_foreach(link_lib, cursor, name, surf_link) {
190         if(surf_link[SURF_LINK_LEVEL])
191     __SD_link_create(surf_link[SURF_LINK_LEVEL], NULL);
192   }
193
194   XBT_DEBUG("Workstation number: %d, link number: %d",
195          SD_workstation_get_number(), SD_link_get_number());
196 #ifdef HAVE_JEDULE
197   jedule_setup_platform();
198 #endif
199 }
200
201 /**
202  * \brief Launches the simulation.
203  *
204  * The function will execute the \ref SD_RUNNABLE runnable tasks.
205  * If \a how_long is positive, then the simulation will be stopped either
206  * when time reaches \a how_long or when a watch point is reached.
207  * A nonpositive value for \a how_long means no time limit, in which case
208  * the simulation will be stopped either when a watch point is reached or
209  * when no more task can be executed.
210  * Then you can call SD_simulate() again.
211  *
212  * \param how_long maximum duration of the simulation (a negative value means no time limit)
213  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
214  * \see SD_task_schedule(), SD_task_watch()
215  */
216
217 xbt_dynar_t SD_simulate(double how_long) {
218   xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
219   SD_task_t task;
220
221   SD_simulate_swag(how_long);
222   while( (task = xbt_swag_extract(sd_global->return_set)) != NULL) {
223     xbt_dynar_push(changed_tasks, &task);
224   }
225
226   return changed_tasks;
227 }
228
229 xbt_swag_t SD_simulate_swag(double how_long) {
230   double total_time = 0.0;      /* we stop the simulation when total_time >= how_long */
231   double elapsed_time = 0.0;
232   SD_task_t task, task_safe, dst;
233   SD_dependency_t dependency;
234   surf_action_t action;
235   unsigned int iter, depcnt;
236   static int first_time = 1;
237
238    if (first_time) {
239      XBT_VERB("Starting simulation...");
240
241      surf_presolve();            /* Takes traces into account */
242      first_time = 0;
243    }
244
245   sd_global->watch_point_reached = 0;
246
247   xbt_swag_reset(sd_global->return_set);
248
249   /* explore the runnable tasks */
250   xbt_swag_foreach_safe(task, task_safe, sd_global->runnable_task_set) {
251     XBT_VERB("Executing task '%s'", SD_task_get_name(task));
252     if (__SD_task_try_to_run(task))
253       xbt_swag_insert(task,sd_global->return_set);
254   }
255
256   /* main loop */
257   elapsed_time = 0.0;
258   while (elapsed_time >= 0.0 &&
259          (how_long < 0.0 || total_time < how_long) &&
260          !sd_global->watch_point_reached) {
261     surf_model_t model = NULL;
262     /* dumb variables */
263
264
265     XBT_DEBUG("Total time: %f", total_time);
266
267     elapsed_time = surf_solve(how_long > 0 ? surf_get_clock() + how_long : -1.0);
268     XBT_DEBUG("surf_solve() returns %f", elapsed_time);
269     if (elapsed_time > 0.0)
270       total_time += elapsed_time;
271
272     /* let's see which tasks are done */
273     xbt_dynar_foreach(model_list, iter, model) {
274       while ((action = xbt_swag_extract(model->states.done_action_set))) {
275         task = action->data;
276         task->start_time =
277             surf_workstation_model->
278             action_get_start_time(task->surf_action);
279         task->finish_time = surf_get_clock();
280         XBT_VERB("Task '%s' done", SD_task_get_name(task));
281         XBT_DEBUG("Calling __SD_task_just_done");
282         __SD_task_just_done(task);
283         XBT_DEBUG("__SD_task_just_done called on task '%s'",
284                SD_task_get_name(task));
285
286         /* the state has changed */
287         xbt_swag_insert(task,sd_global->return_set);
288
289         /* remove the dependencies after this task */
290         xbt_dynar_foreach(task->tasks_after, depcnt, dependency) {
291           dst = dependency->dst;
292           if (dst->unsatisfied_dependencies > 0)
293             dst->unsatisfied_dependencies--;
294           if (dst->is_not_ready > 0)
295             dst->is_not_ready--;
296
297           if (!(dst->unsatisfied_dependencies)) {
298             if (__SD_task_is_scheduled(dst))
299               __SD_task_set_state(dst, SD_RUNNABLE);
300             else
301               __SD_task_set_state(dst, SD_SCHEDULABLE);
302           }
303
304           if (SD_task_get_kind(dst) == SD_TASK_COMM_E2E) {
305             SD_dependency_t comm_dep;
306             SD_task_t comm_dst;
307             xbt_dynar_get_cpy(dst->tasks_after, 0, &comm_dep);
308             comm_dst = comm_dep->dst;
309             if (__SD_task_is_not_scheduled(comm_dst) &&
310                 comm_dst->is_not_ready > 0) {
311               comm_dst->is_not_ready--;
312
313               if (!(comm_dst->is_not_ready)) {
314                 __SD_task_set_state(comm_dst, SD_SCHEDULABLE);
315               }
316             }
317           }
318
319           /* is dst runnable now? */
320           if (__SD_task_is_runnable(dst)
321               && !sd_global->watch_point_reached) {
322             XBT_VERB("Executing task '%s'", SD_task_get_name(dst));
323             if (__SD_task_try_to_run(dst))
324               xbt_swag_insert(dst,sd_global->return_set);
325           }
326         }
327       }
328
329       /* let's see which tasks have just failed */
330       while ((action = xbt_swag_extract(model->states.failed_action_set))) {
331         task = action->data;
332         task->start_time =
333             surf_workstation_model->
334             action_get_start_time(task->surf_action);
335         task->finish_time = surf_get_clock();
336         XBT_VERB("Task '%s' failed", SD_task_get_name(task));
337         __SD_task_set_state(task, SD_FAILED);
338         surf_workstation_model->action_unref(action);
339         task->surf_action = NULL;
340
341         xbt_swag_insert(task,sd_global->return_set);
342       }
343     }
344   }
345
346   if (!sd_global->watch_point_reached && how_long<0){
347     if (xbt_swag_size(sd_global->done_task_set) < sd_global->task_number){
348         XBT_WARN("Simulation is finished but %d tasks are still not done",
349             (sd_global->task_number - xbt_swag_size(sd_global->done_task_set)));
350         xbt_swag_foreach_safe (task, task_safe,sd_global->not_scheduled_task_set){
351                 XBT_WARN("%s is in SD_NOT_SCHEDULED state", SD_task_get_name(task));
352                 }
353         xbt_swag_foreach_safe (task, task_safe,sd_global->schedulable_task_set){
354                 XBT_WARN("%s is in SD_SCHEDULABLE state", SD_task_get_name(task));
355         }
356         xbt_swag_foreach_safe (task, task_safe,sd_global->scheduled_task_set){
357                 XBT_WARN("%s is in SD_SCHEDULED state", SD_task_get_name(task));
358         }
359     }
360   }
361
362   XBT_DEBUG("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
363          elapsed_time, total_time, sd_global->watch_point_reached);
364   XBT_DEBUG("current time = %f", surf_get_clock());
365
366   return sd_global->return_set;
367 }
368
369 /**
370  * \brief Returns the current clock
371  *
372  * \return the current clock, in second
373  */
374 double SD_get_clock(void) {
375   return surf_get_clock();
376 }
377
378 /**
379  * \brief Destroys all SD internal data
380  *
381  * This function should be called when the simulation is over. Don't forget also to destroy
382  * the tasks.
383  *
384  * \see SD_init(), SD_task_destroy()
385  */
386 void SD_exit(void)
387 {
388 #ifdef HAVE_TRACING
389   TRACE_surf_release();
390 #endif
391
392   xbt_mallocator_free(sd_global->task_mallocator);
393
394   XBT_DEBUG("Destroying workstation and link arrays...");
395   xbt_free(sd_global->workstation_list);
396   xbt_free(sd_global->link_list);
397   xbt_free(sd_global->recyclable_route);
398
399   XBT_DEBUG("Destroying the swags...");
400   xbt_swag_free(sd_global->not_scheduled_task_set);
401   xbt_swag_free(sd_global->schedulable_task_set);
402   xbt_swag_free(sd_global->scheduled_task_set);
403   xbt_swag_free(sd_global->runnable_task_set);
404   xbt_swag_free(sd_global->in_fifo_task_set);
405   xbt_swag_free(sd_global->running_task_set);
406   xbt_swag_free(sd_global->done_task_set);
407   xbt_swag_free(sd_global->failed_task_set);
408   xbt_swag_free(sd_global->return_set);
409
410 #ifdef HAVE_TRACING
411   TRACE_end();
412 #endif
413
414   XBT_DEBUG("Exiting Surf...");
415   surf_exit();
416
417   xbt_free(sd_global);
418   sd_global = NULL;
419
420 #ifdef HAVE_JEDULE
421   jedule_sd_dump();
422   jedule_sd_cleanup();
423 #endif
424 }
425
426 /**
427  * \brief load script file
428  */
429
430 void SD_load_environment_script(const char *script_file)
431 {
432 #ifdef HAVE_LUA
433   lua_State *L = lua_open();
434   luaL_openlibs(L);
435
436   if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
437     printf("error: %s\n", lua_tostring(L, -1));
438     return;
439   }
440 #else
441   xbt_die
442       ("Lua is not available!! to call SD_load_environment_script, lua should be available...");
443 #endif
444   return;
445 }