Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e5a7ce7b84aaa5a0dec04fb3fb39c8f81ed2ef4
[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
17 namespace simgrid {
18 namespace surf {
19
20 class CpuModel;
21 class Cpu;
22 class CpuAction;
23 class CpuPlugin;
24
25 /*************
26  * Callbacks *
27  *************/
28 XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
29
30 /** @ingroup SURF_callbacks
31  * @brief Callbacks handler which emit the callbacks after Cpu creation *
32  * @details Callback functions have the following signature: `void(CpuPtr)`
33  */
34 XBT_PUBLIC_DATA( surf_callback(void, Cpu*)) cpuCreatedCallbacks;
35
36 /** @ingroup SURF_callbacks
37  * @brief Callbacks handler which emit the callbacks after Cpu destruction *
38  * @details Callback functions have the following signature: `void(CpuPtr)`
39  */
40 XBT_PUBLIC_DATA( surf_callback(void, Cpu*)) cpuDestructedCallbacks;
41
42 /** @ingroup SURF_callbacks
43  * @brief Callbacks handler which emit the callbacks after Cpu State changed *
44  * @details Callback functions have the following signature: `void(CpuAction *action, e_surf_resource_state_t old, e_surf_resource_state_t current)`
45  */
46 XBT_PUBLIC_DATA( surf_callback(void, Cpu*, e_surf_resource_state_t, e_surf_resource_state_t)) cpuStateChangedCallbacks;
47
48 /** @ingroup SURF_callbacks
49  * @brief Callbacks handler which emit the callbacks after CpuAction State changed *
50  * @details Callback functions have the following signature: `void(CpuAction *action, e_surf_action_state_t old, e_surf_action_state_t current)`
51  */
52 XBT_PUBLIC_DATA( surf_callback(void, CpuAction*, e_surf_action_state_t, e_surf_action_state_t)) cpuActionStateChangedCallbacks;
53
54 XBT_PUBLIC(void) cpu_add_traces();
55
56 /*********
57  * Model *
58  *********/
59
60  /** @ingroup SURF_cpu_interface
61  * @brief SURF cpu model interface class
62  * @details A model is an object which handle the interactions between its Resources and its Actions
63  */
64 XBT_PUBLIC_CLASS CpuModel : public Model {
65 public:
66   CpuModel() : Model() {};
67
68   /**
69    * @brief Create a Cpu
70    *
71    * @param name The name of the Cpu
72    * @param speedPeak The peak spead (max speed in Flops)
73    * @param pstate [TODO]
74    * @param speedScale The speed scale (in [O;1] available speed from peak)
75    * @param speedTrace Trace variations
76    * @param core The number of core of this Cpu
77    * @param state_initial [TODO]
78    * @param state_trace [TODO]
79    */
80   virtual Cpu *createCpu(const char *name, xbt_dynar_t speedPeak,
81                       int pstate, double speedScale,
82                           tmgr_trace_t speedTrace, int core,
83                           e_surf_resource_state_t state_initial,
84                           tmgr_trace_t state_trace)=0;
85
86   void updateActionsStateLazy(double now, double delta);
87   void updateActionsStateFull(double now, double delta);
88   bool shareResourcesIsIdempotent() {return true;}
89 };
90
91 /************
92  * Resource *
93  ************/
94
95 /** @ingroup SURF_cpu_interface
96 * @brief SURF cpu resource interface class
97 * @details A Cpu represent a cpu associated to a host
98 */
99 XBT_PUBLIC_CLASS Cpu : public simgrid::surf::Resource {
100 public:
101   static simgrid::xbt::FacetLevel<simgrid::Host, Cpu> LEVEL;
102   static void init();
103   Cpu();
104
105   /**
106    * @brief Cpu constructor
107    *
108    * @param model The CpuModel associated to this Cpu
109    * @param name The name of the Cpu
110    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
111    * @param core The number of core of this Cpu
112    * @param speedPeak The speed peak of this Cpu in flops (max speed)
113    * @param speedScale The speed scale of this Cpu in [0;1] (available amount)
114    * @param stateInitial whether it is created running or crashed
115    */
116   Cpu(simgrid::surf::Model *model, const char *name,
117           lmm_constraint_t constraint, int core, double speedPeak, double speedScale,
118           e_surf_resource_state_t stateInitial);
119
120   /**
121    * @brief Cpu constructor
122    *
123    * @param model The CpuModel associated to this Cpu
124    * @param name The name of the Cpu
125    * @param core The number of core of this Cpu
126    * @param speedPeak The speed peak of this Cpu in flops (max speed)
127    * @param speedScale The speed scale of this Cpu in [0;1] (available amount)
128    * @param stateInitial whether it is created running or crashed
129    */
130   Cpu(simgrid::surf::Model *model, const char *name,
131           int core, double speedPeak, double speedScale,
132           e_surf_resource_state_t stateInitial);
133
134   Cpu(simgrid::surf::Model *model, const char *name,
135           lmm_constraint_t constraint, int core, double speedPeak, double speedScale);
136   Cpu(simgrid::surf::Model *model, const char *name,
137           int core, double speedPeak, double speedScale);
138
139   ~Cpu();
140
141   /**
142    * @brief Execute some quantity of computation
143    *
144    * @param size The value of the processing amount (in flop) needed to process
145    * @return The CpuAction corresponding to the processing
146    */
147   virtual simgrid::surf::Action *execute(double size)=0;
148
149   /**
150    * @brief Make a process sleep for duration (in seconds)
151    *
152    * @param duration The number of seconds to sleep
153    * @return The CpuAction corresponding to the sleeping
154    */
155   virtual simgrid::surf::Action *sleep(double duration)=0;
156
157   /** @brief Get the amount of cores */
158   virtual int getCore();
159
160   /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
161   virtual double getSpeed(double load);
162
163   /** @brief Get the available speed of the current Cpu */
164   virtual double getAvailableSpeed();
165
166   /** @brief Get the current Cpu power peak */
167   virtual double getCurrentPowerPeak();
168
169   virtual double getPowerPeakAt(int pstate_index)=0;
170
171   virtual int getNbPstates()=0;
172   virtual void setPstate(int pstate_index)=0;
173   virtual int  getPstate()=0;
174
175   void setState(e_surf_resource_state_t state);
176   void plug(simgrid::Host* host);
177
178   void addTraces(void);
179   int m_core = 1;                /* Amount of cores */
180   double m_speedPeak;            /*< CPU speed peak, ie max value */
181   double m_speedScale;           /*< Percentage of CPU available according to the trace, in [O,1] */
182   simgrid::Host* m_host = nullptr;
183
184   simgrid::Host* getHost() { return m_host; }
185
186   /* Note (hypervisor): */
187   lmm_constraint_t *p_constraintCore=NULL;
188   void **p_constraintCoreId=NULL;
189
190 };
191
192 /**********
193  * Action *
194  **********/
195
196  /** @ingroup SURF_cpu_interface
197  * @brief SURF Cpu action interface class
198  * @details A CpuAction represent the execution of code on a Cpu
199  */
200 XBT_PUBLIC_CLASS CpuAction : public simgrid::surf::Action {
201 friend XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
202 public:
203   /** @brief CpuAction constructor */
204   CpuAction(simgrid::surf::Model *model, double cost, bool failed)
205     : Action(model, cost, failed) {} //FIXME:REMOVE
206
207   /** @brief CpuAction constructor */
208   CpuAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
209     : Action(model, cost, failed, var) {}
210
211   /**
212    * @brief Set the affinity of the current CpuAction
213    * @details [TODO]
214    */
215   virtual void setAffinity(Cpu *cpu, unsigned long mask);
216
217   void setState(e_surf_action_state_t state);
218
219   void updateRemainingLazy(double now);
220
221 };
222
223 }
224 }
225
226 #endif /* SURF_CPU_INTERFACE_HPP_ */