Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'add_barrier_python_bindings' into 'master'
[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                   = 0;                         /* 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       XBT_INFO("Go release a semaphore");
38       sem->release();
39       XBT_INFO("Released a semaphore on exit. It's now %d", sem->get_capacity());
40     }
41   }
42   void push(const sg4::SemaphorePtr& sem) { to_release.push_back(sem.get()); }
43   void pop() { to_release.pop_back(); }
44 };
45
46 static void producer(SharedBuffer& buf)
47 {
48   static int todo    = cfg_item_count; // remaining amount of items to exchange
49   SemStack to_release;
50   bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
51
52   XBT_INFO("Producer %s", rebooting ? "rebooting" : "booting");
53   if (not rebooting) // Starting for the first time
54     sg4::this_actor::on_exit(
55         [](bool forcefully) { XBT_INFO("Producer dying %s.", forcefully ? "forcefully" : "peacefully"); });
56
57   while (todo > 0) {
58     sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
59
60     while (buf.sem_empty->acquire_timeout(10)) {
61       XBT_INFO("Timeouted");
62       xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
63                  "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
64     }
65     to_release.push(buf.sem_empty);
66     XBT_INFO("sem_empty acquired");
67
68     sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
69
70     XBT_INFO("Pushing item %d", todo - 1);
71     buf.value = todo - 1;
72     buf.sem_full->release();
73     to_release.pop();
74     XBT_INFO("sem_empty removed from to_release");
75     todo--;
76   }
77 }
78
79 static void consumer(const SharedBuffer& buf)
80 {
81   SemStack to_release;
82   bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
83
84   XBT_INFO("Consumer %s", rebooting ? "rebooting" : "booting");
85   if (not rebooting) // Starting for the first time
86     sg4::this_actor::on_exit(
87         [](bool forcefully) { XBT_INFO("Consumer dying %s.", forcefully ? "forcefully" : "peacefully"); });
88
89   int item;
90   do {
91     sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
92
93     while (buf.sem_full->acquire_timeout(10)) {
94       XBT_INFO("Timeouted");
95       xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
96                  "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
97     }
98     to_release.push(buf.sem_full);
99
100     sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
101
102     item = buf.value;
103     XBT_INFO("Receiving item %d", item);
104     buf.sem_empty->release();
105     to_release.pop();
106   } while (item != 0);
107
108   XBT_INFO("Bye!");
109 }
110
111 int main(int argc, char** argv)
112 {
113   sg4::Engine e(&argc, argv);
114
115   auto* rootzone = sg4::create_full_zone("root");
116   auto* paul     = rootzone->create_host("Paul", 1e9);
117   auto* carol    = rootzone->create_host("Carol", 1e9);
118   sg4::LinkInRoute link(rootzone->create_link("link", "1MBps")->set_latency("24us")->seal());
119   rootzone->add_route(paul->get_netpoint(), carol->get_netpoint(), nullptr, nullptr, {link}, true);
120
121   SharedBuffer buffer;
122   sg4::Actor::create("producer", paul, producer, std::ref(buffer))->set_auto_restart();
123   sg4::Actor::create("consumer", carol, consumer, std::cref(buffer))->set_auto_restart();
124   e.run();
125
126   return 0;
127 }