Logo AND Algorithmique Numérique Distribuée

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