Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bf898aabad7d7024a2836bbc16086da53d16cadb
[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     pajeDefineVariableType("power_used", "HOST", "power_used");
55     pajeDefineVariableType("bandwidth", "LINK", "bandwidth");
56     pajeDefineVariableType("bandwidth_used", "LINK", "bandwidth_used");
57     pajeDefineVariableType("latency", "LINK", "latency");
58     pajeDefineEventType("source", "LINK", "source");
59     pajeDefineEventType("destination", "LINK", "destination");
60   }
61
62   if (IS_TRACING_PROCESSES || IS_TRACING_VOLUME) {
63     //processes grouped by host
64     pajeDefineContainerType("PROCESS", "HOST", "PROCESS");
65   }
66
67   if (IS_TRACING_PROCESSES) {
68     pajeDefineStateType("category", "PROCESS", "category");
69     pajeDefineStateType("presence", "PROCESS", "presence");
70   }
71
72   if (IS_TRACING_VOLUME) {
73     pajeDefineLinkType("volume", "0", "PROCESS", "PROCESS", "volume");
74   }
75
76   if (IS_TRACING_TASKS) {
77     //tasks grouped by host
78     pajeDefineContainerType("TASK", "HOST", "TASK");
79     pajeDefineStateType("category", "TASK", "category");
80     pajeDefineStateType("presence", "TASK", "presence");
81   }
82
83   if (IS_TRACING_SMPI) {
84     if (TRACE_smpi_is_grouped()){
85       pajeDefineContainerType("MPI_PROCESS", "HOST", "MPI_PROCESS");
86     }else{
87       pajeDefineContainerType("MPI_PROCESS", "PLATFORM", "MPI_PROCESS");
88     }
89     pajeDefineStateType("MPI_STATE", "MPI_PROCESS", "MPI_STATE");
90     pajeDefineLinkType("MPI_LINK", "0", "MPI_PROCESS", "MPI_PROCESS",
91                        "MPI_LINK");
92   }
93
94   /* creating the platform */
95   pajeCreateContainer(MSG_get_clock(), "platform", "PLATFORM", "0",
96                       "simgrid-platform");
97
98   /* other trace initialization */
99   defined_types = xbt_dict_new();
100   created_categories = xbt_dict_new();
101   TRACE_msg_task_alloc();
102   TRACE_category_alloc();
103   TRACE_surf_alloc();
104   TRACE_msg_process_alloc();
105   TRACE_smpi_alloc();
106
107   return 0;
108 }
109
110 int TRACE_end()
111 {
112   FILE *file = NULL;
113   if (!IS_TRACING)
114     return 1;
115   file = TRACE_paje_end();
116   fclose(file);
117   return 0;
118 }
119
120 int TRACE_category(const char *category)
121 {
122   static int first_time = 1;
123   if (!IS_TRACING)
124     return 1;
125
126   if (first_time) {
127     TRACE_define_type("user_type", "0", 1);
128     first_time = 0;
129   }
130   return TRACE_create_category(category, "user_type", "0");
131 }
132
133 void TRACE_define_type(const char *type,
134                        const char *parent_type, int final)
135 {
136   char *val_one = NULL;
137   if (!IS_TRACING)
138     return;
139
140   //check if type is already defined
141   if (xbt_dict_get_or_null(defined_types, type)) {
142     THROW1(tracing_error, TRACE_ERROR_TYPE_ALREADY_DEFINED,
143            "Type %s is already defined", type);
144   }
145   //check if parent_type is already defined
146   if (strcmp(parent_type, "0")
147       && !xbt_dict_get_or_null(defined_types, parent_type)) {
148     THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
149            "Type (used as parent) %s is not defined", parent_type);
150   }
151
152   pajeDefineContainerType(type, parent_type, type);
153   if (final) {
154     //for m_process_t
155     if (IS_TRACING_PROCESSES)
156       pajeDefineContainerType("process", type, "process");
157     if (IS_TRACING_PROCESSES)
158       pajeDefineStateType("process-state", "process", "process-state");
159
160     if (IS_TRACING_TASKS)
161       pajeDefineContainerType("task", type, "task");
162     if (IS_TRACING_TASKS)
163       pajeDefineStateType("task-state", "task", "task-state");
164   }
165   val_one = xbt_strdup("1");
166   xbt_dict_set(defined_types, type, &val_one, xbt_free);
167 }
168
169 int TRACE_create_category(const char *category,
170                           const char *type, const char *parent_category)
171 {
172   char state[100];
173   char *val_one = NULL;
174   if (!IS_TRACING)
175     return 1;
176
177   //check if type is defined
178   if (!xbt_dict_get_or_null(defined_types, type)) {
179     THROW1(tracing_error, TRACE_ERROR_TYPE_NOT_DEFINED,
180            "Type %s is not defined", type);
181     return 1;
182   }
183   //check if parent_category exists
184   if (strcmp(parent_category, "0")
185       && !xbt_dict_get_or_null(created_categories, parent_category)) {
186     THROW1(tracing_error, TRACE_ERROR_CATEGORY_NOT_DEFINED,
187            "Category (used as parent) %s is not created", parent_category);
188     return 1;
189   }
190   //check if category is created
191   if (xbt_dict_get_or_null(created_categories, category)) {
192     return 1;
193   }
194
195   pajeCreateContainer(MSG_get_clock(), category, type, parent_category,
196                       category);
197
198   /* for registering application categories on top of platform */
199
200   snprintf(state, 100, "b%s", category);
201   if (IS_TRACING_PLATFORM)
202     pajeDefineVariableType(state, "LINK", state);
203   snprintf(state, 100, "p%s", category);
204   if (IS_TRACING_PLATFORM)
205     pajeDefineVariableType(state, "HOST", state);
206
207   val_one = xbt_strdup("1");
208   xbt_dict_set(created_categories, category, &val_one, xbt_free);
209   return 0;
210 }
211
212 void TRACE_declare_mark(const char *mark_type)
213 {
214   if (!IS_TRACING)
215     return;
216   if (!mark_type)
217     return;
218
219   pajeDefineEventType(mark_type, "0", mark_type);
220 }
221
222 void TRACE_mark(const char *mark_type, const char *mark_value)
223 {
224   if (!IS_TRACING)
225     return;
226   if (!mark_type || !mark_value)
227     return;
228
229   pajeNewEvent(MSG_get_clock(), mark_type, "0", mark_value);
230 }
231
232 #endif                          /* HAVE_TRACING */