Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix memleak in battery examples and task-storm
[simgrid.git] / examples / cpp / battery-energy / s4u-battery-energy.cpp
index 09645ca..6db8812 100644 (file)
@@ -13,23 +13,25 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(battery_energy, "Messages specific for this s4u exa
 
 static void manager()
 {
-  auto battery = simgrid::plugins::Battery::init("Battery", 0.8, 0.9, 0.9, 10, 1000, 0.9);
+  auto battery = simgrid::plugins::Battery::init("Battery", 0.8, -300, 300, 0.9, 0.9, 10, 1000);
 
   auto* host1 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost1");
   auto* host2 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost2");
   auto* host3 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost3");
 
-  battery->create_event(0.2, simgrid::plugins::Battery::DISCHARGE, [battery, &host1, &host2, &host3]() {
-    XBT_INFO("Event -> Battery low: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
-             battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
-             battery->get_energy_provided(), battery->get_energy_consumed());
-    XBT_INFO("Disconnecting hosts %s and %s", host1->get_cname(), host2->get_cname());
-    battery->connect_host(host1, false);
-    battery->connect_host(host2, false);
-    XBT_INFO("Energy consumed this far by: %s: %fJ, %s: %fJ, %s: %fJ", host1->get_cname(),
-             sg_host_get_consumed_energy(host1), host2->get_cname(), sg_host_get_consumed_energy(host2),
-             host3->get_cname(), sg_host_get_consumed_energy(host3));
-  });
+  battery->schedule_handler(
+      0.2, simgrid::plugins::Battery::DISCHARGE, simgrid::plugins::Battery::Handler::PERSISTANT,
+      [&battery, &host1, &host2, &host3]() {
+        XBT_INFO("Handler -> Battery low: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
+                 battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
+                 battery->get_energy_provided(), battery->get_energy_consumed());
+        XBT_INFO("Disconnecting hosts %s and %s", host1->get_cname(), host2->get_cname());
+        battery->connect_host(host1, false);
+        battery->connect_host(host2, false);
+        XBT_INFO("Energy consumed this far by: %s: %fJ, %s: %fJ, %s: %fJ", host1->get_cname(),
+                 sg_host_get_consumed_energy(host1), host2->get_cname(), sg_host_get_consumed_energy(host2),
+                 host3->get_cname(), sg_host_get_consumed_energy(host3));
+      });
 
   XBT_INFO("Battery state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
            battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
@@ -40,7 +42,7 @@ static void manager()
 
   double flops = 1e9;
   XBT_INFO("Host %s will now execute %f flops", host1->get_cname(), flops);
-  host1->execute(flops);
+  host2->execute(flops);
 
   simgrid::s4u::this_actor::sleep_until(200);
   XBT_INFO("Battery state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",