Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix invalid read
[simgrid.git] / src / instr / instr_paje_events.cpp
1 /* Copyright (c) 2012-2017. 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.hpp"
8 #include "src/instr/instr_smpi.hpp"
9 #include "src/smpi/include/private.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_paje_events, instr, "Paje tracing event system (events)");
12 extern FILE* tracing_file;
13 std::map<container_t, FILE*> tracing_files; // TI specific
14
15 namespace simgrid {
16 namespace instr {
17
18 NewEvent::NewEvent(double timestamp, container_t container, Type* type, EntityValue* val)
19     : simgrid::instr::PajeEvent::PajeEvent(container, type, timestamp, PAJE_NewEvent), val(val)
20 {
21   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
22   insertIntoBuffer();
23 }
24
25 LinkEvent::LinkEvent(double timestamp, container_t container, Type* type, e_event_type event_type, container_t endpoint,
26                      std::string value, std::string key)
27     : LinkEvent(timestamp, container, type, event_type, endpoint, value, key, -1)
28 {
29 }
30
31 LinkEvent::LinkEvent(double timestamp, container_t container, Type* type, e_event_type event_type, container_t endpoint,
32                      std::string value, std::string key, int size)
33     : PajeEvent(container, type, timestamp, event_type), endpoint_(endpoint), value_(value), key_(key), size_(size)
34 {
35   XBT_DEBUG("%s: event_type=%u, timestamp=%f, value:%s", __FUNCTION__, eventType_, timestamp_, value_.c_str());
36   insertIntoBuffer();
37 }
38
39 VariableEvent::VariableEvent(double timestamp, Container* container, Type* type, e_event_type event_type, double value)
40     : PajeEvent::PajeEvent(container, type, timestamp, event_type), value(value)
41 {
42   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, this->timestamp_);
43   insertIntoBuffer();
44 }
45
46 StateEvent::StateEvent(double timestamp, Container* container, Type* type, e_event_type event_type, EntityValue* value)
47     : StateEvent(timestamp, container, type, event_type, value, nullptr)
48 {
49 }
50
51 StateEvent::StateEvent(double timestamp, Container* container, Type* type, e_event_type event_type, EntityValue* value,
52                        void* extra)
53     : PajeEvent::PajeEvent(container, type, timestamp, event_type), value(value), extra_(extra)
54 {
55 #if HAVE_SMPI
56   if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
57     smpi_trace_call_location_t* loc = smpi_trace_get_call_location();
58     filename                        = loc->filename;
59     linenumber                      = loc->linenumber;
60   }
61 #endif
62
63   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __FUNCTION__, eventType_, timestamp_);
64   insertIntoBuffer();
65 };
66
67 void NewEvent::print()
68 {
69   std::stringstream stream;
70   stream << std::fixed << std::setprecision(TRACE_precision());
71   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
72   if (instr_fmt_type != instr_fmt_paje)
73     return;
74
75   if (timestamp_ < 1e-12)
76     stream << eventType_ << " " << 0 << " " << type->getId() << " " << container->getId();
77   else
78     stream << eventType_ << " " << timestamp_ << " " << type->getId() << " " << container->getId();
79   stream << " " << val->getId();
80   XBT_DEBUG("Dump %s", stream.str().c_str());
81   fprintf(tracing_file, "%s\n", stream.str().c_str());
82 }
83
84 void LinkEvent::print()
85 {
86   std::stringstream stream;
87   stream << std::fixed << std::setprecision(TRACE_precision());
88   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
89   if (instr_fmt_type != instr_fmt_paje)
90     return;
91   if (timestamp_ < 1e-12)
92     stream << eventType_ << " " << 0 << " " << type->getId() << " " << container->getId() << " " << value_;
93   else
94     stream << eventType_ << " " << timestamp_ << " " << type->getId() << " " << container->getId() << " " << value_;
95
96   stream << " " << endpoint_->getId() << " " << key_;
97
98   if (TRACE_display_sizes()) {
99     stream << " " << size_;
100   }
101   XBT_DEBUG("Dump %s", stream.str().c_str());
102   fprintf(tracing_file, "%s\n", stream.str().c_str());
103 }
104
105 void VariableEvent::print()
106 {
107   std::stringstream stream;
108   stream << std::fixed << std::setprecision(TRACE_precision());
109   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
110   if (instr_fmt_type != instr_fmt_paje)
111     return;
112
113   if (timestamp_ < 1e-12)
114     stream << eventType_ << " " << 0 << " " << type->getId() << " " << container->getId() << " " << value;
115   else
116     stream << eventType_ << " " << timestamp_ << " " << type->getId() << " " << container->getId() << " " << value;
117   XBT_DEBUG("Dump %s", stream.str().c_str());
118   fprintf(tracing_file, "%s\n", stream.str().c_str());
119 }
120
121 void StateEvent::print()
122 {
123   std::stringstream stream;
124   stream << std::fixed << std::setprecision(TRACE_precision());
125   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, eventType_, TRACE_precision(), timestamp_);
126   if (instr_fmt_type == instr_fmt_paje) {
127     if (timestamp_ < 1e-12)
128       stream << eventType_ << " " << 0 << " " << type->getId() << " " << container->getId();
129     else
130       stream << eventType_ << " " << timestamp_ << " " << type->getId() << " " << container->getId();
131
132     if (value != nullptr) // PAJE_PopState Event does not need to have a value
133       stream << " " << value->getId();
134
135     if (TRACE_display_sizes()) {
136       stream << " ";
137       if (extra_ != nullptr) {
138         stream << static_cast<instr_extra_data>(extra_)->send_size;
139       } else {
140         stream << 0;
141       }
142     }
143 #if HAVE_SMPI
144     if (xbt_cfg_get_boolean("smpi/trace-call-location")) {
145       stream << " \"" << filename << "\" " << linenumber;
146     }
147 #endif
148     XBT_DEBUG("Dump %s", stream.str().c_str());
149     fprintf(tracing_file, "%s\n", stream.str().c_str());
150
151     if (extra_ != nullptr) {
152       if (static_cast<instr_extra_data>(extra_)->sendcounts != nullptr)
153         xbt_free(static_cast<instr_extra_data>(extra_)->sendcounts);
154       if (static_cast<instr_extra_data>(extra_)->recvcounts != nullptr)
155         xbt_free(static_cast<instr_extra_data>(extra_)->recvcounts);
156       xbt_free(extra_);
157     }
158   } else if (instr_fmt_type == instr_fmt_TI) {
159     if (extra_ == nullptr)
160       return;
161     instr_extra_data extra = (instr_extra_data)extra_;
162
163     // FIXME: dirty extract "rank-" from the name, as we want the bare process id here
164     if (container->getName().find("rank-") != 0)
165       stream << container->getName() << " ";
166     else
167       stream << container->getName().erase(0, 5) << " ";
168
169     FILE* trace_file = tracing_files.at(container);
170
171     switch (extra->type) {
172       case TRACING_INIT:
173         stream << "init";
174         break;
175       case TRACING_FINALIZE:
176         stream << "finalize";
177         break;
178       case TRACING_SEND:
179         stream << "send " << extra->dst << " " << extra->send_size << " " << extra->datatype1;
180         break;
181       case TRACING_ISEND:
182         stream << "Isend " << extra->dst << " " << extra->send_size << " " << extra->datatype1;
183         break;
184       case TRACING_RECV:
185         stream << "recv " << extra->src << " " << extra->send_size << " " << extra->datatype1;
186         break;
187       case TRACING_IRECV:
188         stream << "Irecv " << extra->src << " " << extra->send_size << " " << extra->datatype1;
189         break;
190       case TRACING_TEST:
191         stream << "test";
192         break;
193       case TRACING_WAIT:
194         stream << "wait";
195         break;
196       case TRACING_WAITALL:
197         stream << "waitAll";
198         break;
199       case TRACING_BARRIER:
200         stream << "barrier";
201         break;
202       case TRACING_BCAST: // rank bcast size (root) (datatype)
203         stream << "bcast " << extra->send_size;
204         if (extra->root != 0 || (extra->datatype1 && strcmp(extra->datatype1, "")))
205           stream << " " << extra->root << " " << extra->datatype1;
206         break;
207       case TRACING_REDUCE: // rank reduce comm_size comp_size (root) (datatype)
208         stream << "reduce " << extra->send_size << " " << extra->comp_size;
209         if (extra->root != 0 || (extra->datatype1 && strcmp(extra->datatype1, "")))
210           stream << " " << extra->root << " " << extra->datatype1;
211         break;
212       case TRACING_ALLREDUCE: // rank allreduce comm_size comp_size (datatype)
213         stream << "allReduce " << extra->send_size << " " << extra->comp_size << " " << extra->datatype1;
214         break;
215       case TRACING_ALLTOALL: // rank alltoall send_size recv_size (sendtype) (recvtype)
216         stream << "allToAll " << extra->send_size << " " << extra->recv_size << " " << extra->datatype1 << " ";
217         stream << extra->datatype2;
218         break;
219       case TRACING_ALLTOALLV: // rank alltoallv send_size [sendcounts] recv_size [recvcounts] (sendtype) (recvtype)
220         stream << "allToAllV " << extra->send_size << " ";
221         for (int i = 0; i < extra->num_processes; i++)
222           stream << extra->sendcounts[i] << " ";
223         stream << extra->recv_size << " ";
224         for (int i = 0; i < extra->num_processes; i++)
225           stream << extra->recvcounts[i] << " ";
226         stream << extra->datatype1 << " " << extra->datatype2;
227         break;
228       case TRACING_GATHER: // rank gather send_size recv_size root (sendtype) (recvtype)
229         stream << "gather " << extra->send_size << " " << extra->recv_size << " " << extra->datatype1 << " ";
230         stream << extra->datatype2;
231         break;
232       case TRACING_ALLGATHERV: // rank allgatherv send_size [recvcounts] (sendtype) (recvtype)
233         stream << "allGatherV " << extra->send_size;
234         for (int i = 0; i < extra->num_processes; i++)
235           stream << extra->recvcounts[i] << " ";
236         stream << extra->datatype1 << " " << extra->datatype2;
237         break;
238       case TRACING_REDUCE_SCATTER: // rank reducescatter [recvcounts] comp_size (sendtype)
239         stream << "reduceScatter ";
240         for (int i = 0; i < extra->num_processes; i++)
241           stream << extra->recvcounts[i] << " ";
242         stream << extra->comp_size << " " << extra->datatype1;
243         break;
244       case TRACING_COMPUTING:
245         stream << "compute " << extra->comp_size;
246         break;
247       case TRACING_SLEEPING:
248         stream << "sleep " << extra->sleep_duration;
249         break;
250       case TRACING_GATHERV: // rank gatherv send_size [recvcounts] root (sendtype) (recvtype)
251         stream << "gatherV " << extra->send_size;
252         for (int i = 0; i < extra->num_processes; i++)
253           stream << extra->recvcounts[i] << " ";
254         stream << extra->root << " " << extra->datatype1 << " " << extra->datatype2;
255         break;
256       case TRACING_ALLGATHER: // rank allgather sendcount recvcounts (sendtype) (recvtype)
257         stream << "allGather " << extra->send_size << " " << extra->recv_size << " " << extra->datatype1 << " ";
258         stream << extra->datatype2;
259         break;
260       default:
261         /* Unimplemented calls are: WAITANY, SENDRECV, SCATTER, SCATTERV, SCAN, EXSCAN, COMM_SIZE, COMM_SPLIT, COMM_DUP,
262          * SSEND, and ISSEND.
263          */
264         XBT_WARN("Call from %s impossible to translate into replay command : Not implemented (yet)", value->getCname());
265         break;
266     }
267     fprintf(trace_file, "%s\n", stream.str().c_str());
268
269     if (extra->recvcounts != nullptr)
270       xbt_free(extra->recvcounts);
271     if (extra->sendcounts != nullptr)
272       xbt_free(extra->sendcounts);
273     xbt_free(extra);
274
275   } else {
276     THROW_IMPOSSIBLE;
277   }
278 }
279 }
280 }