Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] at the end of tracing, dump everything up to last simulated time
[simgrid.git] / src / instr / instr_config.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7
8 #include "instr/instr_private.h"
9
10 #ifdef HAVE_TRACING
11
12 XBT_LOG_NEW_CATEGORY(instr, "Logging the behavior of the tracing system (used for Visualization/Analysis of simulations)");
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_config, instr, "Configuration");
14
15 #define OPT_TRACING               "tracing"
16 #define OPT_TRACING_SMPI          "tracing/smpi"
17 #define OPT_TRACING_SMPI_GROUP    "tracing/smpi/group"
18 #define OPT_TRACING_CATEGORIZED   "tracing/categorized"
19 #define OPT_TRACING_UNCATEGORIZED "tracing/uncategorized"
20 #define OPT_TRACING_MSG_TASK      "tracing/msg/task"
21 #define OPT_TRACING_MSG_PROCESS   "tracing/msg/process"
22 #define OPT_TRACING_MSG_VOLUME    "tracing/msg/volume"
23 #define OPT_TRACING_FILENAME      "tracing/filename"
24 #define OPT_TRACING_PLATFORM_METHOD "tracing/platform/method"
25 #define OPT_TRIVA_UNCAT_CONF      "triva/uncategorized"
26 #define OPT_TRIVA_CAT_CONF        "triva/categorized"
27
28 static int trace_configured = 0;
29 static int trace_active = 0;
30
31 xbt_dict_t created_categories; //declared in instr_interface.c
32
33 int TRACE_start()
34 {
35   // tracing system must be:
36   //    - enabled (with --cfg=tracing:1)
37   //    - already configured (TRACE_global_init already called)
38   if (!(TRACE_is_enabled() && TRACE_is_configured())){
39     return 0;
40   }
41
42   DEBUG0("Tracing starts");
43
44   /* open the trace file */
45   TRACE_paje_start();
46
47   /* activate trace */
48   TRACE_activate ();
49
50   /* other trace initialization */
51   created_categories = xbt_dict_new();
52   TRACE_surf_alloc();
53   TRACE_smpi_alloc();
54   return 0;
55 }
56
57 int TRACE_end()
58 {
59   if (!TRACE_is_active())
60     return 1;
61
62   /* generate uncategorized graph configuration for triva */
63   if (TRACE_get_triva_uncat_conf()){
64     TRACE_generate_triva_uncat_conf();
65   }
66
67   /* generate categorized graph configuration for triva */
68   if (TRACE_get_triva_cat_conf()){
69     TRACE_generate_triva_cat_conf();
70   }
71
72   /* dump trace buffer */
73   TRACE_last_timestamp_to_dump = surf_get_clock();
74   TRACE_paje_dump_buffer();
75
76   /* destroy all data structures of tracing (and free) */
77   destroyAllContainers();
78
79   /* close the trace file */
80   TRACE_paje_end();
81
82   /* activate trace */
83   TRACE_desactivate ();
84   DEBUG0("Tracing system is shutdown");
85   return 0;
86 }
87
88 void TRACE_activate (void)
89 {
90   xbt_assert0 (trace_active==0, "Tracing is already active.");
91   trace_active = 1;
92   DEBUG0 ("Tracing is on");
93 }
94
95 void TRACE_desactivate (void)
96 {
97   trace_active = 0;
98   DEBUG0 ("Tracing is off");
99 }
100
101 int TRACE_is_active (void)
102 {
103   return trace_active;
104 }
105
106 int TRACE_is_enabled(void)
107 {
108   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING);
109 }
110
111 int TRACE_is_configured(void)
112 {
113   return trace_configured;
114 }
115
116 int TRACE_smpi_is_enabled(void)
117 {
118   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI);
119 }
120
121 int TRACE_smpi_is_grouped(void)
122 {
123   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_GROUP);
124 }
125
126 int TRACE_categorized (void)
127 {
128   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_CATEGORIZED);
129 }
130
131 int TRACE_uncategorized (void)
132 {
133   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_UNCATEGORIZED);
134 }
135
136 int TRACE_msg_task_is_enabled(void)
137 {
138   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_TASK);
139 }
140
141 int TRACE_msg_process_is_enabled(void)
142 {
143   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_PROCESS);
144 }
145
146 int TRACE_msg_volume_is_enabled(void)
147 {
148   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_VOLUME);
149 }
150
151 char *TRACE_get_filename(void)
152 {
153   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
154 }
155
156 char *TRACE_get_platform_method(void)
157 {
158   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_PLATFORM_METHOD);
159 }
160
161 char *TRACE_get_triva_uncat_conf (void)
162 {
163   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
164 }
165
166 char *TRACE_get_triva_cat_conf (void)
167 {
168   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
169 }
170
171 void TRACE_global_init(int *argc, char **argv)
172 {
173   /* name of the tracefile */
174   char *default_tracing_filename = xbt_strdup("simgrid.trace");
175   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
176                    "Trace file created by the instrumented SimGrid.",
177                    xbt_cfgelm_string, &default_tracing_filename, 1, 1,
178                    NULL, NULL);
179
180   /* tracing */
181   int default_tracing = 0;
182   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
183                    "Enable Tracing.",
184                    xbt_cfgelm_int, &default_tracing, 0, 1,
185                    NULL, NULL);
186
187   /* smpi */
188   int default_tracing_smpi = 0;
189   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
190                    "Tracing of the SMPI interface.",
191                    xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
192                    NULL, NULL);
193
194   /* smpi grouped */
195   int default_tracing_smpi_grouped = 0;
196   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
197                    "Group MPI processes by host.",
198                    xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
199                    NULL, NULL);
200
201
202   /* platform */
203   int default_tracing_platform = 0;
204   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
205                    "Tracing of categorized platform (host and link) utilization.",
206                    xbt_cfgelm_int, &default_tracing_platform, 0, 1,
207                    NULL, NULL);
208
209   /* tracing uncategorized resource utilization */
210   int default_tracing_uncategorized = 0;
211   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
212                    "Tracing of uncategorized resource (host and link) utilization.",
213                    xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
214                    NULL, NULL);
215
216   /* platform method */
217   char *default_tracing_platform_method = xbt_strdup("a");
218   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_PLATFORM_METHOD,
219                    "Tracing method used to register categorized resource behavior.",
220                    xbt_cfgelm_string, &default_tracing_platform_method, 1,
221                    1, NULL, NULL);
222
223   /* msg task */
224   int default_tracing_msg_task = 0;
225   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_TASK,
226                    "Tracing of MSG task behavior.",
227                    xbt_cfgelm_int, &default_tracing_msg_task, 0, 1,
228                    NULL, NULL);
229
230   /* msg process */
231   int default_tracing_msg_process = 0;
232   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
233                    "Tracing of MSG process behavior.",
234                    xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
235                    NULL, NULL);
236
237   /* msg volume (experimental) */
238   int default_tracing_msg_volume = 0;
239   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_VOLUME,
240                    "Tracing of MSG communication volume (experimental).",
241                    xbt_cfgelm_int, &default_tracing_msg_volume, 0, 1,
242                    NULL, NULL);
243
244   /* Triva graph configuration for uncategorized tracing */
245   char *default_triva_uncat_conf_file = xbt_strdup ("");
246   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
247                    "Triva Graph configuration file for uncategorized resource utilization traces.",
248                    xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
249                    NULL, NULL);
250
251   /* Triva graph configuration for uncategorized tracing */
252   char *default_triva_cat_conf_file = xbt_strdup ("");
253   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
254                    "Triva Graph configuration file for categorized resource utilization traces.",
255                    xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
256                    NULL, NULL);
257
258   /* instrumentation can be considered configured now */
259   trace_configured = 1;
260 }
261
262 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
263 {
264   char str[INSTR_DEFAULT_STR_SIZE];
265   snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
266
267   int len = strlen (str);
268   printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
269   if (!!longdesc && detailed){
270     printf ("%s\n\n", longdesc);
271   }
272 }
273
274 void TRACE_help (int detailed)
275 {
276   printf(
277       "Description of the tracing options accepted by this simulator:\n\n");
278   print_line (OPT_TRACING, "Enable the tracing system",
279       "  It activates the tracing system and register the simulation platform\n"
280       "  in the trace file. You have to enable this option to others take effect.",
281       detailed);
282   print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
283       "  It activates the categorized resource utilization tracing. It should\n"
284       "  be enabled if tracing categories are used by this simulator.",
285       detailed);
286   print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
287       "  It activates the uncategorized resource utilization tracing. Use it if\n"
288       "  this simulator do not use tracing categories and resource use have to be\n"
289       "  traced.",
290       detailed);
291   print_line (OPT_TRACING_PLATFORM_METHOD, "Change the resource utilization tracing method",
292       "  It changes the way resource utilization (categorized or not) is traced\n"
293       "  inside the simulation core. Method 'a' (default) traces all updates defined\n"
294       "  by the CPU/network model of a given resource. Depending on the interface used\n"
295       "  by this simulator (MSG, SMPI, SimDAG), the default method can generate large\n"
296       "  trace files. Method 'b' tries to make smaller tracefiles using clever updates,\n"
297       "  without losing details of resource utilization. Method 'c' generates even\n"
298       "  smaller files by doing time integration during the simulation, but it loses\n"
299       "  precision. If this last method is used, the smallest timeslice used in the\n"
300       "  tracefile analysis must be bigger than the smaller resource utilization. If\n"
301       "  unsure, do not change this option.",
302       detailed);
303   print_line (OPT_TRACING_FILENAME, "Filename to register traces",
304       "  A file with this name will be created to register the simulation. The file\n"
305       "  is in the Paje format and can be analyzed using Triva or Paje visualization\n"
306       "  tools. More information can be found in these webpages:\n"
307       "     http://triva.gforge.inria.fr/\n"
308       "     http://paje.sourceforge.net/",
309       detailed);
310   print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
311       "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
312       "  interface and generates a trace that can be analyzed using Gantt-like\n"
313       "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
314       "  state, and point-to-point communications can be analyzed with arrows.",
315       detailed);
316   print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
317       "  This option only has effect if this simulator is SMPI-based. The processes\n"
318       "  are grouped by the hosts where they were executed.",
319       detailed);
320   print_line (OPT_TRACING_MSG_TASK, "Trace task behavior (MSG)",
321       "  This option only has effect if this simulator is MSG-based. It traces the\n"
322       "  behavior of all categorized MSG tasks, grouping them by hosts.",
323       detailed);
324   print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
325       "  This option only has effect if this simulator is MSG-based. It traces the\n"
326       "  behavior of all categorized MSG processes, grouping them by hosts. This option\n"
327       "  can be used to track process location if this simulator has process migration.",
328       detailed);
329   print_line (OPT_TRACING_MSG_VOLUME, "Tracing of communication volume (MSG)",
330       "  This experimental option only has effect if this simulator is MSG-based.\n"
331       "  It traces the communication volume of MSG send/receive.",
332       detailed);
333   print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
334       "  This option can be used in all types of simulators build with SimGrid\n"
335       "  to generate a uncategorized resource utilization graph to be used as\n"
336       "  configuration for the Triva visualization analysis. This option\n"
337       "  can be used with tracing/categorized:1 and tracing:1 options to\n"
338       "  analyze an unmodified simulator before changing it to contain\n"
339       "  categories.",
340       detailed);
341   print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
342       "  This option can be used if this simulator uses tracing categories\n"
343       "  in its code. The file specified by this option holds a graph configuration\n"
344       "  file for the Triva visualization tool that can be used to analyze a categorized\n"
345       "  resource utilization.",
346       detailed);
347 }
348
349 void TRACE_generate_triva_uncat_conf (void)
350 {
351   char *output = TRACE_get_triva_uncat_conf ();
352   if (output && strlen(output) > 0){
353     xbt_dict_cursor_t cursor=NULL;
354     char *name, *value;
355
356     FILE *file = fopen (output, "w");
357     xbt_assert1 (file != NULL,
358        "Unable to open file (%s) for writing triva graph "
359        "configuration (uncategorized).", output);
360
361     //open
362     fprintf (file, "{\n");
363
364     //register NODE types
365     fprintf (file, "  node = (");
366     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
367       fprintf (file, "%s, ", name);
368     }
369
370     //register EDGE types
371     fprintf (file,
372         ");\n"
373         "  edge = (");
374     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
375       fprintf (file, "%s, ", name);
376     }
377     fprintf (file,
378         ");\n"
379         "\n");
380
381     //register each NODE type layout
382     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
383       fprintf (file, "  %s = {\n", name);
384       if (strcmp (name, "HOST") == 0){
385         fprintf (file,
386             "    type = node;\n"
387             "    size = power;\n"
388             "    host_sep = {\n"
389             "      type = separation;\n"
390             "      size = power;\n"
391             "      values = (power_used);\n"
392             "    };\n");
393       }else if (strcmp (name, "ROUTER") == 0){
394         fprintf (file,
395             "    type = node;\n"
396             "    size = 10;\n");
397       }else if (strcmp (name, "LINK") == 0){
398         fprintf (file,
399             "    type = edge;\n"
400             "    size = bandwidth;\n"
401             "    scale = global;\n"
402             "    link_sep = {\n"
403             "      type = separation;\n"
404             "      size = bandwidth;\n"
405             "      values = (bandwidth_used);\n"
406             "    };\n");
407       }
408       fprintf (file, "  };\n\n");
409     }
410
411     //EDGE configuration
412     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
413       fprintf (file, "  %s = { size = 1; };\n", name);
414     }
415
416     //graphviz configuration
417     fprintf (file, "\n");
418     fprintf (file, "  graphviz-algorithm = neato;\n");
419
420     //close
421     fprintf (file, "}\n");
422     fclose (file);
423   }
424 }
425
426 void TRACE_generate_triva_cat_conf (void)
427 {
428   char *output = TRACE_get_triva_cat_conf();
429   if (output && strlen(output) > 0){
430     xbt_dict_cursor_t cursor=NULL, cursor2=NULL;
431     char *name, *name2, *value, *value2;
432
433     //check if we do have categories declared
434     if (xbt_dict_length(created_categories) == 0){
435       INFO0("No categories declared, ignoring generation of triva graph configuration");
436       return;
437     }
438
439     FILE *file = fopen (output, "w");
440     xbt_assert1 (file != NULL,
441        "Unable to open file (%s) for writing triva graph "
442        "configuration (categorized).", output);
443
444     //open
445     fprintf (file, "{\n");
446
447     //register NODE types
448     fprintf (file, "  node = (");
449     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
450       fprintf (file, "%s, ", name);
451     }
452
453     //register EDGE types
454     fprintf (file,
455         ");\n"
456         "  edge = (");
457     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
458       fprintf (file, "%s, ", name);
459     }
460     fprintf (file,
461         ");\n"
462         "\n");
463
464     //register each NODE type layout
465     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
466       fprintf (file, "  %s = {\n", name);
467       if (strcmp (name, "HOST") == 0){
468         fprintf (file,
469             "    type = node;\n"
470             "    size = power;\n"
471             "    host_sep = {\n"
472             "      type = separation;\n"
473             "      size = power;\n"
474             "      values = (");
475         xbt_dict_foreach(created_categories,cursor2,name2,value2) {
476           fprintf (file, "%s, ", name2);
477         }
478         fprintf (file,
479             ");\n"
480             "    };\n");
481       }else if (strcmp (name, "ROUTER") == 0){
482         fprintf (file,
483             "    type = node;\n"
484             "    size = 10;\n");
485       }else if (strcmp (name, "LINK") == 0){
486         fprintf (file,
487             "    type = edge;\n"
488             "    size = bandwidth;\n"
489             "    scale = global;\n"
490             "    link_sep = {\n"
491             "      type = separation;\n"
492             "      size = bandwidth;\n"
493             "      values = (");
494         xbt_dict_foreach(created_categories,cursor2,name2,value2) {
495           fprintf (file, "%s, ", name2);
496         }
497         fprintf (file,
498             ");\n"
499             "    };\n");
500       }
501       fprintf (file, "  };\n\n");
502     }
503
504     //EDGE configuration
505     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
506       fprintf (file, "  %s = { size = 1; };\n", name);
507     }
508
509     //graphviz configuration
510     fprintf (file, "\n");
511     fprintf (file, "  graphviz-algorithm = neato;\n");
512
513     //close
514     fprintf (file, "}\n");
515     fclose (file);
516   }
517 }
518
519 #undef OPT_TRACING
520 #undef OPT_TRACING_SMPI
521 #undef OPT_TRACING_SMPI_GROUP
522 #undef OPT_TRACING_CATEGORIZED
523 #undef OPT_TRACING_UNCATEGORIZED
524 #undef OPT_TRACING_MSG_TASK
525 #undef OPT_TRACING_MSG_PROCESS
526 #undef OPT_TRACING_MSG_VOLUME
527 #undef OPT_TRACING_FILENAME
528 #undef OPT_TRACING_PLATFORM_METHOD
529 #undef OPT_TRIVA_UNCAT_CONF
530 #undef OPT_TRIVA_CAT_CONF
531
532 #endif /* HAVE_TRACING */