Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correctly set callbacks for tags.
[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 XBT_LOG_NEW_CATEGORY(sd, "Logging specific to SimDag");
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel, sd,
25                                 "Logging specific to SimDag (kernel)");
26
27 SD_global_t sd_global = NULL;
28
29 XBT_LOG_EXTERNAL_CATEGORY(sd_kernel);
30 XBT_LOG_EXTERNAL_CATEGORY(sd_task);
31 XBT_LOG_EXTERNAL_CATEGORY(sd_workstation);
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_assert0(!SD_INITIALISED(), "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->workstations = xbt_dict_new();
61   sd_global->workstation_count = 0;
62   sd_global->workstation_list = NULL;
63   sd_global->links = xbt_dict_new();
64   sd_global->link_count = 0;
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
97 /**
98  * \brief Reinits the application part of the simulation (experimental feature)
99  *
100  * This function allows you to run several simulations on the same platform
101  * by resetting the part describing the application.
102  *
103  * @warning: this function is still experimental and not perfect. For example,
104  * the simulation clock (and traces usage) is not reset. So, do not use it if
105  * you use traces in your simulation, and do not use absolute timing after using it.
106  * That being said, this function is still precious if you want to compare a bunch of
107  * heuristics on the same platforms.
108  */
109 void SD_application_reinit(void)
110 {
111
112   s_SD_task_t task;
113
114   if (SD_INITIALISED()) {
115     DEBUG0("Recreating the swags...");
116     xbt_swag_free(sd_global->not_scheduled_task_set);
117     xbt_swag_free(sd_global->schedulable_task_set);
118     xbt_swag_free(sd_global->scheduled_task_set);
119     xbt_swag_free(sd_global->runnable_task_set);
120     xbt_swag_free(sd_global->in_fifo_task_set);
121     xbt_swag_free(sd_global->running_task_set);
122     xbt_swag_free(sd_global->done_task_set);
123     xbt_swag_free(sd_global->failed_task_set);
124
125     sd_global->not_scheduled_task_set =
126         xbt_swag_new(xbt_swag_offset(task, state_hookup));
127     sd_global->schedulable_task_set =
128         xbt_swag_new(xbt_swag_offset(task, state_hookup));
129     sd_global->scheduled_task_set =
130         xbt_swag_new(xbt_swag_offset(task, state_hookup));
131     sd_global->runnable_task_set =
132         xbt_swag_new(xbt_swag_offset(task, state_hookup));
133     sd_global->in_fifo_task_set =
134         xbt_swag_new(xbt_swag_offset(task, state_hookup));
135     sd_global->running_task_set =
136         xbt_swag_new(xbt_swag_offset(task, state_hookup));
137     sd_global->done_task_set =
138         xbt_swag_new(xbt_swag_offset(task, state_hookup));
139     sd_global->failed_task_set =
140         xbt_swag_new(xbt_swag_offset(task, state_hookup));
141     sd_global->task_number = 0;
142   } else {
143     WARN0("SD_application_reinit called before initialization of SimDag");
144     /* we cannot use exceptions here because xbt is not running! */
145   }
146
147 }
148
149 /**
150  * \brief Creates the environment
151  *
152  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
153  * \ref SD_link_management "links") is created with the data stored in the given XML
154  * platform file.
155  *
156  * \param platform_file name of an XML file describing the environment to create
157  * \see SD_workstation_management, SD_link_management
158  *
159  * The XML file follows this DTD:
160  *
161  *     \include simgrid.dtd
162  *
163  * Here is a small example of such a platform:
164  *
165  *     \include small_platform.xml
166  */
167 void SD_create_environment(const char *platform_file)
168 {
169   xbt_dict_cursor_t cursor = NULL;
170   char *name = NULL;
171   void *surf_workstation = NULL;
172   void *surf_link = NULL;
173
174   platform_filename = bprintf("%s",platform_file);
175
176   // Reset callbacks
177   surf_parse_reset_callbacks();
178   // Add config callbacks
179   surf_parse_add_callback_config();
180   SD_CHECK_INIT_DONE();
181   parse_platform_file(platform_file);
182   surf_config_models_create_elms();
183
184   /* now let's create the SD wrappers for workstations and links */
185   xbt_dict_foreach(surf_model_resource_set(surf_workstation_model), cursor,
186                    name, surf_workstation) {
187     __SD_workstation_create(surf_workstation, NULL);
188   }
189
190   xbt_dict_foreach(surf_model_resource_set(surf_network_model), cursor,
191                    name, surf_link) {
192     __SD_link_create(surf_link, NULL);
193   }
194
195   DEBUG2("Workstation number: %d, link number: %d",
196          SD_workstation_get_number(), SD_link_get_number());
197 }
198
199 /**
200  * \brief Launches the simulation.
201  *
202  * The function will execute the \ref SD_RUNNABLE runnable tasks.
203  * If \a how_long is positive, then the simulation will be stopped either
204  * when time reaches \a how_long or when a watch point is reached.
205  * A nonpositive value for \a how_long means no time limit, in which case
206  * the simulation will be stopped either when a watch point is reached or
207  * when no more task can be executed.
208  * Then you can call SD_simulate() again.
209  *
210  * \param how_long maximum duration of the simulation (a negative value means no time limit)
211  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
212  * \see SD_task_schedule(), SD_task_watch()
213  */
214 xbt_dynar_t SD_simulate(double how_long)
215 {
216   double total_time = 0.0;      /* we stop the simulation when total_time >= how_long */
217   double elapsed_time = 0.0;
218   SD_task_t task, task_safe, dst;
219   SD_dependency_t dependency;
220   surf_action_t action;
221   xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
222   unsigned int iter, depcnt;
223   static int first_time = 1;
224
225   SD_CHECK_INIT_DONE();
226
227    if (first_time) {
228     VERB0("Starting simulation...");
229
230     surf_presolve();            /* Takes traces into account */
231     first_time = 0;
232   }
233
234   sd_global->watch_point_reached = 0;
235
236   /* explore the runnable tasks */
237   xbt_swag_foreach_safe(task, task_safe, sd_global->runnable_task_set) {
238     VERB1("Executing task '%s'", SD_task_get_name(task));
239     if (__SD_task_try_to_run(task)
240         && !xbt_dynar_member(changed_tasks, &task))
241       xbt_dynar_push(changed_tasks, &task);
242   }
243
244   /* main loop */
245   elapsed_time = 0.0;
246   while (elapsed_time >= 0.0 &&
247          (how_long < 0.0 || total_time < how_long) &&
248          !sd_global->watch_point_reached) {
249     surf_model_t model = NULL;
250     /* dumb variables */
251
252
253     DEBUG1("Total time: %f", total_time);
254
255     elapsed_time = surf_solve(how_long > 0 ? surf_get_clock() + how_long : -1.0);
256     DEBUG1("surf_solve() returns %f", elapsed_time);
257     if (elapsed_time > 0.0)
258       total_time += elapsed_time;
259
260     /* let's see which tasks are done */
261     xbt_dynar_foreach(model_list, iter, model) {
262       while ((action = xbt_swag_extract(model->states.done_action_set))) {
263         task = action->data;
264         task->start_time =
265             surf_workstation_model->
266             action_get_start_time(task->surf_action);
267         task->finish_time = surf_get_clock();
268         VERB1("Task '%s' done", SD_task_get_name(task));
269         DEBUG0("Calling __SD_task_just_done");
270         __SD_task_just_done(task);
271         DEBUG1("__SD_task_just_done called on task '%s'",
272                SD_task_get_name(task));
273
274         /* the state has changed */
275         if (!xbt_dynar_member(changed_tasks, &task))
276           xbt_dynar_push(changed_tasks, &task);
277
278         /* remove the dependencies after this task */
279         xbt_dynar_foreach(task->tasks_after, depcnt, dependency) {
280           dst = dependency->dst;
281           if (dst->unsatisfied_dependencies > 0)
282             dst->unsatisfied_dependencies--;
283           if (dst->is_not_ready > 0)
284             dst->is_not_ready--;
285
286           if (!(dst->unsatisfied_dependencies)) {
287             if (__SD_task_is_scheduled(dst))
288               __SD_task_set_state(dst, SD_RUNNABLE);
289             else
290               __SD_task_set_state(dst, SD_SCHEDULABLE);
291           }
292
293           if (SD_task_get_kind(dst) == SD_TASK_COMM_E2E) {
294             SD_dependency_t comm_dep;
295             SD_task_t comm_dst;
296             xbt_dynar_get_cpy(dst->tasks_after, 0, &comm_dep);
297             comm_dst = comm_dep->dst;
298             if (__SD_task_is_not_scheduled(comm_dst) &&
299                 comm_dst->is_not_ready > 0) {
300               comm_dst->is_not_ready--;
301
302               if (!(comm_dst->is_not_ready)) {
303                 __SD_task_set_state(comm_dst, SD_SCHEDULABLE);
304               }
305             }
306           }
307
308           /* is dst runnable now? */
309           if (__SD_task_is_runnable(dst)
310               && !sd_global->watch_point_reached) {
311             VERB1("Executing task '%s'", SD_task_get_name(dst));
312             if (__SD_task_try_to_run(dst) &&
313                 !xbt_dynar_member(changed_tasks, &task))
314               xbt_dynar_push(changed_tasks, &task);
315           }
316         }
317       }
318
319       /* let's see which tasks have just failed */
320       while ((action = xbt_swag_extract(model->states.failed_action_set))) {
321         task = action->data;
322         task->start_time =
323             surf_workstation_model->
324             action_get_start_time(task->surf_action);
325         task->finish_time = surf_get_clock();
326         VERB1("Task '%s' failed", SD_task_get_name(task));
327         __SD_task_set_state(task, SD_FAILED);
328         surf_workstation_model->action_unref(action);
329         task->surf_action = NULL;
330
331         if (!xbt_dynar_member(changed_tasks, &task))
332           xbt_dynar_push(changed_tasks, &task);
333       }
334     }
335   }
336
337   if (!sd_global->watch_point_reached && how_long<0){
338     if (xbt_swag_size(sd_global->done_task_set) < sd_global->task_number){
339         WARN0("Simulation is finished but some tasks are still not done");
340         xbt_swag_foreach_safe (task, task_safe,sd_global->not_scheduled_task_set){
341                 WARN1("%s is in SD_NOT_SCHEDULED state", SD_task_get_name(task));
342                 }
343         xbt_swag_foreach_safe (task, task_safe,sd_global->schedulable_task_set){
344                 WARN1("%s is in SD_SCHEDULABLE state", SD_task_get_name(task));
345         }
346         xbt_swag_foreach_safe (task, task_safe,sd_global->scheduled_task_set){
347                 WARN1("%s is in SD_SCHEDULED state", SD_task_get_name(task));
348         }
349     }
350   }
351
352   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
353          elapsed_time, total_time, sd_global->watch_point_reached);
354   DEBUG1("current time = %f", surf_get_clock());
355
356   return changed_tasks;
357 }
358
359 /**
360  * \brief Returns the current clock
361  *
362  * \return the current clock, in second
363  */
364 double SD_get_clock(void)
365 {
366   SD_CHECK_INIT_DONE();
367
368   return surf_get_clock();
369 }
370
371 /**
372  * \brief Destroys all SD internal data
373  *
374  * This function should be called when the simulation is over. Don't forget also to destroy
375  * the tasks.
376  *
377  * \see SD_init(), SD_task_destroy()
378  */
379 void SD_exit(void)
380 {
381 #ifdef HAVE_TRACING
382   TRACE_surf_release();
383 #endif
384   if (SD_INITIALISED()) {
385     DEBUG0("Destroying workstation and link dictionaries...");
386     xbt_dict_free(&sd_global->workstations);
387     xbt_dict_free(&sd_global->links);
388
389     DEBUG0("Destroying workstation and link arrays if necessary...");
390     if (sd_global->workstation_list != NULL)
391       xbt_free(sd_global->workstation_list);
392
393     if (sd_global->link_list != NULL)
394       xbt_free(sd_global->link_list);
395
396     if (sd_global->recyclable_route != NULL)
397       xbt_free(sd_global->recyclable_route);
398
399     DEBUG0("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
409     xbt_free(sd_global);
410     sd_global = NULL;
411
412 #ifdef HAVE_TRACING
413   TRACE_end();
414 #endif
415
416     DEBUG0("Exiting Surf...");
417     surf_exit();
418   } else {
419     WARN0("SD_exit() called, but SimDag is not running");
420     /* we cannot use exceptions here because xbt is not running! */
421   }
422 }
423
424 /**
425  * \bried load script file
426  */
427
428 void SD_load_environment_script(const char *script_file)
429 {
430 #ifdef HAVE_LUA
431   lua_State *L = lua_open();
432   luaL_openlibs(L);
433
434   if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
435     printf("error: %s\n", lua_tostring(L, -1));
436     return;
437   }
438 #else
439   xbt_die
440       ("Lua is not available!! to call SD_load_environment_script, lua should be available...");
441 #endif
442   return;
443 }