Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c1379c0016768acb177ce741c5d1f2de3f84c423
[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/signal.hpp>
11
12 #include <simgrid/forward.h>
13 #include <simgrid/s4u/host.hpp>
14
15 #include "surf/datatypes.h"
16 #include "surf_interface.hpp"
17 #include "maxmin_private.hpp"
18 #include "trace_mgr.hpp"
19
20 #ifndef SURF_CPU_INTERFACE_HPP_
21 #define SURF_CPU_INTERFACE_HPP_
22
23 /***********
24  * Classes *
25  ***********/
26
27 namespace simgrid {
28 namespace surf {
29
30 class CpuModel;
31 class Cpu;
32 class CpuAction;
33 class CpuPlugin;// FIXME:DEADCODE
34
35  /** @ingroup SURF_cpu_interface
36  * @brief SURF cpu model interface class
37  * @details A model is an object which handle the interactions between its Resources and its Actions
38  */
39 XBT_PUBLIC_CLASS CpuModel : public Model {
40 public:
41   CpuModel() : Model() {};
42   ~CpuModel() override;
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, std::vector<double> *, int core)=0;
52
53   void updateActionsStateLazy(double now, double delta) override;
54   void updateActionsStateFull(double now, double delta) override;
55   bool next_occuring_event_isIdempotent() override;
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,
78       std::vector<double> *speedPerPstate, int core);
79
80   /**
81    * @brief Cpu constructor
82    *
83    * @param model The CpuModel associated to this Cpu
84    * @param host The host in which this Cpu should be plugged
85    * @param speedPerPstate Processor speed (in flop per second) for each pstate
86    * @param core The number of core of this Cpu
87    */
88   Cpu(simgrid::surf::Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core);
89
90   ~Cpu();
91
92   /**
93    * @brief Execute some quantity of computation
94    *
95    * @param size The value of the processing amount (in flop) needed to process
96    * @return The CpuAction corresponding to the processing
97    */
98   virtual simgrid::surf::Action *execution_start(double size)=0;
99
100   /**
101    * @brief Make a process sleep for duration (in seconds)
102    *
103    * @param duration The number of seconds to sleep
104    * @return The CpuAction corresponding to the sleeping
105    */
106   virtual simgrid::surf::Action *sleep(double duration)=0;
107
108   /** @brief Get the amount of cores */
109   virtual int getCore();
110
111   /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
112   virtual double getSpeed(double load);
113
114 protected:
115   /** @brief Take speed changes (either load or max) into account */
116   virtual void onSpeedChange();
117
118 public:
119   /** @brief Get the available speed of the current Cpu */
120   virtual double getAvailableSpeed();
121
122   /** @brief Get the current Cpu computational speed */
123   virtual double getPstateSpeedCurrent();
124   virtual double getPstateSpeed(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   std::vector<double> *speedPerPstate_ = nullptr; /*< List of supported CPU capacities (pstate related) */
137   int pstate_ = 0;                   /*< Current pstate (index in the speedPeakList)*/
138
139 public:
140   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). */
141   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) */
142
143   tmgr_trace_iterator_t stateEvent_ = nullptr;
144   s_surf_metric_t speed_ = {1.0, 0, nullptr};
145 };
146
147 /**********
148  * Action *
149  **********/
150
151  /** @ingroup SURF_cpu_interface
152  * @brief A CpuAction represents the execution of code on one or several Cpus
153  */
154 XBT_PUBLIC_CLASS CpuAction : public simgrid::surf::Action {
155 friend XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
156 public:
157   /** @brief Callbacks handler which emit the callbacks after CpuAction State changed *
158    * @details Callback functions have the following signature: `void(CpuAction *action, simgrid::surf::Action::State previous)`
159    */
160   static simgrid::xbt::signal<void(simgrid::surf::CpuAction*, simgrid::surf::Action::State)> onStateChange;
161
162   CpuAction(simgrid::surf::Model *model, double cost, bool failed)
163   : Action(model, cost, failed) {} //FIXME:DEADCODE?
164   CpuAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
165   : Action(model, cost, failed, var) {}
166
167   void setState(simgrid::surf::Action::State state) override;
168
169   void updateRemainingLazy(double now) override;
170   std::list<Cpu*> cpus();
171 };
172
173 }
174 }
175
176 #endif /* SURF_CPU_INTERFACE_HPP_ */