Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] encapsulating the open/close of the trace file
[simgrid.git] / src / instr / instr_paje.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 "instr/instr_private.h"
8
9 #ifdef HAVE_TRACING
10
11 static FILE *tracing_file = NULL;
12
13 static int pajeDefineContainerTypeId = 0;
14 static int pajeDefineStateTypeId = 1;
15 static int pajeDefineEntityValueId = 2;
16 static int pajeDefineEventTypeId = 3;
17 static int pajeDefineLinkTypeId = 4;
18 static int pajeCreateContainerId = 5;
19 static int pajeSetStateId = 6;
20 #define UNUSED007 7
21 static int pajePopStateId = 8;
22 static int pajeDestroyContainerId = 9;
23 #define UNUSED006 10
24 #define UNUSED003 11
25 static int pajeStartLinkId = 12;
26 static int pajeEndLinkId = 13;
27 #define UNUSED000 14
28 #define UNUSED004 15
29 #define UNUSED008 16
30 #define UNUSED009 17
31 #define UNUSED005 18
32 static int pajePushStateId = 19;
33 #define UNUSED001 20
34 static int pajeDefineVariableTypeWithColorId = 21;
35 static int pajeSetVariableId = 22;
36 static int pajeAddVariableId = 23;
37 static int pajeSubVariableId = 24;
38 static int pajeDefineVariableTypeId = 25;
39 static int pajeStartLinkWithVolumeId = 26;
40 static int pajeNewEventId = 27;
41
42 #define TRACE_LINE_SIZE 1000
43
44 void TRACE_paje_start(void)
45 {
46   char *filename = TRACE_get_filename();
47   tracing_file = fopen(filename, "w");
48   if (!tracing_file) {
49     THROW1(tracing_error, TRACE_ERROR_FILE_OPEN,
50            "Tracefile %s could not be opened for writing.", filename);
51   }
52 }
53
54 void TRACE_paje_end(void)
55 {
56   fclose(tracing_file);
57 }
58
59 void TRACE_paje_create_header(void)
60 {
61   if (!TRACE_is_active())
62     return;
63   fprintf(tracing_file, "\
64 %%EventDef PajeDefineContainerType %d \n\
65 %%       Alias string \n\
66 %%       ContainerType string \n\
67 %%       Name string \n\
68 %%EndEventDef \n\
69 %%EventDef PajeDefineStateType %d \n\
70 %%       Alias string \n\
71 %%       ContainerType string \n\
72 %%       Name string \n\
73 %%EndEventDef \n\
74 %%EventDef PajeDefineEntityValue %d \n\
75 %%       Alias string \n\
76 %%       EntityType string \n\
77 %%       Name string \n\
78 %%EndEventDef \n\
79 %%EventDef PajeDefineEventType %d \n\
80 %%       Alias string \n\
81 %%       EntityType string \n\
82 %%       Name string \n\
83 %%EndEventDef \n\
84 %%EventDef PajeDefineLinkType %d \n\
85 %%       Alias string \n\
86 %%       ContainerType string \n\
87 %%       SourceContainerType string \n\
88 %%       DestContainerType string \n\
89 %%       Name string \n\
90 %%EndEventDef \n\
91 %%EventDef PajeCreateContainer %d \n\
92 %%       Time date \n\
93 %%       Alias string \n\
94 %%       Type string \n\
95 %%       Container string \n\
96 %%       Name string \n\
97 %%EndEventDef \n\
98 %%EventDef PajeDestroyContainer %d \n\
99 %%       Time date \n\
100 %%       Type string \n\
101 %%       Container string \n\
102 %%EndEventDef \n\
103 %%EventDef PajeSetState %d \n\
104 %%       Time date \n\
105 %%       EntityType string \n\
106 %%       Container string \n\
107 %%       Value string \n\
108 %%EndEventDef\n\
109 %%EventDef PajePopState %d \n\
110 %%       Time date \n\
111 %%       EntityType string \n\
112 %%       Container string \n\
113 %%EndEventDef\n\
114 %%EventDef PajeStartLink %d \n\
115 %%       Time date \n\
116 %%       EntityType string \n\
117 %%       Container string \n\
118 %%       Value string \n\
119 %%       SourceContainer string \n\
120 %%       Key string \n\
121 %%EndEventDef\n\
122 %%EventDef PajeEndLink %d \n\
123 %%       Time date \n\
124 %%       EntityType string \n\
125 %%       Container string \n\
126 %%       Value string \n\
127 %%       DestContainer string \n\
128 %%       Key string \n\
129 %%EndEventDef\n\
130 %%EventDef PajePushState %d \n\
131 %%       Time date \n\
132 %%       EntityType string \n\
133 %%       Container string \n\
134 %%       Value string \n\
135 %%EndEventDef\n\
136 %%EventDef PajeSetVariable %d \n\
137 %%       Time date \n\
138 %%       EntityType string \n\
139 %%       Container string \n\
140 %%       Value string \n\
141 %%EndEventDef\n\
142 %%EventDef PajeAddVariable %d \n\
143 %%       Time date \n\
144 %%       EntityType string \n\
145 %%       Container string \n\
146 %%       Value string \n\
147 %%EndEventDef\n\
148 %%EventDef PajeSubVariable %d \n\
149 %%       Time date \n\
150 %%       EntityType string \n\
151 %%       Container string \n\
152 %%       Value string \n\
153 %%EndEventDef\n\
154 %%EventDef PajeDefineVariableType %d \n\
155 %%       Alias string \n\
156 %%       ContainerType string \n\
157 %%       Name string \n\
158 %%EndEventDef \n\
159 %%EventDef PajeDefineVariableType %d \n\
160 %%       Alias string \n\
161 %%       ContainerType string \n\
162 %%       Name string \n\
163 %%       Color color \n\
164 %%EndEventDef \n\
165 %%EventDef PajeStartLink %d \n\
166 %%       Time date \n\
167 %%       EntityType string \n\
168 %%       Container string \n\
169 %%       Value string \n\
170 %%       SourceContainer string \n\
171 %%       Key string \n\
172 %%       Volume string \n\
173 %%EndEventDef\n\
174 %%EventDef PajeNewEvent %d \n\
175 %%       Time date \n\
176 %%       EntityType string \n\
177 %%       Container string \n\
178 %%       Value string \n\
179 %%EndEventDef\n", pajeDefineContainerTypeId, pajeDefineStateTypeId, pajeDefineEntityValueId, pajeDefineEventTypeId, pajeDefineLinkTypeId, pajeCreateContainerId, pajeDestroyContainerId, pajeSetStateId, pajePopStateId, pajeStartLinkId, pajeEndLinkId, pajePushStateId, pajeSetVariableId, pajeAddVariableId, pajeSubVariableId, pajeDefineVariableTypeId, pajeDefineVariableTypeWithColorId, pajeStartLinkWithVolumeId, pajeNewEventId);
180 }
181
182 /* internal to this file */
183 static void __pajeCreateContainer(char *output, int len, int eventid,
184                                   double time, const char *alias,
185                                   const char *type, const char *container,
186                                   const char *name)
187 {
188   snprintf(output, len, "%d %lf %s %s %s %s", eventid, time,
189            alias, type, container, name);
190 }
191
192 static void __pajeSetState(char *output, int len, int eventid, double time,
193                            const char *entityType, const char *container,
194                            const char *value)
195 {
196   snprintf(output, len, "%d %lf %s %s %s", eventid, time, entityType,
197            container, value);
198 }
199
200 static void __pajeSetVariable(char *output, int len, int eventid,
201                               double time, const char *entityType,
202                               const char *container, const char *value)
203 {
204   snprintf(output, len, "%d %lf %s %s %s", eventid, time, entityType,
205            container, value);
206 }
207
208 static void __pajeStartLink(char *output, int len, int eventid,
209                             double time, const char *entityType,
210                             const char *container, const char *value,
211                             const char *sourceContainer, const char *key)
212 {
213   snprintf(output, len, "%d %lf %s %s %s %s %s", eventid, time, entityType,
214            container, value, sourceContainer, key);
215 }
216
217 /* internal do the instrumentation module */
218 void pajeDefineContainerType(const char *alias, const char *containerType,
219                              const char *name)
220 {
221   fprintf(tracing_file, "%d %s %s %s\n", pajeDefineContainerTypeId, alias,
222           containerType, name);
223 }
224
225 void pajeDefineStateType(const char *alias, const char *containerType,
226                          const char *name)
227 {
228   fprintf(tracing_file, "%d %s %s %s\n", pajeDefineStateTypeId, alias,
229           containerType, name);
230 }
231
232 void pajeDefineEventType(const char *alias, const char *containerType,
233                          const char *name)
234 {
235   fprintf(tracing_file, "%d %s %s %s\n", pajeDefineEventTypeId, alias,
236           containerType, name);
237 }
238
239 void pajeDefineLinkType(const char *alias, const char *containerType,
240                         const char *sourceContainerType,
241                         const char *destContainerType, const char *name)
242 {
243   fprintf(tracing_file, "%d %s %s %s %s %s\n", pajeDefineLinkTypeId, alias,
244           containerType, sourceContainerType, destContainerType, name);
245 }
246
247 void pajeCreateContainer(double time, const char *alias, const char *type,
248                          const char *container, const char *name)
249 {
250   char line[TRACE_LINE_SIZE];
251   __pajeCreateContainer(line, TRACE_LINE_SIZE, pajeCreateContainerId, time,
252                         alias, type, container, name);
253   fprintf(tracing_file, "%s\n", line);
254 }
255
256 void pajeDestroyContainer(double time, const char *type,
257                           const char *container)
258 {
259   fprintf(tracing_file, "%d %lf %s %s\n", pajeDestroyContainerId, time,
260           type, container);
261 }
262
263 void pajeSetState(double time, const char *entityType,
264                   const char *container, const char *value)
265 {
266   char line[TRACE_LINE_SIZE];
267   __pajeSetState(line, TRACE_LINE_SIZE, pajeSetStateId, time, entityType,
268                  container, value);
269   fprintf(tracing_file, "%s\n", line);
270 }
271
272 void pajePushState(double time, const char *entityType,
273                    const char *container, const char *value)
274 {
275   char line[TRACE_LINE_SIZE];
276   __pajeSetState(line, TRACE_LINE_SIZE, pajePushStateId, time, entityType,
277                  container, value);
278   fprintf(tracing_file, "%s\n", line);
279 }
280
281 void pajePopState(double time, const char *entityType,
282                   const char *container)
283 {
284   fprintf(tracing_file, "%d %lf %s %s\n", pajePopStateId, time, entityType,
285           container);
286 }
287
288 void pajeStartLink(double time, const char *entityType,
289                    const char *container, const char *value,
290                    const char *sourceContainer, const char *key)
291 {
292   char line[TRACE_LINE_SIZE];
293   __pajeStartLink(line, TRACE_LINE_SIZE, pajeStartLinkId, time, entityType,
294                   container, value, sourceContainer, key);
295   fprintf(tracing_file, "%s\n", line);
296 }
297
298 void pajeStartLinkWithVolume(double time, const char *entityType,
299                              const char *container, const char *value,
300                              const char *sourceContainer, const char *key,
301                              double volume)
302 {
303   char line[TRACE_LINE_SIZE];
304   __pajeStartLink(line, TRACE_LINE_SIZE, pajeStartLinkWithVolumeId, time,
305                   entityType, container, value, sourceContainer, key);
306   fprintf(tracing_file, "%s %f\n", line, volume);
307 }
308
309 void pajeEndLink(double time, const char *entityType,
310                  const char *container, const char *value,
311                  const char *destContainer, const char *key)
312 {
313   fprintf(tracing_file, "%d %lf %s %s %s %s %s\n", pajeEndLinkId, time,
314           entityType, container, value, destContainer, key);
315 }
316
317 void pajeDefineVariableType(const char *alias, const char *containerType,
318                             const char *name)
319 {
320   fprintf(tracing_file, "%d %s %s %s\n", pajeDefineVariableTypeId, alias,
321           containerType, name);
322 }
323
324 void pajeDefineVariableTypeWithColor(const char *alias, const char *containerType,
325                             const char *name, const char *color)
326 {
327   fprintf(tracing_file, "%d %s %s %s \"%s\"\n", pajeDefineVariableTypeWithColorId, alias,
328           containerType, name, color);
329 }
330
331 void pajeSetVariable(double time, const char *entityType,
332                      const char *container, const char *value)
333 {
334   char line[TRACE_LINE_SIZE];
335   __pajeSetVariable(line, TRACE_LINE_SIZE, pajeSetVariableId, time,
336                     entityType, container, value);
337   fprintf(tracing_file, "%s\n", line);
338 }
339
340 void pajeAddVariable(double time, const char *entityType,
341                      const char *container, const char *value)
342 {
343   char line[TRACE_LINE_SIZE];
344   __pajeSetVariable(line, TRACE_LINE_SIZE, pajeAddVariableId, time,
345                     entityType, container, value);
346   fprintf(tracing_file, "%s\n", line);
347 }
348
349 void pajeSubVariable(double time, const char *entityType,
350                      const char *container, const char *value)
351 {
352   char line[TRACE_LINE_SIZE];
353   __pajeSetVariable(line, TRACE_LINE_SIZE, pajeSubVariableId, time,
354                     entityType, container, value);
355   fprintf(tracing_file, "%s\n", line);
356 }
357
358 void pajeNewEvent(double time, const char *entityType,
359                   const char *container, const char *value)
360 {
361   fprintf(tracing_file, "%d %lf %s %s %s\n", pajeNewEventId, time,
362           entityType, container, value);
363 }
364
365 #endif /* HAVE_TRACING */