From c934e7b052c09df2fae52a005e69951ed0235574 Mon Sep 17 00:00:00 2001 From: Olivier Lemasle Date: Sun, 8 Jun 2014 17:59:17 +0200 Subject: [PATCH] CLOUDSTACK-6850: Return cpu cores, cpu speed and memory in listUsageRecords Signed-off-by: Sebastien Goasguen (cherry picked from commit a1f278e9d47e8029d58df6d3aae13495965ca434) --- .../api/response/UsageRecordResponse.java | 24 ++++++++ .../org/apache/cloudstack/usage/Usage.java | 6 ++ .../com/cloud/usage/UsageVMInstanceVO.java | 17 ++++++ .../schema/src/com/cloud/usage/UsageVO.java | 46 ++++++++++++++++ .../usage/dao/UsageVMInstanceDaoImpl.java | 28 +++++++--- .../src/com/cloud/api/ApiResponseHelper.java | 4 ++ setup/db/db/schema-430to440.sql | 5 ++ .../usage/parser/VMInstanceUsageParser.java | 55 +++++++++++++------ 8 files changed, 160 insertions(+), 25 deletions(-) diff --git a/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java b/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java index 5e2e85dea4b..87a085c2a5c 100644 --- a/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java +++ b/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java @@ -101,6 +101,18 @@ public class UsageRecordResponse extends BaseResponse implements ControlledEntit @Param(description = "virtual size of resource") private Long virtualSize; + @SerializedName(ApiConstants.CPU_NUMBER) + @Param(description = "number of cpu of resource") + private Long cpuNumber; + + @SerializedName(ApiConstants.CPU_SPEED) + @Param(description = "speed of each cpu of resource") + private Long cpuSpeed; + + @SerializedName(ApiConstants.MEMORY) + @Param(description = "memory allocated for the resource") + private Long memory; + @SerializedName(ApiConstants.START_DATE) @Param(description = "start date of the usage record") private String startDate; @@ -229,4 +241,16 @@ public class UsageRecordResponse extends BaseResponse implements ControlledEntit public void setVirtualSize(Long virtualSize) { this.virtualSize = virtualSize; } + + public void setCpuNumber(Long cpuNumber) { + this.cpuNumber = cpuNumber; + } + + public void setCpuSpeed(Long cpuSpeed) { + this.cpuSpeed = cpuSpeed; + } + + public void setMemory(Long memory) { + this.memory = memory; + } } diff --git a/api/src/org/apache/cloudstack/usage/Usage.java b/api/src/org/apache/cloudstack/usage/Usage.java index 20dc189ae1a..fe35390dd4a 100644 --- a/api/src/org/apache/cloudstack/usage/Usage.java +++ b/api/src/org/apache/cloudstack/usage/Usage.java @@ -40,6 +40,12 @@ public interface Usage { public String getVmName(); + public Long getCpuCores(); + + public Long getCpuSpeed(); + + public Long getMemory(); + public Long getOfferingId(); public Long getTemplateId(); diff --git a/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java b/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java index 06d4876c1b3..1e7b424050d 100644 --- a/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java +++ b/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java @@ -86,6 +86,23 @@ public class UsageVMInstanceVO { this.endDate = endDate; } + public UsageVMInstanceVO(int usageType, long zoneId, long accountId, long vmInstanceId, String vmName, long serviceOfferingId, long templateId, + Long cpuSpeed, Long cpuCores, Long memory, String hypervisorType, Date startDate, Date endDate) { + this.usageType = usageType; + this.zoneId = zoneId; + this.accountId = accountId; + this.vmInstanceId = vmInstanceId; + this.vmName = vmName; + this.serviceOfferingId = serviceOfferingId; + this.templateId = templateId; + this.cpuSpeed = cpuSpeed; + this.cpuCores = cpuCores; + this.memory = memory; + this.hypervisorType = hypervisorType; + this.startDate = startDate; + this.endDate = endDate; + } + public int getUsageType() { return usageType; } diff --git a/engine/schema/src/com/cloud/usage/UsageVO.java b/engine/schema/src/com/cloud/usage/UsageVO.java index 67014ef4fda..c46abb3a9b5 100644 --- a/engine/schema/src/com/cloud/usage/UsageVO.java +++ b/engine/schema/src/com/cloud/usage/UsageVO.java @@ -65,6 +65,15 @@ public class UsageVO implements Usage, InternalIdentity { @Column(name = "vm_name") private String vmName = null; + @Column(name = "cpu_cores") + private Long cpuCores = null; + + @Column(name = "memory") + private Long memory = null; + + @Column(name = "cpu_speed") + private Long cpuSpeed = null; + @Column(name = "offering_id") private Long offeringId = null; @@ -171,6 +180,28 @@ public class UsageVO implements Usage, InternalIdentity { this.endDate = endDate; } + public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay, int usageType, Double rawUsage, Long vmId, String vmName, + Long cpuCores, Long cpuSpeed, Long memory, Long offeringId, Long templateId, Long usageId, Date startDate, Date endDate, String type) { + this.zoneId = zoneId; + this.accountId = accountId; + this.domainId = domainId; + this.description = description; + this.usageDisplay = usageDisplay; + this.usageType = usageType; + this.rawUsage = rawUsage; + this.vmInstanceId = vmId; + this.vmName = vmName; + this.cpuCores = cpuCores; + this.cpuSpeed = cpuSpeed; + this.memory = memory; + this.offeringId = offeringId; + this.templateId = templateId; + this.usageId = usageId; + this.type = type; + this.startDate = startDate; + this.endDate = endDate; + } + //IPAddress Usage public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay, int usageType, Double rawUsage, Long usageId, long size, String type, Date startDate, Date endDate) { @@ -238,6 +269,21 @@ public class UsageVO implements Usage, InternalIdentity { return vmName; } + @Override + public Long getCpuCores() { + return cpuCores; + } + + @Override + public Long getCpuSpeed() { + return cpuSpeed; + } + + @Override + public Long getMemory() { + return memory; + } + @Override public Long getOfferingId() { return offeringId; diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java index b94e12f9c5c..930ad899992 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java @@ -42,8 +42,8 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase= ?)))"; public UsageVMInstanceDaoImpl() { @@ -113,11 +113,23 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase> usageVMUptimeMap = new HashMap>(); Map> allocatedVMMap = new HashMap>(); - Map vmServiceOfferingMap = new HashMap(); + Map vmInfosMap = new HashMap(); // loop through all the usage instances, create a usage record for each for (UsageVMInstanceVO usageInstance : usageInstances) { @@ -84,10 +84,13 @@ public class VMInstanceUsageParser { long zoneId = usageInstance.getZoneId(); long tId = usageInstance.getTemplateId(); int usageType = usageInstance.getUsageType(); - String key = vmId + "-" + soId + "-" + usageType; + Long cpuCores = usageInstance.getCpuCores(); + Long cpuSpeed = usageInstance.getCpuSpeed(); + Long memory = usageInstance.getMemory(); + String key = StringUtils.join("-", vmId, soId, usageType, cpuCores, cpuSpeed, memory); - // store the info in the service offering map - vmServiceOfferingMap.put(key, new VMInfo(vmId, zoneId, soId, tId, usageInstance.getHypervisorType())); + // store the info in the VMs map + vmInfosMap.put(key, new VMInfo(vmId, zoneId, soId, tId, usageInstance.getHypervisorType(), cpuCores, cpuSpeed, memory)); Date vmStartDate = usageInstance.getStartDate(); Date vmEndDate = usageInstance.getEndDate(); @@ -119,9 +122,9 @@ public class VMInstanceUsageParser { // Only create a usage record if we have a runningTime of bigger than zero. if (runningTime > 0L) { - VMInfo info = vmServiceOfferingMap.get(vmIdKey); + VMInfo info = vmInfosMap.get(vmIdKey); createUsageRecord(UsageTypes.RUNNING_VM, runningTime, startDate, endDate, account, info.getVirtualMachineId(), vmUptimeInfo.first(), info.getZoneId(), - info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType()); + info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType(), info.getCpuCores(), info.getCpuSpeed(), info.getMemory()); } } @@ -131,9 +134,9 @@ public class VMInstanceUsageParser { // Only create a usage record if we have a runningTime of bigger than zero. if (allocatedTime > 0L) { - VMInfo info = vmServiceOfferingMap.get(vmIdKey); + VMInfo info = vmInfosMap.get(vmIdKey); createUsageRecord(UsageTypes.ALLOCATED_VM, allocatedTime, startDate, endDate, account, info.getVirtualMachineId(), vmAllocInfo.first(), info.getZoneId(), - info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType()); + info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType(), info.getCpuCores(), info.getCpuSpeed(), info.getMemory()); } } @@ -153,7 +156,7 @@ public class VMInstanceUsageParser { } private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId, - long serviceOfferingId, long templateId, String hypervisorType) { + long serviceOfferingId, long templateId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) { // Our smallest increment is hourly for now if (s_logger.isDebugEnabled()) { s_logger.debug("Total running time " + runningTime + "ms"); @@ -179,23 +182,29 @@ public class VMInstanceUsageParser { usageDesc += " (ServiceOffering: " + serviceOfferingId + ") (Template: " + templateId + ")"; UsageVO usageRecord = new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), Long.valueOf(vmId), - vmName, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType); + vmName, cpuCores, cpuSpeed, memory, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType); s_usageDao.persist(usageRecord); } private static class VMInfo { - private long virtualMachineId; - private long zoneId; - private long serviceOfferingId; - private long templateId; - private String hypervisorType; + private final long virtualMachineId; + private final long zoneId; + private final long serviceOfferingId; + private final long templateId; + private final String hypervisorType; + private final Long cpuCores; + private final Long cpuSpeed; + private final Long memory; - public VMInfo(long vmId, long zId, long soId, long tId, String hypervisorType) { + public VMInfo(long vmId, long zId, long soId, long tId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) { virtualMachineId = vmId; zoneId = zId; serviceOfferingId = soId; templateId = tId; this.hypervisorType = hypervisorType; + this.cpuCores = cpuCores; + this.cpuSpeed = cpuSpeed; + this.memory = memory; } public long getZoneId() { @@ -217,5 +226,17 @@ public class VMInstanceUsageParser { private String getHypervisorType() { return hypervisorType; } + + public Long getCpuCores() { + return cpuCores; + } + + public Long getCpuSpeed() { + return cpuSpeed; + } + + public Long getMemory() { + return memory; + } } }