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 <list>
8
9 #include <xbt/base.h>
10 #include <xbt/dynar.h>
11 #include <xbt/signal.hpp>
12
13 #include <simgrid/forward.h>
14 #include <simgrid/s4u/host.hpp>
15
16 #include "surf/datatypes.h"
17 #include "surf_interface.hpp"
18 #include "maxmin_private.hpp"
19 #include "trace_mgr.hpp"
20
21 #ifndef SURF_CPU_INTERFACE_HPP_
22 #define SURF_CPU_INTERFACE_HPP_
23
24 /***********
25  * Classes *
26  ***********/
27
28 namespace simgrid {
29 namespace surf {
30
31 class CpuModel;
32 class Cpu;
33 class CpuAction;
34 class CpuPlugin;// FIXME:DEADCODE
35
36  /** @ingroup SURF_cpu_interface
37  * @brief SURF cpu model interface class
38  * @details A model is an object which handle the interactions between its Resources and its Actions
39  */
40 XBT_PUBLIC_CLASS CpuModel : public Model {
41 public:
42   CpuModel() : Model() {};
43
44   /**
45    * @brief Create a Cpu
46    *
47    * @param host The host that will have this CPU
48    * @param speedPerPstate Processor speed (in Flops) of each pstate. This ignores any potential external load coming from a trace.
49    * @param core The number of core of this Cpu
50    */
51   virtual Cpu *createCpu(simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core)=0;
52
53   void updateActionsStateLazy(double now, double delta);
54   void updateActionsStateFull(double now, double delta);
55   bool next_occuring_event_isIdempotent() {return true;}
56 };
57
58 /************
59  * Resource *
60  ************/
61
62 /** @ingroup SURF_cpu_interface
63 * @brief SURF cpu resource interface class
64 * @details A Cpu represent a cpu associated to a host
65 */
66 XBT_PUBLIC_CLASS Cpu : public simgrid::surf::Resource {
67 public:
68   /**
69    * @brief Cpu constructor
70    *
71    * @param model The CpuModel associated to this Cpu
72    * @param host The host in which this Cpu should be plugged
73    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
74    * @param speedPerPstate Processor speed (in flop per second) for each pstate
75    * @param core The number of core of this Cpu
76    */
77   Cpu(simgrid::surf::Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint, xbt_dynar_t speedPerPstate, int core);
78
79   /**
80    * @brief Cpu constructor
81    *
82    * @param model The CpuModel associated to this Cpu
83    * @param host The host in which this Cpu should be plugged
84    * @param speedPerPstate Processor speed (in flop per second) for each pstate
85    * @param core The number of core of this Cpu
86    */
87   Cpu(simgrid::surf::Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core);
88
89   ~Cpu();
90
91   /**
92    * @brief Execute some quantity of computation
93    *
94    * @param size The value of the processing amount (in flop) needed to process
95    * @return The CpuAction corresponding to the processing
96    */
97   virtual simgrid::surf::Action *execution_start(double size)=0;
98
99   /**
100    * @brief Make a process sleep for duration (in seconds)
101    *
102    * @param duration The number of seconds to sleep
103    * @return The CpuAction corresponding to the sleeping
104    */
105   virtual simgrid::surf::Action *sleep(double duration)=0;
106
107   /** @brief Get the amount of cores */
108   virtual int getCore();
109
110   /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
111   virtual double getSpeed(double load);
112
113 protected:
114   /** @brief Take speed changes (either load or max) into account */
115   virtual void onSpeedChange();
116
117 public:
118   /** @brief Get the available speed of the current Cpu */
119   virtual double getAvailableSpeed();
120
121   /** @brief Get the current Cpu power peak */
122   virtual double getCurrentPowerPeak();
123
124   virtual double getPowerPeakAt(int pstate_index);
125
126   virtual int getNbPStates();
127   virtual void setPState(int pstate_index);
128   virtual int  getPState();
129
130   simgrid::s4u::Host* getHost() { return host_; }
131
132 public:
133   int coresAmount_ = 1;
134   simgrid::s4u::Host* host_;
135
136   xbt_dynar_t speedPerPstate_ = NULL; /*< List of supported CPU capacities (pstate related) */
137   int pstate_ = 0;                   /*< Current pstate (index in the speedPeakList)*/
138
139   /* Note (hypervisor): */
140   lmm_constraint_t *p_constraintCore=NULL;
141   void **p_constraintCoreId=NULL;
142
143 public:
144   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). */
145   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) */
146
147   tmgr_trace_iterator_t stateEvent_ = nullptr;
148   s_surf_metric_t speed_ = {1.0, 0, nullptr};
149 };
150
151 /**********
152  * Action *
153  **********/
154
155  /** @ingroup SURF_cpu_interface
156  * @brief A CpuAction represents the execution of code on one or several Cpus
157  */
158 XBT_PUBLIC_CLASS CpuAction : public simgrid::surf::Action {
159 friend XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
160 public:
161   /** @brief Callbacks handler which emit the callbacks after CpuAction State changed *
162    * @details Callback functions have the following signature: `void(CpuAction *action, simgrid::surf::Action::State previous)`
163    */
164   static simgrid::xbt::signal<void(simgrid::surf::CpuAction*, simgrid::surf::Action::State)> onStateChange;
165
166   CpuAction(simgrid::surf::Model *model, double cost, bool failed)
167   : Action(model, cost, failed) {} //FIXME:DEADCODE?
168   CpuAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
169   : Action(model, cost, failed, var) {}
170
171   /** @brief Set the affinity of the current CpuAction */
172   virtual void setAffinity(Cpu *cpu, unsigned long mask);
173
174   void setState(simgrid::surf::Action::State state) override;
175
176   void updateRemainingLazy(double now) override;
177   std::list<Cpu*> cpus();
178 };
179
180 }
181 }
182
183 #endif /* SURF_CPU_INTERFACE_HPP_ */