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/instr_private.h"
16 #include "surf/surfxml_parse.h"
24 #include "instr/jedule/jedule_sd_binding.h"
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)");
31 SD_global_t sd_global = NULL;
33 XBT_LOG_EXTERNAL_CATEGORY(sd_kernel);
34 XBT_LOG_EXTERNAL_CATEGORY(sd_task);
35 XBT_LOG_EXTERNAL_CATEGORY(sd_workstation);
38 * \brief Initialises SD internal data
40 * This function must be called before any other SD function. Then you
41 * should call SD_create_environment().
43 * \param argc argument number
44 * \param argv argument list
45 * \see SD_create_environment(), SD_exit()
47 void SD_init(int *argc, char **argv)
50 TRACE_global_init(argc, argv);
55 xbt_assert0(!SD_INITIALISED(), "SD_init() already called");
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);
63 sd_global = xbt_new(s_SD_global_t, 1);
64 sd_global->workstations = xbt_dict_new();
65 sd_global->workstation_count = 0;
66 sd_global->workstation_list = NULL;
67 sd_global->link_list = NULL;
68 sd_global->recyclable_route = NULL;
69 sd_global->watch_point_reached = 0;
71 sd_global->not_scheduled_task_set =
72 xbt_swag_new(xbt_swag_offset(task, state_hookup));
73 sd_global->schedulable_task_set =
74 xbt_swag_new(xbt_swag_offset(task, state_hookup));
75 sd_global->scheduled_task_set =
76 xbt_swag_new(xbt_swag_offset(task, state_hookup));
77 sd_global->runnable_task_set =
78 xbt_swag_new(xbt_swag_offset(task, state_hookup));
79 sd_global->in_fifo_task_set =
80 xbt_swag_new(xbt_swag_offset(task, state_hookup));
81 sd_global->running_task_set =
82 xbt_swag_new(xbt_swag_offset(task, state_hookup));
83 sd_global->done_task_set =
84 xbt_swag_new(xbt_swag_offset(task, state_hookup));
85 sd_global->failed_task_set =
86 xbt_swag_new(xbt_swag_offset(task, state_hookup));
87 sd_global->task_number = 0;
89 surf_init(argc, argv);
91 xbt_cfg_setdefault_string(_surf_cfg_set, "workstation/model",
102 XBT_DEBUG("ADD SD LEVELS");
103 SD_HOST_LEVEL = xbt_lib_add_level(host_lib,free);
104 SD_LINK_LEVEL = xbt_lib_add_level(link_lib,free);
108 * \brief Reinits the application part of the simulation (experimental feature)
110 * This function allows you to run several simulations on the same platform
111 * by resetting the part describing the application.
113 * @warning: this function is still experimental and not perfect. For example,
114 * the simulation clock (and traces usage) is not reset. So, do not use it if
115 * you use traces in your simulation, and do not use absolute timing after using it.
116 * That being said, this function is still precious if you want to compare a bunch of
117 * heuristics on the same platforms.
119 void SD_application_reinit(void)
124 if (SD_INITIALISED()) {
125 XBT_DEBUG("Recreating the swags...");
126 xbt_swag_free(sd_global->not_scheduled_task_set);
127 xbt_swag_free(sd_global->schedulable_task_set);
128 xbt_swag_free(sd_global->scheduled_task_set);
129 xbt_swag_free(sd_global->runnable_task_set);
130 xbt_swag_free(sd_global->in_fifo_task_set);
131 xbt_swag_free(sd_global->running_task_set);
132 xbt_swag_free(sd_global->done_task_set);
133 xbt_swag_free(sd_global->failed_task_set);
135 sd_global->not_scheduled_task_set =
136 xbt_swag_new(xbt_swag_offset(task, state_hookup));
137 sd_global->schedulable_task_set =
138 xbt_swag_new(xbt_swag_offset(task, state_hookup));
139 sd_global->scheduled_task_set =
140 xbt_swag_new(xbt_swag_offset(task, state_hookup));
141 sd_global->runnable_task_set =
142 xbt_swag_new(xbt_swag_offset(task, state_hookup));
143 sd_global->in_fifo_task_set =
144 xbt_swag_new(xbt_swag_offset(task, state_hookup));
145 sd_global->running_task_set =
146 xbt_swag_new(xbt_swag_offset(task, state_hookup));
147 sd_global->done_task_set =
148 xbt_swag_new(xbt_swag_offset(task, state_hookup));
149 sd_global->failed_task_set =
150 xbt_swag_new(xbt_swag_offset(task, state_hookup));
151 sd_global->task_number = 0;
159 XBT_WARN("SD_application_reinit called before initialization of SimDag");
160 /* we cannot use exceptions here because xbt is not running! */
166 * \brief Creates the environment
168 * The environment (i.e. the \ref SD_workstation_management "workstations" and the
169 * \ref SD_link_management "links") is created with the data stored in the given XML
172 * \param platform_file name of an XML file describing the environment to create
173 * \see SD_workstation_management, SD_link_management
175 * The XML file follows this DTD:
177 * \include simgrid.dtd
179 * Here is a small example of such a platform:
181 * \include small_platform.xml
183 void SD_create_environment(const char *platform_file)
185 xbt_dict_cursor_t cursor = NULL;
187 void *surf_workstation = NULL;
188 void *surf_link = NULL;
190 platform_filename = bprintf("%s",platform_file);
193 surf_parse_reset_callbacks();
194 // Add config callbacks
195 surf_parse_add_callback_config();
196 SD_CHECK_INIT_DONE();
197 parse_platform_file(platform_file);
198 surf_config_models_create_elms();
200 /* now let's create the SD wrappers for workstations and links */
201 xbt_dict_foreach(surf_model_resource_set(surf_workstation_model), cursor,
202 name, surf_workstation) {
203 __SD_workstation_create(surf_workstation, NULL);
206 xbt_dict_foreach(surf_model_resource_set(surf_network_model), cursor,
208 __SD_link_create(surf_link, NULL);
211 XBT_DEBUG("Workstation number: %d, link number: %d",
212 SD_workstation_get_number(), SD_link_get_number());
214 jedule_setup_platform();
219 * \brief Launches the simulation.
221 * The function will execute the \ref SD_RUNNABLE runnable tasks.
222 * If \a how_long is positive, then the simulation will be stopped either
223 * when time reaches \a how_long or when a watch point is reached.
224 * A nonpositive value for \a how_long means no time limit, in which case
225 * the simulation will be stopped either when a watch point is reached or
226 * when no more task can be executed.
227 * Then you can call SD_simulate() again.
229 * \param how_long maximum duration of the simulation (a negative value means no time limit)
230 * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
231 * \see SD_task_schedule(), SD_task_watch()
233 xbt_dynar_t SD_simulate(double how_long)
235 double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
236 double elapsed_time = 0.0;
237 SD_task_t task, task_safe, dst;
238 SD_dependency_t dependency;
239 surf_action_t action;
240 xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
241 unsigned int iter, depcnt;
242 static int first_time = 1;
244 SD_CHECK_INIT_DONE();
247 XBT_VERB("Starting simulation...");
249 surf_presolve(); /* Takes traces into account */
253 sd_global->watch_point_reached = 0;
255 /* explore the runnable tasks */
256 xbt_swag_foreach_safe(task, task_safe, sd_global->runnable_task_set) {
257 XBT_VERB("Executing task '%s'", SD_task_get_name(task));
258 if (__SD_task_try_to_run(task)
259 && !xbt_dynar_member(changed_tasks, &task))
260 xbt_dynar_push(changed_tasks, &task);
265 while (elapsed_time >= 0.0 &&
266 (how_long < 0.0 || total_time < how_long) &&
267 !sd_global->watch_point_reached) {
268 surf_model_t model = NULL;
272 XBT_DEBUG("Total time: %f", total_time);
274 elapsed_time = surf_solve(how_long > 0 ? surf_get_clock() + how_long : -1.0);
275 XBT_DEBUG("surf_solve() returns %f", elapsed_time);
276 if (elapsed_time > 0.0)
277 total_time += elapsed_time;
279 /* let's see which tasks are done */
280 xbt_dynar_foreach(model_list, iter, model) {
281 while ((action = xbt_swag_extract(model->states.done_action_set))) {
284 surf_workstation_model->
285 action_get_start_time(task->surf_action);
286 task->finish_time = surf_get_clock();
287 XBT_VERB("Task '%s' done", SD_task_get_name(task));
288 XBT_DEBUG("Calling __SD_task_just_done");
289 __SD_task_just_done(task);
290 XBT_DEBUG("__SD_task_just_done called on task '%s'",
291 SD_task_get_name(task));
293 /* the state has changed */
294 if (!xbt_dynar_member(changed_tasks, &task))
295 xbt_dynar_push(changed_tasks, &task);
297 /* remove the dependencies after this task */
298 xbt_dynar_foreach(task->tasks_after, depcnt, dependency) {
299 dst = dependency->dst;
300 if (dst->unsatisfied_dependencies > 0)
301 dst->unsatisfied_dependencies--;
302 if (dst->is_not_ready > 0)
305 if (!(dst->unsatisfied_dependencies)) {
306 if (__SD_task_is_scheduled(dst))
307 __SD_task_set_state(dst, SD_RUNNABLE);
309 __SD_task_set_state(dst, SD_SCHEDULABLE);
312 if (SD_task_get_kind(dst) == SD_TASK_COMM_E2E) {
313 SD_dependency_t comm_dep;
315 xbt_dynar_get_cpy(dst->tasks_after, 0, &comm_dep);
316 comm_dst = comm_dep->dst;
317 if (__SD_task_is_not_scheduled(comm_dst) &&
318 comm_dst->is_not_ready > 0) {
319 comm_dst->is_not_ready--;
321 if (!(comm_dst->is_not_ready)) {
322 __SD_task_set_state(comm_dst, SD_SCHEDULABLE);
327 /* is dst runnable now? */
328 if (__SD_task_is_runnable(dst)
329 && !sd_global->watch_point_reached) {
330 XBT_VERB("Executing task '%s'", SD_task_get_name(dst));
331 if (__SD_task_try_to_run(dst) &&
332 !xbt_dynar_member(changed_tasks, &task))
333 xbt_dynar_push(changed_tasks, &task);
338 /* let's see which tasks have just failed */
339 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
342 surf_workstation_model->
343 action_get_start_time(task->surf_action);
344 task->finish_time = surf_get_clock();
345 XBT_VERB("Task '%s' failed", SD_task_get_name(task));
346 __SD_task_set_state(task, SD_FAILED);
347 surf_workstation_model->action_unref(action);
348 task->surf_action = NULL;
350 if (!xbt_dynar_member(changed_tasks, &task))
351 xbt_dynar_push(changed_tasks, &task);
356 if (!sd_global->watch_point_reached && how_long<0){
357 if (xbt_swag_size(sd_global->done_task_set) < sd_global->task_number){
358 XBT_WARN("Simulation is finished but some tasks are still not done");
359 xbt_swag_foreach_safe (task, task_safe,sd_global->not_scheduled_task_set){
360 XBT_WARN("%s is in SD_NOT_SCHEDULED state", SD_task_get_name(task));
362 xbt_swag_foreach_safe (task, task_safe,sd_global->schedulable_task_set){
363 XBT_WARN("%s is in SD_SCHEDULABLE state", SD_task_get_name(task));
365 xbt_swag_foreach_safe (task, task_safe,sd_global->scheduled_task_set){
366 XBT_WARN("%s is in SD_SCHEDULED state", SD_task_get_name(task));
371 XBT_DEBUG("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
372 elapsed_time, total_time, sd_global->watch_point_reached);
373 XBT_DEBUG("current time = %f", surf_get_clock());
375 return changed_tasks;
379 * \brief Returns the current clock
381 * \return the current clock, in second
383 double SD_get_clock(void)
385 SD_CHECK_INIT_DONE();
387 return surf_get_clock();
391 * \brief Destroys all SD internal data
393 * This function should be called when the simulation is over. Don't forget also to destroy
396 * \see SD_init(), SD_task_destroy()
401 TRACE_surf_release();
403 if (SD_INITIALISED()) {
404 XBT_DEBUG("Destroying workstation and link dictionaries...");
405 xbt_dict_free(&sd_global->workstations);
407 XBT_DEBUG("Destroying workstation and link arrays if necessary...");
408 if (sd_global->workstation_list != NULL)
409 xbt_free(sd_global->workstation_list);
411 if (sd_global->link_list != NULL)
412 xbt_free(sd_global->link_list);
414 if (sd_global->recyclable_route != NULL)
415 xbt_free(sd_global->recyclable_route);
417 XBT_DEBUG("Destroying the swags...");
418 xbt_swag_free(sd_global->not_scheduled_task_set);
419 xbt_swag_free(sd_global->schedulable_task_set);
420 xbt_swag_free(sd_global->scheduled_task_set);
421 xbt_swag_free(sd_global->runnable_task_set);
422 xbt_swag_free(sd_global->in_fifo_task_set);
423 xbt_swag_free(sd_global->running_task_set);
424 xbt_swag_free(sd_global->done_task_set);
425 xbt_swag_free(sd_global->failed_task_set);
438 XBT_DEBUG("Exiting Surf...");
441 XBT_WARN("SD_exit() called, but SimDag is not running");
442 /* we cannot use exceptions here because xbt is not running! */
447 * \bried load script file
450 void SD_load_environment_script(const char *script_file)
453 lua_State *L = lua_open();
456 if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
457 printf("error: %s\n", lua_tostring(L, -1));
462 ("Lua is not available!! to call SD_load_environment_script, lua should be available...");