Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / surf / cpu_interface.hpp
1 /* Copyright (c) 2004-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf_interface.hpp"
8 #include "maxmin_private.hpp"
9
10 #ifndef SURF_CPU_INTERFACE_HPP_
11 #define SURF_CPU_INTERFACE_HPP_
12
13 /***********
14  * Classes *
15  ***********/
16 class CpuModel;
17 class Cpu;
18 class CpuAction;
19 class CpuPlugin;
20
21 /*************
22  * Callbacks *
23  *************/
24 XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
25
26 /** @ingroup SURF_callbacks
27  * @brief Callbacks handler which emit the callbacks after Cpu creation *
28  * @details Callback functions have the following signature: `void(CpuPtr)`
29  */
30 XBT_PUBLIC_DATA( surf_callback(void, Cpu*)) cpuCreatedCallbacks;
31
32 /** @ingroup SURF_callbacks
33  * @brief Callbacks handler which emit the callbacks after Cpu destruction *
34  * @details Callback functions have the following signature: `void(CpuPtr)`
35  */
36 XBT_PUBLIC_DATA( surf_callback(void, Cpu*)) cpuDestructedCallbacks;
37
38 /** @ingroup SURF_callbacks
39  * @brief Callbacks handler which emit the callbacks after Cpu State changed *
40  * @details Callback functions have the following signature: `void(CpuAction *action, e_surf_resource_state_t old, e_surf_resource_state_t current)`
41  */
42 XBT_PUBLIC_DATA( surf_callback(void, Cpu*, e_surf_resource_state_t, e_surf_resource_state_t)) cpuStateChangedCallbacks;
43
44 /** @ingroup SURF_callbacks
45  * @brief Callbacks handler which emit the callbacks after CpuAction State changed *
46  * @details Callback functions have the following signature: `void(CpuAction *action, e_surf_action_state_t old, e_surf_action_state_t current)`
47  */
48 XBT_PUBLIC_DATA( surf_callback(void, CpuAction*, e_surf_action_state_t, e_surf_action_state_t)) cpuActionStateChangedCallbacks;
49
50 XBT_PUBLIC(void) cpu_parse_init(sg_platf_host_cbarg_t host);
51
52 XBT_PUBLIC(void) cpu_add_traces();
53
54 /*********
55  * Model *
56  *********/
57
58  /** @ingroup SURF_cpu_interface
59  * @brief SURF cpu model interface class
60  * @details A model is an object which handle the interactions between its Resources and its Actions
61  */
62 XBT_PUBLIC_CLASS CpuModel : public Model {
63 public:
64   /** @brief Constructor */
65   CpuModel() : Model() {};
66
67   /**
68    * @brief Create a Cpu
69    *
70    * @param name The name of the Cpu
71    * @param power_peak The power peak of this Cpu
72    * @param pstate [TODO]
73    * @param power_scale The power scale of this Cpu
74    * @param power_trace [TODO]
75    * @param core The number of core of this Cpu
76    * @param state_initial [TODO]
77    * @param state_trace [TODO]
78    * @param cpu_properties Dictionary of properties associated to this Cpu
79    */
80   virtual Cpu *createCpu(const char *name, xbt_dynar_t power_peak,
81                       int pstate, double power_scale,
82                           tmgr_trace_t power_trace, int core,
83                           e_surf_resource_state_t state_initial,
84                           tmgr_trace_t state_trace,
85                           xbt_dict_t cpu_properties)=0;
86
87   void updateActionsStateLazy(double now, double delta);
88   void updateActionsStateFull(double now, double delta);
89   bool shareResourcesIsIdempotent() {return true;}
90 };
91
92 /************
93  * Resource *
94  ************/
95
96 /** @ingroup SURF_cpu_interface
97 * @brief SURF cpu resource interface class
98 * @details A Cpu represent a cpu associated to a host
99 */
100 XBT_PUBLIC_CLASS Cpu : public Resource {
101 public:
102   /**
103    * @brief Cpu constructor
104    */
105   Cpu();
106
107   /**
108    * @brief Cpu constructor
109    *
110    * @param model The CpuModel associated to this Cpu
111    * @param name The name of the Cpu
112    * @param props Dictionary of properties associated to this Cpu
113    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
114    * @param core The number of core of this Cpu
115    * @param powerPeak The power peak of this Cpu
116    * @param powerScale The power scale of this Cpu
117    */
118   Cpu(Model *model, const char *name, xbt_dict_t props,
119           lmm_constraint_t constraint, int core, double powerPeak, double powerScale);
120
121   /**
122    * @brief Cpu constructor
123    *
124    * @param model The CpuModel associated to this Cpu
125    * @param name The name of the Cpu
126    * @param props Dictionary of properties associated to this Cpu
127    * @param core The number of core of this Cpu
128    * @param powerPeak The power peak of this Cpu in [TODO]
129    * @param powerScale The power scale of this Cpu in [TODO]
130    */
131   Cpu(Model *model, const char *name, xbt_dict_t props,
132           int core, double powerPeak, double powerScale);
133
134   /**
135    * @brief Cpu destructor
136    */
137   ~Cpu();
138
139   /**
140    * @brief Execute some quantity of computation
141    *
142    * @param size The value of the processing amount (in flop) needed to process
143    * @return The CpuAction corresponding to the processing
144    */
145   virtual CpuAction *execute(double size)=0;
146
147   /**
148    * @brief Make a process sleep for duration (in seconds)
149    *
150    * @param duration The number of seconds to sleep
151    * @return The CpuAction corresponding to the sleeping
152    */
153   virtual CpuAction *sleep(double duration)=0;
154
155   /**
156    * @brief Get the number of cores of the current Cpu
157    *
158    * @return The number of cores of the current Cpu
159    */
160   virtual int getCore();
161
162   /**
163    * @brief Get the speed of the current Cpu
164    * @details [TODO] load * m_powerPeak
165    *
166    * @param load [TODO]
167    *
168    * @return The speed of the current Cpu
169    */
170   virtual double getSpeed(double load);
171
172   /**
173    * @brief Get the available speed of the current Cpu
174    * @details [TODO]
175    *
176    * @return The available speed of the current Cpu
177    */
178   virtual double getAvailableSpeed();
179
180   /**
181    * @brief Get the current Cpu power peak
182    *
183    * @return The current Cpu power peak
184    */
185   virtual double getCurrentPowerPeak();
186
187
188   virtual double getPowerPeakAt(int pstate_index)=0;
189
190   virtual int getNbPstates()=0;
191
192   virtual void setPstate(int pstate_index)=0;
193   virtual int  getPstate()=0;
194
195   void setState(e_surf_resource_state_t state);
196
197   void addTraces(void);
198   int m_core;
199   double m_powerPeak;            /*< CPU power peak */
200   double m_powerScale;           /*< Percentage of CPU disponible */
201
202   /* Note (hypervisor): */
203   lmm_constraint_t *p_constraintCore;
204   void **p_constraintCoreId;
205
206   // ////
207   // Adrien - Oct 2015. Additional code related to VM energy extensions
208   /* TODO: 
209      1./ create a VirtualCpuModel that inherits from CpuModel or create
210      a VirtualCpuCas01Model that inherist from CpuCas01Model
211           => Question: not sure which one is really usefull :( The objective is to
212      be able to create a VirtualCpu instead of a Cpu (i.e. a CpuCas01 to be exact). 
213      2. create VirtualCpu class that inherits from Cpu or create
214      a VirtualCpuCas01 class that inherits from CpuCas01 (same issue as the one described above).       
215           Such modifications would enable us to remove the isVirtual method and
216      actually use the polymorphism feature of C++. It will also enable us 
217      to overwrite the cpu_energy hashmap in energyCpuCreateCallBack instead of invoking
218      setVirtual(). 
219      So to make a long story short, it will be cleaner from the software viewpoint 
220      and patches welcome ;) - Adrien October 20 2015 
221   */
222   Cpu *physCpu = NULL;
223
224         /**
225          * @brief Set the current Cpu as virtual or physical
226          *
227          * @param isVirtual true to indicate this Cpu is virtual, false otherwise
228          */
229         void setVirtual(Cpu *physCpu);
230
231         /**
232          * @brief Learn if this Cpu is physical or virtual
233          *
234          * @return phys CPU  if the current Cpu is a VM, null otherwise
235          */
236         Cpu* isVirtual(void);
237    
238    // Adrien - Oct 2015. End of additional code related to VM energy extensions
239    // ////
240 };
241
242 /**********
243  * Action *
244  **********/
245
246  /** @ingroup SURF_cpu_interface
247  * @brief SURF Cpu action interface class
248  * @details A CpuAction represent the execution of code on a Cpu
249  */
250 XBT_PUBLIC_CLASS CpuAction : public Action {
251 friend XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
252 public:
253   /**
254    * @brief CpuAction constructor
255    *
256    * @param model The CpuModel associated to this CpuAction
257    * @param cost [TODO]
258    * @param failed [TODO]
259    */
260   CpuAction(Model *model, double cost, bool failed)
261     : Action(model, cost, failed) {} //FIXME:REMOVE
262
263   /**
264    * @brief CpuAction constructor
265    *
266    * @param model The CpuModel associated to this CpuAction
267    * @param cost [TODO]
268    * @param failed [TODO]
269    * @param var The lmm variable associated to this CpuAction if it is part of a LMM component
270    */
271   CpuAction(Model *model, double cost, bool failed, lmm_variable_t var)
272     : Action(model, cost, failed, var) {}
273
274   /**
275    * @brief Set the affinity of the current CpuAction
276    * @details [TODO]
277    *
278    * @param cpu [TODO]
279    * @param mask [TODO]
280    */
281   virtual void setAffinity(Cpu *cpu, unsigned long mask);
282
283   void setState(e_surf_action_state_t state);
284
285   void updateRemainingLazy(double now);
286
287 };
288
289 #endif /* SURF_CPU_INTERFACE_HPP_ */