Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new: Actor::get_restart_count(): Returns the number of reboots that this actor did
[simgrid.git] / teshsuite / s4u / monkey-semaphore / monkey-semaphore.cpp
1 /* Copyright (c) 2006-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 // This example implements a simple producer/consumer schema, passing a bunch of items from one to the other,
7 // hopefully implemented in a way that resists resource failures.
8
9 #include <simgrid/s4u.hpp>
10 #include <xbt/config.hpp>
11
12 namespace sg4 = simgrid::s4u;
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(sem_monkey, "Simple test of the semaphore");
15
16 static simgrid::config::Flag<int> cfg_item_count{"item-count", "Amount of items that must be exchanged to succeed", 2};
17 static simgrid::config::Flag<double> cfg_deadline{"deadline", "When to fail the simulation (infinite loop detection)",
18                                                   120};
19
20 struct SharedBuffer {
21   int value;                                               /* Where the data is exchanged */
22   sg4::SemaphorePtr sem_empty = sg4::Semaphore::create(1); /* indicates whether the buffer is empty */
23   sg4::SemaphorePtr sem_full  = sg4::Semaphore::create(0); /* indicates whether the buffer is full */
24 };
25
26 // A stack to keep track of semaphores.  When destroyed, semaphores remaining on stack are automatically released.
27 class SemStack {
28   std::vector<sg4::Semaphore*> to_release;
29
30 public:
31   SemStack()                = default;
32   SemStack(const SemStack&) = delete;
33   SemStack& operator=(const SemStack&) = delete;
34   ~SemStack()
35   {
36     for (auto* sem : to_release) {
37       sem->release();
38       XBT_INFO("Released a semaphore on exit. It's now %d", sem->get_capacity());
39     }
40   }
41   void push(const sg4::SemaphorePtr& sem) { to_release.push_back(sem.get()); }
42   void pop() { to_release.pop_back(); }
43 };
44
45 static void producer(SharedBuffer& buf)
46 {
47   static int todo    = cfg_item_count; // remaining amount of items to exchange
48   SemStack to_release;
49   bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
50
51   XBT_INFO("Producer %s", rebooting ? "rebooting" : "booting");
52   if (not rebooting) // Starting for the first time
53     sg4::this_actor::on_exit(
54         [](bool forcefully) { XBT_INFO("Producer dying %s.", forcefully ? "forcefully" : "peacefully"); });
55
56   while (todo > 0) {
57     xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
58                "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
59
60     sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
61
62     while (buf.sem_empty->acquire_timeout(10))
63       XBT_INFO("Timeouted");
64     to_release.push(buf.sem_empty);
65     XBT_INFO("sem_empty acquired");
66
67     sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
68
69     XBT_INFO("Pushing item %d", todo - 1);
70     buf.value = todo - 1;
71     buf.sem_full->release();
72     to_release.pop();
73     XBT_INFO("sem_empty removed from to_release");
74     todo--;
75   }
76 }
77
78 static void consumer(const SharedBuffer& buf)
79 {
80   SemStack to_release;
81   bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
82
83   XBT_INFO("Consumer %s", rebooting ? "rebooting" : "booting");
84   if (not rebooting) // Starting for the first time
85     sg4::this_actor::on_exit(
86         [](bool forcefully) { XBT_INFO("Consumer dying %s.", forcefully ? "forcefully" : "peacefully"); });
87
88   int item;
89   do {
90     xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
91                "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
92
93     sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
94
95     while (buf.sem_full->acquire_timeout(10))
96       XBT_INFO("Timeouted");
97     to_release.push(buf.sem_full);
98
99     sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
100
101     item = buf.value;
102     XBT_INFO("Receiving item %d", item);
103     buf.sem_empty->release();
104     to_release.pop();
105   } while (item != 0);
106
107   XBT_INFO("Bye!");
108 }
109
110 int main(int argc, char** argv)
111 {
112   sg4::Engine e(&argc, argv);
113
114   auto* rootzone = sg4::create_full_zone("root");
115   auto* paul     = rootzone->create_host("Paul", 1e9);
116   auto* carol    = rootzone->create_host("Carol", 1e9);
117   sg4::LinkInRoute link(rootzone->create_link("link", "1MBps")->set_latency("24us")->seal());
118   rootzone->add_route(paul->get_netpoint(), carol->get_netpoint(), nullptr, nullptr, {link}, true);
119
120   SharedBuffer buffer;
121   sg4::Actor::create("producer", paul, producer, std::ref(buffer))->set_auto_restart();
122   sg4::Actor::create("consumer", carol, consumer, std::cref(buffer))->set_auto_restart();
123   e.run();
124
125   return 0;
126 }