1 /* Copyright (c) 2023. 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 #ifndef SIMGRID_PLUGINS_BATTERY_HPP_
7 #define SIMGRID_PLUGINS_BATTERY_HPP_
10 #include <simgrid/kernel/resource/Model.hpp>
11 #include <simgrid/s4u/Activity.hpp>
12 #include <xbt/Extendable.hpp>
14 namespace simgrid::plugins {
17 using BatteryPtr = boost::intrusive_ptr<Battery>;
18 XBT_PUBLIC void intrusive_ptr_release(Battery* o);
19 XBT_PUBLIC void intrusive_ptr_add_ref(Battery* o);
21 class BatteryModel : public kernel::resource::Model {
22 std::vector<BatteryPtr> batteries_;
25 explicit BatteryModel();
27 void add_battery(BatteryPtr b);
28 void update_actions_state(double now, double delta) override;
29 double next_occurring_event(double now) override;
37 enum Flow { CHARGE, DISCHARGE };
43 double state_of_charge_;
45 double time_delta_ = -1;
46 std::function<void()> callback_;
50 Event(double state_of_charge, Flow flow, std::function<void()> callback, bool repeat);
51 static std::shared_ptr<Event> init(double state_of_charge, Flow flow, std::function<void()> callback, bool repeat);
53 /** @ingroup plugin_battery
54 * @return The state of charge at which the Event will happen.
55 * @note For Battery::Event objects
57 double get_state_of_charge() { return state_of_charge_; }
58 /** @ingroup plugin_battery
59 * @return The flow in which the Event will happen, either when the Battery is charging or discharging.
60 * @note For Battery::Event objects
62 Flow get_flow() { return flow_; }
63 /** @ingroup plugin_battery
64 * @return The time delta until the Event happen.
65 -1 means that is will never happen with the current state the Battery,
66 for instance when there is no load connected to the Battery.
67 * @note For Battery::Event objects
69 double get_time_delta() { return time_delta_; }
70 /** @ingroup plugin_battery
71 * @return The callback to trigger when the Event happen.
72 * @note For Battery::Event objects
74 std::function<void()> get_callback() { return callback_; }
75 /** @ingroup plugin_battery
76 * @return true if its a recurrent Event.
77 * @note For Battery::Event objects
79 bool get_repeat() { return repeat_; }
83 static std::shared_ptr<BatteryModel> battery_model_;
86 double charge_efficiency_;
87 double discharge_efficiency_;
88 double initial_capacity_wh_;
89 double energy_budget_j_;
91 std::map<const s4u::Host*, bool> host_loads_ = {};
92 std::map<const std::string, double> named_loads_ = {};
93 std::vector<std::shared_ptr<Event>> events_;
96 double energy_stored_j_;
97 double energy_provided_j_ = 0;
98 double energy_consumed_j_ = 0;
99 double last_updated_ = 0;
101 explicit Battery(const std::string& name, double state_of_charge, double charge_efficiency,
102 double discharge_efficiency, double initial_capacity_wh, int cycles);
103 static void init_plugin();
105 double next_occurring_event();
107 std::atomic_int_fast32_t refcount_{0};
109 friend void intrusive_ptr_release(Battery* o)
111 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
112 std::atomic_thread_fence(std::memory_order_acquire);
116 friend void intrusive_ptr_add_ref(Battery* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
120 static BatteryPtr init(const std::string& name, double state_of_charge, double charge_efficiency,
121 double discharge_efficiency, double initial_capacity_wh, int cycles);
122 void set_load(const std::string& name, double power_w);
123 void connect_host(s4u::Host* host, bool active = true);
124 double get_state_of_charge();
125 double get_state_of_health();
126 double get_capacity();
127 double get_energy_provided();
128 double get_energy_consumed();
129 double get_energy_stored(std::string unit = "J");
130 std::shared_ptr<Event> create_event(double state_of_charge, Flow flow, std::function<void()> callback,
131 bool repeat = false);
132 std::vector<std::shared_ptr<Event>> get_events();
133 void delete_event(std::shared_ptr<Event> event);
135 } // namespace simgrid::plugins