Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #256 from Flamefire/master
[simgrid.git] / src / mc / mc_request.cpp
1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cassert>
7
8 #include "src/include/mc/mc.h"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/mc/ModelChecker.hpp"
11 #include "src/mc/mc_request.hpp"
12 #include "src/mc/mc_smx.hpp"
13 #include "src/mc/mc_xbt.hpp"
14
15 using simgrid::mc::remote;
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
18                                 "Logging specific to MC (request)");
19
20 static char *pointer_to_string(void *pointer);
21 static char *buff_size_to_string(size_t size);
22
23 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
24 {
25   switch (r->call ) {
26   case SIMCALL_COMM_WAIT:
27     return static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(r));
28   case SIMCALL_COMM_TEST:
29     return static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(r));
30   default:
31     return nullptr;
32   }
33 }
34
35 static inline
36 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
37 {
38   switch(r->call) {
39   case SIMCALL_COMM_ISEND:
40     return simcall_comm_isend__get__mbox(r);
41   case SIMCALL_COMM_IRECV:
42     return simcall_comm_irecv__get__mbox(r);
43   default:
44     return nullptr;
45   }
46 }
47
48 namespace simgrid {
49 namespace mc {
50
51 // Does half the job
52 static inline
53 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
54 {
55   if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
56     return false;
57
58   if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
59     return false;
60
61   // Those are internal requests, we do not need indirection
62   // because those objects are copies:
63   simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
64   simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
65
66   if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
67       && r2->call == SIMCALL_COMM_WAIT) {
68
69     smx_mailbox_t mbox = MC_get_mbox(r1);
70
71     if (mbox != synchro2->mbox_cpy
72         && simcall_comm_wait__get__timeout(r2) <= 0)
73       return false;
74
75     if ((r1->issuer != synchro2->src_proc)
76         && (r1->issuer != synchro2->dst_proc)
77         && simcall_comm_wait__get__timeout(r2) <= 0)
78       return false;
79
80     if ((r1->call == SIMCALL_COMM_ISEND)
81         && (synchro2->type == SIMIX_COMM_SEND)
82         && (synchro2->src_buff !=
83             simcall_comm_isend__get__src_buff(r1))
84         && simcall_comm_wait__get__timeout(r2) <= 0)
85       return false;
86
87     if ((r1->call == SIMCALL_COMM_IRECV)
88         && (synchro2->type == SIMIX_COMM_RECEIVE)
89         && (synchro2->dst_buff != simcall_comm_irecv__get__dst_buff(r1))
90         && simcall_comm_wait__get__timeout(r2) <= 0)
91       return false;
92   }
93
94   /* FIXME: the following rule assumes that the result of the
95    * isend/irecv call is not stored in a buffer used in the
96    * test call. */
97 #if 0
98   if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
99      &&  r2->call == SIMCALL_COMM_TEST)
100      return FALSE;
101 #endif
102
103   if (r1->call == SIMCALL_COMM_WAIT
104       && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST)
105       && (synchro1->src_proc == nullptr || synchro1->dst_proc == nullptr))
106     return false;
107
108   if (r1->call == SIMCALL_COMM_TEST &&
109       (simcall_comm_test__get__comm(r1) == nullptr
110        || synchro1->src_buff == nullptr
111        || synchro1->dst_buff == nullptr))
112     return false;
113
114   if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
115       && synchro1->src_buff == synchro2->src_buff
116       && synchro1->dst_buff == synchro2->dst_buff)
117     return false;
118
119   if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
120       && synchro1->src_buff != nullptr
121       && synchro1->dst_buff != nullptr
122       && synchro2->src_buff != nullptr
123       && synchro2->dst_buff != nullptr
124       && synchro1->dst_buff != synchro2->src_buff
125       && synchro1->dst_buff != synchro2->dst_buff
126       && synchro2->dst_buff != synchro1->src_buff)
127     return false;
128
129   return true;
130 }
131
132 // Those are internal_req
133 bool request_depend(smx_simcall_t r1, smx_simcall_t r2)
134 {
135   if (r1->issuer == r2->issuer)
136     return false;
137
138   /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
139   if ((r1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(r1) > 0)
140       || (r2->call == SIMCALL_COMM_WAIT
141           && simcall_comm_wait__get__timeout(r2) > 0))
142     return TRUE;
143
144   if (r1->call != r2->call)
145     return request_depend_asymmetric(r1, r2)
146       && request_depend_asymmetric(r2, r1);
147
148   // Those are internal requests, we do not need indirection
149   // because those objects are copies:
150   simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
151   simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
152
153   switch(r1->call) {
154   case SIMCALL_COMM_ISEND:
155     return simcall_comm_isend__get__mbox(r1)
156       == simcall_comm_isend__get__mbox(r2);
157   case SIMCALL_COMM_IRECV:
158     return simcall_comm_irecv__get__mbox(r1)
159       == simcall_comm_irecv__get__mbox(r2);
160   case SIMCALL_COMM_WAIT:
161     if (synchro1->src_buff == synchro2->src_buff
162         && synchro1->dst_buff == synchro2->dst_buff)
163       return false;
164     if (synchro1->src_buff != nullptr && synchro1->dst_buff != nullptr && synchro2->src_buff != nullptr &&
165         synchro2->dst_buff != nullptr && synchro1->dst_buff != synchro2->src_buff &&
166         synchro1->dst_buff != synchro2->dst_buff && synchro2->dst_buff != synchro1->src_buff)
167       return false;
168     return true;
169   default:
170     return true;
171   }
172 }
173
174 }
175 }
176
177 static char *pointer_to_string(void *pointer)
178 {
179
180   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
181     return bprintf("%p", pointer);
182
183   return xbt_strdup("(verbose only)");
184 }
185
186 static char *buff_size_to_string(size_t buff_size)
187 {
188
189   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
190     return bprintf("%zu", buff_size);
191
192   return xbt_strdup("(verbose only)");
193 }
194
195
196 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
197 {
198   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
199
200   bool use_remote_comm = true;
201   switch(request_type) {
202   case simgrid::mc::RequestType::simix:
203     use_remote_comm = true;
204     break;
205   case simgrid::mc::RequestType::executed:
206   case simgrid::mc::RequestType::internal:
207     use_remote_comm = false;
208     break;
209   default:
210     THROW_IMPOSSIBLE;
211   }
212
213   const char* type = nullptr;
214   char *args = nullptr;
215
216   smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
217
218   switch (req->call) {
219
220   case SIMCALL_COMM_ISEND: {
221     type = "iSend";
222     char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
223     char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
224     if (issuer->host)
225       args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->pid, MC_smx_actor_get_host_name(issuer),
226                      MC_smx_actor_get_name(issuer), p, bs);
227     else
228       args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->pid, MC_smx_actor_get_name(issuer), p, bs);
229     xbt_free(bs);
230     xbt_free(p);
231     break;
232   }
233
234   case SIMCALL_COMM_IRECV: {
235     size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
236     size_t size = 0;
237     if (remote_size)
238       mc_model_checker->process().read_bytes(&size, sizeof(size),
239         remote(remote_size));
240
241     type = "iRecv";
242     char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
243     char* bs = buff_size_to_string(size);
244     if (issuer->host)
245       args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->pid, MC_smx_actor_get_host_name(issuer),
246                      MC_smx_actor_get_name(issuer), p, bs);
247     else
248       args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->pid, MC_smx_actor_get_name(issuer), p, bs);
249     xbt_free(bs);
250     xbt_free(p);
251     break;
252   }
253
254   case SIMCALL_COMM_WAIT: {
255     simgrid::kernel::activity::CommImpl* remote_act =
256         static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(req));
257     char* p;
258     if (value == -1) {
259       type = "WaitTimeout";
260       p = pointer_to_string(remote_act);
261       args = bprintf("comm=%s", p);
262     } else {
263       type = "Wait";
264       p = pointer_to_string(remote_act);
265
266       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
267       simgrid::kernel::activity::CommImpl* act;
268       if (use_remote_comm) {
269         mc_model_checker->process().read(temp_synchro,
270                                          remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
271         act = temp_synchro.getBuffer();
272       } else
273         act = remote_act;
274
275       smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->src_proc));
276       smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->dst_proc));
277       args =
278           bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->pid : 0,
279                   src_proc ? MC_smx_actor_get_host_name(src_proc) : "", src_proc ? MC_smx_actor_get_name(src_proc) : "",
280                   dst_proc ? dst_proc->pid : 0, dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
281                   dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
282     }
283     xbt_free(p);
284     break;
285   }
286
287   case SIMCALL_COMM_TEST: {
288     simgrid::kernel::activity::CommImpl* remote_act =
289         static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(req));
290     simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
291     simgrid::kernel::activity::CommImpl* act;
292     if (use_remote_comm) {
293       mc_model_checker->process().read(temp_synchro,
294                                        remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
295       act = temp_synchro.getBuffer();
296     } else
297       act = remote_act;
298
299     char* p;
300     if (act->src_proc == nullptr || act->dst_proc == nullptr) {
301       type = "Test FALSE";
302       p = pointer_to_string(remote_act);
303       args = bprintf("comm=%s", p);
304     } else {
305       type = "Test TRUE";
306       p = pointer_to_string(remote_act);
307
308       smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->src_proc));
309       smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->dst_proc));
310       args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->pid, MC_smx_actor_get_name(src_proc),
311                      MC_smx_actor_get_host_name(src_proc), dst_proc->pid, MC_smx_actor_get_name(dst_proc),
312                      MC_smx_actor_get_host_name(dst_proc));
313     }
314     xbt_free(p);
315     break;
316   }
317
318   case SIMCALL_COMM_WAITANY: {
319     type = "WaitAny";
320     s_xbt_dynar_t comms;
321     mc_model_checker->process().read_bytes(
322       &comms, sizeof(comms), remote(simcall_comm_waitany__get__comms(req)));
323     if (not xbt_dynar_is_empty(&comms)) {
324       smx_activity_t remote_sync;
325       read_element(mc_model_checker->process(),
326         &remote_sync, remote(simcall_comm_waitany__get__comms(req)), value,
327         sizeof(remote_sync));
328       char* p = pointer_to_string(remote_sync.get());
329       args = bprintf("comm=%s (%d of %lu)",
330         p, value + 1, xbt_dynar_length(&comms));
331       xbt_free(p);
332     } else
333       args = bprintf("comm at idx %d", value);
334     break;
335   }
336
337   case SIMCALL_COMM_TESTANY:
338     if (value == -1) {
339       type = "TestAny FALSE";
340       args = xbt_strdup("-");
341     } else {
342       type = "TestAny";
343       args =
344           bprintf("(%d of %zu)", value + 1,
345                     simcall_comm_testany__get__count(req));
346     }
347     break;
348
349   case SIMCALL_MUTEX_TRYLOCK:
350   case SIMCALL_MUTEX_LOCK: {
351     if (req->call == SIMCALL_MUTEX_LOCK)
352       type = "Mutex LOCK";
353     else
354       type = "Mutex TRYLOCK";
355
356     simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
357     mc_model_checker->process().read_bytes(mutex.getBuffer(), sizeof(mutex),
358       remote(
359         req->call == SIMCALL_MUTEX_LOCK
360         ? simcall_mutex_lock__get__mutex(req)
361         : simcall_mutex_trylock__get__mutex(req)
362       ));
363     args =
364         bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.getBuffer()->locked,
365                 mutex.getBuffer()->owner != nullptr
366                     ? (int)mc_model_checker->process().resolveActor(simgrid::mc::remote(mutex.getBuffer()->owner))->pid
367                     : -1);
368     break;
369   }
370
371   case SIMCALL_MC_RANDOM:
372     type = "MC_RANDOM";
373     args = bprintf("%d", value);
374     break;
375
376   default:
377     type = SIMIX_simcall_name(req->call);
378     args = bprintf("??");
379     break;
380   }
381
382   std::string str;
383   if (args != nullptr)
384     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->pid, MC_smx_actor_get_host_name(issuer),
385                                       MC_smx_actor_get_name(issuer), type, args);
386   else
387     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->pid, MC_smx_actor_get_host_name(issuer),
388                                       MC_smx_actor_get_name(issuer), type);
389   xbt_free(args);
390   return str;
391 }
392
393 namespace simgrid {
394 namespace mc {
395
396 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
397 {
398   simgrid::kernel::activity::ActivityImpl* remote_act = nullptr;
399   switch (req->call) {
400
401   case SIMCALL_COMM_WAIT:
402     /* FIXME: check also that src and dst processes are not suspended */
403     remote_act = simcall_comm_wait__getraw__comm(req);
404     break;
405
406   case SIMCALL_COMM_WAITANY:
407     read_element(mc_model_checker->process(), &remote_act, remote(simcall_comm_waitany__getraw__comms(req)), idx,
408                  sizeof(remote_act));
409     break;
410
411   case SIMCALL_COMM_TESTANY:
412     remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__getraw__comms(req) + idx));
413     break;
414
415   default:
416     return true;
417   }
418
419   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
420   mc_model_checker->process().read(temp_comm, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
421   simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
422   return comm->src_proc && comm->dst_proc;
423 }
424
425
426 static const char* colors[] = {
427   "blue",
428   "red",
429   "green3",
430   "goldenrod",
431   "brown",
432   "purple",
433   "magenta",
434   "turquoise4",
435   "gray25",
436   "forestgreen",
437   "hotpink",
438   "lightblue",
439   "tan",
440 };
441
442 static inline const char* get_color(int id)
443 {
444   return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
445 }
446
447 std::string request_get_dot_output(smx_simcall_t req, int value)
448 {
449   std::string label;
450
451   const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
452
453   switch (req->call) {
454   case SIMCALL_COMM_ISEND:
455     if (issuer->host)
456       label = simgrid::xbt::string_printf("[(%ld)%s] iSend", issuer->pid, MC_smx_actor_get_host_name(issuer));
457     else
458       label = bprintf("[(%ld)] iSend", issuer->pid);
459     break;
460
461   case SIMCALL_COMM_IRECV:
462     if (issuer->host)
463       label = simgrid::xbt::string_printf("[(%ld)%s] iRecv", issuer->pid, MC_smx_actor_get_host_name(issuer));
464     else
465       label = simgrid::xbt::string_printf("[(%ld)] iRecv", issuer->pid);
466     break;
467
468   case SIMCALL_COMM_WAIT:
469     if (value == -1) {
470       if (issuer->host)
471         label = simgrid::xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->pid, MC_smx_actor_get_host_name(issuer));
472       else
473         label = simgrid::xbt::string_printf("[(%ld)] WaitTimeout", issuer->pid);
474     } else {
475       simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
476       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
477       mc_model_checker->process().read(temp_comm,
478                                        remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
479       simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
480
481       smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->src_proc));
482       smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_proc));
483       if (issuer->host)
484         label = simgrid::xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->pid,
485                                             MC_smx_actor_get_host_name(issuer), src_proc ? src_proc->pid : 0,
486                                             dst_proc ? dst_proc->pid : 0);
487       else
488         label = simgrid::xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]",
489                     issuer->pid,
490                     src_proc ? src_proc->pid : 0,
491                     dst_proc ? dst_proc->pid : 0);
492     }
493     break;
494
495   case SIMCALL_COMM_TEST: {
496     simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
497     simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
498     mc_model_checker->process().read(temp_comm, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
499     simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
500     if (comm->src_proc == nullptr || comm->dst_proc == nullptr) {
501       if (issuer->host)
502         label = simgrid::xbt::string_printf("[(%ld)%s] Test FALSE", issuer->pid, MC_smx_actor_get_host_name(issuer));
503       else
504         label = bprintf("[(%ld)] Test FALSE", issuer->pid);
505     } else {
506       if (issuer->host)
507         label = simgrid::xbt::string_printf("[(%ld)%s] Test TRUE", issuer->pid, MC_smx_actor_get_host_name(issuer));
508       else
509         label = simgrid::xbt::string_printf("[(%ld)] Test TRUE", issuer->pid);
510     }
511     break;
512   }
513
514   case SIMCALL_COMM_WAITANY: {
515     unsigned long comms_size = read_length(
516       mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(req)));
517     if (issuer->host)
518       label = simgrid::xbt::string_printf("[(%ld)%s] WaitAny [%d of %lu]", issuer->pid,
519                                           MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
520     else
521       label = simgrid::xbt::string_printf("[(%ld)] WaitAny [%d of %lu]",
522                   issuer->pid, value + 1, comms_size);
523     break;
524   }
525
526   case SIMCALL_COMM_TESTANY:
527     if (value == -1) {
528       if (issuer->host)
529         label = simgrid::xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->pid, MC_smx_actor_get_host_name(issuer));
530       else
531         label = simgrid::xbt::string_printf("[(%ld)] TestAny FALSE", issuer->pid);
532     } else {
533       if (issuer->host)
534         label = simgrid::xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->pid,
535                                             MC_smx_actor_get_host_name(issuer), value + 1,
536                                             simcall_comm_testany__get__count(req));
537       else
538         label = simgrid::xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]",
539                     issuer->pid,
540                     value + 1,
541                     simcall_comm_testany__get__count(req));
542     }
543     break;
544
545   case SIMCALL_MUTEX_TRYLOCK:
546     label = simgrid::xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->pid);
547     break;
548
549   case SIMCALL_MUTEX_LOCK:
550     label = simgrid::xbt::string_printf("[(%ld)] Mutex LOCK", issuer->pid);
551     break;
552
553   case SIMCALL_MC_RANDOM:
554     if (issuer->host)
555       label = simgrid::xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->pid, MC_smx_actor_get_host_name(issuer),
556                                           value);
557     else
558       label = simgrid::xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->pid, value);
559     break;
560
561   default:
562     THROW_UNIMPLEMENTED;
563   }
564
565   const char* color = get_color(issuer->pid - 1);
566   return  simgrid::xbt::string_printf(
567         "label = \"%s\", color = %s, fontcolor = %s", label.c_str(),
568         color, color);
569 }
570
571 }
572 }