Logo AND Algorithmique Numérique Distribuée

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