Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5e9e7a024690694866b1283a2cd2999e385cd1b3
[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(std::list<Cpu*>) getActionCpus(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 host The host that will have this CPU
46    * @param speedPerPstate Processor speed (in Flops) of each pstate. This ignores any potential external load coming from a trace.
47    * @param core The number of core of this Cpu
48    */
49   virtual Cpu *createCpu(simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core)=0;
50
51   void updateActionsStateLazy(double now, double delta);
52   void updateActionsStateFull(double now, double delta);
53   bool next_occuring_event_isIdempotent() {return true;}
54 };
55
56 /************
57  * Resource *
58  ************/
59
60 /** @ingroup SURF_cpu_interface
61 * @brief SURF cpu resource interface class
62 * @details A Cpu represent a cpu associated to a host
63 */
64 XBT_PUBLIC_CLASS Cpu : public simgrid::surf::Resource {
65 public:
66   /**
67    * @brief Cpu constructor
68    *
69    * @param model The CpuModel associated to this Cpu
70    * @param host The host in which this Cpu should be plugged
71    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
72    * @param speedPerPstate Processor speed (in flop per second) for each pstate
73    * @param core The number of core of this Cpu
74    */
75   Cpu(simgrid::surf::Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint, xbt_dynar_t speedPerPstate, 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(simgrid::surf::Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core);
86
87   ~Cpu();
88
89   /**
90    * @brief Execute some quantity of computation
91    *
92    * @param size The value of the processing amount (in flop) needed to process
93    * @return The CpuAction corresponding to the processing
94    */
95   virtual simgrid::surf::Action *execution_start(double size)=0;
96
97   /**
98    * @brief Make a process sleep for duration (in seconds)
99    *
100    * @param duration The number of seconds to sleep
101    * @return The CpuAction corresponding to the sleeping
102    */
103   virtual simgrid::surf::Action *sleep(double duration)=0;
104
105   /** @brief Get the amount of cores */
106   virtual int getCore();
107
108   /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
109   virtual double getSpeed(double load);
110
111 protected:
112   /** @brief Take speed changes (either load or max) into account */
113   virtual void onSpeedChange();
114
115 public:
116   /** @brief Get the available speed of the current Cpu */
117   virtual double getAvailableSpeed();
118
119   /** @brief Get the current Cpu power peak */
120   virtual double getCurrentPowerPeak();
121
122   virtual double getPowerPeakAt(int pstate_index);
123
124   virtual int getNbPStates();
125   virtual void setPState(int pstate_index);
126   virtual int  getPState();
127
128   simgrid::s4u::Host* getHost() { return host_; }
129
130 public:
131   int coresAmount_ = 1;
132   simgrid::s4u::Host* host_;
133
134   xbt_dynar_t speedPerPstate_ = NULL; /*< List of supported CPU capacities (pstate related) */
135   int pstate_ = 0;                   /*< Current pstate (index in the speedPeakList)*/
136
137   /* Note (hypervisor): */
138   lmm_constraint_t *p_constraintCore=NULL;
139   void **p_constraintCoreId=NULL;
140
141 public:
142   virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF). Trace must contain boolean values (0 or 1). */
143   virtual void setSpeedTrace(tmgr_trace_t trace); /*< setup the trace file with availability events (peak speed changes due to external load). Trace must contain relative values (ratio between 0 and 1) */
144
145   tmgr_trace_iterator_t stateEvent_ = nullptr;
146   s_surf_metric_t speed_ = {1.0, 0, nullptr};
147 };
148
149 /**********
150  * Action *
151  **********/
152
153  /** @ingroup SURF_cpu_interface
154  * @brief A CpuAction represents the execution of code on one or several Cpus
155  */
156 XBT_PUBLIC_CLASS CpuAction : public simgrid::surf::Action {
157 friend XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
158 public:
159   /** @brief Callbacks handler which emit the callbacks after CpuAction State changed *
160    * @details Callback functions have the following signature: `void(CpuAction *action, e_surf_action_state_t previous)`
161    */
162   static simgrid::xbt::signal<void(simgrid::surf::CpuAction*, e_surf_action_state_t)> onStateChange;
163
164   CpuAction(simgrid::surf::Model *model, double cost, bool failed)
165   : Action(model, cost, failed) {} //FIXME:DEADCODE?
166   CpuAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
167   : Action(model, cost, failed, var) {}
168
169   /** @brief Set the affinity of the current CpuAction */
170   virtual void setAffinity(Cpu *cpu, unsigned long mask);
171
172   void setState(e_surf_action_state_t state);
173
174   void updateRemainingLazy(double now);
175 };
176
177 }
178 }
179
180 #endif /* SURF_CPU_INTERFACE_HPP_ */