Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use new macros THROWF and RETHROWF.
[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 XBT_LOG_EXTERNAL_CATEGORY(sd_kernel);
34 XBT_LOG_EXTERNAL_CATEGORY(sd_task);
35 XBT_LOG_EXTERNAL_CATEGORY(sd_workstation);
36
37 /**
38  * \brief Initialises SD internal data
39  *
40  * This function must be called before any other SD function. Then you
41  * should call SD_create_environment().
42  *
43  * \param argc argument number
44  * \param argv argument list
45  * \see SD_create_environment(), SD_exit()
46  */
47 void SD_init(int *argc, char **argv)
48 {
49 #ifdef HAVE_TRACING
50   TRACE_global_init(argc, argv);
51 #endif
52
53   s_SD_task_t task;
54
55   xbt_assert0(!SD_INITIALISED(), "SD_init() already called");
56
57   /* Connect our log channels: that must be done manually under windows */
58   XBT_LOG_CONNECT(sd_kernel, sd);
59   XBT_LOG_CONNECT(sd_task, sd);
60   XBT_LOG_CONNECT(sd_workstation, sd);
61
62
63   sd_global = xbt_new(s_SD_global_t, 1);
64   sd_global->workstation_list = NULL;
65   sd_global->link_list = NULL;
66   sd_global->recyclable_route = NULL;
67   sd_global->watch_point_reached = 0;
68
69   sd_global->not_scheduled_task_set =
70       xbt_swag_new(xbt_swag_offset(task, state_hookup));
71   sd_global->schedulable_task_set =
72       xbt_swag_new(xbt_swag_offset(task, state_hookup));
73   sd_global->scheduled_task_set =
74       xbt_swag_new(xbt_swag_offset(task, state_hookup));
75   sd_global->runnable_task_set =
76       xbt_swag_new(xbt_swag_offset(task, state_hookup));
77   sd_global->in_fifo_task_set =
78       xbt_swag_new(xbt_swag_offset(task, state_hookup));
79   sd_global->running_task_set =
80       xbt_swag_new(xbt_swag_offset(task, state_hookup));
81   sd_global->done_task_set =
82       xbt_swag_new(xbt_swag_offset(task, state_hookup));
83   sd_global->failed_task_set =
84       xbt_swag_new(xbt_swag_offset(task, state_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   if (SD_INITIALISED()) {
123     XBT_DEBUG("Recreating the swags...");
124     xbt_swag_free(sd_global->not_scheduled_task_set);
125     xbt_swag_free(sd_global->schedulable_task_set);
126     xbt_swag_free(sd_global->scheduled_task_set);
127     xbt_swag_free(sd_global->runnable_task_set);
128     xbt_swag_free(sd_global->in_fifo_task_set);
129     xbt_swag_free(sd_global->running_task_set);
130     xbt_swag_free(sd_global->done_task_set);
131     xbt_swag_free(sd_global->failed_task_set);
132
133     sd_global->not_scheduled_task_set =
134         xbt_swag_new(xbt_swag_offset(task, state_hookup));
135     sd_global->schedulable_task_set =
136         xbt_swag_new(xbt_swag_offset(task, state_hookup));
137     sd_global->scheduled_task_set =
138         xbt_swag_new(xbt_swag_offset(task, state_hookup));
139     sd_global->runnable_task_set =
140         xbt_swag_new(xbt_swag_offset(task, state_hookup));
141     sd_global->in_fifo_task_set =
142         xbt_swag_new(xbt_swag_offset(task, state_hookup));
143     sd_global->running_task_set =
144         xbt_swag_new(xbt_swag_offset(task, state_hookup));
145     sd_global->done_task_set =
146         xbt_swag_new(xbt_swag_offset(task, state_hookup));
147     sd_global->failed_task_set =
148         xbt_swag_new(xbt_swag_offset(task, state_hookup));
149     sd_global->task_number = 0;
150
151 #ifdef HAVE_JEDULE
152     jedule_sd_cleanup();
153     jedule_sd_init();
154 #endif
155
156   } else {
157     XBT_WARN("SD_application_reinit called before initialization of SimDag");
158     /* we cannot use exceptions here because xbt is not running! */
159   }
160
161 }
162
163 /**
164  * \brief Creates the environment
165  *
166  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
167  * \ref SD_link_management "links") is created with the data stored in the given XML
168  * platform file.
169  *
170  * \param platform_file name of an XML file describing the environment to create
171  * \see SD_workstation_management, SD_link_management
172  *
173  * The XML file follows this DTD:
174  *
175  *     \include simgrid.dtd
176  *
177  * Here is a small example of such a platform:
178  *
179  *     \include small_platform.xml
180  */
181 void SD_create_environment(const char *platform_file)
182 {
183   xbt_lib_cursor_t cursor = NULL;
184   char *name = NULL;
185   void **surf_workstation = NULL;
186   void **surf_link = NULL;
187
188   platform_filename = xbt_strdup(platform_file);
189
190   // Reset callbacks
191   surf_parse_reset_callbacks();
192   // Add config callbacks
193   surf_parse_add_callback_config();
194   SD_CHECK_INIT_DONE();
195   parse_platform_file(platform_file);
196   surf_config_models_create_elms();
197
198   /* now let's create the SD wrappers for workstations and links */
199   xbt_lib_foreach(host_lib, cursor, name, surf_workstation){
200           if(surf_workstation[SURF_WKS_LEVEL])
201                   __SD_workstation_create(surf_workstation[SURF_WKS_LEVEL], NULL);
202   }
203
204   xbt_lib_foreach(link_lib, cursor, name, surf_link) {
205     __SD_link_create(surf_link[SURF_LINK_LEVEL], NULL);
206   }
207
208   XBT_DEBUG("Workstation number: %d, link number: %d",
209          SD_workstation_get_number(), SD_link_get_number());
210 #ifdef HAVE_JEDULE
211   jedule_setup_platform();
212 #endif
213 }
214
215 /**
216  * \brief Launches the simulation.
217  *
218  * The function will execute the \ref SD_RUNNABLE runnable tasks.
219  * If \a how_long is positive, then the simulation will be stopped either
220  * when time reaches \a how_long or when a watch point is reached.
221  * A nonpositive value for \a how_long means no time limit, in which case
222  * the simulation will be stopped either when a watch point is reached or
223  * when no more task can be executed.
224  * Then you can call SD_simulate() again.
225  *
226  * \param how_long maximum duration of the simulation (a negative value means no time limit)
227  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
228  * \see SD_task_schedule(), SD_task_watch()
229  */
230 xbt_dynar_t SD_simulate(double how_long)
231 {
232   double total_time = 0.0;      /* we stop the simulation when total_time >= how_long */
233   double elapsed_time = 0.0;
234   SD_task_t task, task_safe, dst;
235   SD_dependency_t dependency;
236   surf_action_t action;
237   xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
238   unsigned int iter, depcnt;
239   static int first_time = 1;
240
241   SD_CHECK_INIT_DONE();
242
243    if (first_time) {
244     XBT_VERB("Starting simulation...");
245
246     surf_presolve();            /* Takes traces into account */
247     first_time = 0;
248   }
249
250   sd_global->watch_point_reached = 0;
251
252   /* explore the runnable tasks */
253   xbt_swag_foreach_safe(task, task_safe, sd_global->runnable_task_set) {
254     XBT_VERB("Executing task '%s'", SD_task_get_name(task));
255     if (__SD_task_try_to_run(task)
256         && !xbt_dynar_member(changed_tasks, &task))
257       xbt_dynar_push(changed_tasks, &task);
258   }
259
260   /* main loop */
261   elapsed_time = 0.0;
262   while (elapsed_time >= 0.0 &&
263          (how_long < 0.0 || total_time < how_long) &&
264          !sd_global->watch_point_reached) {
265     surf_model_t model = NULL;
266     /* dumb variables */
267
268
269     XBT_DEBUG("Total time: %f", total_time);
270
271     elapsed_time = surf_solve(how_long > 0 ? surf_get_clock() + how_long : -1.0);
272     XBT_DEBUG("surf_solve() returns %f", elapsed_time);
273     if (elapsed_time > 0.0)
274       total_time += elapsed_time;
275
276     /* let's see which tasks are done */
277     xbt_dynar_foreach(model_list, iter, model) {
278       while ((action = xbt_swag_extract(model->states.done_action_set))) {
279         task = action->data;
280         task->start_time =
281             surf_workstation_model->
282             action_get_start_time(task->surf_action);
283         task->finish_time = surf_get_clock();
284         XBT_VERB("Task '%s' done", SD_task_get_name(task));
285         XBT_DEBUG("Calling __SD_task_just_done");
286         __SD_task_just_done(task);
287         XBT_DEBUG("__SD_task_just_done called on task '%s'",
288                SD_task_get_name(task));
289
290         /* the state has changed */
291         if (!xbt_dynar_member(changed_tasks, &task))
292           xbt_dynar_push(changed_tasks, &task);
293
294         /* remove the dependencies after this task */
295         xbt_dynar_foreach(task->tasks_after, depcnt, dependency) {
296           dst = dependency->dst;
297           if (dst->unsatisfied_dependencies > 0)
298             dst->unsatisfied_dependencies--;
299           if (dst->is_not_ready > 0)
300             dst->is_not_ready--;
301
302           if (!(dst->unsatisfied_dependencies)) {
303             if (__SD_task_is_scheduled(dst))
304               __SD_task_set_state(dst, SD_RUNNABLE);
305             else
306               __SD_task_set_state(dst, SD_SCHEDULABLE);
307           }
308
309           if (SD_task_get_kind(dst) == SD_TASK_COMM_E2E) {
310             SD_dependency_t comm_dep;
311             SD_task_t comm_dst;
312             xbt_dynar_get_cpy(dst->tasks_after, 0, &comm_dep);
313             comm_dst = comm_dep->dst;
314             if (__SD_task_is_not_scheduled(comm_dst) &&
315                 comm_dst->is_not_ready > 0) {
316               comm_dst->is_not_ready--;
317
318               if (!(comm_dst->is_not_ready)) {
319                 __SD_task_set_state(comm_dst, SD_SCHEDULABLE);
320               }
321             }
322           }
323
324           /* is dst runnable now? */
325           if (__SD_task_is_runnable(dst)
326               && !sd_global->watch_point_reached) {
327             XBT_VERB("Executing task '%s'", SD_task_get_name(dst));
328             if (__SD_task_try_to_run(dst) &&
329                 !xbt_dynar_member(changed_tasks, &task))
330               xbt_dynar_push(changed_tasks, &task);
331           }
332         }
333       }
334
335       /* let's see which tasks have just failed */
336       while ((action = xbt_swag_extract(model->states.failed_action_set))) {
337         task = action->data;
338         task->start_time =
339             surf_workstation_model->
340             action_get_start_time(task->surf_action);
341         task->finish_time = surf_get_clock();
342         XBT_VERB("Task '%s' failed", SD_task_get_name(task));
343         __SD_task_set_state(task, SD_FAILED);
344         surf_workstation_model->action_unref(action);
345         task->surf_action = NULL;
346
347         if (!xbt_dynar_member(changed_tasks, &task))
348           xbt_dynar_push(changed_tasks, &task);
349       }
350     }
351   }
352
353   if (!sd_global->watch_point_reached && how_long<0){
354     if (xbt_swag_size(sd_global->done_task_set) < sd_global->task_number){
355         XBT_WARN("Simulation is finished but some tasks are still not done");
356         xbt_swag_foreach_safe (task, task_safe,sd_global->not_scheduled_task_set){
357                 XBT_WARN("%s is in SD_NOT_SCHEDULED state", SD_task_get_name(task));
358                 }
359         xbt_swag_foreach_safe (task, task_safe,sd_global->schedulable_task_set){
360                 XBT_WARN("%s is in SD_SCHEDULABLE state", SD_task_get_name(task));
361         }
362         xbt_swag_foreach_safe (task, task_safe,sd_global->scheduled_task_set){
363                 XBT_WARN("%s is in SD_SCHEDULED state", SD_task_get_name(task));
364         }
365     }
366   }
367
368   XBT_DEBUG("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
369          elapsed_time, total_time, sd_global->watch_point_reached);
370   XBT_DEBUG("current time = %f", surf_get_clock());
371
372   return changed_tasks;
373 }
374
375 /**
376  * \brief Returns the current clock
377  *
378  * \return the current clock, in second
379  */
380 double SD_get_clock(void)
381 {
382   SD_CHECK_INIT_DONE();
383
384   return surf_get_clock();
385 }
386
387 /**
388  * \brief Destroys all SD internal data
389  *
390  * This function should be called when the simulation is over. Don't forget also to destroy
391  * the tasks.
392  *
393  * \see SD_init(), SD_task_destroy()
394  */
395 void SD_exit(void)
396 {
397 #ifdef HAVE_TRACING
398   TRACE_surf_release();
399 #endif
400
401   if (SD_INITIALISED()) {
402     XBT_DEBUG("Destroying workstation and link dictionaries...");
403
404     XBT_DEBUG("Destroying workstation and link arrays if necessary...");
405     if (sd_global->workstation_list != NULL)
406       xbt_free(sd_global->workstation_list);
407
408     if (sd_global->link_list != NULL)
409       xbt_free(sd_global->link_list);
410
411     if (sd_global->recyclable_route != NULL)
412       xbt_free(sd_global->recyclable_route);
413
414     XBT_DEBUG("Destroying the swags...");
415     xbt_swag_free(sd_global->not_scheduled_task_set);
416     xbt_swag_free(sd_global->schedulable_task_set);
417     xbt_swag_free(sd_global->scheduled_task_set);
418     xbt_swag_free(sd_global->runnable_task_set);
419     xbt_swag_free(sd_global->in_fifo_task_set);
420     xbt_swag_free(sd_global->running_task_set);
421     xbt_swag_free(sd_global->done_task_set);
422     xbt_swag_free(sd_global->failed_task_set);
423
424     XBT_DEBUG("Exiting Surf...");
425     surf_exit();
426
427     // Exit the LIB host_lib
428     xbt_lib_free(&host_lib);
429     xbt_lib_free(&link_lib);
430     xbt_lib_free(&as_router_lib);
431
432     xbt_free(sd_global);
433     sd_global = NULL;
434
435 #ifdef HAVE_TRACING
436   TRACE_end();
437 #endif
438 #ifdef HAVE_JEDULE
439   jedule_sd_dump();
440   jedule_sd_cleanup();
441 #endif
442
443   } else {
444     XBT_WARN("SD_exit() called, but SimDag is not running");
445     /* we cannot use exceptions here because xbt is not running! */
446   }
447 }
448
449 /**
450  * \bried load script file
451  */
452
453 void SD_load_environment_script(const char *script_file)
454 {
455 #ifdef HAVE_LUA
456   lua_State *L = lua_open();
457   luaL_openlibs(L);
458
459   if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
460     printf("error: %s\n", lua_tostring(L, -1));
461     return;
462   }
463 #else
464   xbt_die
465       ("Lua is not available!! to call SD_load_environment_script, lua should be available...");
466 #endif
467   return;
468 }