1 /* Copyright (c) 2004-2018. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SURF_CPU_INTERFACE_HPP_
7 #define SURF_CPU_INTERFACE_HPP_
9 #include "simgrid/kernel/resource/Model.hpp"
10 #include "simgrid/kernel/resource/Resource.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "src/kernel/lmm/maxmin.hpp"
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
27 class XBT_PUBLIC CpuModel : public kernel::resource::Model {
29 explicit CpuModel(kernel::resource::Model::UpdateAlgo algo) : Model(algo) {}
34 * @param host The host that will have this CPU
35 * @param speedPerPstate Processor speed (in Flops) of each pstate. This ignores any potential external load coming from a trace.
36 * @param core The number of core of this Cpu
38 virtual Cpu *createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)=0;
40 void update_actions_state_lazy(double now, double delta) override;
41 void update_actions_state_full(double now, double delta) override;
48 /** @ingroup SURF_cpu_interface
49 * @brief SURF cpu resource interface class
50 * @details A Cpu represent a cpu associated to a host
52 class XBT_PUBLIC Cpu : public simgrid::kernel::resource::Resource {
55 * @brief Cpu constructor
57 * @param model The CpuModel associated to this Cpu
58 * @param host The host in which this Cpu should be plugged
59 * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
60 * @param speedPerPstate Processor speed (in flop per second) for each pstate
61 * @param core The number of core of this Cpu
63 Cpu(simgrid::kernel::resource::Model * model, simgrid::s4u::Host * host, kernel::lmm::Constraint * constraint,
64 std::vector<double> * speedPerPstate, int core);
67 * @brief Cpu constructor
69 * @param model The CpuModel associated to this Cpu
70 * @param host The host in which this Cpu should be plugged
71 * @param speedPerPstate Processor speed (in flop per second) for each pstate
72 * @param core The number of core of this Cpu
74 Cpu(simgrid::kernel::resource::Model * model, simgrid::s4u::Host * host, std::vector<double> * speedPerPstate,
80 * @brief Execute some quantity of computation
82 * @param size The value of the processing amount (in flop) needed to process
83 * @return The CpuAction corresponding to the processing
85 virtual simgrid::kernel::resource::Action* execution_start(double size) = 0;
88 * @brief Execute some quantity of computation on more than one core
90 * @param size The value of the processing amount (in flop) needed to process
91 * @param requestedCores The desired amount of cores. Must be >= 1
92 * @return The CpuAction corresponding to the processing
94 virtual simgrid::kernel::resource::Action* execution_start(double size, int requestedCores) = 0;
97 * @brief Make a process sleep for duration (in seconds)
99 * @param duration The number of seconds to sleep
100 * @return The CpuAction corresponding to the sleeping
102 virtual simgrid::kernel::resource::Action* sleep(double duration) = 0;
104 /** @brief Get the amount of cores */
105 virtual int coreCount();
107 /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
108 virtual double getSpeed(double load);
111 /** @brief Take speed changes (either load or max) into account */
112 virtual void onSpeedChange();
115 /** @brief Get the available speed of the current Cpu */
116 virtual double get_available_speed();
118 /** @brief Get the current Cpu computational speed */
119 virtual double getPstateSpeed(int pstate_index);
121 virtual int getNbPStates();
122 virtual void setPState(int pstate_index);
123 virtual int getPState();
125 simgrid::s4u::Host* getHost() { return host_; }
127 int coresAmount_ = 1;
128 simgrid::s4u::Host* host_;
130 std::vector<double> speedPerPstate_; /*< List of supported CPU capacities (pstate related) */
131 int pstate_ = 0; /*< Current pstate (index in the speedPeakList)*/
133 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). */
135 set_speed_trace(tmgr_trace_t trace); /*< setup the trace file with availability events (peak speed changes due to
136 external load). Trace must contain relative values (ratio between 0 and 1) */
138 tmgr_trace_event_t stateEvent_ = nullptr;
139 Metric speed_ = {1.0, 0, nullptr};
146 /** @ingroup SURF_cpu_interface
147 * @brief A CpuAction represents the execution of code on one or several Cpus
149 class XBT_PUBLIC CpuAction : public simgrid::kernel::resource::Action {
150 friend XBT_PUBLIC Cpu* getActionCpu(CpuAction * action);
153 /** @brief Signal emitted when the action state changes (ready/running/done, etc)
154 * Signature: `void(CpuAction *action, simgrid::kernel::resource::Action::State previous)`
156 static simgrid::xbt::signal<void(simgrid::surf::CpuAction*, simgrid::kernel::resource::Action::State)> onStateChange;
157 /** @brief Signal emitted when the action share changes (amount of flops it gets)
158 * Signature: `void(CpuAction *action)`
160 static simgrid::xbt::signal<void(simgrid::surf::CpuAction*)> onShareChange;
162 CpuAction(simgrid::kernel::resource::Model * model, double cost, bool failed) : Action(model, cost, failed) {}
163 CpuAction(simgrid::kernel::resource::Model * model, double cost, bool failed, kernel::lmm::Variable* var)
164 : Action(model, cost, failed, var)
168 void set_state(simgrid::kernel::resource::Action::State state) override;
170 void update_remains_lazy(double now) override;
171 std::list<Cpu*> cpus();
173 void suspend() override;
174 void resume() override;
180 #endif /* SURF_CPU_INTERFACE_HPP_ */