Logo AND Algorithmique Numérique Distribuée

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