Logo AND Algorithmique Numérique Distribuée

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