Logo AND Algorithmique Numérique Distribuée

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