1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "xbt/sysdep.h"
10 #include "surf/surf.h"
14 #include "xbt/config.h"
15 #include "instr/private.h"
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)");
26 SD_global_t sd_global = NULL;
28 XBT_LOG_EXTERNAL_CATEGORY(sd_kernel);
29 XBT_LOG_EXTERNAL_CATEGORY(sd_task);
30 XBT_LOG_EXTERNAL_CATEGORY(sd_workstation);
33 * \brief Initialises SD internal data
35 * This function must be called before any other SD function. Then you
36 * should call SD_create_environment().
38 * \param argc argument number
39 * \param argv argument list
40 * \see SD_create_environment(), SD_exit()
42 void SD_init(int *argc, char **argv)
45 TRACE_global_init(argc, argv);
50 xbt_assert0(!SD_INITIALISED(), "SD_init() already called");
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);
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;
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;
86 surf_init(argc, argv);
88 xbt_cfg_setdefault_string(_surf_cfg_set, "workstation/model",
93 * \brief Reinits the application part of the simulation (experimental feature)
95 * This function allows you to run several simulations on the same platform
96 * by resetting the part describing the application.
98 * @warning: this function is still experimental and not perfect. For example,
99 * the simulation clock (and traces usage) is not reset. So, do not use it if
100 * you use traces in your simulation, and do not use absolute timing after using it.
101 * That being said, this function is still precious if you want to compare a bunch of
102 * heuristics on the same platforms.
104 void SD_application_reinit(void)
109 if (SD_INITIALISED()) {
110 DEBUG0("Recreating the swags...");
111 xbt_swag_free(sd_global->not_scheduled_task_set);
112 xbt_swag_free(sd_global->schedulable_task_set);
113 xbt_swag_free(sd_global->scheduled_task_set);
114 xbt_swag_free(sd_global->runnable_task_set);
115 xbt_swag_free(sd_global->in_fifo_task_set);
116 xbt_swag_free(sd_global->running_task_set);
117 xbt_swag_free(sd_global->done_task_set);
118 xbt_swag_free(sd_global->failed_task_set);
120 sd_global->not_scheduled_task_set =
121 xbt_swag_new(xbt_swag_offset(task, state_hookup));
122 sd_global->schedulable_task_set =
123 xbt_swag_new(xbt_swag_offset(task, state_hookup));
124 sd_global->scheduled_task_set =
125 xbt_swag_new(xbt_swag_offset(task, state_hookup));
126 sd_global->runnable_task_set =
127 xbt_swag_new(xbt_swag_offset(task, state_hookup));
128 sd_global->in_fifo_task_set =
129 xbt_swag_new(xbt_swag_offset(task, state_hookup));
130 sd_global->running_task_set =
131 xbt_swag_new(xbt_swag_offset(task, state_hookup));
132 sd_global->done_task_set =
133 xbt_swag_new(xbt_swag_offset(task, state_hookup));
134 sd_global->failed_task_set =
135 xbt_swag_new(xbt_swag_offset(task, state_hookup));
136 sd_global->task_number = 0;
138 WARN0("SD_application_reinit called before initialization of SimDag");
139 /* we cannot use exceptions here because xbt is not running! */
145 * \brief Creates the environment
147 * The environment (i.e. the \ref SD_workstation_management "workstations" and the
148 * \ref SD_link_management "links") is created with the data stored in the given XML
151 * \param platform_file name of an XML file describing the environment to create
152 * \see SD_workstation_management, SD_link_management
154 * The XML file follows this DTD:
156 * \include simgrid.dtd
158 * Here is a small example of such a platform:
160 * \include small_platform.xml
162 void SD_create_environment(const char *platform_file)
164 xbt_dict_cursor_t cursor = NULL;
166 void *surf_workstation = NULL;
167 void *surf_link = NULL;
169 SD_CHECK_INIT_DONE();
171 DEBUG0("SD_create_environment");
173 surf_config_models_setup(platform_file);
174 parse_platform_file(platform_file);
175 surf_config_models_create_elms();
177 /* now let's create the SD wrappers for workstations and links */
178 xbt_dict_foreach(surf_model_resource_set(surf_workstation_model), cursor,
179 name, surf_workstation) {
180 __SD_workstation_create(surf_workstation, NULL);
183 xbt_dict_foreach(surf_model_resource_set(surf_network_model), cursor,
185 __SD_link_create(surf_link, NULL);
188 DEBUG2("Workstation number: %d, link number: %d",
189 SD_workstation_get_number(), SD_link_get_number());
192 TRACE_surf_save_onelink();
197 * \brief Launches the simulation.
199 * The function will execute the \ref SD_RUNNABLE runnable tasks.
200 * The simulation will be stopped when its time reaches \a how_long,
201 * when a watch point is reached, or when no more task can be executed.
202 * Then you can call SD_simulate() again.
204 * \param how_long maximum duration of the simulation (a negative value means no time limit)
205 * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
206 * \see SD_task_schedule(), SD_task_watch()
208 xbt_dynar_t SD_simulate(double how_long)
210 double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
211 double elapsed_time = 0.0;
212 SD_task_t task, task_safe, dst;
213 SD_dependency_t dependency;
214 surf_action_t action;
215 xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
216 unsigned int iter, depcnt;
217 static int first_time = 1;
219 SD_CHECK_INIT_DONE();
221 VERB0("Starting simulation...");
224 surf_presolve(); /* Takes traces into account */
229 surf_timer_model->extension.timer.set(surf_get_clock() + how_long,
232 sd_global->watch_point_reached = 0;
234 /* explore the runnable tasks */
235 xbt_swag_foreach_safe(task, task_safe, sd_global->runnable_task_set) {
236 VERB1("Executing task '%s'", SD_task_get_name(task));
237 if (__SD_task_try_to_run(task)
238 && !xbt_dynar_member(changed_tasks, &task))
239 xbt_dynar_push(changed_tasks, &task);
244 while (elapsed_time >= 0.0 &&
245 (how_long < 0.0 || total_time < how_long) &&
246 !sd_global->watch_point_reached) {
247 surf_model_t model = NULL;
253 DEBUG1("Total time: %f", total_time);
255 elapsed_time = surf_solve();
256 DEBUG1("surf_solve() returns %f", elapsed_time);
257 if (elapsed_time > 0.0)
258 total_time += elapsed_time;
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))) {
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));
274 /* the state has changed */
275 if (!xbt_dynar_member(changed_tasks, &task))
276 xbt_dynar_push(changed_tasks, &task);
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)
286 if (!(dst->unsatisfied_dependencies)) {
287 if (__SD_task_is_scheduled(dst))
288 __SD_task_set_state(dst, SD_RUNNABLE);
290 __SD_task_set_state(dst, SD_SCHEDULABLE);
293 if (SD_task_get_kind(dst) == SD_TASK_COMM_E2E) {
294 SD_dependency_t comm_dep;
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--;
302 if (!(comm_dst->is_not_ready)) {
303 __SD_task_set_state(comm_dst, SD_SCHEDULABLE);
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);
319 /* let's see which tasks have just failed */
320 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
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;
331 if (!xbt_dynar_member(changed_tasks, &task))
332 xbt_dynar_push(changed_tasks, &task);
336 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
340 VERB0("Simulation finished");
341 DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
342 elapsed_time, total_time, sd_global->watch_point_reached);
343 DEBUG1("current time = %f", surf_get_clock());
345 return changed_tasks;
349 * \brief Returns the current clock
351 * \return the current clock, in second
353 double SD_get_clock(void)
355 SD_CHECK_INIT_DONE();
357 return surf_get_clock();
361 * \brief Destroys all SD internal data
363 * This function should be called when the simulation is over. Don't forget also to destroy
366 * \see SD_init(), SD_task_destroy()
371 TRACE_surf_release();
373 if (SD_INITIALISED()) {
374 DEBUG0("Destroying workstation and link dictionaries...");
375 xbt_dict_free(&sd_global->workstations);
376 xbt_dict_free(&sd_global->links);
378 DEBUG0("Destroying workstation and link arrays if necessary...");
379 if (sd_global->workstation_list != NULL)
380 xbt_free(sd_global->workstation_list);
382 if (sd_global->link_list != NULL)
383 xbt_free(sd_global->link_list);
385 if (sd_global->recyclable_route != NULL)
386 xbt_free(sd_global->recyclable_route);
388 DEBUG0("Destroying the swags...");
389 xbt_swag_free(sd_global->not_scheduled_task_set);
390 xbt_swag_free(sd_global->schedulable_task_set);
391 xbt_swag_free(sd_global->scheduled_task_set);
392 xbt_swag_free(sd_global->runnable_task_set);
393 xbt_swag_free(sd_global->in_fifo_task_set);
394 xbt_swag_free(sd_global->running_task_set);
395 xbt_swag_free(sd_global->done_task_set);
396 xbt_swag_free(sd_global->failed_task_set);
401 DEBUG0("Exiting Surf...");
404 WARN0("SD_exit() called, but SimDag is not running");
405 /* we cannot use exceptions here because xbt is not running! */
410 * \bried load script file
413 void SD_load_environment_script(const char *script_file)
416 lua_State *L = lua_open();
419 if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
420 printf("error: %s\n", lua_tostring(L, -1));
425 ("Lua is not available!! to call SD_load_environment_script, lua should be available...");