From feb052fb20b56edfa2e5db5073d89924b6726d3f Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 13 Nov 2026 03:30:16 -0300 Subject: [PATCH] Part of cloudsimplus/cloudsimplus#650: Stop sharing stateful UtilizationModels CloudSim Plus now throws when a stateful UtilizationModel is shared by many VMs or Cloudlets, since that gives inaccurate results. - LoggingExample, VmDestructionExample, MigrationExample1 and InterDatacenterMigration1 create one UtilizationModelDynamic with an update function for each Cloudlet. - UtilizationModelStochasticPerformance enables alwaysGenNewRandUtilization by default, so its single shared model is stateless, and gives each model a distinct seed when using one per Cloudlet. Co-Authored-By: Claude Opus 5.5 --- .../cloudsimplus/examples/LoggingExample.java | 25 ++++++++++++------- .../examples/VmDestructionExample.java | 24 ++++++++++-------- .../migration/InterDatacenterMigration1.java | 7 ++++-- .../examples/migration/MigrationExample1.java | 7 ++++-- ...UtilizationModelStochasticPerformance.java | 18 ++++++------- 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/cloudsimplus/examples/LoggingExample.java b/src/main/java/org/cloudsimplus/examples/LoggingExample.java index 05a0d8a..873c811 100755 --- a/src/main/java/org/cloudsimplus/examples/LoggingExample.java +++ b/src/main/java/org/cloudsimplus/examples/LoggingExample.java @@ -49,6 +49,7 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.function.Supplier; /// An example showing how CloudSim Plus /// is flexible in managing logs of different entities. @@ -109,14 +110,14 @@ private LoggingExample() { broker0.submitVmList(vmList); //Creates Cloudlets that uses 5% of all resources all the time - cloudletList = createCloudlets(new UtilizationModelDynamic(0.05)); + cloudletList = createCloudlets(() -> new UtilizationModelDynamic(0.05)); //Creates Cloudlets that increase resource utilization along the time - cloudletList.addAll(createCloudlets(createDynamicUtilizationModel())); + cloudletList.addAll(createCloudlets(this::createDynamicUtilizationModel)); //Creates Cloudlets that increase resource utilization along the time, //but are submitted just after some delay. - cloudletList.addAll(createCloudlets(createDynamicUtilizationModel(), 10)); + cloudletList.addAll(createCloudlets(this::createDynamicUtilizationModel, 10)); Comparator hostComparator = Comparator.comparingLong(c -> c.getVm().getHost().getId()); cloudletList.sort(hostComparator.thenComparingLong(c -> c.getVm().getId()).thenComparingLong(Identifiable::getId)); @@ -195,19 +196,25 @@ private List createVms() { } /// Creates a list of Cloudlets. - /// @param utilizationModel the UtilizationModel to be used for all Cloudlet resources (CPU, RAM and BW). - private List createCloudlets(final UtilizationModel utilizationModel) { - return createCloudlets(utilizationModel, 0); + /// @param utilizationModelSupplier creates the UtilizationModel for all resources (CPU, RAM and BW) of each Cloudlet + private List createCloudlets(final Supplier utilizationModelSupplier) { + return createCloudlets(utilizationModelSupplier, 0); } /// Creates a list of Cloudlets. - /// @param utilizationModel the UtilizationModel to be used for all Cloudlet resources (CPU, RAM and BW). + /// Each Cloudlet gets its own UtilizationModel, since some of them are stateful, + /// such as a [UtilizationModelDynamic] that increases the utilization along the time: + /// it computes each utilization from the previous one and stores it. + /// If many Cloudlets shared such a model, a request from one Cloudlet would change + /// the utilization the others get, giving inaccurate results. + /// That's why CloudSim Plus throws an exception when a stateful model is shared. + /// @param utilizationModelSupplier creates the UtilizationModel for all resources (CPU, RAM and BW) of each Cloudlet /// @param submissionDelay the delay to submit Cloudlets to the broker - private List createCloudlets(final UtilizationModel utilizationModel, final double submissionDelay) { + private List createCloudlets(final Supplier utilizationModelSupplier, final double submissionDelay) { final var list = new ArrayList(CLOUDLETS); for (int i = 0; i < CLOUDLETS; i++) { - final var cloudlet = CloudletSimple.of(CLOUDLET_LENGTH, CLOUDLET_CORES, utilizationModel); + final var cloudlet = CloudletSimple.of(CLOUDLET_LENGTH, CLOUDLET_CORES, utilizationModelSupplier.get()); cloudlet.setSubmissionDelay(submissionDelay); cloudlet.setSizes(1024); list.add(cloudlet); diff --git a/src/main/java/org/cloudsimplus/examples/VmDestructionExample.java b/src/main/java/org/cloudsimplus/examples/VmDestructionExample.java index 63d131b..2364568 100755 --- a/src/main/java/org/cloudsimplus/examples/VmDestructionExample.java +++ b/src/main/java/org/cloudsimplus/examples/VmDestructionExample.java @@ -209,15 +209,6 @@ private List createVms() { private List createCloudlets() { final var list = new ArrayList(CLOUDLETS); final var utilization = new UtilizationModelFull(); - - /*Creates a utilization model that enables increasing resource usage over time. - * It is used to define the CPU usage of VMs dynamically.*/ - final var dynamicUtilization = new UtilizationModelDynamic(); - - /// Sets a [Function] that enables the utilization model to increment resource usage by 10% over time. - /// This function is defined using a Lambda Expression. - dynamicUtilization.setUtilizationUpdateFunction(um -> um.getUtilization() + um.getTimeSpan()*0.1); - for (int i = 0; i < CLOUDLETS; i++) { final var cloudlet = CloudletSimple.of(i, CLOUDLET_LENGTH, CLOUDLET_CORES) @@ -225,10 +216,23 @@ private List createCloudlets() { .setOutputSize(1024) .setUtilizationModelRam(utilization) .setUtilizationModelBw(utilization) - .setUtilizationModelCpu(dynamicUtilization); + .setUtilizationModelCpu(createDynamicUtilizationModel()); list.add(cloudlet); } return list; } + + /// Creates a utilization model that enables increasing resource usage over time. + /// It is used to define the CPU usage of Cloudlets dynamically. + /// + /// Since the model has an update function, it's stateful: it computes each utilization + /// from the previous one and stores it. So each Cloudlet needs its own instance. + /// If many Cloudlets shared it, a request from one Cloudlet would change the utilization + /// the others get, giving inaccurate results (CloudSim Plus throws an exception in such a case). + private UtilizationModelDynamic createDynamicUtilizationModel() { + /// Sets a [Function] that enables the utilization model to increment resource usage by 10% over time. + /// This function is defined using a Lambda Expression. + return new UtilizationModelDynamic().setUtilizationUpdateFunction(um -> um.getUtilization() + um.getTimeSpan()*0.1); + } } diff --git a/src/main/java/org/cloudsimplus/examples/migration/InterDatacenterMigration1.java b/src/main/java/org/cloudsimplus/examples/migration/InterDatacenterMigration1.java index 0cbade5..f5e347e 100755 --- a/src/main/java/org/cloudsimplus/examples/migration/InterDatacenterMigration1.java +++ b/src/main/java/org/cloudsimplus/examples/migration/InterDatacenterMigration1.java @@ -323,9 +323,12 @@ private void onVmsCreatedListener(final DatacenterBrokerEventInfo info) { public void createAndSubmitCloudlets(final DatacenterBroker broker) { final var cloudletList = new ArrayList(VM_CORES.length); - final UtilizationModelDynamic um = createCpuUtilizationModel(CLOUDLET_INITIAL_CPU_PERCENTAGE, 1); for(final var vm: broker.getVmWaitingList()){ - final var cloudlet = createCloudlet(vm, um); + /* Each Cloudlet needs its own model, since it increases the CPU usage along the time. + * Such a model is stateful: it computes each utilization from the previous one and stores it. + * If Cloudlets shared it, a request from one Cloudlet would change the utilization the others get, + * giving inaccurate results (CloudSim Plus throws an exception in such a case). */ + final var cloudlet = createCloudlet(vm, createCpuUtilizationModel(CLOUDLET_INITIAL_CPU_PERCENTAGE, 1)); cloudletList.add(cloudlet); } diff --git a/src/main/java/org/cloudsimplus/examples/migration/MigrationExample1.java b/src/main/java/org/cloudsimplus/examples/migration/MigrationExample1.java index 82569bf..6f1c521 100755 --- a/src/main/java/org/cloudsimplus/examples/migration/MigrationExample1.java +++ b/src/main/java/org/cloudsimplus/examples/migration/MigrationExample1.java @@ -290,9 +290,12 @@ private void showHostAllocatedMips(final double time, final Host host) { public void createAndSubmitCloudlets(DatacenterBroker broker) { final var newCloudletList = new ArrayList(VM_CORES.length); var cloudlet = Cloudlet.NULL; - UtilizationModelDynamic um = createCpuUtilizationModel(CLOUDLET_INITIAL_CPU_PERCENTAGE, 1); for(final var vm: vmList){ - cloudlet = createCloudlet(vm, broker, um); + /* Each Cloudlet needs its own model, since it increases the CPU usage along the time. + * Such a model is stateful: it computes each utilization from the previous one and stores it. + * If Cloudlets shared it, a request from one Cloudlet would change the utilization the others get, + * giving inaccurate results (CloudSim Plus throws an exception in such a case). */ + cloudlet = createCloudlet(vm, broker, createCpuUtilizationModel(CLOUDLET_INITIAL_CPU_PERCENTAGE, 1)); newCloudletList.add(cloudlet); } diff --git a/src/main/java/org/cloudsimplus/examples/performance/UtilizationModelStochasticPerformance.java b/src/main/java/org/cloudsimplus/examples/performance/UtilizationModelStochasticPerformance.java index 2a452ab..6bbb2ee 100755 --- a/src/main/java/org/cloudsimplus/examples/performance/UtilizationModelStochasticPerformance.java +++ b/src/main/java/org/cloudsimplus/examples/performance/UtilizationModelStochasticPerformance.java @@ -106,18 +106,18 @@ public class UtilizationModelStochasticPerformance { /// Defines if one [UtilizationModel] must be instantiated /// for each Cloudlet or a single instance used by all Cloudlets. /// - /// Realize that if you use a single [UtilizationModel] for all - /// Cloudlets, the resource utilization will usually be the same - /// for all of them. - /// Using the [UtilizationModelStochastic], if two Cloudlets - /// consecutively request the current resource utilization for the same time, - /// it will be returned the same value. If they send consecutive requests for different times, - /// probably it will be returned distinct randomly generated values. + /// A single [UtilizationModelStochastic] can only be shared by all Cloudlets + /// if [#ALWAYS_GENERATE_NEW_RANDOM_UTILIZATION] is true. + /// Otherwise, the model is stateful: it stores the last utilization generated and returns it + /// to any Cloudlet requesting the utilization for the same time. + /// So Cloudlets sharing it would get the same random values, making their usage fully correlated. + /// That's why CloudSim Plus throws an exception when a stateful model is shared. + /// When using multiple models, each one gets a distinct seed, so that Cloudlets get different values. private static final boolean MULTIPLE_UTILIZATION_MODELS = false; /// See [UtilizationModelStochastic#isAlwaysGenNewRandUtilization()] /// for details. - private static final boolean ALWAYS_GENERATE_NEW_RANDOM_UTILIZATION = false; + private static final boolean ALWAYS_GENERATE_NEW_RANDOM_UTILIZATION = true; /// Indicates if the [UtilizationModelStochastic] instances /// will keep a map that stores the resource utilization along simulation @@ -219,7 +219,7 @@ private List createCloudlets() { final var newCloudletList = new ArrayList(CLOUDLETS); for (int i = 0; i < CLOUDLETS; i++) { final var cloudlet = CloudletSimple.of(CLOUDLET_LENGTH, CLOUDLET_CORES); - this.um = MULTIPLE_UTILIZATION_MODELS || this.um == null ? new UtilizationModelStochastic(SEED) : this.um; + this.um = MULTIPLE_UTILIZATION_MODELS || this.um == null ? new UtilizationModelStochastic(SEED + i) : this.um; this.um .setHistoryEnabled(STORE_CLOUDLETS_CPU_UTILIZATION_HISTORY) .setAlwaysGenNewRandUtilization(ALWAYS_GENERATE_NEW_RANDOM_UTILIZATION);