Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] do not rely on preprocessor constants to control what is traced (part 1)
[simgrid.git] / src / instr / instr_interface.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 #include "simgrid_config.h"
8
9 #ifdef HAVE_TRACING
10
11 #include "instr/instr_private.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(tracing, "Tracing Interface");
14
15 static xbt_dict_t defined_types;
16 static xbt_dict_t created_categories;
17
18 int TRACE_start()
19 {
20   // tracing system must be:
21   //    - enabled (with --cfg=tracing:1)
22   //    - already configured (TRACE_global_init already called)
23   if (!(TRACE_is_enabled() && TRACE_is_configured())){
24     return 0;
25   }
26
27   /* open the trace file */
28   char *filename = TRACE_get_filename();
29   if (!filename) {
30     THROW0(tracing_error, TRACE_ERROR_START,
31            "Trace filename is not initialized.");
32     return 0;
33   }
34   FILE *file = fopen(filename, "w");
35   if (!file) {
36     THROW1(tracing_error, TRACE_ERROR_START,
37            "Tracefile %s could not be opened for writing.", filename);
38   } else {
39     TRACE_paje_start(file);
40   }
41
42   /* activate trace */
43   TRACE_activate ();
44
45   /* output header */
46   TRACE_paje_create_header();
47
48   /* define paje hierarchy for tracing */
49   pajeDefineContainerType("PLATFORM", "0", "platform");
50   pajeDefineContainerType("HOST", "PLATFORM", "HOST");
51   pajeDefineContainerType("LINK", "PLATFORM", "LINK");
52   pajeDefineVariableType("power", "HOST", "power");
53   pajeDefineVariableType("bandwidth", "LINK", "bandwidth");
54   pajeDefineVariableType("latency", "LINK", "latency");
55   pajeDefineEventType("source", "LINK", "source");
56   pajeDefineEventType("destination", "LINK", "destination");
57
58   if (TRACE_platform_is_enabled()) {
59     if (TRACE_uncategorized()){
60       pajeDefineVariableType("power_used", "HOST", "power_used");
61       pajeDefineVariableType("bandwidth_used", "LINK", "bandwidth_used");
62     }
63   }
64
65   if (TRACE_msg_process_is_enabled() || TRACE_msg_volume_is_enabled()) {
66     //processes grouped by host
67     pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
68   }
69
70   if (TRACE_msg_process_is_enabled()) {
71     pajeDefineStateType("category", "PROCESS", "category");
72     pajeDefineStateType("presence", "PROCESS", "presence");
73   }
74
75   if (TRACE_msg_volume_is_enabled()) {
76     pajeDefineLinkType("volume", "0", "PROCESS", "PROCESS", "volume");
77   }
78
79   if (TRACE_msg_task_is_enabled()) {
80     //tasks grouped by host
81     pajeDefineContainerType("TASK", "HOST", "TASK");
82     pajeDefineStateType("category", "TASK", "category");
83     pajeDefineStateType("presence", "TASK", "presence");
84   }
85
86   if (TRACE_smpi_is_enabled()) {
87     if (TRACE_smpi_is_grouped()){
88       pajeDefineContainerType("MPI_PROCESS", "HOST", "MPI_PROCESS");
89     }else{
90       pajeDefineContainerType("MPI_PROCESS", "PLATFORM", "MPI_PROCESS");
91     }
92     pajeDefineStateType("MPI_STATE", "MPI_PROCESS", "MPI_STATE");
93     pajeDefineLinkType("MPI_LINK", "0", "MPI_PROCESS", "MPI_PROCESS",
94                        "MPI_LINK");
95   }
96
97   /* creating the platform */
98   pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0",
99                       "simgrid-platform");
100
101   /* other trace initialization */
102   defined_types = xbt_dict_new();
103   created_categories = xbt_dict_new();
104   TRACE_msg_task_alloc();
105   TRACE_category_alloc();
106   TRACE_surf_alloc();
107   TRACE_msg_process_alloc();
108   TRACE_smpi_alloc();
109   return 0;
110 }
111
112 int TRACE_end()
113 {
114   if (!TRACE_is_active())
115     return 1;
116   FILE *file = TRACE_paje_end();
117   fclose(file);
118
119   TRACE_desactivate ();
120   return 0;
121 }
122
123 int TRACE_category(const char *category)
124 {
125   return TRACE_category_with_color (category, NULL);
126 }
127
128 int TRACE_category_with_color (const char *category, const char *color)
129 {
130   static int first_time = 1;
131   if (!TRACE_is_active())
132     return 1;
133
134   if (first_time) {
135     TRACE_define_type("user_type", "0", 1);
136     first_time = 0;
137   }
138   return TRACE_create_category_with_color(category, "user_type", "0", color);
139 }
140
141 void TRACE_define_type(const char *type,
142                        const char *parent_type, int final)
143 {
144   char *val_one = NULL;
145   if (!TRACE_is_active())
146     return;
147
148   //check if type is already defined
149   if (xbt_dict_get_or_null(defined_types, type)) {
150     THROW1(tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED,
151            "Type %s is already defined", type);
152   }
153   //check if parent_type is already defined
154   if (strcmp(parent_type, "0")
155       && !xbt_dict_get_or_null(defined_types, parent_type)) {
156     THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
157            "Type (used as parent) %s is not defined", parent_type);
158   }
159
160   pajeDefineContainerType(type, parent_type, type);
161   if (final) {
162     //for m_process_t
163     if (TRACE_msg_process_is_enabled())
164       pajeDefineContainerType("process", type, "process");
165     if (TRACE_msg_process_is_enabled())
166       pajeDefineStateType("process-state", "process", "process-state");
167
168     if (TRACE_msg_task_is_enabled())
169       pajeDefineContainerType("task", type, "task");
170     if (TRACE_msg_task_is_enabled())
171       pajeDefineStateType("task-state", "task", "task-state");
172   }
173   val_one = xbt_strdup("1");
174   xbt_dict_set(defined_types, type, &val_one, xbt_free);
175 }
176
177 int TRACE_create_category(const char *category,
178                           const char *type, const char *parent_category)
179 {
180   return TRACE_create_category_with_color (category, type, parent_category, NULL);
181 }
182
183 int TRACE_create_category_with_color(const char *category,
184                           const char *type,
185                           const char *parent_category,
186                           const char *color)
187 {
188   char state[100];
189   char *val_one = NULL;
190   if (!TRACE_is_active())
191     return 1;
192
193   //check if type is defined
194   if (!xbt_dict_get_or_null(defined_types, type)) {
195     THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
196            "Type %s is not defined", type);
197     return 1;
198   }
199   //check if parent_category exists
200   if (strcmp(parent_category, "0")
201       && !xbt_dict_get_or_null(created_categories, parent_category)) {
202     THROW1(tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED,
203            "Category (used as parent) %s is not created", parent_category);
204     return 1;
205   }
206   //check if category is created
207   if (xbt_dict_get_or_null(created_categories, category)) {
208     return 1;
209   }
210
211   pajeCreateContainer(MSG_get_clock(), category, type, parent_category,
212                       category);
213
214   char final_color[INSTR_DEFAULT_STR_SIZE];
215   if (!color){
216     //generate a random color
217     double red = drand48();
218     double green = drand48();
219     double blue = drand48();
220     snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%f %f %f", red, green, blue);
221   }else{
222     snprintf (final_color, INSTR_DEFAULT_STR_SIZE, "%s", color);
223   }
224
225   /* for registering application categories on top of platform */
226   snprintf(state, 100, "b%s", category);
227   if (TRACE_platform_is_enabled())
228     pajeDefineVariableTypeWithColor(state, "LINK", state, final_color);
229   snprintf(state, 100, "p%s", category);
230   if (TRACE_platform_is_enabled())
231     pajeDefineVariableTypeWithColor(state, "HOST", state, final_color);
232
233   val_one = xbt_strdup("1");
234   xbt_dict_set(created_categories, category, &val_one, xbt_free);
235   return 0;
236 }
237
238 void TRACE_declare_mark(const char *mark_type)
239 {
240   if (!TRACE_is_active())
241     return;
242   if (!mark_type)
243     return;
244
245   pajeDefineEventType(mark_type, "0", mark_type);
246 }
247
248 void TRACE_mark(const char *mark_type, const char *mark_value)
249 {
250   if (!TRACE_is_active())
251     return;
252   if (!mark_type || !mark_value)
253     return;
254
255   pajeNewEvent(MSG_get_clock(), mark_type, "0", mark_value);
256 }
257
258 #endif /* HAVE_TRACING */