Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
96f19ca14b0691479238e73760441621c0e31206
[simgrid.git] / src / mc / transition / Transition.hpp
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 #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 {
16 namespace mc {
17
18 /** An element in the recorded path
19  *
20  *  At each decision point, we need to record which process transition
21  *  is triggered and potentially which value is associated with this
22  *  transition. The value is used to find which communication is triggered
23  *  in things like waitany and for associating a given value of MC_random()
24  *  calls.
25  */
26 class Transition {
27   /* Textual representation of the transition, to display backtraces */
28   static unsigned long executed_transitions_;
29   static unsigned long replayed_transitions_;
30
31   friend State; // FIXME remove this once we have a proper class to handle the statistics
32
33 public:
34   /* Ordering is important here. depends() implementations only consider subsequent types in this ordering */
35   XBT_DECLARE_ENUM_CLASS(Type, RANDOM,     /* First because indep with anybody */
36                          TESTANY, WAITANY, /* high priority because they can rewrite themselves to *_WAIT */
37                          COMM_RECV, COMM_SEND, COMM_TEST, COMM_WAIT, /* Alphabetical ordering of COMM_* */
38                          MUTEX_LOCK, MUTEX_TEST, MUTEX_TRYLOCK, MUTEX_UNLOCK, MUTEX_WAIT, /* alphabetical */
39                          /* UNKNOWN must be last */ UNKNOWN);
40   Type type_ = Type::UNKNOWN;
41
42   aid_t aid_ = 0;
43
44   /* Which transition was executed for this simcall
45    *
46    * Some simcalls can lead to different transitions:
47    *
48    * * waitany/testany can trigger on different messages;
49    *
50    * * random can produce different values.
51    */
52   int times_considered_ = 0;
53
54   Transition() = default;
55   Transition(Type type, aid_t issuer, int times_considered)
56       : type_(type), aid_(issuer), times_considered_(times_considered)
57   {
58   }
59   virtual ~Transition();
60
61   /** Returns a textual representation of the transition. Pointer adresses are omitted if verbose=false */
62   virtual std::string to_string(bool verbose = false) const;
63   /** Returns something like >>label = "desc", color = c<< to describe the transition in dot format */
64   virtual std::string dot_string() const;
65
66   /* Moves the application toward a path that was already explored, but don't change the current transition */
67   void replay() const;
68
69   virtual bool depends(const Transition* other) const { return true; }
70
71   /* Returns the total amount of transitions executed so far (for statistics) */
72   static unsigned long get_executed_transitions() { return executed_transitions_; }
73   /* Returns the total amount of transitions replayed so far while backtracing (for statistics) */
74   static unsigned long get_replayed_transitions() { return replayed_transitions_; }
75 };
76
77 /** Make a new transition from serialized description */
78 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream);
79
80 } // namespace mc
81 } // namespace simgrid
82
83 #endif