How to Optimize Container Density to Improve Resource Utilization, Performance, and Cost-Efficiency in Containercloudsim?
Share
Condition for Optimize Container Density to Improve Resource Utilization, Performance, and Cost-Efficiency in Containercloudsim
Description: This work presents an enhanced simulation framework using ContainerCloudSim, designed to analyze and optimize container density—the ratio of containers to virtual machines (VMs)—in cloud data centers. The proposed simulation integrates a Container Density Optimizer that evaluates current resource utilization and determines the optimal container-to-VM ratio based on RAM, MIPS, and workload performance metrics. By combining resource-based, performance-based, and cost-based optimization analyses, the system identifies under-utilized and over-utilized VM configurations and recommends density adjustment strategies. Additionally, the simulation introduces a Comprehensive Density Optimization Module that evaluates multiple deployment scenarios (high, balanced, and low density) to achieve a trade-off between performance and cost. Experimental results from the simulation demonstrate how adjusting container density can significantly improve overall resource efficiency, reduce operational costs, and maintain stable application performance in dynamic cloud environments.
/**
* A simple example showing how to create a data center with one host, one VM,
* one container and run one cloudlet on it.
* Enhanced with Container Density Optimizer feature.
*/
public class ContainerDensity {
/**
* The cloudlet list.
*/
private static List cloudletList;
/**
* The vmlist.
*/
private static List vmList;
/**
* The vmlist.
*/
private static List containerList;
/**
* The hostList.
*/
private static List hostList;
/**
* Creates main() to run this example.
*
* @param args the args
*/
public static void main(String[] args) {
Log.printLine("Starting ContainerCloudSim with Density Optimizer...");
try {
/**
* number of cloud Users
*/
int num_user = 1;
/**
* The fields of calender have been initialized with the current
* date and time.
*/
Calendar calendar = Calendar.getInstance();
/**
* Deactivating the event tracing
*/
boolean trace_flag = false;
/**
* 1- Like CloudSim the first step is initializing the CloudSim
* Package before creating any entities.
*
*/
CloudSim.init(num_user, calendar, trace_flag);
/**
* 2- Defining the container allocation Policy. This policy
* determines how Containers are allocated to VMs in the data
* center.
*
*/
ContainerAllocationPolicy containerAllocationPolicy = new PowerContainerAllocationPolicySimple();
/**
* 3- Defining the VM selection Policy. This policy determines which
* VMs should be selected for migration when a host is identified as
* over-loaded.
*
*/
PowerContainerVmSelectionPolicy vmSelectionPolicy = new PowerContainerVmSelectionPolicyMaximumUsage();
/**
* 4- Defining the host selection Policy. This policy determines
* which hosts should be selected as migration destination.
*
*/
HostSelectionPolicy hostSelectionPolicy = new HostSelectionPolicyFirstFit();
/**
* 5- Defining the thresholds for selecting the under-utilized and
* over-utilized hosts.
*/
double overUtilizationThreshold = 0.80;
double underUtilizationThreshold = 0.70;
/**
* 6- The host list is created considering the number of hosts, and
* host types which are specified in the {@link ConstantsExamples}.
*/
hostList = new ArrayList();
hostList = createHostList(ConstantsExamples.NUMBER_HOSTS);
cloudletList = new ArrayList();
vmList = new ArrayList();
/**
* 7- The container allocation policy which defines the allocation
* of VMs to containers.
*/
ContainerVmAllocationPolicy vmAllocationPolicy = new PowerContainerVmAllocationPolicyMigrationAbstractHostSelection(hostList, vmSelectionPolicy,
hostSelectionPolicy, overUtilizationThreshold, underUtilizationThreshold);
/**
* 8- The overbooking factor for allocating containers to VMs. This
* factor is used by the broker for the allocation process.
*/
int overBookingFactor = 80;
ContainerDatacenterBroker broker = createBroker(overBookingFactor);
int brokerId = broker.getId();
/**
* 9- Creating the cloudlet, container and VM lists for submitting
* to the broker.
*/
cloudletList = createContainerCloudletList(brokerId, ConstantsExamples.NUMBER_CLOUDLETS);
containerList = createContainerList(brokerId, ConstantsExamples.NUMBER_CLOUDLETS);
vmList = createVmList(brokerId, ConstantsExamples.NUMBER_VMS);
/**
* 10- The address for logging the statistics of the VMs, containers
* in the data center.
*/
String logAddress = "~/Results";
/**
* 11- Submitting the cloudlet's , container's , and VM's lists to
* the broker.
*/
broker.submitCloudletList(cloudletList.subList(0, containerList.size()));
broker.submitContainerList(containerList);
broker.submitVmList(vmList);
/**
* 12- Determining the simulation termination time according to the
* cloudlet's workload.
*/
CloudSim.terminateSimulation(86400.00);
/**
* 13- Starting the simualtion.
*/
CloudSim.startSimulation();
/**
* 14- Stopping the simualtion.
*/
CloudSim.stopSimulation();
/**
* 15- Printing the results when the simulation is finished.
*/
List newList = broker.getCloudletReceivedList();
printCloudletList(newList);
/**
* 16- Run container density analysis
*/
analyzeContainerDensity(vmList, containerList);
/**
* 17- Run comprehensive density optimization analysis
*/
runComprehensiveDensityAnalysis(vmList, containerList, hostList, newList);
// Provide recommendations
if (currentDensity < optimalDensity * 0.7) {
Log.printLine("Recommendation: STRONGLY UNDER-UTILIZED - Increase container density by " +
String.format("%.0f", (optimalDensity - currentDensity)) + " containers/VM");
} else if (currentDensity < optimalDensity * 0.9) {
Log.printLine("Recommendation: UNDER-UTILIZED - Consider increasing container density");
} else if (currentDensity > optimalDensity * 1.3) {
Log.printLine("Recommendation: STRONGLY OVER-UTILIZED - Decrease container density by " +
String.format("%.0f", (currentDensity - optimalDensity)) + " containers/VM");
} else if (currentDensity > optimalDensity * 1.1) {
Log.printLine("Recommendation: OVER-UTILIZED - Consider decreasing container density");
} else {
Log.printLine("Recommendation: OPTIMAL - Current density is well balanced");
}
}
/**
* Comprehensive Density Analysis with multiple optimization strategies
*/
private static void runComprehensiveDensityAnalysis(List vms, List containers,
List hosts, List cloudlets) {
Log.printLine("\n=== COMPREHENSIVE DENSITY OPTIMIZATION ANALYSIS ===");
// 1. Basic Density Analysis
analyzeBasicDensity(vms, containers);
// Calculate optimal density ranges
calculateOptimalDensityRanges(vms, containers);
}
/**
* Calculate optimal density ranges based on resource constraints
*/
private static void calculateOptimalDensityRanges(List vms, List containers) {
Log.printLine("\n--- OPTIMAL DENSITY RANGES ---");
// Analyze different VM types
Map> vmsByType = new HashMap<>();
for (ContainerVm vm : vms) {
int ram = (int) vm.getRam();
if (!vmsByType.containsKey(ram)) {
vmsByType.put(ram, new ArrayList<>());
}
vmsByType.get(ram).add(vm);
}
for (Map.Entry> entry : vmsByType.entrySet()) {
int ram = entry.getKey();
List sameTypeVms = entry.getValue();
double avgMips = sameTypeVms.get(0).getMips();
// Calculate optimal containers based on average container requirements
double avgContainerRam = calculateAverageContainerRam(containers);
double avgContainerMips = calculateAverageContainerMips(containers);
int maxByRam = (int) (ram / avgContainerRam);
int maxByMips = (int) (avgMips / avgContainerMips);
int optimalDensity = Math.min(maxByRam, maxByMips);
/**
* Performance-based Density Analysis
*/
private static void analyzePerformanceBasedDensity(List vms, List containers,
List cloudlets) {
Log.printLine("\n--- 3. PERFORMANCE-BASED DENSITY ANALYSIS ---");
Map vmPerformance = new HashMap<>();
// Initialize performance metrics for each VM
for (ContainerVm vm : vms) {
vmPerformance.put(vm.getId(), new PerformanceMetrics());
}
// Collect performance data
for (ContainerCloudlet cloudlet : cloudlets) {
if (cloudlet.getCloudletStatusString().equals("Success") ) {
int vmId = cloudlet.getVmId();
PerformanceMetrics metrics = vmPerformance.get(vmId);
if (metrics != null) {
metrics.totalExecutionTime += cloudlet.getActualCPUTime();
metrics.completedCloudlets++;
}
}
}
// Calculate containers per VM and correlate with performance
Map containersPerVm = new HashMap<>();
for (Container container : containers) {
if (container.getVm() != null) {
int vmId = container.getVm().getId();
containersPerVm.put(vmId, containersPerVm.getOrDefault(vmId, 0) + 1);
}
}
// Print performance vs density correlation
Log.printLine("VM ID | Containers | Completed Cloudlets | Avg Execution Time");
Log.printLine("------|------------|---------------------|------------------");
for (ContainerVm vm : vms) {
PerformanceMetrics metrics = vmPerformance.get(vm.getId());
int containerCount = containersPerVm.getOrDefault(vm.getId(), 0);
if (metrics != null && metrics.completedCloudlets > 0) {
double avgTime = metrics.totalExecutionTime / metrics.completedCloudlets;
Log.printLine(String.format("%5d | %10d | %19d | %16.2f",
vm.getId(), containerCount, metrics.completedCloudlets, avgTime));
}
}
}
/**
* Cost-based Density Optimization
*/
private static void analyzeCostBasedDensity(List vms, List containers,
List hosts) {
Log.printLine("\n--- 4. COST-BASED OPTIMIZATION ---");
double vmCostPerHour = 0.05; // $0.05 per VM per hour
double containerCostPerHour = 0.01; // $0.01 per container per hour
double simulationHours = 24.0; // Assuming 24-hour simulation
// Calculate cost for different density scenarios
double[] densityScenarios = {2.0, 4.0, 6.0, 8.0, 10.0};
Log.printLine("\nCost comparison for different density scenarios:");
Log.printLine("Density | VMs Needed | Total Cost | Savings");
Log.printLine("--------|------------|------------|---------");
// Recommendation
Log.printLine("\nRECOMMENDATION:");
if (currentDensity < 2.0) {
Log.printLine(" Current setup is UNDER-UTILIZED. Consider Scenario 1 (High Density) to reduce costs.");
} else if (currentDensity > 8.0) {
Log.printLine(" Current setup is OVER-UTILIZED. Consider Scenario 3 (Low Density) to improve performance.");
} else {
Log.printLine(" Current setup is WELL-BALANCED. Scenario 2 (Balanced) maintains good performance and cost.");
}
}
// Helper methods
private static double calculateAverageContainerRam(List containers) {
double total = 0;
for (Container container : containers) {
total += container.getRam();
}
return containers.isEmpty() ? 0 : total / containers.size();
}
private static double calculateAverageContainerMips(List containers) {
double total = 0;
for (Container container : containers) {
total += container.getMips();
}
return containers.isEmpty() ? 0 : total / containers.size();
}
/**
* Performance metrics class for density analysis
*/
private static class PerformanceMetrics {
int completedCloudlets = 0;
double totalExecutionTime = 0;
}
/**
* It creates a specific name for the experiment which is used for creating
* the Log address folder.
*/
private static String getExperimentName(String... args) {
StringBuilder experimentName = new StringBuilder();
for (int i = 0; i < args.length; ++i) {
if (!args[i].isEmpty()) {
if (i != 0) {
experimentName.append("_");
}
/**
* Prints the Cloudlet objects.
*
* @param list list of Cloudlets
*/
private static void printCloudletList(List list) {
int size = list.size();
ContainerCloudlet cloudlet;
/**
* Create the Virtual machines and add them to the list
*
* @param brokerId
* @param containerVmsNumber
*/
private static ArrayList createVmList(int brokerId, int containerVmsNumber) {
ArrayList containerVms = new ArrayList();
for (int i = 0; i < containerVmsNumber; ++i) {
ArrayList peList = new ArrayList();
int vmType = i / (int) Math.ceil((double) containerVmsNumber / 4.0D);
for (int j = 0; j < ConstantsExamples.VM_PES[vmType]; ++j) {
peList.add(new ContainerPe(j,
new ContainerPeProvisionerSimple((double) ConstantsExamples.VM_MIPS[vmType])));
}
containerVms.add(new PowerContainerVm(IDs.pollId(ContainerVm.class), brokerId,
(double) ConstantsExamples.VM_MIPS[vmType], (float) ConstantsExamples.VM_RAM[vmType],
ConstantsExamples.VM_BW, ConstantsExamples.VM_SIZE, "Xen",
new ContainerSchedulerTimeSharedOverSubscription(peList),
new ContainerRamProvisionerSimple(ConstantsExamples.VM_RAM[vmType]),
new ContainerBwProvisionerSimple(ConstantsExamples.VM_BW),
peList, ConstantsExamples.SCHEDULING_INTERVAL));
}
return containerVms;
}
/**
* Create the host list considering the specs listed in the
* {@link ConstantsExamples}.
*
* @param hostsNumber
* @return
*/
public static List createHostList(int hostsNumber) {
ArrayList hostList = new ArrayList();
for (int i = 0; i < hostsNumber; ++i) {
int hostType = i / (int) Math.ceil((double) hostsNumber / 3.0D);
ArrayList peList = new ArrayList();
for (int j = 0; j < ConstantsExamples.HOST_PES[hostType]; ++j) {
peList.add(new ContainerVmPe(j,
new ContainerVmPeProvisionerSimple((double) ConstantsExamples.HOST_MIPS[hostType])));
}
hostList.add(new PowerContainerHostUtilizationHistory(IDs.pollId(ContainerHost.class),
new ContainerVmRamProvisionerSimple(ConstantsExamples.HOST_RAM[hostType]),
new ContainerVmBwProvisionerSimple(1000000L), 1000000L, peList,
new ContainerVmSchedulerTimeSharedOverSubscription(peList),
ConstantsExamples.HOST_POWER[hostType]));
}
/**
* create the containers for hosting the cloudlets and binding them
* together.
*
* @param brokerId
* @param containersNumber
* @return
*/
public static List createContainerList(int brokerId, int containersNumber) {
ArrayList containers = new ArrayList();
for (int i = 0; i < containersNumber; ++i) {
int containerType = i / (int) Math.ceil((double) containersNumber / 3.0D);
/**
* Creating the cloudlet list that are going to run on containers
*
* @param brokerId
* @param numberOfCloudlets
* @return
* @throws FileNotFoundException
*/
public static List createContainerCloudletList(int brokerId, int numberOfCloudlets)
throws FileNotFoundException {
String inputFolderName = "/home/soft13/soft13/Project 2019/Cloudsim-4.0-examples/src/workload/planetlab";
ArrayList cloudletList = new ArrayList();
long fileSize = 300L;
long outputSize = 300L;
UtilizationModelNull utilizationModelNull = new UtilizationModelNull();
java.io.File inputFolder1 = new java.io.File(inputFolderName);
java.io.File[] files1 = inputFolder1.listFiles();
int createdCloudlets = 0;
for (java.io.File aFiles1 : files1) {
java.io.File inputFolder = new java.io.File(aFiles1.toString());
java.io.File[] files = inputFolder.listFiles();
for (int i = 0; i < files.length; ++i) {
if (createdCloudlets < numberOfCloudlets) {
ContainerCloudlet cloudlet = null;