1 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved. */
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. */
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.
9 #include <simgrid/s4u.hpp>
10 #include <xbt/config.hpp>
12 namespace sg4 = simgrid::s4u;
14 XBT_LOG_NEW_DEFAULT_CATEGORY(sem_monkey, "Simple test of the semaphore");
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)",
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 */
26 // A stack to keep track of semaphores. When destroyed, semaphores remaining on stack are automatically released.
28 std::vector<sg4::Semaphore*> to_release;
32 SemStack(const SemStack&) = delete;
33 SemStack& operator=(const SemStack&) = delete;
36 for (auto* sem : to_release) {
38 XBT_INFO("Released a semaphore on exit. It's now %d", sem->get_capacity());
41 void push(const sg4::SemaphorePtr& sem) { to_release.push_back(sem.get()); }
42 void pop() { to_release.pop_back(); }
45 static void producer(SharedBuffer& buf)
47 static int todo = cfg_item_count; // remaining amount of items to exchange
49 bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
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"); });
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);
60 sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
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");
67 sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
69 XBT_INFO("Pushing item %d", todo - 1);
71 buf.sem_full->release();
73 XBT_INFO("sem_empty removed from to_release");
78 static void consumer(const SharedBuffer& buf)
81 bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
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"); });
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);
93 sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
95 while (buf.sem_full->acquire_timeout(10))
96 XBT_INFO("Timeouted");
97 to_release.push(buf.sem_full);
99 sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
102 XBT_INFO("Receiving item %d", item);
103 buf.sem_empty->release();
110 int main(int argc, char** argv)
112 sg4::Engine e(&argc, argv);
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);
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();