Logo AND Algorithmique Numérique Distribuée

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