Logo AND Algorithmique Numérique Distribuée

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