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"
13 #include "src/surf/trace_mgr.hpp"
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
28 class XBT_PUBLIC CpuModel : public kernel::resource::Model {
30 explicit CpuModel(kernel::resource::Model::UpdateAlgo algo) : Model(algo) {}
35 * @param host The host that will have this CPU
36 * @param speedPerPstate Processor speed (in Flops) of each pstate. This ignores any potential external load coming from a trace.
37 * @param core The number of core of this Cpu
39 virtual Cpu *createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)=0;
41 void update_actions_state_lazy(double now, double delta) override;
42 void update_actions_state_full(double now, double delta) override;
49 /** @ingroup SURF_cpu_interface
50 * @brief SURF cpu resource interface class
51 * @details A Cpu represent a cpu associated to a host
53 class XBT_PUBLIC Cpu : public simgrid::kernel::resource::Resource {
56 * @brief Cpu constructor
58 * @param model The CpuModel associated to this Cpu
59 * @param host The host in which this Cpu should be plugged
60 * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
61 * @param speedPerPstate Processor speed (in flop per second) for each pstate
62 * @param core The number of core of this Cpu
64 Cpu(simgrid::kernel::resource::Model * model, simgrid::s4u::Host * host, kernel::lmm::Constraint * constraint,
65 std::vector<double> * speedPerPstate, int core);
68 * @brief Cpu constructor
70 * @param model The CpuModel associated to this Cpu
71 * @param host The host in which this Cpu should be plugged
72 * @param speedPerPstate Processor speed (in flop per second) for each pstate
73 * @param core The number of core of this Cpu
75 Cpu(simgrid::kernel::resource::Model * model, simgrid::s4u::Host * host, std::vector<double> * speedPerPstate,
81 * @brief Execute some quantity of computation
83 * @param size The value of the processing amount (in flop) needed to process
84 * @return The CpuAction corresponding to the processing
86 virtual simgrid::kernel::resource::Action* execution_start(double size) = 0;
89 * @brief Execute some quantity of computation on more than one core
91 * @param size The value of the processing amount (in flop) needed to process
92 * @param requestedCores The desired amount of cores. Must be >= 1
93 * @return The CpuAction corresponding to the processing
95 virtual simgrid::kernel::resource::Action* execution_start(double size, int requestedCores) = 0;
98 * @brief Make a process sleep for duration (in seconds)
100 * @param duration The number of seconds to sleep
101 * @return The CpuAction corresponding to the sleeping
103 virtual simgrid::kernel::resource::Action* sleep(double duration) = 0;
105 /** @brief Get the amount of cores */
106 virtual int get_cores_count();
108 /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
109 virtual double getSpeed(double load);
112 /** @brief Take speed changes (either load or max) into account */
113 virtual void onSpeedChange();
116 /** @brief Get the available speed of the current Cpu */
117 virtual double get_available_speed();
119 /** @brief Get the current Cpu computational speed */
120 virtual double getPstateSpeed(int pstate_index);
122 virtual int get_pstates_count();
123 virtual void set_pstate(int pstate_index);
124 virtual int get_pstate();
126 simgrid::s4u::Host* get_host() { return host_; }
129 int cores_count_ = 1;
130 simgrid::s4u::Host* host_;
132 int pstate_ = 0; /*< Current pstate (index in the speedPeakList)*/
134 std::vector<double> speed_per_pstate_; /*< List of supported CPU capacities (pstate related) */
137 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). */
139 set_speed_trace(tmgr_trace_t trace); /*< setup the trace file with availability events (peak speed changes due to
140 external load). Trace must contain relative values (ratio between 0 and 1) */
142 tmgr_trace_event_t state_event_ = nullptr;
143 Metric speed_ = {1.0, 0, nullptr};
150 /** @ingroup SURF_cpu_interface
151 * @brief A CpuAction represents the execution of code on one or several Cpus
153 class XBT_PUBLIC CpuAction : public simgrid::kernel::resource::Action {
154 friend XBT_PUBLIC Cpu* getActionCpu(CpuAction * action);
157 /** @brief Signal emitted when the action state changes (ready/running/done, etc)
158 * Signature: `void(CpuAction *action, simgrid::kernel::resource::Action::State previous)`
160 static simgrid::xbt::signal<void(simgrid::surf::CpuAction*, simgrid::kernel::resource::Action::State)> onStateChange;
161 /** @brief Signal emitted when the action share changes (amount of flops it gets)
162 * Signature: `void(CpuAction *action)`
164 static simgrid::xbt::signal<void(simgrid::surf::CpuAction*)> onShareChange;
166 CpuAction(simgrid::kernel::resource::Model * model, double cost, bool failed) : Action(model, cost, failed) {}
167 CpuAction(simgrid::kernel::resource::Model * model, double cost, bool failed, kernel::lmm::Variable* var)
168 : Action(model, cost, failed, var)
172 void set_state(simgrid::kernel::resource::Action::State state) override;
174 void update_remains_lazy(double now) override;
175 std::list<Cpu*> cpus();
177 void suspend() override;
178 void resume() override;
184 #endif /* SURF_CPU_INTERFACE_HPP_ */