Logo AND Algorithmique Numérique Distribuée

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