Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dev-mailbox-clear' into 'master'
[simgrid.git] / src / kernel / resource / profile / Profile.hpp
1 /* Copyright (c) 2004-2022. 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_KERNEL_PROFILE_HPP
7 #define SIMGRID_KERNEL_PROFILE_HPP
8
9 #include "simgrid/forward.h"
10 #include "simgrid/kernel/ProfileBuilder.hpp"
11 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
12 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
13
14 #include <queue>
15 #include <vector>
16 #include <string>
17
18 namespace simgrid {
19 namespace kernel {
20 namespace profile {
21
22 /** @brief A profile is a set of timed values, encoding the value that a variable takes at what time
23  *
24  * It is useful to model dynamic platforms, where an external load that makes the resource availability change over
25  * time. To model that, you have to set several profiles per resource: one for the on/off state and one for each
26  * numerical value (computational speed, bandwidth and/or latency).
27  *
28  * There are two behaviours. Either a callback is used to populate the profile when the set has been exhausted,
29  * or the callback is called only during construction and the initial set is repeated over and over, after a fixed
30  * repeating delay.
31  */
32 class XBT_PUBLIC Profile {
33 public:
34   /** @brief Create a profile. There are two behaviours. Either the callback is
35    *
36    * @param name The name of the profile (checked for uniqueness)
37    * @param cb A callback object/function that populates the profile.
38    * @param repeat_delay If strictly negative, it is ignored and the callback is called when an event reached the end of
39    * the event_list. If zero or positive, the initial set repeats after the provided delay.
40    */
41   explicit Profile(const std::string& name, const std::function<ProfileBuilder::UpdateCb>& cb, double repeat_delay);
42   virtual ~Profile()=default;
43   Event* schedule(FutureEvtSet* fes, resource::Resource* resource);
44   DatedValue next(Event* event);
45
46   const std::vector<DatedValue>& get_event_list() const { return event_list; }
47   const std::string& get_name() const { return name; }
48   bool is_repeating() const { return repeat_delay>=0;}
49   double get_repeat_delay() const { return repeat_delay;}
50
51 private:
52   std::string name;
53   std::function<ProfileBuilder::UpdateCb> cb;
54   std::vector<DatedValue> event_list;
55   FutureEvtSet* fes_  = nullptr;
56   double repeat_delay;
57
58   bool get_enough_events(size_t index)
59   {
60     if (index >= event_list.size() && cb)
61       cb(event_list);
62     return index < event_list.size();
63   }
64 };
65
66 } // namespace profile
67 } // namespace kernel
68 } // namespace simgrid
69
70 /** Module finalizer: frees all profiles */
71 XBT_PUBLIC void tmgr_finalize();
72
73 #endif