Logo AND Algorithmique Numérique Distribuée

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