Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9b41ff60a99c1bcafa4a45d629a4d8226eea76d9
[simgrid.git] / src / mc / transition / Transition.cpp
1 /* Copyright (c) 2015-2022. 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 "src/mc/transition/Transition.hpp"
7 #include "xbt/asserts.h"
8 #include "xbt/string.hpp"
9 #include <simgrid/config.h>
10
11 #if SIMGRID_HAVE_MC
12 #include "src/mc/ModelChecker.hpp"
13 #include "src/mc/transition/TransitionAny.hpp"
14 #include "src/mc/transition/TransitionComm.hpp"
15 #include "src/mc/transition/TransitionRandom.hpp"
16 #include "src/mc/transition/TransitionSynchro.hpp"
17 #endif
18
19 #include <sstream>
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_transition, mc, "Logging specific to MC transitions");
22
23 namespace simgrid::mc {
24 unsigned long Transition::executed_transitions_ = 0;
25 unsigned long Transition::replayed_transitions_ = 0;
26
27 // Do not move this to the header, to ensure that we have a vtable for Transition
28 Transition::~Transition() = default;
29
30 std::string Transition::to_string(bool) const
31 {
32   return "";
33 }
34 std::string Transition::dot_string() const
35 {
36   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
37                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
38                                                        "lightblue", "tan"}};
39   const char* color = colors[(aid_ - 1) % colors.size()];
40
41   return xbt::string_printf("label = \"[(%ld)] %s\", color = %s, fontcolor = %s", aid_, Transition::to_c_str(type_),
42                             color, color);
43 }
44 void Transition::replay() const
45 {
46   replayed_transitions_++;
47
48 #if SIMGRID_HAVE_MC
49   mc_model_checker->handle_simcall(aid_, times_considered_, false);
50   mc_model_checker->wait_for_requests();
51 #endif
52 }
53
54 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream)
55 {
56 #if SIMGRID_HAVE_MC
57   short type;
58   xbt_assert(stream >> type);
59   xbt_assert(type >= 0 && type <= static_cast<short>(Transition::Type::UNKNOWN), "Invalid transition type %d received",
60              type);
61
62   switch (auto simcall = static_cast<Transition::Type>(type)) {
63     case Transition::Type::BARRIER_LOCK:
64     case Transition::Type::BARRIER_WAIT:
65       return new BarrierTransition(issuer, times_considered, simcall, stream);
66
67     case Transition::Type::COMM_RECV:
68       return new CommRecvTransition(issuer, times_considered, stream);
69     case Transition::Type::COMM_SEND:
70       return new CommSendTransition(issuer, times_considered, stream);
71     case Transition::Type::COMM_TEST:
72       return new CommTestTransition(issuer, times_considered, stream);
73     case Transition::Type::COMM_WAIT:
74       return new CommWaitTransition(issuer, times_considered, stream);
75
76     case Transition::Type::TESTANY:
77       return new TestAnyTransition(issuer, times_considered, stream);
78     case Transition::Type::WAITANY:
79       return new WaitAnyTransition(issuer, times_considered, stream);
80
81     case Transition::Type::RANDOM:
82       return new RandomTransition(issuer, times_considered, stream);
83
84     case Transition::Type::MUTEX_TRYLOCK:
85     case Transition::Type::MUTEX_LOCK:
86     case Transition::Type::MUTEX_TEST:
87     case Transition::Type::MUTEX_WAIT:
88     case Transition::Type::MUTEX_UNLOCK:
89       return new MutexTransition(issuer, times_considered, simcall, stream);
90
91     case Transition::Type::SEM_LOCK:
92     case Transition::Type::SEM_UNLOCK:
93     case Transition::Type::SEM_WAIT:
94       return new SemaphoreTransition(issuer, times_considered, simcall, stream);
95
96     case Transition::Type::UNKNOWN:
97       return new Transition(Transition::Type::UNKNOWN, issuer, times_considered);
98   }
99   THROW_IMPOSSIBLE; // Some compilers don't detect that each branch of the above switch has a return
100 #else
101   xbt_die("Deserializing transitions is only interesting in MC mode.");
102 #endif
103 }
104
105 } // namespace simgrid::mc