Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Refactoring profiles to use generic callbacks
[simgrid.git] / include / simgrid / kernel / ProfileBuilder.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_PROFILEBUILDER_HPP
7 #define SIMGRID_KERNEL_PROFILEBUILDER_HPP
8
9 #include <simgrid/forward.h>
10 #include <functional>
11
12 namespace simgrid {
13 namespace kernel {
14 namespace profile {
15
16
17 /** @brief Modeling of the availability profile (due to an external load) or the churn
18  *
19  * There is 4 main concepts in this module:
20  * - #simgrid::kernel::profile::DatedValue: a pair <timestamp, value> (both are of type double)
21  * - #simgrid::kernel::profile::Profile: a list of dated values
22  * - #simgrid::kernel::profile::Event: links a given trace to a given SimGrid resource.
23  *   A Cpu for example has 2 kinds of events: state (ie, is it ON/OFF) and speed,
24  *   while a link has 3 iterators: state, bandwidth and latency.
25  * - #simgrid::kernel::profile::FutureEvtSet: makes it easy to find the next occurring event of all profiles
26  */
27 class XBT_PUBLIC DatedValue {
28 public:
29   double date_          = 0;
30   double value_         = 0;
31   explicit DatedValue() = default;
32   explicit DatedValue(double d, double v) : date_(d), value_(v) {}
33   bool operator==(DatedValue const& e2) const;
34   bool operator!=(DatedValue const& e2) const { return not(*this == e2); }
35 };
36
37 std::ostream& operator << (std::ostream& out, const DatedValue& dv);
38 /**
39  * @brief Simple builder for Profile classes.
40  *
41  * It can be used to create profiles for links, hosts or disks.
42  */
43 class XBT_PUBLIC ProfileBuilder {
44 public:
45   
46   /** @brief A function called to populate the set of timed-values of a profile.
47    *
48    * When the profile is repeating, the callback is called only once upon Profile construction. 
49    * Then the timed values are repeated after a fixed repeat_delay.
50    *
51    * When the profile is not repeating, the callback is called each time the profile data has been exhausted. 
52    * More precisely, each time an FutureEvtSet reached the end of the vector of timed values of the profile. 
53    *
54    * Note that the callback is only supposed to append values to the values vector, not modify or remove existing ones, 
55    * because other FutureEvtSet may still need previous values.
56    *   
57    * @param values The vector of the profile values, where new data can be appended.
58    *
59    */
60   using UpdateCb = void(std::vector<DatedValue>& values);
61
62   static Profile* from_file(const std::string& path);
63   static Profile* from_string(const std::string& name, const std::string& input, double periodicity);
64
65   static Profile* from_void();  
66
67   /** Create a profile from a callback
68   * 
69   * @param name: The *unique* name of the profile 
70   * @param cb: A callback object/function to populate the profile
71   * @param repeat_delay: Ignored if strictly negative. Otherwise, profile is repeating and repeat_delay is inserted between two iterations.
72   *
73   * @return the newly created profile
74   */
75   static Profile* from_callback(const std::string& name, const std::function<UpdateCb>& cb, double repeat_delay);
76
77 };
78
79 } // namespace profile
80 } // namespace kernel
81 } // namespace simgrid
82
83 #endif /* SIMGRID_KERNEL_PROFILEBUILDER_HPP */