Logo AND Algorithmique Numérique Distribuée

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