Logo AND Algorithmique Numérique Distribuée

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