Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ee029e07bf199f3def63acb3ee7144bae567cef1
[simgrid.git] / src / simdag / sd_global.c
1 #include "private.h"
2 #include "xbt/sysdep.h"
3 #include "xbt/dynar.h"
4 #include "surf/surf.h"
5 #include "xbt/ex.h"
6 #include "xbt/log.h"
7 #include "xbt/str.h"
8 #include "xbt/config.h"
9
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)");
13
14 SD_global_t sd_global = NULL;
15
16 /* $Id$ */
17
18 /* Copyright (c) 2007 Arnaud Legrand.
19    All rights reserved.                                          */
20
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. */
23
24 static int _sd_init_status = 0; /* 0: beginning of time; 
25                                    1: pre-inited (cfg_set created); 
26                                    2: inited (running) */
27 static xbt_cfg_t _sd_cfg_set = NULL;
28
29 /* callback of the workstation_model variable */
30 static void _sd_cfg_cb__workstation_model(const char *name, int pos)
31 {
32   char *val;
33
34   xbt_assert0(_sd_init_status < 2,
35               "Cannot change the model after the initialization");
36
37   val = xbt_cfg_get_string(_sd_cfg_set, name);
38   find_model_description(surf_workstation_model_description,
39                             surf_workstation_model_description_size,
40                             val);
41 }
42
43 /* callback of the cpu_model variable */
44 static void _sd_cfg_cb__cpu_model(const char *name, int pos)
45 {
46   char *val;
47
48   xbt_assert0(_sd_init_status < 2,
49               "Cannot change the model after the initialization");
50
51   val = xbt_cfg_get_string(_sd_cfg_set, name);
52   find_model_description(surf_cpu_model_description,
53                             surf_cpu_model_description_size, val);
54 }
55
56 /* callback of the workstation_model variable */
57 static void _sd_cfg_cb__network_model(const char *name, int pos)
58 {
59   char *val;
60
61   xbt_assert0(_sd_init_status < 2,
62               "Cannot change the model after the initialization");
63
64   val = xbt_cfg_get_string(_sd_cfg_set, name);
65   find_model_description(surf_network_model_description,
66                             surf_network_model_description_size, val);
67 }
68
69 /* create the config set and register what should be */
70 static void sd_config_init(void)
71 {
72
73   if (_sd_init_status)
74     return;                     /* Already inited, nothing to do */
75
76   _sd_init_status = 1;
77   _sd_cfg_set = xbt_cfg_new();
78
79   xbt_cfg_register(_sd_cfg_set,
80                    "workstation_model", xbt_cfgelm_string, 1, 1,
81                    &_sd_cfg_cb__workstation_model, NULL);
82
83   xbt_cfg_register(_sd_cfg_set,
84                    "cpu_model", xbt_cfgelm_string, 1, 1,
85                    &_sd_cfg_cb__cpu_model, NULL);
86   xbt_cfg_register(_sd_cfg_set,
87                    "network_model", xbt_cfgelm_string, 1, 1,
88                    &_sd_cfg_cb__network_model, NULL);
89
90   xbt_cfg_set_string(_sd_cfg_set, "workstation_model", "ptask_L07");
91 }
92
93 static void sd_config_finalize(void)
94 {
95
96   if (!_sd_init_status)
97     return;                     /* Not initialized yet. Nothing to do */
98
99   xbt_cfg_free(&_sd_cfg_set);
100   _sd_init_status = 0;
101 }
102
103 static void sd_config(const char *name, va_list pa)
104 {
105   if (!_sd_init_status) {
106     sd_config_init();
107   }
108   xbt_cfg_set_vargs(_sd_cfg_set, name, pa);
109 }
110
111
112 static void __sd_config_helper(const char *name, ...)
113 {
114   va_list pa;
115   va_start(pa, name);
116
117   sd_config(name, pa);
118
119   va_end(pa);
120 }
121
122 static void sd_cfg_control_set(const char *control_string)
123 {
124   /* To split the string in commands, and the cursors */
125   xbt_dynar_t set_strings;
126   char *str;
127   unsigned int cpt;
128
129   if (!control_string)
130     return;
131   DEBUG1("Parse log settings '%s'", control_string);
132
133   /* split the string, and remove empty entries */
134   set_strings = xbt_str_split_quoted(control_string);
135
136   if (xbt_dynar_length(set_strings) == 0) {     /* vicious user! */
137     xbt_dynar_free(&set_strings);
138     return;
139   }
140   /* Parse each entry and either use it right now (if the category was already
141      created), or store it for further use */
142   xbt_dynar_foreach(set_strings, cpt, str) {
143     char *control_string, *control_string_sav, *name, *value;
144
145
146     control_string = control_string_sav = strdup(str);
147     control_string += strspn(control_string, " ");
148     name = control_string;
149     control_string += strcspn(str, ":=");
150     value = control_string;
151     *value = 0;
152     value++;
153
154     xbt_assert1(strlen(name) != 0, "Invalid name for configuration: '%s'",
155                 name);
156     xbt_assert1(strlen(value) != 0,
157                 "Invalid value for configuration: '%s'", value);
158     INFO2("setting '%s' to '%s'", name, value);
159
160     __sd_config_helper(name, value);
161
162     free(control_string_sav);
163   }
164   xbt_dynar_free(&set_strings);
165 }
166
167 static void sd_cfg_init(int *argc, char **argv)
168 {
169   int i, j;
170   char *opt;
171
172   for (i = 1; i < *argc; i++) {
173     if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
174       opt = strchr(argv[i], '=');
175       opt++;
176
177       sd_cfg_control_set(opt);
178       DEBUG1("Did apply '%s' as config setting", opt);
179       /*remove this from argv */
180
181       for (j = i + 1; j < *argc; j++) {
182         argv[j - 1] = argv[j];
183       }
184
185       argv[j - 1] = NULL;
186       (*argc)--;
187       i--;                      /* compensate effect of next loop incrementation */
188     }
189   }
190 }
191
192 /**
193  * \brief Initialises SD internal data
194  *
195  * This function must be called before any other SD function. Then you
196  * should call SD_create_environment().
197  *
198  * \param argc argument number
199  * \param argv argument list
200  * \see SD_create_environment(), SD_exit()
201  */
202 void SD_init(int *argc, char **argv) {
203
204   s_SD_task_t task;
205   
206   xbt_assert0( !SD_INITIALISED() , "SD_init() already called");
207
208   sd_global = xbt_new(s_SD_global_t, 1);
209   sd_global->workstations = xbt_dict_new();
210   sd_global->workstation_count = 0;
211   sd_global->workstation_list = NULL;
212   sd_global->links = xbt_dict_new();
213   sd_global->link_count = 0;
214   sd_global->link_list = NULL;
215   sd_global->recyclable_route = NULL;
216   sd_global->watch_point_reached = 0;
217
218   sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
219   sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
220   sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
221   sd_global->in_fifo_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
222   sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
223   sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
224   sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
225   sd_global->task_number = 0;
226
227   surf_init(argc, argv);
228   sd_cfg_init(argc, argv);
229 }
230
231 /**
232  * \brief Reinits the application part of the simulation (experimental feature)
233  * 
234  * This function allows you to run several simulations on the same platform 
235  * by resetting the part describing the application. 
236  * 
237  * @warning: this function is still experimental and not perfect. For example,
238  * the simulation clock (and traces usage) is not reset. So, do not use it if
239  * you use traces in your simulation, and do not use absolute timing after using it.
240  * That being said, this function is still precious if you want to compare a bunch of
241  * heuristics on the same platforms.
242  */
243 void SD_application_reinit(void) {
244    
245   s_SD_task_t task;
246    
247   if (SD_INITIALISED()) {
248     DEBUG0("Recreating the swags...");
249     xbt_swag_free(sd_global->not_scheduled_task_set);
250     xbt_swag_free(sd_global->scheduled_task_set);
251     xbt_swag_free(sd_global->ready_task_set);
252     xbt_swag_free(sd_global->in_fifo_task_set);
253     xbt_swag_free(sd_global->running_task_set);
254     xbt_swag_free(sd_global->done_task_set);
255     xbt_swag_free(sd_global->failed_task_set);
256
257     sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
258     sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
259     sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
260     sd_global->in_fifo_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
261     sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
262     sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
263     sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
264     sd_global->task_number = 0;
265   } else {
266     WARN0("SD_application_reinit called before initialization of SimDag");
267     /* we cannot use exceptions here because xbt is not running! */
268   }
269
270 }
271
272 /**
273  * \brief Creates the environment
274  *
275  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
276  * \ref SD_link_management "links") is created with the data stored in the given XML
277  * platform file.
278  *
279  * \param platform_file name of an XML file describing the environment to create
280  * \see SD_workstation_management, SD_link_management
281  *
282  * The XML file follows this DTD:
283  *
284  *     \include surfxml.dtd
285  *
286  * Here is a small example of such a platform: 
287  *
288  *     \include small_platform.xml
289  */
290 void SD_create_environment(const char *platform_file) {
291   xbt_dict_cursor_t cursor = NULL;
292   char *name = NULL;
293   void *surf_workstation = NULL;
294   void *surf_link = NULL;
295   char *workstation_model_name;
296   int workstation_id = -1;
297
298   SD_CHECK_INIT_DONE();
299
300   DEBUG0("SD_create_environment");
301
302   sd_config_init();
303   surf_timer_model_init(platform_file);
304
305   workstation_model_name =
306       xbt_cfg_get_string(_sd_cfg_set, "workstation_model");
307
308   DEBUG1("Model : %s", workstation_model_name);
309   workstation_id =
310       find_model_description(surf_workstation_model_description,
311                                 surf_workstation_model_description_size,
312                                 workstation_model_name);
313   if (!strcmp(workstation_model_name, "compound")) {
314     xbt_ex_t e;
315     char *network_model_name = NULL;
316     char *cpu_model_name = NULL;
317     int network_id = -1;
318     int cpu_id = -1;
319
320     TRY {
321       cpu_model_name = xbt_cfg_get_string(_sd_cfg_set, "cpu_model");
322     } CATCH(e) {
323       if (e.category == bound_error) {
324         xbt_assert0(0,
325                     "Set a cpu model to use with the 'compound' workstation model");
326         xbt_ex_free(e);
327       } else {
328         RETHROW;
329       }
330     }
331
332     TRY {
333       network_model_name =
334           xbt_cfg_get_string(_sd_cfg_set, "network_model");
335     }
336     CATCH(e) {
337       if (e.category == bound_error) {
338         xbt_assert0(0,
339                     "Set a network model to use with the 'compound' workstation model");
340         xbt_ex_free(e);
341       } else {
342         RETHROW;
343       }
344     }
345
346     network_id =
347         find_model_description(surf_network_model_description,
348                                   surf_network_model_description_size,
349                                   network_model_name);
350     cpu_id =
351         find_model_description(surf_cpu_model_description,
352                                   surf_cpu_model_description_size,
353                                   cpu_model_name);
354
355     surf_cpu_model_description[cpu_id].model_init(platform_file);
356     surf_network_model_description[network_id].model_init(platform_file);
357   }
358
359   DEBUG0("Call workstation_model_init");
360   surf_workstation_model_description[workstation_id].
361       model_init(platform_file);
362
363   parse_platform_file(platform_file);
364
365   _sd_init_status = 2;
366
367   /* now let's create the SD wrappers for workstations and links */
368   xbt_dict_foreach(workstation_set, cursor, name, surf_workstation) {
369     __SD_workstation_create(surf_workstation, NULL);
370   }
371
372   xbt_dict_foreach(link_set, cursor, name, surf_link) {
373     __SD_link_create(surf_link, NULL);
374   }
375
376   DEBUG2("Workstation number: %d, link number: %d", SD_workstation_get_number(), SD_link_get_number());
377 }
378
379 /**
380  * \brief Launches the simulation.
381  *
382  * The function will execute the \ref SD_READY ready tasks.
383  * The simulation will be stopped when its time reaches \a how_long,
384  * when a watch point is reached, or when no more task can be executed.
385  * Then you can call SD_simulate() again.
386  * 
387  * \param how_long maximum duration of the simulation (a negative value means no time limit)
388  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
389  * \see SD_task_schedule(), SD_task_watch()
390  */
391 SD_task_t* SD_simulate(double how_long)
392 {
393   double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
394   double elapsed_time = 0.0;
395   SD_task_t task, dst;
396   SD_dependency_t dependency;
397   surf_action_t action;
398   SD_task_t *changed_tasks = NULL;
399   int changed_task_number = 0;
400   int changed_task_capacity = sd_global->task_number + 1;
401   unsigned int iter;
402   static int first_time = 1;
403
404   SD_CHECK_INIT_DONE();
405
406   INFO0("Starting simulation...");
407
408   /* create the array that will be returned */
409   changed_tasks = xbt_new(SD_task_t, changed_task_capacity);
410   changed_tasks[0] = NULL;
411
412   if (first_time) {
413     surf_solve(); /* Takes traces into account. Returns 0.0 */
414     first_time = 0;
415   }
416
417   if(how_long>0) {
418     surf_timer_model->extension_public->set(surf_get_clock()+how_long,
419                                                NULL,NULL);
420   }
421   sd_global->watch_point_reached = 0;
422
423   /* explore the ready tasks */
424   xbt_swag_foreach(task, sd_global->ready_task_set) {
425     INFO1("Executing task '%s'", SD_task_get_name(task));
426     if ((task->state_changed = __SD_task_try_to_run(task))) {
427       changed_tasks[changed_task_number++] = task; /* replace NULL by the task */
428       /*
429       if (changed_task_number == changed_task_capacity) {
430         changed_task_capacity *= 2;
431         changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
432       }
433       */
434       changed_tasks[changed_task_number] = NULL;
435     }
436   }
437
438   /* main loop */
439   elapsed_time = 0.0;
440   while (elapsed_time >= 0.0 &&
441          (how_long < 0.0 || total_time < how_long) &&
442          !sd_global->watch_point_reached) {
443     surf_model_t model = NULL;
444     /* dumb variables */
445     void *fun = NULL;
446     void *arg = NULL;
447
448
449     DEBUG1("Total time: %f", total_time);
450
451     elapsed_time = surf_solve();
452     DEBUG1("surf_solve() returns %f", elapsed_time);
453     if (elapsed_time > 0.0)
454       total_time += elapsed_time;
455
456     /* let's see which tasks are done */
457     xbt_dynar_foreach(model_list, iter, model) {
458       while ((action = xbt_swag_extract(model->common_public->
459                                         states.done_action_set))) {
460         task = action->data;
461         INFO1("Task '%s' done", SD_task_get_name(task));
462         DEBUG0("Calling __SD_task_just_done");
463         __SD_task_just_done(task);
464         DEBUG1("__SD_task_just_done called on task '%s'", SD_task_get_name(task));
465         
466         /* the state has changed */
467         if (!task->state_changed) {
468           task->state_changed = 1;
469           changed_tasks[changed_task_number++] = task;
470           /*
471             if (changed_task_number == changed_task_capacity) {
472             changed_task_capacity *= 2;
473             changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
474             }
475           */
476           changed_tasks[changed_task_number] = NULL;
477         }
478
479         /* remove the dependencies after this task */
480         while (xbt_dynar_length(task->tasks_after) > 0) {
481           xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
482           dst = dependency->dst;
483           SD_task_dependency_remove(task, dst);
484           
485           /* is dst ready now? */
486           if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
487             INFO1("Executing task '%s'", SD_task_get_name(dst));
488             if (__SD_task_try_to_run(dst)) {
489               changed_tasks[changed_task_number++] = dst;
490               /*
491                 if (changed_task_number == changed_task_capacity) {
492                 changed_task_capacity *= 2;
493                 changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
494                 }
495               */
496               changed_tasks[changed_task_number] = NULL;
497             }
498           }
499         }
500       }
501
502       /* let's see which tasks have just failed */
503       while ((action = xbt_swag_extract(model->common_public->states.failed_action_set))) {
504         task = action->data;
505         INFO1("Task '%s' failed", SD_task_get_name(task));
506         __SD_task_set_state(task, SD_FAILED);
507         surf_workstation_model->common_public->action_free(action);
508         task->surf_action = NULL;
509         
510         if (!task->state_changed) {
511           task->state_changed = 1;
512           changed_tasks[changed_task_number++] = task;
513           /*
514             if (changed_task_number == changed_task_capacity) {
515             changed_task_capacity *= 2;
516             changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
517             }
518           */
519           changed_tasks[changed_task_number] = NULL;
520         }
521       }
522     }
523
524     while (surf_timer_model->extension_public->get(&fun,(void*)&arg)) {
525     }
526   }
527
528   /* we must reset every task->state_changed */
529   iter = 0;
530   while (changed_tasks[iter] != NULL) {
531     changed_tasks[iter]->state_changed = 0;
532     iter++;
533   }
534
535   INFO0("Simulation finished");
536   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d", elapsed_time, total_time, sd_global->watch_point_reached);
537   DEBUG1("current time = %f", surf_get_clock());
538
539   return changed_tasks;
540 }
541
542 /**
543  * \brief Returns the current clock
544  *
545  * \return the current clock, in second
546  */
547 double SD_get_clock(void) {
548   SD_CHECK_INIT_DONE();
549
550   return surf_get_clock();
551 }
552
553 /**
554  * \brief Destroys all SD internal data
555  *
556  * This function should be called when the simulation is over. Don't forget also to destroy
557  * the tasks.
558  *
559  * \see SD_init(), SD_task_destroy()
560  */
561 void SD_exit(void) {
562   if (SD_INITIALISED()) {
563     DEBUG0("Destroying workstation and link dictionaries...");
564     xbt_dict_free(&sd_global->workstations);
565     xbt_dict_free(&sd_global->links);
566
567     DEBUG0("Destroying workstation and link arrays if necessary...");
568     if (sd_global->workstation_list != NULL)
569       xbt_free(sd_global->workstation_list);
570
571     if (sd_global->link_list != NULL)
572       xbt_free(sd_global->link_list);
573
574     if (sd_global->recyclable_route != NULL)
575       xbt_free(sd_global->recyclable_route);
576
577     DEBUG0("Destroying the swags...");
578     xbt_swag_free(sd_global->not_scheduled_task_set);
579     xbt_swag_free(sd_global->scheduled_task_set);
580     xbt_swag_free(sd_global->ready_task_set);
581     xbt_swag_free(sd_global->in_fifo_task_set);
582     xbt_swag_free(sd_global->running_task_set);
583     xbt_swag_free(sd_global->done_task_set);
584     xbt_swag_free(sd_global->failed_task_set);
585
586     xbt_free(sd_global);
587     sd_global = NULL;
588
589     DEBUG0("Exiting Surf...");
590     surf_exit();
591   }
592   else {
593     WARN0("SD_exit() called, but SimDag is not running");
594     /* we cannot use exceptions here because xbt is not running! */
595   }
596 }