+Cpu::Cpu(Model *model, simgrid::s4u::Host *host,
+ xbt_dynar_t speedPeakList, int core, double speedPeak)
+ : Cpu(model, host, NULL/*constraint*/, speedPeakList, core, speedPeak)
+{
+}
+
+Cpu::Cpu(Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint,
+ xbt_dynar_t speedPeakList, int core, double speedPeak)
+ : Resource(model, host->name().c_str(), constraint)
+ , m_core(core)
+ , m_host(host)
+{
+ p_speed.peak = speedPeak;
+ p_speed.scale = 1;
+ host->pimpl_cpu = this;
+ xbt_assert(p_speed.scale > 0, "Available speed has to be >0");
+
+ // Copy the power peak array:
+ p_speedPeakList = xbt_dynar_new(sizeof(double), nullptr);
+ unsigned long n = xbt_dynar_length(speedPeakList);
+ for (unsigned long i = 0; i != n; ++i) {
+ double value = xbt_dynar_get_as(speedPeakList, i, double);
+ xbt_dynar_push(p_speedPeakList, &value);
+ }
+
+ /* Currently, we assume that a VM does not have a multicore CPU. */
+ if (core > 1)
+ xbt_assert(model == surf_cpu_model_pm);
+
+ if (model->getUpdateMechanism() != UM_UNDEFINED) {
+ p_constraintCore = xbt_new(lmm_constraint_t, core);
+ p_constraintCoreId = xbt_new(void*, core);
+
+ int i;
+ for (i = 0; i < core; i++) {
+ /* just for a unique id, never used as a string. */
+ p_constraintCoreId[i] = bprintf("%s:%i", host->name().c_str(), i);
+ p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], p_speed.scale * p_speed.peak);
+ }
+ }
+}
+
+Cpu::~Cpu()
+{
+ if (p_constraintCoreId){
+ for (int i = 0; i < m_core; i++) {
+ xbt_free(p_constraintCoreId[i]);
+ }
+ xbt_free(p_constraintCore);
+ }
+ if (p_constraintCoreId)
+ xbt_free(p_constraintCoreId);
+ if (p_speedPeakList)
+ xbt_dynar_free(&p_speedPeakList);
+}
+
+double Cpu::getCurrentPowerPeak()
+{
+ return p_speed.peak;
+}
+
+int Cpu::getNbPStates()
+{
+ return xbt_dynar_length(p_speedPeakList);
+}
+
+void Cpu::setPState(int pstate_index)
+{
+ xbt_dynar_t plist = p_speedPeakList;
+ xbt_assert(pstate_index <= (int)xbt_dynar_length(plist),
+ "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", getName(), pstate_index, (int)xbt_dynar_length(plist));