2 #include "xbt/sysdep.h"
8 #include "xbt/config.h"
10 XBT_LOG_NEW_CATEGORY(sd, "Logging specific to SimDag");
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel, sd,
12 "Logging specific to SimDag (kernel)");
14 SD_global_t sd_global = NULL;
18 /* Copyright (c) 2007 Arnaud Legrand.
19 All rights reserved. */
21 /* This program is free software; you can redistribute it and/or modify it
22 * under the terms of the license (GNU LGPL) which comes with this package. */
24 XBT_LOG_EXTERNAL_CATEGORY(sd_kernel);
25 XBT_LOG_EXTERNAL_CATEGORY(sd_task);
26 XBT_LOG_EXTERNAL_CATEGORY(sd_workstation);
29 * \brief Initialises SD internal data
31 * This function must be called before any other SD function. Then you
32 * should call SD_create_environment().
34 * \param argc argument number
35 * \param argv argument list
36 * \see SD_create_environment(), SD_exit()
38 void SD_init(int *argc, char **argv)
43 xbt_assert0(!SD_INITIALISED(), "SD_init() already called");
45 /* Connect our log channels: that must be done manually under windows */
46 XBT_LOG_CONNECT(sd_kernel, sd);
47 XBT_LOG_CONNECT(sd_task, sd);
48 XBT_LOG_CONNECT(sd_workstation, sd);
51 sd_global = xbt_new(s_SD_global_t, 1);
52 sd_global->workstations = xbt_dict_new();
53 sd_global->workstation_count = 0;
54 sd_global->workstation_list = NULL;
55 sd_global->links = xbt_dict_new();
56 sd_global->link_count = 0;
57 sd_global->link_list = NULL;
58 sd_global->recyclable_route = NULL;
59 sd_global->watch_point_reached = 0;
61 sd_global->not_scheduled_task_set =
62 xbt_swag_new(xbt_swag_offset(task, state_hookup));
63 sd_global->scheduled_task_set =
64 xbt_swag_new(xbt_swag_offset(task, state_hookup));
65 sd_global->ready_task_set =
66 xbt_swag_new(xbt_swag_offset(task, state_hookup));
67 sd_global->in_fifo_task_set =
68 xbt_swag_new(xbt_swag_offset(task, state_hookup));
69 sd_global->running_task_set =
70 xbt_swag_new(xbt_swag_offset(task, state_hookup));
71 sd_global->done_task_set =
72 xbt_swag_new(xbt_swag_offset(task, state_hookup));
73 sd_global->failed_task_set =
74 xbt_swag_new(xbt_swag_offset(task, state_hookup));
75 sd_global->task_number = 0;
77 surf_init(argc, argv);
78 xbt_cfg_set_string(_surf_cfg_set, "workstation_model", "ptask_L07");
82 * \brief Reinits the application part of the simulation (experimental feature)
84 * This function allows you to run several simulations on the same platform
85 * by resetting the part describing the application.
87 * @warning: this function is still experimental and not perfect. For example,
88 * the simulation clock (and traces usage) is not reset. So, do not use it if
89 * you use traces in your simulation, and do not use absolute timing after using it.
90 * That being said, this function is still precious if you want to compare a bunch of
91 * heuristics on the same platforms.
93 void SD_application_reinit(void)
98 if (SD_INITIALISED()) {
99 DEBUG0("Recreating the swags...");
100 xbt_swag_free(sd_global->not_scheduled_task_set);
101 xbt_swag_free(sd_global->scheduled_task_set);
102 xbt_swag_free(sd_global->ready_task_set);
103 xbt_swag_free(sd_global->in_fifo_task_set);
104 xbt_swag_free(sd_global->running_task_set);
105 xbt_swag_free(sd_global->done_task_set);
106 xbt_swag_free(sd_global->failed_task_set);
108 sd_global->not_scheduled_task_set =
109 xbt_swag_new(xbt_swag_offset(task, state_hookup));
110 sd_global->scheduled_task_set =
111 xbt_swag_new(xbt_swag_offset(task, state_hookup));
112 sd_global->ready_task_set =
113 xbt_swag_new(xbt_swag_offset(task, state_hookup));
114 sd_global->in_fifo_task_set =
115 xbt_swag_new(xbt_swag_offset(task, state_hookup));
116 sd_global->running_task_set =
117 xbt_swag_new(xbt_swag_offset(task, state_hookup));
118 sd_global->done_task_set =
119 xbt_swag_new(xbt_swag_offset(task, state_hookup));
120 sd_global->failed_task_set =
121 xbt_swag_new(xbt_swag_offset(task, state_hookup));
122 sd_global->task_number = 0;
124 WARN0("SD_application_reinit called before initialization of SimDag");
125 /* we cannot use exceptions here because xbt is not running! */
131 * \brief Creates the environment
133 * The environment (i.e. the \ref SD_workstation_management "workstations" and the
134 * \ref SD_link_management "links") is created with the data stored in the given XML
137 * \param platform_file name of an XML file describing the environment to create
138 * \see SD_workstation_management, SD_link_management
140 * The XML file follows this DTD:
142 * \include simgrid.dtd
144 * Here is a small example of such a platform:
146 * \include small_platform.xml
148 void SD_create_environment(const char *platform_file)
150 xbt_dict_cursor_t cursor = NULL;
152 void *surf_workstation = NULL;
153 void *surf_link = NULL;
155 SD_CHECK_INIT_DONE();
157 DEBUG0("SD_create_environment");
159 surf_timer_model_init(platform_file);
160 surf_config_models_setup(platform_file);
162 parse_platform_file(platform_file);
164 /* now let's create the SD wrappers for workstations and links */
165 xbt_dict_foreach(surf_model_resource_set(surf_workstation_model), cursor,
166 name, surf_workstation) {
167 __SD_workstation_create(surf_workstation, NULL);
170 xbt_dict_foreach(link_set, cursor, name, surf_link) {
171 __SD_link_create(surf_link, NULL);
174 DEBUG2("Workstation number: %d, link number: %d",
175 SD_workstation_get_number(), SD_link_get_number());
179 * \brief Launches the simulation.
181 * The function will execute the \ref SD_READY ready tasks.
182 * The simulation will be stopped when its time reaches \a how_long,
183 * when a watch point is reached, or when no more task can be executed.
184 * Then you can call SD_simulate() again.
186 * \param how_long maximum duration of the simulation (a negative value means no time limit)
187 * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
188 * \see SD_task_schedule(), SD_task_watch()
190 SD_task_t *SD_simulate(double how_long)
192 double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
193 double elapsed_time = 0.0;
194 SD_task_t task, task_safe, dst;
195 SD_dependency_t dependency;
196 surf_action_t action;
197 SD_task_t *res = NULL;
198 xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
200 static int first_time = 1;
202 SD_CHECK_INIT_DONE();
204 INFO0("Starting simulation...");
207 surf_presolve(); /* Takes traces into account */
212 surf_timer_model->extension.timer.set(surf_get_clock() + how_long,
215 sd_global->watch_point_reached = 0;
217 /* explore the ready tasks */
218 xbt_swag_foreach_safe(task, task_safe, sd_global->ready_task_set) {
219 INFO1("Executing task '%s'", SD_task_get_name(task));
220 if (__SD_task_try_to_run(task) && !xbt_dynar_member(changed_tasks, &task))
221 xbt_dynar_push(changed_tasks, &task);
226 while (elapsed_time >= 0.0 &&
227 (how_long < 0.0 || total_time < how_long) &&
228 !sd_global->watch_point_reached) {
229 surf_model_t model = NULL;
235 DEBUG1("Total time: %f", total_time);
237 elapsed_time = surf_solve();
238 DEBUG1("surf_solve() returns %f", elapsed_time);
239 if (elapsed_time > 0.0)
240 total_time += elapsed_time;
242 /* let's see which tasks are done */
243 xbt_dynar_foreach(model_list, iter, model) {
244 while ((action = xbt_swag_extract(model->states.done_action_set))) {
246 INFO1("Task '%s' done", SD_task_get_name(task));
247 DEBUG0("Calling __SD_task_just_done");
248 __SD_task_just_done(task);
249 DEBUG1("__SD_task_just_done called on task '%s'",
250 SD_task_get_name(task));
252 /* the state has changed */
253 if (!xbt_dynar_member(changed_tasks, &task))
254 xbt_dynar_push(changed_tasks, &task);
256 /* remove the dependencies after this task */
257 while (xbt_dynar_length(task->tasks_after) > 0) {
258 xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
259 dst = dependency->dst;
260 SD_task_dependency_remove(task, dst);
262 /* is dst ready now? */
263 if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
264 INFO1("Executing task '%s'", SD_task_get_name(dst));
265 if (__SD_task_try_to_run(dst) &&
266 !xbt_dynar_member(changed_tasks, &task))
267 xbt_dynar_push(changed_tasks, &task);
272 /* let's see which tasks have just failed */
273 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
275 INFO1("Task '%s' failed", SD_task_get_name(task));
276 __SD_task_set_state(task, SD_FAILED);
277 surf_workstation_model->action_unref(action);
278 task->surf_action = NULL;
280 if (!xbt_dynar_member(changed_tasks, &task))
281 xbt_dynar_push(changed_tasks, &task);
285 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
289 res = xbt_new0(SD_task_t, (xbt_dynar_length(changed_tasks) + 1));
291 xbt_dynar_foreach(changed_tasks, iter, task) {
294 xbt_dynar_free(&changed_tasks);
296 INFO0("Simulation finished");
297 DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
298 elapsed_time, total_time, sd_global->watch_point_reached);
299 DEBUG1("current time = %f", surf_get_clock());
305 * \brief Returns the current clock
307 * \return the current clock, in second
309 double SD_get_clock(void)
311 SD_CHECK_INIT_DONE();
313 return surf_get_clock();
317 * \brief Destroys all SD internal data
319 * This function should be called when the simulation is over. Don't forget also to destroy
322 * \see SD_init(), SD_task_destroy()
326 if (SD_INITIALISED()) {
327 DEBUG0("Destroying workstation and link dictionaries...");
328 xbt_dict_free(&sd_global->workstations);
329 xbt_dict_free(&sd_global->links);
331 DEBUG0("Destroying workstation and link arrays if necessary...");
332 if (sd_global->workstation_list != NULL)
333 xbt_free(sd_global->workstation_list);
335 if (sd_global->link_list != NULL)
336 xbt_free(sd_global->link_list);
338 if (sd_global->recyclable_route != NULL)
339 xbt_free(sd_global->recyclable_route);
341 DEBUG0("Destroying the swags...");
342 xbt_swag_free(sd_global->not_scheduled_task_set);
343 xbt_swag_free(sd_global->scheduled_task_set);
344 xbt_swag_free(sd_global->ready_task_set);
345 xbt_swag_free(sd_global->in_fifo_task_set);
346 xbt_swag_free(sd_global->running_task_set);
347 xbt_swag_free(sd_global->done_task_set);
348 xbt_swag_free(sd_global->failed_task_set);
353 DEBUG0("Exiting Surf...");
356 WARN0("SD_exit() called, but SimDag is not running");
357 /* we cannot use exceptions here because xbt is not running! */