Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a add_route with s4u::Link instead of LinkInRoute
[simgrid.git] / teshsuite / s4u / monkey-semaphore / monkey-semaphore.cpp
1 /* Copyright (c) 2006-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 // 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   void clear()
32   {
33     while (not to_release.empty()) {
34       auto* sem = to_release.back();
35       XBT_INFO("Go release a semaphore");
36       sem->release();
37       XBT_INFO("Released a semaphore on reboot. It's now %d", sem->get_capacity());
38       to_release.pop_back();
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   static 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   to_release.clear();
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   static 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   to_release.clear();
89
90   int item;
91   do {
92     sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
93
94     while (buf.sem_full->acquire_timeout(10)) {
95       XBT_INFO("Timeouted");
96       xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
97                  "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
98     }
99     to_release.push(buf.sem_full);
100
101     sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
102
103     item = buf.value;
104     XBT_INFO("Receiving item %d", item);
105     buf.sem_empty->release();
106     to_release.pop();
107   } while (item != 0);
108
109   XBT_INFO("Bye!");
110 }
111
112 int main(int argc, char** argv)
113 {
114   sg4::Engine e(&argc, argv);
115
116   auto* rootzone = sg4::create_full_zone("root");
117   auto* paul     = rootzone->create_host("Paul", 1e9);
118   auto* carol    = rootzone->create_host("Carol", 1e9);
119   auto* link     = rootzone->create_link("link", "1MBps")->set_latency("24us")->seal();
120   rootzone->add_route(paul, carol, {link});
121
122   SharedBuffer buffer;
123   sg4::Actor::create("producer", paul, producer, std::ref(buffer))->set_auto_restart();
124   sg4::Actor::create("consumer", carol, consumer, std::cref(buffer))->set_auto_restart();
125   e.run();
126
127   return 0;
128 }