Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that VM::get_speed() returns the correct value after a migration
[simgrid.git] / src / surf / cpu_interface.hpp
1 /* Copyright (c) 2004-2020. 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 SURF_CPU_INTERFACE_HPP_
7 #define SURF_CPU_INTERFACE_HPP_
8
9 #include "simgrid/kernel/resource/Model.hpp"
10 #include "simgrid/kernel/resource/Resource.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "src/kernel/lmm/maxmin.hpp"
13
14 #include <list>
15
16 /***********
17  * Classes *
18  ***********/
19
20 namespace simgrid {
21 namespace kernel {
22 namespace resource {
23
24 /** @ingroup SURF_cpu_interface
25  * @brief SURF cpu model interface class
26  * @details A model is an object which handle the interactions between its Resources and its Actions
27  */
28 class XBT_PUBLIC CpuModel : public Model {
29 public:
30   explicit CpuModel(Model::UpdateAlgo algo) : Model(algo) {}
31
32   /**
33    * @brief Create a Cpu
34    *
35    * @param host The host that will have this CPU
36    * @param speed_per_pstate Processor speed (in Flops) of each pstate.
37    *                         This ignores any potential external load coming from a trace.
38    * @param core The number of core of this Cpu
39    */
40   virtual Cpu* create_cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate, int core) = 0;
41
42   void update_actions_state_lazy(double now, double delta) override;
43   void update_actions_state_full(double now, double delta) override;
44 };
45
46 /************
47  * Resource *
48  ************/
49
50 class CpuAction;
51
52 /** @ingroup SURF_cpu_interface
53 * @brief SURF cpu resource interface class
54 * @details A Cpu represent a cpu associated to a host
55 */
56 class XBT_PUBLIC Cpu : public Resource {
57   int core_count_ = 1;
58   s4u::Host* host_;
59   int pstate_ = 0;                       /*< Current pstate (index in the speed_per_pstate_)*/
60   std::vector<double> speed_per_pstate_; /*< List of supported CPU capacities (pstate related). Not 'const' because VCPU
61                                             get modified on migration */
62   friend simgrid::vm::VirtualMachineImpl; // Resets the VCPU
63
64 public:
65   /**
66    * @brief Cpu constructor
67    *
68    * @param model The CpuModel associated to this Cpu
69    * @param host The host in which this Cpu should be plugged
70    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
71    * @param speedPerPstate Processor speed (in flop per second) for each pstate
72    * @param core The number of core of this Cpu
73    */
74   Cpu(Model* model, s4u::Host* host, lmm::Constraint* constraint, const std::vector<double>& speed_per_pstate,
75       int core);
76
77   /**
78    * @brief Cpu constructor
79    *
80    * @param model The CpuModel associated to this Cpu
81    * @param host The host in which this Cpu should be plugged
82    * @param speedPerPstate Processor speed (in flop per second) for each pstate
83    * @param core The number of core of this Cpu
84    */
85   Cpu(Model* model, s4u::Host* host, const std::vector<double>& speed_per_pstate, int core);
86
87   Cpu(const Cpu&) = delete;
88   Cpu& operator=(const Cpu&) = delete;
89
90   /**
91    * @brief Execute some quantity of computation
92    *
93    * @param size The value of the processing amount (in flop) needed to process
94    * @return The CpuAction corresponding to the processing
95    */
96   virtual CpuAction* execution_start(double size) = 0;
97
98   /**
99    * @brief Execute some quantity of computation on more than one core
100    *
101    * @param size The value of the processing amount (in flop) needed to process
102    * @param requested_cores The desired amount of cores. Must be >= 1
103    * @return The CpuAction corresponding to the processing
104    */
105   virtual CpuAction* execution_start(double size, int requested_cores) = 0;
106
107   /**
108    * @brief Make a process sleep for duration (in seconds)
109    *
110    * @param duration The number of seconds to sleep
111    * @return The CpuAction corresponding to the sleeping
112    */
113   virtual CpuAction* sleep(double duration) = 0;
114
115   /** @brief Get the amount of cores */
116   virtual int get_core_count();
117
118   /** @brief Get a forecast of the speed (in flops/s) if the load were as provided.
119    *
120    * The provided load should encompasses both the application's activities and the external load that come from a trace.
121    *
122    * Use a load of 1.0 to compute the amount of flops that the Cpu would deliver with one CPU-bound task.
123    * If you use a load of 0, this function will return 0: when nobody is using the Cpu, it delivers nothing.
124    *
125    * If you want to know the amount of flops currently delivered, use  load = get_load()*get_speed_ratio()
126    */
127   virtual double get_speed(double load) const;
128
129 protected:
130   /** @brief Take speed changes (either load or max) into account */
131   virtual void on_speed_change();
132
133   /** Reset most characteristics of this CPU to the one of that CPU.
134    *
135    * Used to reset a VCPU when its VM migrates to another host, so it only resets the fields that should be in this
136    *case.
137    **/
138   virtual void reset_vcpu(Cpu* that);
139
140 public:
141   /** @brief Get the available speed ratio, between 0 and 1.
142    *
143    * This accounts for external load (see @ref set_speed_trace()).
144    */
145   virtual double get_speed_ratio();
146
147   /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
148   virtual double get_pstate_peak_speed(int pstate_index) const;
149
150   virtual int get_pstate_count() const;
151   virtual void set_pstate(int pstate_index);
152   virtual int get_pstate() const;
153
154   s4u::Host* get_host() { return host_; }
155
156   /*< @brief Setup the trace file with availability events (peak speed changes due to external load).
157    * Trace must contain relative values (ratio between 0 and 1)
158    */
159   virtual void set_speed_profile(profile::Profile* profile);
160
161 protected:
162   Metric speed_                  = {1.0, 0, nullptr};
163 };
164
165 /**********
166  * Action *
167  **********/
168
169  /** @ingroup SURF_cpu_interface
170  * @brief A CpuAction represents the execution of code on one or several Cpus
171  */
172 class XBT_PUBLIC CpuAction : public Action {
173 public:
174   /** @brief Signal emitted when the action state changes (ready/running/done, etc)
175    *  Signature: `void(CpuAction const& action, simgrid::kernel::resource::Action::State previous)`
176    */
177   static xbt::signal<void(CpuAction const&, Action::State)> on_state_change;
178
179   CpuAction(Model* model, double cost, bool failed) : Action(model, cost, failed) {}
180   CpuAction(Model* model, double cost, bool failed, lmm::Variable* var) : Action(model, cost, failed, var) {}
181
182   void set_state(Action::State state) override;
183
184   void update_remains_lazy(double now) override;
185   std::list<Cpu*> cpus() const;
186
187   void suspend() override;
188   void resume() override;
189 };
190 } // namespace resource
191 } // namespace kernel
192 } // namespace simgrid
193
194 #endif /* SURF_CPU_INTERFACE_HPP_ */