Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5823a71414b9febdd95b48dd9c65626c24c11e5b
[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
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   ~CpuModel() override;
42
43   /**
44    * @brief Create a Cpu
45    *
46    * @param host The host that will have this CPU
47    * @param speedPerPstate Processor speed (in Flops) of each pstate. This ignores any potential external load coming from a trace.
48    * @param core The number of core of this Cpu
49    */
50   virtual Cpu *createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)=0;
51
52   void updateActionsStateLazy(double now, double delta) override;
53   void updateActionsStateFull(double now, double delta) override;
54   bool next_occuring_event_isIdempotent() override;
55 };
56
57 /************
58  * Resource *
59  ************/
60
61 /** @ingroup SURF_cpu_interface
62 * @brief SURF cpu resource interface class
63 * @details A Cpu represent a cpu associated to a host
64 */
65 XBT_PUBLIC_CLASS Cpu : public simgrid::surf::Resource {
66 public:
67   /**
68    * @brief Cpu constructor
69    *
70    * @param model The CpuModel associated to this Cpu
71    * @param host The host in which this Cpu should be plugged
72    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
73    * @param speedPerPstate Processor speed (in flop per second) for each pstate
74    * @param core The number of core of this Cpu
75    */
76   Cpu(simgrid::surf::Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint,
77       std::vector<double> *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, std::vector<double> *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 getCoreCount();
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 computational speed */
122   virtual double getPstateSpeedCurrent();
123   virtual double getPstateSpeed(int pstate_index);
124
125   virtual int getNbPStates();
126   virtual void setPState(int pstate_index);
127   virtual int  getPState();
128
129   simgrid::s4u::Host* getHost() { return host_; }
130
131 public:
132   int coresAmount_ = 1;
133   simgrid::s4u::Host* host_;
134
135   std::vector<double> speedPerPstate_; /*< List of supported CPU capacities (pstate related) */
136   int pstate_ = 0;                     /*< Current pstate (index in the speedPeakList)*/
137
138 public:
139   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). */
140   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) */
141
142   tmgr_trace_iterator_t stateEvent_ = nullptr;
143   s_surf_metric_t speed_ = {1.0, 0, nullptr};
144 };
145
146 /**********
147  * Action *
148  **********/
149
150  /** @ingroup SURF_cpu_interface
151  * @brief A CpuAction represents the execution of code on one or several Cpus
152  */
153 XBT_PUBLIC_CLASS CpuAction : public simgrid::surf::Action {
154 friend XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
155 public:
156 /** @brief Signal emitted when the action state changes (ready/running/done, etc)
157  *  Signature: `void(CpuAction *action, simgrid::surf::Action::State previous)`
158  */
159 static simgrid::xbt::signal<void(simgrid::surf::CpuAction*, simgrid::surf::Action::State)> onStateChange;
160 /** @brief Signal emitted when the action share changes (amount of flops it gets)
161  *  Signature: `void(CpuAction *action)`
162  */
163 static simgrid::xbt::signal<void(simgrid::surf::CpuAction*)> onShareChange;
164
165   CpuAction(simgrid::surf::Model *model, double cost, bool failed)
166   : Action(model, cost, failed) {}
167   CpuAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
168   : Action(model, cost, failed, var) {}
169
170   void setState(simgrid::surf::Action::State state) override;
171
172   void updateRemainingLazy(double now) override;
173   std::list<Cpu*> cpus();
174 };
175
176 }
177 }
178
179 #endif /* SURF_CPU_INTERFACE_HPP_ */