1 /* Copyright (c) 2006-2016. 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. */
7 #include "src/surf/surf_interface.hpp"
8 #include "src/simdag/simdag_private.h"
9 #include "instr/instr_interface.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/dynar.h"
12 #include "surf/surf.h"
13 #include "simgrid/sg_config.h"
14 #include "simgrid/host.h"
18 #include "xbt/config.h"
19 #include "surf/surfxml_parse.h"
27 #include "instr/jedule/jedule_sd_binding.h"
30 XBT_LOG_NEW_CATEGORY(sd, "Logging specific to SimDag");
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel, sd,
32 "Logging specific to SimDag (kernel)");
34 SD_global_t sd_global = NULL;
37 * \brief Initializes SD internal data
39 * This function must be called before any other SD function. Then you
40 * should call SD_create_environment().
42 * \param argc argument number
43 * \param argv argument list
44 * \see SD_create_environment(), SD_exit()
46 void SD_init(int *argc, char **argv)
48 TRACE_global_init(argc, argv);
52 xbt_assert(sd_global == NULL, "SD_init() already called");
54 sd_global = xbt_new(s_SD_global_t, 1);
55 sd_global->workstation_list = NULL;
56 sd_global->link_list = NULL;
57 sd_global->recyclable_route = NULL;
58 sd_global->watch_point_reached = 0;
60 sd_global->task_mallocator=xbt_mallocator_new(65536, SD_task_new_f,
64 sd_global->initial_task_set = xbt_dynar_new(sizeof(SD_task_t), NULL);
65 sd_global->executable_task_set = xbt_dynar_new(sizeof(SD_task_t), NULL);
66 sd_global->completed_task_set = xbt_dynar_new(sizeof(SD_task_t), NULL);
68 sd_global->return_set =
69 xbt_swag_new(xbt_swag_offset(task, return_hookup));
70 sd_global->task_number = 0;
72 surf_init(argc, argv);
74 xbt_cfg_setdefault_string(_sg_cfg_set, "host/model",
81 XBT_DEBUG("ADD SD LEVELS");
82 SD_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,__SD_storage_destroy);
84 if (_sg_cfg_exit_asap) {
90 /** \brief set a configuration variable
92 * Do --help on any simgrid binary to see the list of currently existing
93 * configuration variables, and see Section @ref options.
96 * SD_config("host/model","default");
98 void SD_config(const char *key, const char *value){
99 xbt_assert(sd_global,"ERROR: Please call SD_init() before using SD_config()");
100 xbt_cfg_set_as_string(_sg_cfg_set, key, value);
105 * \brief Reinits the application part of the simulation (experimental feature)
107 * This function allows you to run several simulations on the same platform
108 * by resetting the part describing the application.
110 * @warning: this function is still experimental and not perfect. For example,
111 * the simulation clock (and traces usage) is not reset. So, do not use it if
112 * you use traces in your simulation, and do not use absolute timing after
114 * That being said, this function is still precious if you want to compare a
115 * bunch of heuristics on the same platforms.
117 void SD_application_reinit(void)
122 // SD_task_t done_task, next_done_task;
123 xbt_die("This function is not working since the C++ links and others. Please report the problem if you really need that function.");
125 XBT_DEBUG("Recreating the swags...");
127 // xbt_swag_foreach_safe(done_task, next_done_task, sd_global->done_task_set){
128 // if (xbt_dynar_is_empty(done_task->tasks_before)){
129 // __SD_task_set_state(done_task, SD_SCHEDULABLE);
131 // __SD_task_set_state(done_task, SD_NOT_SCHEDULED);
132 // done_task->unsatisfied_dependencies =
133 // xbt_dynar_length(done_task->tasks_before);
134 // done_task->is_not_ready = done_task->unsatisfied_dependencies;
136 // free(done_task->workstation_list);
137 // done_task->workstation_list = NULL;
138 // done_task->workstation_nb = 0;
141 sd_global->task_number = 0;
152 * \brief Creates the environment
154 * The environment (i.e. the \ref SD_workstation_management "workstations"
155 * and the \ref SD_link_management "links") is created with the data stored
156 * in the given XML platform file.
158 * \param platform_file name of an XML file describing the environment to create
159 * \see SD_workstation_management, SD_link_management
161 * The XML file follows this DTD:
163 * \include simgrid.dtd
165 * Here is a small example of such a platform:
167 * \include small_platform.xml
169 void SD_create_environment(const char *platform_file)
171 xbt_lib_cursor_t cursor = NULL;
173 void **surf_storage = NULL;
175 parse_platform_file(platform_file);
177 /* now let's create the SD wrappers for workstations, storages and links */
179 xbt_dict_cursor_t cursor = NULL;
180 simgrid_Host* host = NULL;
181 xbt_dict_foreach(host_list, cursor, name, host){
182 __SD_workstation_create((const char*)name);
186 xbt_lib_foreach(storage_lib, cursor, name, surf_storage) {
187 if(surf_storage[SURF_STORAGE_LEVEL])
188 __SD_storage_create(surf_storage[SURF_STORAGE_LEVEL], NULL);
192 XBT_DEBUG("Workstation number: %d, link number: %d",
193 SD_workstation_get_number(), SD_link_get_number());
195 jedule_setup_platform();
200 * \brief Launches the simulation.
202 * The function will execute the \ref SD_RUNNABLE runnable tasks.
203 * If \a how_long is positive, then the simulation will be stopped either
204 * when time reaches \a how_long or when a watch point is reached.
205 * A non-positive value for \a how_long means no time limit, in which case
206 * the simulation will be stopped either when a watch point is reached or
207 * when no more task can be executed.
208 * Then you can call SD_simulate() again.
210 * \param how_long maximum duration of the simulation (a negative value means
212 * \return a dynar of \ref SD_task_t whose state has changed.
213 * \see SD_task_schedule(), SD_task_watch()
216 xbt_dynar_t SD_simulate(double how_long) {
217 xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
220 SD_simulate_swag(how_long);
221 while( (task = (SD_task_t)xbt_swag_extract(sd_global->return_set)) != NULL) {
222 xbt_dynar_push(changed_tasks, &task);
225 return changed_tasks;
228 xbt_swag_t SD_simulate_swag(double how_long) {
229 /* we stop the simulation when total_time >= how_long */
230 double total_time = 0.0;
231 double elapsed_time = 0.0;
233 SD_dependency_t dependency;
234 surf_action_t action;
235 unsigned int iter, depcnt;
236 static int first_time = 1;
239 XBT_VERB("Starting simulation...");
241 surf_presolve(); /* Takes traces into account */
245 XBT_VERB("Run simulation for %f seconds", how_long);
246 sd_global->watch_point_reached = 0;
248 xbt_swag_reset(sd_global->return_set);
250 /* explore the runnable tasks */
251 xbt_dynar_foreach(sd_global->executable_task_set , iter, task) {
252 XBT_VERB("Executing task '%s'", SD_task_get_name(task));
253 if (__SD_task_try_to_run(task)){
254 xbt_swag_insert(task,sd_global->return_set);
261 while (elapsed_time >= 0.0 &&
262 (how_long < 0.0 || 0.00001 < (how_long -total_time)) &&
263 !sd_global->watch_point_reached) {
264 surf_model_t model = NULL;
268 XBT_DEBUG("Total time: %f", total_time);
270 elapsed_time = surf_solve(how_long > 0 ? surf_get_clock() + how_long - total_time: -1.0);
271 XBT_DEBUG("surf_solve() returns %f", elapsed_time);
272 if (elapsed_time > 0.0)
273 total_time += elapsed_time;
275 /* let's see which tasks are done */
276 xbt_dynar_foreach(all_existing_models, iter, model) {
277 while ((action = surf_model_extract_done_action_set(model))) {
278 task = (SD_task_t) action->getData();
279 task->start_time = task->surf_action->getStartTime();
281 task->finish_time = surf_get_clock();
282 XBT_VERB("Task '%s' done", SD_task_get_name(task));
283 XBT_DEBUG("Calling __SD_task_just_done");
284 __SD_task_just_done(task);
285 XBT_DEBUG("__SD_task_just_done called on task '%s'",
286 SD_task_get_name(task));
288 /* the state has changed */
289 XBT_INFO("%d",xbt_swag_belongs(task, sd_global->return_set));
290 xbt_swag_insert(task,sd_global->return_set);
292 /* remove the dependencies after this task */
293 xbt_dynar_foreach(task->tasks_after, depcnt, dependency) {
294 dst = dependency->dst;
295 if (dst->unsatisfied_dependencies > 0)
296 dst->unsatisfied_dependencies--;
297 if (dst->is_not_ready > 0)
300 XBT_DEBUG("Released a dependency on %s: %d remain(s). Became schedulable if %d=0",
301 SD_task_get_name(dst), dst->unsatisfied_dependencies,
304 if (!(dst->unsatisfied_dependencies)) {
305 if (SD_task_get_state(dst) == SD_SCHEDULED)
306 SD_task_set_state(dst, SD_RUNNABLE);
308 SD_task_set_state(dst, SD_SCHEDULABLE);
311 if (SD_task_get_state(dst) == SD_NOT_SCHEDULED &&
312 !(dst->is_not_ready)) {
313 SD_task_set_state(dst, SD_SCHEDULABLE);
316 if (SD_task_get_kind(dst) == SD_TASK_COMM_E2E) {
317 SD_dependency_t comm_dep;
319 xbt_dynar_get_cpy(dst->tasks_after, 0, &comm_dep);
320 comm_dst = comm_dep->dst;
321 if (SD_task_get_state(comm_dst) == SD_NOT_SCHEDULED &&
322 comm_dst->is_not_ready > 0) {
323 comm_dst->is_not_ready--;
325 XBT_DEBUG("%s is a transfer, %s may be ready now if %d=0",
326 SD_task_get_name(dst), SD_task_get_name(comm_dst),
327 comm_dst->is_not_ready);
329 if (!(comm_dst->is_not_ready)) {
330 SD_task_set_state(comm_dst, SD_SCHEDULABLE);
335 /* is dst runnable now? */
336 if (SD_task_get_state(dst) == SD_RUNNABLE
337 && !sd_global->watch_point_reached) {
338 XBT_VERB("Executing task '%s'", SD_task_get_name(dst));
339 if (__SD_task_try_to_run(dst))
340 xbt_swag_insert(dst,sd_global->return_set);
345 /* let's see which tasks have just failed */
346 while ((action = surf_model_extract_failed_action_set(model))) {
347 task = (SD_task_t) action->getData();
348 task->start_time = surf_action_get_start_time(task->surf_action);
349 task->finish_time = surf_get_clock();
350 XBT_VERB("Task '%s' failed", SD_task_get_name(task));
351 SD_task_set_state(task, SD_FAILED);
353 task->surf_action = NULL;
355 xbt_swag_insert(task,sd_global->return_set);
360 if (!sd_global->watch_point_reached && how_long<0){
361 if ((int) xbt_dynar_length(sd_global->completed_task_set) <
362 sd_global->task_number){
363 XBT_WARN("Simulation is finished but %d tasks are still not done",
364 (sd_global->task_number -
365 (int) xbt_dynar_length(sd_global->completed_task_set)));
366 static const char* state_names[] =
367 { "SD_NOT_SCHEDULED", "SD_SCHEDULABLE", "SD_SCHEDULED",
368 "SD_RUNNABLE", "SD_IN_INFO", "SD_RUNNING", "SD_DONE",
370 xbt_dynar_foreach(sd_global->initial_task_set, iter, task){
371 XBT_WARN("%s is in %s state", SD_task_get_name(task),
372 state_names[SD_task_get_state(task)]);
377 XBT_DEBUG("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
378 elapsed_time, total_time, sd_global->watch_point_reached);
379 XBT_DEBUG("current time = %f", surf_get_clock());
381 return sd_global->return_set;
385 * \brief Returns the current clock
387 * \return the current clock, in second
389 double SD_get_clock(void) {
390 return surf_get_clock();
394 * \brief Destroys all SD internal data
396 * This function should be called when the simulation is over. Don't forget
399 * \see SD_init(), SD_task_destroy()
403 TRACE_surf_resource_utilization_release();
405 xbt_mallocator_free(sd_global->task_mallocator);
407 XBT_DEBUG("Destroying workstation and link arrays...");
408 xbt_free(sd_global->workstation_list);
409 xbt_free(sd_global->link_list);
410 xbt_free(sd_global->recyclable_route);
412 XBT_DEBUG("Destroying the dynars and swags...");
413 xbt_dynar_free_container(&(sd_global->initial_task_set));
414 xbt_dynar_free_container(&(sd_global->executable_task_set));
415 xbt_dynar_free_container(&(sd_global->completed_task_set));
416 xbt_swag_free(sd_global->return_set);
429 XBT_DEBUG("Exiting Surf...");