Logo AND Algorithmique Numérique Distribuée

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