Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Split TransitionAny and TransitionRandom to their own files
[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 #endif
17
18 #include <sstream>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_transition, mc, "Logging specific to MC transitions");
21
22 namespace simgrid {
23 namespace 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_label() const
35 {
36   return xbt::string_printf("[(%ld)] %s", aid_, Transition::to_c_str(type_));
37 }
38 void Transition::replay() const
39 {
40   replayed_transitions_++;
41
42 #if SIMGRID_HAVE_MC
43   mc_model_checker->handle_simcall(aid_, times_considered_, false);
44   mc_model_checker->wait_for_requests();
45 #endif
46 }
47
48 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream)
49 {
50 #if SIMGRID_HAVE_MC
51   short type;
52   xbt_assert(stream >> type);
53   xbt_assert(type >= 0 && type <= static_cast<short>(Transition::Type::UNKNOWN), "Invalid transition type %d received",
54              type);
55
56   auto simcall = static_cast<Transition::Type>(type);
57
58   switch (simcall) {
59     case Transition::Type::COMM_RECV:
60       return new CommRecvTransition(issuer, times_considered, stream);
61     case Transition::Type::COMM_SEND:
62       return new CommSendTransition(issuer, times_considered, stream);
63     case Transition::Type::COMM_TEST:
64       return new CommTestTransition(issuer, times_considered, stream);
65     case Transition::Type::COMM_WAIT:
66       return new CommWaitTransition(issuer, times_considered, stream);
67
68     case Transition::Type::TESTANY:
69       return new TestAnyTransition(issuer, times_considered, stream);
70     case Transition::Type::WAITANY:
71       return new WaitAnyTransition(issuer, times_considered, stream);
72
73     case Transition::Type::RANDOM:
74       return new RandomTransition(issuer, times_considered, stream);
75
76     case Transition::Type::UNKNOWN:
77       return new Transition(Transition::Type::UNKNOWN, issuer, times_considered);
78   }
79   THROW_IMPOSSIBLE; // Some compilers don't detect that each branch of the above switch has a return
80 #else
81   xbt_die("Deserializing transitions is only interesting in MC mode.");
82 #endif
83 }
84
85 } // namespace mc
86 } // namespace simgrid