Logo AND Algorithmique Numérique Distribuée

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