2 #include "xbt/sysdep.h"
6 XBT_LOG_NEW_CATEGORY(sd,"Logging specific to SimDag");
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel,sd,
8 "Logging specific to SimDag (kernel)");
10 SD_global_t sd_global = NULL;
13 * \brief Initialises SD internal data
15 * This function must be called before any other SD function. Then you
16 * should call SD_create_environment().
18 * \param argc argument number
19 * \param argv argument list
20 * \see SD_create_environment(), SD_exit()
22 void SD_init(int *argc, char **argv) {
23 xbt_assert0(sd_global == NULL, "SD_init already called");
25 sd_global = xbt_new0(s_SD_global_t, 1);
26 sd_global->workstations = xbt_dict_new();
27 sd_global->workstation_count = 0;
28 sd_global->links = xbt_dict_new();
29 sd_global->watch_point_reached = 0;
32 sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
33 sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
34 sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
35 sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
36 sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
37 sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
39 surf_init(argc, argv);
43 * \brief Creates the environment
45 * The environment (i.e. the \ref SD_workstation_management "workstations" and the
46 * \ref SD_link_management "links") is created with the data stored in the given XML
49 * \param platform_file name of an XML file describing the environment to create
50 * \see SD_workstation_management, SD_link_management
52 * The XML file follows this DTD:
54 * \include surfxml.dtd
56 * Here is a small example of such a platform:
58 * \include small_platform.xml
60 void SD_create_environment(const char *platform_file) {
61 xbt_dict_cursor_t cursor = NULL;
63 void *surf_workstation = NULL;
64 void *surf_link = NULL;
68 surf_timer_resource_init(platform_file); /* tell Surf to create the environnement */
71 /*printf("surf_workstation_resource = %p, workstation_set = %p\n", surf_workstation_resource, workstation_set);
72 printf("surf_network_resource = %p, network_link_set = %p\n", surf_network_resource, network_link_set);*/
74 /*surf_workstation_resource_init_KCCFLN05(platform_file);*/
75 surf_workstation_resource_init_CLM03(platform_file);
77 /*printf("surf_workstation_resource = %p, workstation_set = %p\n", surf_workstation_resource, workstation_set);
78 printf("surf_network_resource = %p, network_link_set = %p\n", surf_network_resource, network_link_set);*/
81 /* now let's create the SD wrappers for workstations and links */
82 xbt_dict_foreach(workstation_set, cursor, name, surf_workstation) {
83 __SD_workstation_create(surf_workstation, NULL);
86 xbt_dict_foreach(network_link_set, cursor, name, surf_link) {
87 __SD_link_create(surf_link, NULL);
92 * \brief Launches the simulation.
94 * The function will execute the \ref SD_READY ready tasks.
95 * The simulation will be stopped when its time reaches \a how_long,
96 * when a watch point is reached, or when no more task can be executed.
97 * Then you can call SD_simulate() again.
99 * \param how_long maximum duration of the simulation
100 * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
101 * \see SD_task_schedule(), SD_task_watch()
103 SD_task_t* SD_simulate(double how_long)
105 double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
106 double elapsed_time = 0.0;
108 surf_action_t action;
109 SD_task_t *changed_tasks = NULL;
110 int changed_task_number = 0;
111 int changed_task_capacity = 16; /* will be increased if necessary */
113 /* create the array that will be returned */
114 changed_tasks = xbt_new0(SD_task_t, changed_task_capacity);
115 changed_tasks[0] = NULL;
117 surf_solve(); /* Takes traces into account. Returns 0.0 */
119 sd_global->watch_point_reached = 0;
122 while (elapsed_time >= 0.0 && total_time < how_long && !sd_global->watch_point_reached) {
124 /* explore the ready tasks */
125 xbt_swag_foreach(task, sd_global->ready_task_set) {
126 INFO1("Executing task '%s'", SD_task_get_name(task));
127 task->state_changed = 0;
128 action = __SD_task_run(task);
129 surf_workstation_resource->common_public->action_set_data(action, task);
130 task->state_changed = 1;
132 changed_tasks[changed_task_number++] = task; /* replace NULL by the task */
133 if (changed_task_number == changed_task_capacity) {
134 changed_task_capacity *= 2;
135 changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
137 changed_tasks[changed_task_number] = NULL;
140 elapsed_time = surf_solve();
141 if (elapsed_time > 0.0)
142 total_time += elapsed_time;
144 /* let's see which tasks are done */
145 while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.done_action_set))) {
147 INFO1("Task '%s' done", SD_task_get_name(task));
148 __SD_task_set_state(task, SD_DONE);
149 __SD_task_remove_dependencies(task);
150 if (!task->state_changed) {
151 task->state_changed = 1;
152 changed_tasks[changed_task_number++] = task;
156 /* let's see which tasks have just failed */
157 while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.failed_action_set))) {
159 INFO1("Task '%s' failed", SD_task_get_name(task));
160 __SD_task_set_state(task, SD_FAILED);
161 if (!task->state_changed) {
162 task->state_changed = 1;
163 changed_tasks[changed_task_number++] = task;
168 INFO0("Simulation finished");
169 INFO1("Number of tasks whose state has changed: %d", changed_task_number);
171 return changed_tasks;
175 * \brief Destroys all SD internal data.
177 * This function should be called when the simulation is over. Don't forget also to destroy
180 * \see SD_init(), SD_task_destroy()
183 if (sd_global != NULL) {
184 xbt_dict_free(&sd_global->workstations);
185 xbt_dict_free(&sd_global->links);
188 xbt_swag_free(sd_global->not_scheduled_task_set);
189 xbt_swag_free(sd_global->scheduled_task_set);
190 xbt_swag_free(sd_global->ready_task_set);
191 xbt_swag_free(sd_global->running_task_set);
192 xbt_swag_free(sd_global->done_task_set);
193 xbt_swag_free(sd_global->failed_task_set);