Logo AND Algorithmique Numérique Distribuée

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