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;
12 /* Initialises SD internal data. This function should be called before any other SD function.
14 void SD_init(int *argc, char **argv) {
15 xbt_assert0(sd_global == NULL, "SD_init already called");
17 sd_global = xbt_new0(s_SD_global_t, 1);
18 sd_global->workstations = xbt_dict_new();
19 sd_global->workstation_count = 0;
20 sd_global->links = xbt_dict_new();
21 sd_global->watch_point_reached = 0;
24 sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
25 sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
26 sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
27 sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
28 sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
29 sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
31 surf_init(argc, argv);
34 /* Creates the environnement described in a xml file of a platform description.
36 void SD_create_environment(const char *platform_file) {
37 xbt_dict_cursor_t cursor = NULL;
39 void *surf_workstation = NULL;
40 void *surf_link = NULL;
44 surf_timer_resource_init(platform_file); /* tell Surf to create the environnement */
47 /*printf("surf_workstation_resource = %p, workstation_set = %p\n", surf_workstation_resource, workstation_set);
48 printf("surf_network_resource = %p, network_link_set = %p\n", surf_network_resource, network_link_set);*/
50 /*surf_workstation_resource_init_KCCFLN05(platform_file);*/
51 surf_workstation_resource_init_CLM03(platform_file);
53 /*printf("surf_workstation_resource = %p, workstation_set = %p\n", surf_workstation_resource, workstation_set);
54 printf("surf_network_resource = %p, network_link_set = %p\n", surf_network_resource, network_link_set);*/
57 /* now let's create the SD wrappers for workstations and links */
58 xbt_dict_foreach(workstation_set, cursor, name, surf_workstation) {
59 __SD_workstation_create(surf_workstation, NULL);
62 xbt_dict_foreach(network_link_set, cursor, name, surf_link) {
63 __SD_link_create(surf_link, NULL);
67 /* Launches the simulation. Returns a NULL-terminated array of SD_task_t whose state has changed.
69 SD_task_t* SD_simulate(double how_long)
71 double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
72 double elapsed_time = 0.0;
75 SD_task_t *changed_tasks = NULL;
76 int changed_task_number = 0;
77 int changed_task_capacity = 16; /* will be increased if necessary */
79 /* create the array that will be returned */
80 changed_tasks = xbt_new0(SD_task_t, changed_task_capacity);
81 changed_tasks[0] = NULL;
83 surf_solve(); /* Takes traces into account. Returns 0.0 */
85 sd_global->watch_point_reached = 0;
88 while (elapsed_time >= 0.0 && total_time < how_long && !sd_global->watch_point_reached) {
90 /* explore the ready tasks */
91 xbt_swag_foreach(task, sd_global->ready_task_set) {
92 INFO1("Executing task '%s'", SD_task_get_name(task));
93 task->state_changed = 0;
94 action = __SD_task_run(task);
95 surf_workstation_resource->common_public->action_set_data(action, task);
96 task->state_changed = 1;
98 changed_tasks[changed_task_number++] = task; /* replace NULL by the task */
99 if (changed_task_number == changed_task_capacity) {
100 changed_task_capacity *= 2;
101 changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
103 changed_tasks[changed_task_number] = NULL;
106 elapsed_time = surf_solve();
107 if (elapsed_time > 0.0)
108 total_time += elapsed_time;
110 /* let's see which tasks are done */
111 while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.done_action_set))) {
113 INFO1("Task '%s' done", SD_task_get_name(task));
114 __SD_task_set_state(task, SD_DONE);
115 __SD_task_remove_dependencies(task);
116 if (!task->state_changed) {
117 task->state_changed = 1;
118 changed_tasks[changed_task_number++] = task;
122 /* let's see which tasks have just failed */
123 while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.failed_action_set))) {
125 INFO1("Task '%s' failed", SD_task_get_name(task));
126 __SD_task_set_state(task, SD_FAILED);
127 if (!task->state_changed) {
128 task->state_changed = 1;
129 changed_tasks[changed_task_number++] = task;
134 INFO0("Simulation finished");
135 INFO1("Number of tasks whose state has changed: %d", changed_task_number);
137 return changed_tasks;
140 /* Destroys all SD internal data. This function should be called when the simulation is over.
141 * The tasks should have been destroyed first.
144 if (sd_global != NULL) {
145 xbt_dict_free(&sd_global->workstations);
146 xbt_dict_free(&sd_global->links);
149 xbt_swag_free(sd_global->not_scheduled_task_set);
150 xbt_swag_free(sd_global->scheduled_task_set);
151 xbt_swag_free(sd_global->ready_task_set);
152 xbt_swag_free(sd_global->running_task_set);
153 xbt_swag_free(sd_global->done_task_set);
154 xbt_swag_free(sd_global->failed_task_set);