Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / mc-bugged1-liveness / s4u-mc-bugged1-liveness.cpp
1 /* Copyright (c) 2012-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 /***************** Centralized Mutual Exclusion Algorithm *******************/
7 /* This example implements a centralized mutual exclusion algorithm.        */
8 /* Bug : CS requests of client 1 not satisfied                              */
9 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok)          */
10 /****************************************************************************/
11
12 #ifdef GARBAGE_STACK
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #endif
17
18 #include <simgrid/modelchecker.h>
19 #include <simgrid/s4u.hpp>
20 #include <xbt/dynar.h>
21
22 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
23
24 class Message {
25 public:
26   enum class Kind { GRANT, REQUEST, RELEASE };
27   Kind kind                             = Kind::GRANT;
28   simgrid::s4u::Mailbox* return_mailbox = nullptr;
29   explicit Message(Message::Kind kind, simgrid::s4u::Mailbox* mbox) : kind(kind), return_mailbox(mbox) {}
30 };
31
32 int r  = 0;
33 int cs = 0;
34
35 #ifdef GARBAGE_STACK
36 /** Do not use a clean stack */
37 static void garbage_stack(void)
38 {
39   const size_t size = 256;
40   int fd            = open("/dev/urandom", O_RDONLY);
41   char foo[size];
42   read(fd, foo, size);
43   close(fd);
44 }
45 #endif
46
47 static void coordinator()
48 {
49   bool CS_used = false;
50   std::queue<simgrid::s4u::Mailbox*> requests;
51
52   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name("coordinator");
53
54   while (true) {
55     auto m = mbox->get_unique<Message>();
56     if (m->kind == Message::Kind::REQUEST) {
57       if (CS_used) {
58         XBT_INFO("CS already used. Queue the request.");
59         requests.push(m->return_mailbox);
60       } else {
61         if (m->return_mailbox->get_name() != "1") {
62           XBT_INFO("CS idle. Grant immediately");
63           m->return_mailbox->put(new Message(Message::Kind::GRANT, mbox), 1000);
64           CS_used = true;
65         }
66       }
67     } else {
68       if (not requests.empty()) {
69         XBT_INFO("CS release. Grant to queued requests (queue size: %zu)", requests.size());
70         simgrid::s4u::Mailbox* req = requests.front();
71         requests.pop();
72         if (req->get_name() != "1") {
73           req->put(new Message(Message::Kind::GRANT, mbox), 1000);
74         } else {
75           requests.push(req);
76           CS_used = false;
77         }
78       } else {
79         XBT_INFO("CS release. resource now idle");
80         CS_used = false;
81       }
82     }
83   }
84 }
85
86 static void client(int id)
87 {
88   aid_t my_pid = simgrid::s4u::this_actor::get_pid();
89
90   simgrid::s4u::Mailbox* my_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id));
91
92   while (true) {
93     XBT_INFO("Ask the request");
94     simgrid::s4u::Mailbox::by_name("coordinator")->put(new Message(Message::Kind::REQUEST, my_mailbox), 1000);
95
96     if (id == 1) {
97       r  = 1;
98       cs = 0;
99       XBT_INFO("Propositions changed : r=1, cs=0");
100     }
101
102     auto grant = my_mailbox->get_unique<Message>();
103
104     if ((id == 1) && (grant->kind == Message::Kind::GRANT)) {
105       cs = 1;
106       r  = 0;
107       XBT_INFO("Propositions changed : r=0, cs=1");
108     }
109
110     XBT_INFO("%d got the answer. Sleep a bit and release it", id);
111
112     simgrid::s4u::this_actor::sleep_for(1);
113
114     simgrid::s4u::Mailbox::by_name("coordinator")->put(new Message(Message::Kind::RELEASE, my_mailbox), 1000);
115
116     simgrid::s4u::this_actor::sleep_for(static_cast<double>(my_pid));
117
118     if (id == 1) {
119       cs = 0;
120       r  = 0;
121       XBT_INFO("Propositions changed : r=0, cs=0");
122     }
123   }
124 }
125
126 static void raw_client(int id)
127 {
128 #ifdef GARBAGE_STACK
129   // At this point the stack of the callee (client) is probably filled with
130   // zeros and uninitialized variables will contain 0. This call will place
131   // random byes in the stack of the callee:
132   garbage_stack();
133 #endif
134   client(id);
135 }
136
137 int main(int argc, char* argv[])
138 {
139   simgrid::s4u::Engine e(&argc, argv);
140
141   MC_automaton_new_propositional_symbol_pointer("r", &r);
142   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
143
144   e.load_platform(argv[1]);
145
146   simgrid::s4u::Actor::create("coordinator", e.host_by_name("Tremblay"), coordinator)
147       ->set_kill_time(argc > 3 ? std::stod(argv[3]) : -1.0);
148   if (std::stod(argv[2]) == 0) {
149     simgrid::s4u::Actor::create("client", e.host_by_name("Boivin"), raw_client, 1);
150     simgrid::s4u::Actor::create("client", e.host_by_name("Fafard"), raw_client, 2);
151   } else { // "Visited" case
152     simgrid::s4u::Actor::create("client", e.host_by_name("Boivin"), raw_client, 2);
153     simgrid::s4u::Actor::create("client", e.host_by_name("Fafard"), raw_client, 1);
154   }
155   e.run();
156
157   return 0;
158 }