Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: add an observer to sleep simcalls
[simgrid.git] / src / mc / transition / Transition.hpp
1 /* Copyright (c) 2015-2023. 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 #ifndef SIMGRID_MC_TRANSITION_HPP
7 #define SIMGRID_MC_TRANSITION_HPP
8
9 #include "simgrid/forward.h" // aid_t
10 #include "xbt/utility.hpp"   // XBT_DECLARE_ENUM_CLASS
11
12 #include <sstream>
13 #include <string>
14
15 namespace simgrid::mc {
16
17 /** An element in the recorded path
18  *
19  *  At each decision point, we need to record which process transition
20  *  is triggered and potentially which value is associated with this
21  *  transition. The value is used to find which communication is triggered
22  *  in things like waitany and for associating a given value of MC_random()
23  *  calls.
24  */
25 class Transition {
26   /* Textual representation of the transition, to display backtraces */
27   static unsigned long executed_transitions_;
28   static unsigned long replayed_transitions_;
29
30   friend State; // FIXME remove this once we have a proper class to handle the statistics
31
32 public:
33   /* Ordering is important here. depends() implementations only consider subsequent types in this ordering */
34   XBT_DECLARE_ENUM_CLASS(Type,
35                          /* First because indep with anybody including themselves */
36                          RANDOM, ACTOR_JOIN, ACTOR_SLEEP,
37                          /* high priority because indep with almost everybody */
38                          OBJECT_ACCESS,
39                          /* high priority because they can rewrite themselves to *_WAIT */
40                          TESTANY, WAITANY,
41                          /* BARRIER transitions sorted alphabetically */
42                          BARRIER_ASYNC_LOCK, BARRIER_WAIT,
43                          /* Alphabetical ordering of COMM_* */
44                          COMM_ASYNC_RECV, COMM_ASYNC_SEND, COMM_TEST, COMM_WAIT,
45                          /* alphabetical */
46                          MUTEX_ASYNC_LOCK, MUTEX_TEST, MUTEX_TRYLOCK, MUTEX_UNLOCK, MUTEX_WAIT,
47                          /* alphabetical ordering of SEM transitions */
48                          SEM_ASYNC_LOCK, SEM_UNLOCK, SEM_WAIT,
49                          /* UNKNOWN must be last */
50                          UNKNOWN);
51   Type type_ = Type::UNKNOWN;
52
53   aid_t aid_ = 0;
54
55   /** The user function call that caused this transition to exist. Format: >>filename:line:function()<< */
56   std::string call_location_ = "";
57
58   /* Which transition was executed for this simcall
59    *
60    * Some simcalls can lead to different transitions:
61    *
62    * * waitany/testany can trigger on different messages;
63    *
64    * * random can produce different values.
65    */
66   int times_considered_ = 0;
67
68   Transition() = default;
69   Transition(Type type, aid_t issuer, int times_considered)
70       : type_(type), aid_(issuer), times_considered_(times_considered)
71   {
72   }
73   virtual ~Transition();
74
75   /** Returns a textual representation of the transition. Pointer adresses are omitted if verbose=false */
76   virtual std::string to_string(bool verbose = false) const;
77   /** Returns something like >>label = "desc", color = c<< to describe the transition in dot format */
78   virtual std::string dot_string() const;
79
80   std::string const& get_call_location() const { return call_location_; }
81
82   /* Moves the application toward a path that was already explored, but don't change the current transition */
83   void replay(RemoteApp& app) const;
84
85   virtual bool depends(const Transition* other) const { return true; }
86
87   /* Returns the total amount of transitions executed so far (for statistics) */
88   static unsigned long get_executed_transitions() { return executed_transitions_; }
89   /* Returns the total amount of transitions replayed so far while backtracing (for statistics) */
90   static unsigned long get_replayed_transitions() { return replayed_transitions_; }
91 };
92
93 /** Make a new transition from serialized description */
94 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream);
95
96 } // namespace simgrid::mc
97
98 #endif