Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8cec10544a951ce98d90b5cf7b2822b093c93a6a
[simgrid.git] / src / instr / instr_paje_trace.cpp
1 /* Copyright (c) 2010-2016. 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 "src/instr/instr_private.h"
8 #include "xbt/virtu.h" /* sg_cmdline */
9 #include <sstream>
10 #include <iomanip> /** std::setprecision **/
11 #include "simgrid/sg_config.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_trace, instr_trace, "tracing event system");
14
15 extern FILE * tracing_file;
16 extern s_instr_trace_writer_t active_writer;
17
18 static std::stringstream stream;
19
20 static void print_paje_debug(std::string functionName, PajeEvent* event) {
21   XBT_DEBUG("%s: event_type=%d, timestamp=%.*f", __FUNCTION__, (int)event->event_type, TRACE_precision(),
22             event->timestamp);
23 }
24
25 static void init_stream(PajeEvent* event) {
26   stream << std::fixed << std::setprecision(TRACE_precision());
27   stream << (int) event->event_type;
28 }
29
30 static void print_row() {
31   stream << std::endl;
32   fprintf(tracing_file, "%s", stream.str().c_str());
33   stream.str("");
34   stream.clear();
35 }
36
37 static void print_timestamp(PajeEvent* event) {
38   stream << " ";
39   /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
40   if (event->timestamp < 1e-12)
41     stream << 0;
42   else 
43     stream << event->timestamp;
44 }  
45
46 void TRACE_paje_start() {
47   char *filename = TRACE_get_filename();
48   tracing_file = fopen(filename, "w");
49   if (tracing_file == nullptr){
50     THROWF (system_error, 1, "Tracefile %s could not be opened for writing.", filename);
51   }
52
53   XBT_DEBUG("Filename %s is open for writing", filename);
54
55   /* output generator version */
56   fprintf (tracing_file, "#This file was generated using SimGrid-%d.%d.%d\n",
57            SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR, SIMGRID_VERSION_PATCH);
58   fprintf (tracing_file, "#[");
59   unsigned int cpt;
60   char *str;
61   xbt_dynar_foreach (xbt_cmdline, cpt, str){
62     fprintf(tracing_file, "%s ",str);
63   }
64   fprintf (tracing_file, "]\n");
65
66   /* output one line comment */
67   dump_comment (TRACE_get_comment());
68
69   /* output comment file */
70   dump_comment_file (TRACE_get_comment_file());
71
72   /* output header */
73   TRACE_header(TRACE_basic(),TRACE_display_sizes());
74 }
75
76 void TRACE_paje_end() {
77   fclose(tracing_file);
78   char *filename = TRACE_get_filename();
79   XBT_DEBUG("Filename %s is closed", filename);
80 }
81
82 void DefineContainerEvent::print() {
83   print_paje_debug(__FUNCTION__, this);
84   init_stream(this);
85   stream << " " << type->id
86          << " " << type->father->id
87          << " " << type->name;
88   print_row();
89 }
90
91 void DefineVariableTypeEvent::print() {
92   print_paje_debug(__FUNCTION__, this);
93  init_stream(this);
94   stream << " " << type->id
95          << " " << type->father->id
96          << " " << type->name;
97   if (type->color)
98     stream << " \"" << type->color << "\"";
99   print_row();
100 }
101
102 void DefineStateTypeEvent::print() {
103   print_paje_debug(__FUNCTION__, this);
104   init_stream(this);
105   stream << " " << type->id
106          << " " << type->father->id
107          << " " << type->name;
108   print_row();
109 }
110
111 void DefineEventTypeEvent::print() {
112   print_paje_debug(__FUNCTION__, this);
113   init_stream(this);
114   stream << " " << type->id
115          << " " << type->father->id
116          << " " << type->name;
117   print_row();
118 }
119
120 void DefineLinkTypeEvent::print() {
121   print_paje_debug(__FUNCTION__, this);
122   init_stream (this);
123   stream << " " << type->id 
124          << " " << type->father->id 
125          << " " << source->id 
126          << " " << dest->id 
127          << " " << type->name;
128   print_row();
129 }
130
131 void DefineEntityValueEvent::print() {
132   print_paje_debug(__FUNCTION__, this);
133   init_stream(this);
134   stream << " "   << value->id
135          << " "   << value->father->id
136          << " "   << value->name;
137   if(value->color)
138     stream << " \"" << value->color << "\"";
139   print_row();
140 }
141
142 void CreateContainerEvent::print() {
143   print_paje_debug(__FUNCTION__, this);
144   init_stream(this);
145   print_timestamp(this);
146   stream << " "   << container->id
147          << " "   << container->type->id
148          << " "   << container->father->id
149          << " \"" << container->name << "\"";
150
151   print_row();
152 }
153
154 void DestroyContainerEvent::print() {
155   print_paje_debug(__FUNCTION__, this);
156   init_stream(this);
157   print_timestamp(this);
158   stream << " "   << container->type->id
159          << " "   << container->id;
160
161   print_row();
162 }
163
164 void SetVariableEvent::print() {
165   print_paje_debug(__FUNCTION__, this);
166   init_stream(this);
167   print_timestamp(this);
168   stream << " " << type->id
169          << " " << container->id
170          << " " << value;
171   print_row();
172 }
173
174 void AddVariableEvent::print() {
175   print_paje_debug(__FUNCTION__, this);
176   init_stream(this);
177   print_timestamp(this);
178   stream << " " << type->id
179          << " " << container->id
180          << " " << value;
181   print_row();
182 }
183
184 void SubVariableEvent::print() {
185   print_paje_debug(__FUNCTION__, this);
186   init_stream(this);
187   print_timestamp(this);
188   stream << " " << type->id
189          << " " << container->id
190          << " " << value;
191   print_row();
192 }
193
194 void SetStateEvent::print() {
195   print_paje_debug(__FUNCTION__, this);
196   init_stream(this);
197   print_timestamp(this);
198   stream << " " << type->id
199          << " " << container->id;
200   stream << " " <<value->id;
201 #if HAVE_SMPI
202   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
203     stream << " \"" << filename
204            << "\" " << linenumber;
205   }
206 #endif
207   print_row();
208 }
209
210 void PushStateEvent::print() {
211   print_paje_debug(__FUNCTION__, this);
212     init_stream(this);
213   print_timestamp(this);
214   stream << " " << type->id
215          << " " << container->id;
216   stream << " " <<value->id;
217
218   if (TRACE_display_sizes()) {
219     stream << " ";
220     if (extra != nullptr) {
221       stream << static_cast<instr_extra_data>(extra)->send_size;
222     }
223     else {
224       stream << 0;
225     }
226   }
227 #if HAVE_SMPI
228   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
229     stream << " \"" << filename
230            << "\" " << linenumber;
231   }
232 #endif
233   print_row();
234
235   if (extra != nullptr) {
236     if (static_cast<instr_extra_data>(extra)->sendcounts != nullptr)
237       xbt_free(static_cast<instr_extra_data>(extra)->sendcounts);
238     if (static_cast<instr_extra_data>(extra)->recvcounts != nullptr)
239       xbt_free(static_cast<instr_extra_data>(extra)->recvcounts);
240     xbt_free(extra);
241   }
242 }
243
244 void PopStateEvent::print() {
245   print_paje_debug(__FUNCTION__, this);
246     init_stream(this);
247   print_timestamp(this);
248   stream << " " << type->id
249          << " " << container->id;
250   print_row();
251 }
252
253 void ResetStateEvent::print() {
254   print_paje_debug(__FUNCTION__, this);
255     init_stream(this);
256   print_timestamp(this);
257   stream << " " << type->id
258          << " " << container->id;
259   print_row();
260 }
261
262 void StartLinkEvent::print() {
263   print_paje_debug(__FUNCTION__, this);
264     init_stream(this);
265   print_timestamp(this);
266   stream << " " <<type->id
267          << " " <<container->id
268          << " " <<value;
269   stream << " " << sourceContainer->id
270          << " " << key;
271
272   if (TRACE_display_sizes()) {
273     stream << " " << size;
274   }
275   print_row();
276 }
277
278 void EndLinkEvent::print() {
279   print_paje_debug(__FUNCTION__, this);
280     init_stream(this);
281   print_timestamp(this);
282   stream << " " <<type->id
283          << " " <<container->id
284          << " " <<value;
285   stream << " " << destContainer->id
286          << " " << key;
287   print_row();
288 }
289
290 void NewEvent::print () {
291   print_paje_debug(__FUNCTION__, this);
292   init_stream (this);
293   print_timestamp(this);
294   stream << " " << type->id
295          << " " << container->id
296          << " " << value->id;
297   print_row();
298 }