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_config_models_setup(platform_file);
161 parse_platform_file(platform_file);
163 /* now let's create the SD wrappers for workstations and links */
164 xbt_dict_foreach(surf_model_resource_set(surf_workstation_model), cursor,
165 name, surf_workstation) {
166 __SD_workstation_create(surf_workstation, NULL);
169 xbt_dict_foreach(surf_model_resource_set(surf_network_model), cursor, name, surf_link) {
170 __SD_link_create(surf_link, NULL);
173 DEBUG2("Workstation number: %d, link number: %d",
174 SD_workstation_get_number(), SD_link_get_number());
178 * \brief Launches the simulation.
180 * The function will execute the \ref SD_READY ready tasks.
181 * The simulation will be stopped when its time reaches \a how_long,
182 * when a watch point is reached, or when no more task can be executed.
183 * Then you can call SD_simulate() again.
185 * \param how_long maximum duration of the simulation (a negative value means no time limit)
186 * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
187 * \see SD_task_schedule(), SD_task_watch()
189 SD_task_t *SD_simulate(double how_long)
191 double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
192 double elapsed_time = 0.0;
193 SD_task_t task, task_safe, dst;
194 SD_dependency_t dependency;
195 surf_action_t action;
196 SD_task_t *res = NULL;
197 xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
199 static int first_time = 1;
201 SD_CHECK_INIT_DONE();
203 INFO0("Starting simulation...");
206 surf_presolve(); /* Takes traces into account */
211 surf_timer_model->extension.timer.set(surf_get_clock() + how_long,
214 sd_global->watch_point_reached = 0;
216 /* explore the ready tasks */
217 xbt_swag_foreach_safe(task, task_safe, sd_global->ready_task_set) {
218 INFO1("Executing task '%s'", SD_task_get_name(task));
219 if (__SD_task_try_to_run(task) && !xbt_dynar_member(changed_tasks, &task))
220 xbt_dynar_push(changed_tasks, &task);
225 while (elapsed_time >= 0.0 &&
226 (how_long < 0.0 || total_time < how_long) &&
227 !sd_global->watch_point_reached) {
228 surf_model_t model = NULL;
234 DEBUG1("Total time: %f", total_time);
236 elapsed_time = surf_solve();
237 DEBUG1("surf_solve() returns %f", elapsed_time);
238 if (elapsed_time > 0.0)
239 total_time += elapsed_time;
241 /* let's see which tasks are done */
242 xbt_dynar_foreach(model_list, iter, model) {
243 while ((action = xbt_swag_extract(model->states.done_action_set))) {
245 INFO1("Task '%s' done", SD_task_get_name(task));
246 DEBUG0("Calling __SD_task_just_done");
247 __SD_task_just_done(task);
248 DEBUG1("__SD_task_just_done called on task '%s'",
249 SD_task_get_name(task));
251 /* the state has changed */
252 if (!xbt_dynar_member(changed_tasks, &task))
253 xbt_dynar_push(changed_tasks, &task);
255 /* remove the dependencies after this task */
256 while (xbt_dynar_length(task->tasks_after) > 0) {
257 xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
258 dst = dependency->dst;
259 SD_task_dependency_remove(task, dst);
261 /* is dst ready now? */
262 if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
263 INFO1("Executing task '%s'", SD_task_get_name(dst));
264 if (__SD_task_try_to_run(dst) &&
265 !xbt_dynar_member(changed_tasks, &task))
266 xbt_dynar_push(changed_tasks, &task);
271 /* let's see which tasks have just failed */
272 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
274 INFO1("Task '%s' failed", SD_task_get_name(task));
275 __SD_task_set_state(task, SD_FAILED);
276 surf_workstation_model->action_unref(action);
277 task->surf_action = NULL;
279 if (!xbt_dynar_member(changed_tasks, &task))
280 xbt_dynar_push(changed_tasks, &task);
284 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
288 res = xbt_new0(SD_task_t, (xbt_dynar_length(changed_tasks) + 1));
290 xbt_dynar_foreach(changed_tasks, iter, task) {
293 xbt_dynar_free(&changed_tasks);
295 INFO0("Simulation finished");
296 DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
297 elapsed_time, total_time, sd_global->watch_point_reached);
298 DEBUG1("current time = %f", surf_get_clock());
304 * \brief Returns the current clock
306 * \return the current clock, in second
308 double SD_get_clock(void)
310 SD_CHECK_INIT_DONE();
312 return surf_get_clock();
316 * \brief Destroys all SD internal data
318 * This function should be called when the simulation is over. Don't forget also to destroy
321 * \see SD_init(), SD_task_destroy()
325 if (SD_INITIALISED()) {
326 DEBUG0("Destroying workstation and link dictionaries...");
327 xbt_dict_free(&sd_global->workstations);
328 xbt_dict_free(&sd_global->links);
330 DEBUG0("Destroying workstation and link arrays if necessary...");
331 if (sd_global->workstation_list != NULL)
332 xbt_free(sd_global->workstation_list);
334 if (sd_global->link_list != NULL)
335 xbt_free(sd_global->link_list);
337 if (sd_global->recyclable_route != NULL)
338 xbt_free(sd_global->recyclable_route);
340 DEBUG0("Destroying the swags...");
341 xbt_swag_free(sd_global->not_scheduled_task_set);
342 xbt_swag_free(sd_global->scheduled_task_set);
343 xbt_swag_free(sd_global->ready_task_set);
344 xbt_swag_free(sd_global->in_fifo_task_set);
345 xbt_swag_free(sd_global->running_task_set);
346 xbt_swag_free(sd_global->done_task_set);
347 xbt_swag_free(sd_global->failed_task_set);
352 DEBUG0("Exiting Surf...");
355 WARN0("SD_exit() called, but SimDag is not running");
356 /* we cannot use exceptions here because xbt is not running! */