CLOUDSTACK-6850: Return cpu cores, cpu speed and memory in listUsageRecords

Signed-off-by: Sebastien Goasguen <runseb@gmail.com>
(cherry picked from commit a1f278e9d4)
This commit is contained in:
Olivier Lemasle 2014-06-08 17:59:17 +02:00 committed by Daan Hoogland
parent b490da25ba
commit c934e7b052
8 changed files with 160 additions and 25 deletions

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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;
}

View File

@ -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;

View File

@ -42,8 +42,8 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase<UsageVMInstanceVO, Lo
+ "WHERE account_id = ? and vm_instance_id = ? and usage_type = ? and end_date IS NULL";
protected static final String DELETE_USAGE_INSTANCE_SQL = "DELETE FROM usage_vm_instance WHERE account_id = ? and vm_instance_id = ? and usage_type = ?";
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT =
"SELECT usage_type, zone_id, account_id, vm_instance_id, vm_name, service_offering_id, template_id, hypervisor_type, start_date, end_date "
+ "FROM usage_vm_instance " + "WHERE account_id = ? AND ((end_date IS NULL) OR (start_date BETWEEN ? AND ?) OR "
"SELECT usage_type, zone_id, account_id, vm_instance_id, vm_name, cpu_speed, cpu_cores, memory, service_offering_id, template_id, hypervisor_type, start_date, end_date "
+ "FROM usage_vm_instance WHERE account_id = ? AND ((end_date IS NULL) OR (start_date BETWEEN ? AND ?) OR "
+ " (end_date BETWEEN ? AND ?) OR ((start_date <= ?) AND (end_date >= ?)))";
public UsageVMInstanceDaoImpl() {
@ -113,11 +113,23 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase<UsageVMInstanceVO, Lo
long r_accountId = rs.getLong(3);
long r_vmId = rs.getLong(4);
String r_vmName = rs.getString(5);
long r_soId = rs.getLong(6);
long r_tId = rs.getLong(7);
String hypervisorType = rs.getString(8);
String r_startDate = rs.getString(9);
String r_endDate = rs.getString(10);
Long r_cpuSpeed = rs.getLong(6);
if (rs.wasNull()) {
r_cpuSpeed = null;
}
Long r_cpuCores = rs.getLong(7);
if (rs.wasNull()) {
r_cpuCores = null;
}
Long r_memory = rs.getLong(8);
if (rs.wasNull()) {
r_memory = null;
}
long r_soId = rs.getLong(9);
long r_tId = rs.getLong(10);
String hypervisorType = rs.getString(11);
String r_startDate = rs.getString(12);
String r_endDate = rs.getString(13);
Date instanceStartDate = null;
Date instanceEndDate = null;
if (r_startDate != null) {
@ -127,7 +139,7 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase<UsageVMInstanceVO, Lo
instanceEndDate = DateUtil.parseDateString(s_gmtTimeZone, r_endDate);
}
UsageVMInstanceVO usageInstance =
new UsageVMInstanceVO(r_usageType, r_zoneId, r_accountId, r_vmId, r_vmName, r_soId, r_tId, hypervisorType, instanceStartDate, instanceEndDate);
new UsageVMInstanceVO(r_usageType, r_zoneId, r_accountId, r_vmId, r_vmName, r_soId, r_tId, r_cpuSpeed, r_cpuCores, r_memory, hypervisorType, instanceStartDate, instanceEndDate);
usageInstances.add(usageInstance);
}
} catch (Exception ex) {

View File

@ -3306,6 +3306,10 @@ public class ApiResponseHelper implements ResponseGenerator {
}
//Hypervisor Type
usageRecResponse.setType(usageRecord.getType());
//Dynamic compute offerings details
usageRecResponse.setCpuNumber(usageRecord.getCpuCores());
usageRecResponse.setCpuSpeed(usageRecord.getCpuSpeed());
usageRecResponse.setMemory(usageRecord.getMemory());
} else if (usageRecord.getUsageType() == UsageTypes.IP_ADDRESS) {
//isSourceNAT

View File

@ -461,6 +461,11 @@ CREATE VIEW `cloud`.`user_vm_view` AS
left join
`cloud`.`user_vm_details` `custom_ram_size` ON (((`custom_ram_size`.`vm_id` = `cloud`.`vm_instance`.`id`) and (`custom_ram_size`.`name` = 'memory')));
ALTER TABLE `cloud_usage`.`cloud_usage` ADD COLUMN `cpu_speed` INT(10) UNSIGNED NULL COMMENT 'speed per core in Mhz',
ADD COLUMN `cpu_cores` INT(10) UNSIGNED NULL COMMENT 'number of cpu cores',
ADD COLUMN `memory` INT(10) UNSIGNED NULL COMMENT 'memory in MB';
-- ACL DB schema
CREATE TABLE `cloud`.`iam_group` (
`id` bigint unsigned NOT NULL UNIQUE auto_increment,

View File

@ -27,7 +27,6 @@ import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageVMInstanceVO;
@ -36,6 +35,7 @@ import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVMInstanceDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
import com.cloud.utils.StringUtils;
@Component
public class VMInstanceUsageParser {
@ -75,7 +75,7 @@ public class VMInstanceUsageParser {
Map<String, Pair<String, Long>> usageVMUptimeMap = new HashMap<String, Pair<String, Long>>();
Map<String, Pair<String, Long>> allocatedVMMap = new HashMap<String, Pair<String, Long>>();
Map<String, VMInfo> vmServiceOfferingMap = new HashMap<String, VMInfo>();
Map<String, VMInfo> vmInfosMap = new HashMap<String, VMInfo>();
// 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;
}
}
}