diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java index 45bb177c08b..083a6fabeca 100644 --- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java +++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java @@ -469,7 +469,7 @@ public class PresetVariableHelper { } value.setTags(getPresetVariableValueResourceTags(volumeId, ResourceObjectType.Volume)); - value.setSize(ByteScaleUtils.bytesToMib(volumeVo.getSize())); + value.setSize(ByteScaleUtils.bytesToMebibytes(volumeVo.getSize())); } protected GenericPresetVariable getPresetVariableValueDiskOffering(Long diskOfferingId) { @@ -536,7 +536,7 @@ public class PresetVariableHelper { value.setName(vmTemplateVo.getName()); value.setOsName(getPresetVariableValueOsName(vmTemplateVo.getGuestOSId())); value.setTags(getPresetVariableValueResourceTags(templateOrIsoId, usageType == UsageTypes.ISO ? ResourceObjectType.ISO : ResourceObjectType.Template)); - value.setSize(ByteScaleUtils.bytesToMib(vmTemplateVo.getSize())); + value.setSize(ByteScaleUtils.bytesToMebibytes(vmTemplateVo.getSize())); } protected void loadPresetVariableValueForSnapshot(UsageVO usageRecord, Value value) { @@ -554,7 +554,7 @@ public class PresetVariableHelper { value.setId(snapshotVo.getUuid()); value.setName(snapshotVo.getName()); - value.setSize(ByteScaleUtils.bytesToMib(snapshotVo.getSize())); + value.setSize(ByteScaleUtils.bytesToMebibytes(snapshotVo.getSize())); value.setSnapshotType(Snapshot.Type.values()[snapshotVo.getSnapshotType()]); value.setStorage(getPresetVariableValueStorage(getSnapshotDataStoreId(snapshotId), usageType)); value.setTags(getPresetVariableValueResourceTags(snapshotId, ResourceObjectType.Snapshot)); diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java index d195b4ffc99..bfc4bd463f7 100644 --- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java +++ b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java @@ -657,7 +657,7 @@ public class PresetVariableHelperTest { Value result = new Value(); presetVariableHelperSpy.loadPresetVariableValueForVolume(usageVoMock, result); - Long expectedSize = ByteScaleUtils.bytesToMib(expected.getSize()); + Long expectedSize = ByteScaleUtils.bytesToMebibytes(expected.getSize()); assertPresetVariableIdAndName(expected, result); Assert.assertEquals(expected.getDiskOffering(), result.getDiskOffering()); @@ -693,7 +693,7 @@ public class PresetVariableHelperTest { Value result = new Value(); presetVariableHelperSpy.loadPresetVariableValueForVolume(usageVoMock, result); - Long expectedSize = ByteScaleUtils.bytesToMib(expected.getSize()); + Long expectedSize = ByteScaleUtils.bytesToMebibytes(expected.getSize()); assertPresetVariableIdAndName(expected, result); Assert.assertEquals(expected.getDiskOffering(), result.getDiskOffering()); @@ -824,7 +824,7 @@ public class PresetVariableHelperTest { Value result = new Value(); presetVariableHelperSpy.loadPresetVariableValueForTemplateAndIso(usageVoMock, result); - Long expectedSize = ByteScaleUtils.bytesToMib(expected.getSize()); + Long expectedSize = ByteScaleUtils.bytesToMebibytes(expected.getSize()); assertPresetVariableIdAndName(expected, result); Assert.assertEquals(expected.getOsName(), result.getOsName()); @@ -872,7 +872,7 @@ public class PresetVariableHelperTest { Value result = new Value(); presetVariableHelperSpy.loadPresetVariableValueForSnapshot(usageVoMock, result); - Long expectedSize = ByteScaleUtils.bytesToMib(expected.getSize()); + Long expectedSize = ByteScaleUtils.bytesToMebibytes(expected.getSize()); assertPresetVariableIdAndName(expected, result); Assert.assertEquals(expected.getSnapshotType(), result.getSnapshotType()); diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index bcfff1b6a32..b1e31e55a28 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -2814,7 +2814,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv grd.setMemBalloning(!_noMemBalloon); - Long maxRam = ByteScaleUtils.bytesToKib(vmTO.getMaxRam()); + Long maxRam = ByteScaleUtils.bytesToKibibytes(vmTO.getMaxRam()); grd.setMemorySize(maxRam); grd.setCurrentMem(getCurrentMemAccordingToMemBallooning(vmTO, maxRam)); @@ -2833,7 +2833,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv s_logger.warn(String.format("Setting VM's [%s] current memory as max memory [%s] due to memory ballooning is disabled. If you are using a custom service offering, verify if memory ballooning really should be disabled.", vmTO.toString(), maxRam)); return maxRam; } else { - return ByteScaleUtils.bytesToKib(vmTO.getMinRam()); + return ByteScaleUtils.bytesToKibibytes(vmTO.getMinRam()); } } diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtScaleVmCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtScaleVmCommandWrapper.java index 96c3e844abf..963d13bff24 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtScaleVmCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtScaleVmCommandWrapper.java @@ -37,7 +37,7 @@ public class LibvirtScaleVmCommandWrapper extends CommandWrapper 0) { - maxMemory = ByteScaleUtils.mibToBytes(maxMemoryConfig); + maxMemory = ByteScaleUtils.mebibytesToBytes(maxMemoryConfig); } else { s_logger.info(String.format("Config [%s] has value less or equal '0'. Using %s host or last host max memory [%s] as VM max memory in the hypervisor.", maxMemoryConfigKey, vmDescription, maxHostMemory)); maxMemory = maxHostMemory; diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index df6c0d17694..dd579f8a246 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -2053,7 +2053,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir // #1 Check existing host has capacity if (!excludes.shouldAvoid(ApiDBUtils.findHostById(vmInstance.getHostId()))) { existingHostHasCapacity = _capacityMgr.checkIfHostHasCpuCapability(vmInstance.getHostId(), newCpu, newSpeed) - && _capacityMgr.checkIfHostHasCapacity(vmInstance.getHostId(), cpuDiff, ByteScaleUtils.mibToBytes(memoryDiff), false, + && _capacityMgr.checkIfHostHasCapacity(vmInstance.getHostId(), cpuDiff, ByteScaleUtils.mebibytesToBytes(memoryDiff), false, _capacityMgr.getClusterOverProvisioningFactor(host.getClusterId(), Capacity.CAPACITY_TYPE_CPU), _capacityMgr.getClusterOverProvisioningFactor(host.getClusterId(), Capacity.CAPACITY_TYPE_MEMORY), false); excludes.addHost(vmInstance.getHostId()); diff --git a/server/src/test/java/com/cloud/hypervisor/KVMGuruTest.java b/server/src/test/java/com/cloud/hypervisor/KVMGuruTest.java index 0999cd03909..875ba8270e2 100644 --- a/server/src/test/java/com/cloud/hypervisor/KVMGuruTest.java +++ b/server/src/test/java/com/cloud/hypervisor/KVMGuruTest.java @@ -176,7 +176,7 @@ public class KVMGuruTest { long result = guru.getVmMaxMemory(serviceOfferingVoMock, "Vm description", 1l); - Assert.assertEquals(ByteScaleUtils.mibToBytes(maxCustomOfferingMemory), result); + Assert.assertEquals(ByteScaleUtils.mebibytesToBytes(maxCustomOfferingMemory), result); } @Test @@ -190,12 +190,12 @@ public class KVMGuruTest { Mockito.when(vmServiceOfferingMaxRAMSize.value()).thenReturn(maxMemoryConfig); long result = guru.getVmMaxMemory(serviceOfferingVoMock, "Vm description", 1l); - Assert.assertEquals(ByteScaleUtils.mibToBytes(maxMemoryConfig), result); + Assert.assertEquals(ByteScaleUtils.mebibytesToBytes(maxMemoryConfig), result); } @Test public void validateGetVmMaxMemoryReturnMaxHostMemory(){ - long maxHostMemory = ByteScaleUtils.mibToBytes(2000); + long maxHostMemory = ByteScaleUtils.mebibytesToBytes(2000); Mockito.when(serviceOfferingVoMock.getDetail(ApiConstants.MAX_MEMORY)).thenReturn(null); ConfigKey vmServiceOfferingMaxRAMSize = Mockito.mock(ConfigKey.class); @@ -490,4 +490,4 @@ public class KVMGuruTest { Assert.assertNull(clusterId); } -} \ No newline at end of file +} diff --git a/utils/src/main/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtils.java b/utils/src/main/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtils.java index 13f565f19fc..97043dbe82f 100644 --- a/utils/src/main/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtils.java +++ b/utils/src/main/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtils.java @@ -15,7 +15,7 @@ package org.apache.cloudstack.utils.bytescale; /** - * This class provides a facility to convert bytes through his scales (b, Kib, Kb, Mib, Mb...). + * This class provides a facility to convert bytes through the scales (B, KiB, KB, MiB, MB...). * */ public class ByteScaleUtils { @@ -30,29 +30,29 @@ public class ByteScaleUtils { * Converts mebibytes to bytes. * * @param mib The value to convert to bytes (eq: 1, 2, 3, ..., 42,...). - * @return The parameter multiplied by 1048576 (1024 * 1024, 1 MiB). + * @return The parameter multiplied by 2²⁰ (1048576 | 1024 * 1024 | 1 mebibyte). */ - public static long mibToBytes(long mib) { + public static long mebibytesToBytes(long mib) { return mib * MiB; } /** - * Converts bytes to kibibytes. + * Converts bytes to kibibyte. * - * @param b The value in bytes to convert to kibibytes. - * @return The parameter divided by 1024 (1 KiB). + * @param bytes The value in bytes to convert to kibibyte. + * @return The parameter divided by 2¹⁰ (1024 | 1 kibibyte). */ - public static long bytesToKib(long b) { - return b / KiB; + public static long bytesToKibibytes(long bytes) { + return bytes / KiB; } /** * Converts bytes to mebibytes. * - * @param b The value in bytes to convert to mebibytes. - * @return The parameter divided by 1024 * 1024 (1 MiB). + * @param bytes The value in bytes to convert to mebibytes. + * @return The parameter divided by 2²⁰ (1048576 | 1024 * 1024 | 1 mebibyte). */ - public static long bytesToMib(long b) { - return b / MiB; + public static long bytesToMebibytes(long bytes) { + return bytes / MiB; } } diff --git a/utils/src/test/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtilsTest.java b/utils/src/test/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtilsTest.java index e644d207e9b..dd3b577148b 100644 --- a/utils/src/test/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtilsTest.java +++ b/utils/src/test/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtilsTest.java @@ -14,45 +14,45 @@ package org.apache.cloudstack.utils.bytescale; -import junit.framework.TestCase; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) -public class ByteScaleUtilsTest extends TestCase { +public class ByteScaleUtilsTest { @Test - public void validateMibToBytes() { + public void validateMebibytesToBytes() { long mib = 3000L; - long b = 1024L * 1024L * mib; - assertEquals(b, ByteScaleUtils.mibToBytes(mib)); + long bytes = 1024L * 1024L * mib; + Assert.assertEquals(bytes, ByteScaleUtils.mebibytesToBytes(mib)); } @Test - public void validateBytesToKib() { + public void validateBytesToKibibytes() { long kib = 3000L; - long b = 1024 * kib; - assertEquals(kib, ByteScaleUtils.bytesToKib(b)); + long bytes = 1024 * kib; + Assert.assertEquals(kib, ByteScaleUtils.bytesToKibibytes(bytes)); } @Test - public void validateBytesToMib() { + public void validateBytesToMebibytes() { long mib = 3000L; - long b = 1024L * 1024L * mib; - assertEquals(mib, ByteScaleUtils.bytesToMib(b)); + long bytes = 1024L * 1024L * mib; + Assert.assertEquals(mib, ByteScaleUtils.bytesToMebibytes(bytes)); } @Test - public void validateMibToBytesIfIntTimesIntThenMustExtrapolateIntMaxValue() { + public void validateMebibytesToBytesIfIntTimesIntThenMustExtrapolateIntMaxValue() { int mib = 3000; - long b = 1024L * 1024L * mib; - assertEquals(b, ByteScaleUtils.mibToBytes(mib)); + long bytes = 1024L * 1024L * mib; + Assert.assertEquals(bytes, ByteScaleUtils.mebibytesToBytes(mib)); } @Test - public void validateBytesToKibIfIntByIntThenMustExtrapolateIntMaxValue(){ - int b = Integer.MAX_VALUE; - assertEquals(b, ByteScaleUtils.bytesToKib(b * 1024L)); + public void validateBytesToKibibytesIfIntByIntThenMustExtrapolateIntMaxValue(){ + int bytes = Integer.MAX_VALUE; + Assert.assertEquals(bytes, ByteScaleUtils.bytesToKibibytes(bytes * 1024L)); } }