Logo AND Algorithmique Numérique Distribuée

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