Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to fix initializer order
[simgrid.git] / include / simgrid / plugins / battery.hpp
1 /* Copyright (c) 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 #ifndef SIMGRID_PLUGINS_BATTERY_HPP_
7 #define SIMGRID_PLUGINS_BATTERY_HPP_
8
9 #include <memory>
10 #include <simgrid/kernel/resource/Model.hpp>
11 #include <simgrid/s4u/Activity.hpp>
12 #include <xbt/Extendable.hpp>
13
14 namespace simgrid::plugins {
15
16 class Battery;
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);
20
21 class BatteryModel : public kernel::resource::Model {
22   std::vector<BatteryPtr> batteries_;
23
24 public:
25   explicit BatteryModel();
26
27   void add_battery(BatteryPtr b);
28   void update_actions_state(double now, double delta) override;
29   double next_occurring_event(double now) override;
30 };
31
32 class Battery {
33
34   friend BatteryModel;
35
36 public:
37   enum Flow { CHARGE, DISCHARGE };
38
39   class Event {
40   public:
41     double state_of_charge_;
42     Flow flow_;
43     double time_delta_ = -1;
44     std::function<void()> callback_;
45     bool repeat_;
46
47     Event(double state_of_charge, Flow flow, std::function<void()> callback, bool repeat);
48     static std::shared_ptr<Event> init(double state_of_charge, Flow flow, std::function<void()> callback, bool repeat);
49   };
50
51 private:
52   static std::shared_ptr<BatteryModel> battery_model_;
53
54   std::string name_;
55   double charge_efficiency_;
56   double discharge_efficiency_;
57   double initial_capacity_wh_;
58   int cycles_; // total complete cycles (charge + discharge) the battery can do before complete depletion.
59   double depth_of_discharge_;
60   double energy_budget_j_;
61
62   std::map<const s4u::Host*, bool> host_loads_     = {};
63   std::map<const std::string, double> named_loads_ = {};
64   std::vector<std::shared_ptr<Event>> events_;
65
66   double capacity_wh_;
67   double energy_stored_j_;
68   double energy_provided_j_ = 0;
69   double energy_consumed_j_ = 0;
70   double last_updated_      = 0;
71
72   explicit Battery(const std::string& name, double state_of_charge, double charge_efficiency,
73                    double discharge_efficiency, double initial_capacity_wh, int cycles, double depth_of_discharge);
74   static void init_plugin();
75   void update();
76   double next_occurring_event();
77
78   std::atomic_int_fast32_t refcount_{0};
79 #ifndef DOXYGEN
80   friend void intrusive_ptr_release(Battery* o)
81   {
82     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
83       std::atomic_thread_fence(std::memory_order_acquire);
84       delete o;
85     }
86   }
87   friend void intrusive_ptr_add_ref(Battery* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
88 #endif
89
90 public:
91   static BatteryPtr init(const std::string& name, double state_of_charge, double charge_efficiency,
92                          double discharge_efficiency, double initial_capacity_wh, int cycles,
93                          double depth_of_discharge);
94   void set_load(const std::string& name, double power_w);
95   void connect_host(s4u::Host* host, bool active = true);
96   double get_state_of_charge();
97   double get_state_of_health();
98   double get_capacity();
99   double get_energy_provided();
100   double get_energy_consumed();
101   double get_energy_stored(std::string unit = "J");
102   std::shared_ptr<Event> create_event(double state_of_charge, Flow flow, std::function<void()> callback,
103                                       bool repeat = false);
104   std::vector<std::shared_ptr<Event>> get_events();
105   void delete_event(std::shared_ptr<Event> event);
106 };
107 } // namespace simgrid::plugins
108 #endif