Logo AND Algorithmique Numérique Distribuée

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