Skip to content

Commit a5902f1

Browse files
olivierlemaslesebgoa
authored andcommitted
CLOUDSTACK-6850: Return cpu cores, cpu speed and memory in listUsageRecords
Signed-off-by: Sebastien Goasguen <[email protected]>
1 parent 044c5e0 commit a5902f1

8 files changed

Lines changed: 161 additions & 26 deletions

File tree

api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ public class UsageRecordResponse extends BaseResponse implements ControlledEntit
101101
@Param(description = "virtual size of resource")
102102
private Long virtualSize;
103103

104+
@SerializedName(ApiConstants.CPU_NUMBER)
105+
@Param(description = "number of cpu of resource")
106+
private Long cpuNumber;
107+
108+
@SerializedName(ApiConstants.CPU_SPEED)
109+
@Param(description = "speed of each cpu of resource")
110+
private Long cpuSpeed;
111+
112+
@SerializedName(ApiConstants.MEMORY)
113+
@Param(description = "memory allocated for the resource")
114+
private Long memory;
115+
104116
@SerializedName(ApiConstants.START_DATE)
105117
@Param(description = "start date of the usage record")
106118
private String startDate;
@@ -229,4 +241,16 @@ public void setDefault(Boolean isDefault) {
229241
public void setVirtualSize(Long virtualSize) {
230242
this.virtualSize = virtualSize;
231243
}
244+
245+
public void setCpuNumber(Long cpuNumber) {
246+
this.cpuNumber = cpuNumber;
247+
}
248+
249+
public void setCpuSpeed(Long cpuSpeed) {
250+
this.cpuSpeed = cpuSpeed;
251+
}
252+
253+
public void setMemory(Long memory) {
254+
this.memory = memory;
255+
}
232256
}

api/src/org/apache/cloudstack/usage/Usage.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public interface Usage {
4040

4141
public String getVmName();
4242

43+
public Long getCpuCores();
44+
45+
public Long getCpuSpeed();
46+
47+
public Long getMemory();
48+
4349
public Long getOfferingId();
4450

4551
public Long getTemplateId();

engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ public UsageVMInstanceVO(int usageType, long zoneId, long accountId, long vmInst
8686
this.endDate = endDate;
8787
}
8888

89+
public UsageVMInstanceVO(int usageType, long zoneId, long accountId, long vmInstanceId, String vmName, long serviceOfferingId, long templateId,
90+
Long cpuSpeed, Long cpuCores, Long memory, String hypervisorType, Date startDate, Date endDate) {
91+
this.usageType = usageType;
92+
this.zoneId = zoneId;
93+
this.accountId = accountId;
94+
this.vmInstanceId = vmInstanceId;
95+
this.vmName = vmName;
96+
this.serviceOfferingId = serviceOfferingId;
97+
this.templateId = templateId;
98+
this.cpuSpeed = cpuSpeed;
99+
this.cpuCores = cpuCores;
100+
this.memory = memory;
101+
this.hypervisorType = hypervisorType;
102+
this.startDate = startDate;
103+
this.endDate = endDate;
104+
}
105+
89106
public int getUsageType() {
90107
return usageType;
91108
}

engine/schema/src/com/cloud/usage/UsageVO.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public class UsageVO implements Usage, InternalIdentity {
6565
@Column(name = "vm_name")
6666
private String vmName = null;
6767

68+
@Column(name = "cpu_cores")
69+
private Long cpuCores = null;
70+
71+
@Column(name = "memory")
72+
private Long memory = null;
73+
74+
@Column(name = "cpu_speed")
75+
private Long cpuSpeed = null;
76+
6877
@Column(name = "offering_id")
6978
private Long offeringId = null;
7079

@@ -171,6 +180,28 @@ public UsageVO(Long zoneId, Long accountId, Long domainId, String description, S
171180
this.endDate = endDate;
172181
}
173182

183+
public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay, int usageType, Double rawUsage, Long vmId, String vmName,
184+
Long cpuCores, Long cpuSpeed, Long memory, Long offeringId, Long templateId, Long usageId, Date startDate, Date endDate, String type) {
185+
this.zoneId = zoneId;
186+
this.accountId = accountId;
187+
this.domainId = domainId;
188+
this.description = description;
189+
this.usageDisplay = usageDisplay;
190+
this.usageType = usageType;
191+
this.rawUsage = rawUsage;
192+
this.vmInstanceId = vmId;
193+
this.vmName = vmName;
194+
this.cpuCores = cpuCores;
195+
this.cpuSpeed = cpuSpeed;
196+
this.memory = memory;
197+
this.offeringId = offeringId;
198+
this.templateId = templateId;
199+
this.usageId = usageId;
200+
this.type = type;
201+
this.startDate = startDate;
202+
this.endDate = endDate;
203+
}
204+
174205
//IPAddress Usage
175206
public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay, int usageType, Double rawUsage, Long usageId, long size,
176207
String type, Date startDate, Date endDate) {
@@ -238,6 +269,21 @@ public String getVmName() {
238269
return vmName;
239270
}
240271

272+
@Override
273+
public Long getCpuCores() {
274+
return cpuCores;
275+
}
276+
277+
@Override
278+
public Long getCpuSpeed() {
279+
return cpuSpeed;
280+
}
281+
282+
@Override
283+
public Long getMemory() {
284+
return memory;
285+
}
286+
241287
@Override
242288
public Long getOfferingId() {
243289
return offeringId;

engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase<UsageVMInstanceVO, Lo
4242
+ "WHERE account_id = ? and vm_instance_id = ? and usage_type = ? and end_date IS NULL";
4343
protected static final String DELETE_USAGE_INSTANCE_SQL = "DELETE FROM usage_vm_instance WHERE account_id = ? and vm_instance_id = ? and usage_type = ?";
4444
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT =
45-
"SELECT usage_type, zone_id, account_id, vm_instance_id, vm_name, service_offering_id, template_id, hypervisor_type, start_date, end_date "
46-
+ "FROM usage_vm_instance " + "WHERE account_id = ? AND ((end_date IS NULL) OR (start_date BETWEEN ? AND ?) OR "
45+
"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 "
46+
+ "FROM usage_vm_instance WHERE account_id = ? AND ((end_date IS NULL) OR (start_date BETWEEN ? AND ?) OR "
4747
+ " (end_date BETWEEN ? AND ?) OR ((start_date <= ?) AND (end_date >= ?)))";
4848

4949
public UsageVMInstanceDaoImpl() {
@@ -113,11 +113,23 @@ public List<UsageVMInstanceVO> getUsageRecords(long accountId, Date startDate, D
113113
long r_accountId = rs.getLong(3);
114114
long r_vmId = rs.getLong(4);
115115
String r_vmName = rs.getString(5);
116-
long r_soId = rs.getLong(6);
117-
long r_tId = rs.getLong(7);
118-
String hypervisorType = rs.getString(8);
119-
String r_startDate = rs.getString(9);
120-
String r_endDate = rs.getString(10);
116+
Long r_cpuSpeed = rs.getLong(6);
117+
if (rs.wasNull()) {
118+
r_cpuSpeed = null;
119+
}
120+
Long r_cpuCores = rs.getLong(7);
121+
if (rs.wasNull()) {
122+
r_cpuCores = null;
123+
}
124+
Long r_memory = rs.getLong(8);
125+
if (rs.wasNull()) {
126+
r_memory = null;
127+
}
128+
long r_soId = rs.getLong(9);
129+
long r_tId = rs.getLong(10);
130+
String hypervisorType = rs.getString(11);
131+
String r_startDate = rs.getString(12);
132+
String r_endDate = rs.getString(13);
121133
Date instanceStartDate = null;
122134
Date instanceEndDate = null;
123135
if (r_startDate != null) {
@@ -127,7 +139,7 @@ public List<UsageVMInstanceVO> getUsageRecords(long accountId, Date startDate, D
127139
instanceEndDate = DateUtil.parseDateString(s_gmtTimeZone, r_endDate);
128140
}
129141
UsageVMInstanceVO usageInstance =
130-
new UsageVMInstanceVO(r_usageType, r_zoneId, r_accountId, r_vmId, r_vmName, r_soId, r_tId, hypervisorType, instanceStartDate, instanceEndDate);
142+
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);
131143
usageInstances.add(usageInstance);
132144
}
133145
} catch (Exception ex) {

server/src/com/cloud/api/ApiResponseHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,6 +3038,10 @@ public UsageRecordResponse createUsageResponse(Usage usageRecord) {
30383038
}
30393039
//Hypervisor Type
30403040
usageRecResponse.setType(usageRecord.getType());
3041+
//Dynamic compute offerings details
3042+
usageRecResponse.setCpuNumber(usageRecord.getCpuCores());
3043+
usageRecResponse.setCpuSpeed(usageRecord.getCpuSpeed());
3044+
usageRecResponse.setMemory(usageRecord.getMemory());
30413045

30423046
} else if (usageRecord.getUsageType() == UsageTypes.IP_ADDRESS) {
30433047
//isSourceNAT

setup/db/db/schema-430to440.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,11 @@ CREATE VIEW `cloud`.`user_vm_view` AS
461461
left join
462462
`cloud`.`user_vm_details` `custom_ram_size` ON (((`custom_ram_size`.`vm_id` = `cloud`.`vm_instance`.`id`) and (`custom_ram_size`.`name` = 'memory')));
463463

464+
ALTER TABLE `cloud_usage`.`cloud_usage` ADD COLUMN `cpu_speed` INT(10) UNSIGNED NULL COMMENT 'speed per core in Mhz',
465+
ADD COLUMN `cpu_cores` INT(10) UNSIGNED NULL COMMENT 'number of cpu cores',
466+
ADD COLUMN `memory` INT(10) UNSIGNED NULL COMMENT 'memory in MB';
467+
468+
464469
-- ACL DB schema
465470
CREATE TABLE `cloud`.`iam_group` (
466471
`id` bigint unsigned NOT NULL UNIQUE auto_increment,

usage/src/com/cloud/usage/parser/VMInstanceUsageParser.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import org.apache.log4j.Logger;
2929
import org.springframework.stereotype.Component;
30-
3130
import org.apache.cloudstack.usage.UsageTypes;
3231

3332
import com.cloud.usage.UsageVMInstanceVO;
@@ -36,6 +35,7 @@
3635
import com.cloud.usage.dao.UsageVMInstanceDao;
3736
import com.cloud.user.AccountVO;
3837
import com.cloud.utils.Pair;
38+
import com.cloud.utils.StringUtils;
3939

4040
@Component
4141
public class VMInstanceUsageParser {
@@ -75,7 +75,7 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
7575
Map<String, Pair<String, Long>> usageVMUptimeMap = new HashMap<String, Pair<String, Long>>();
7676
Map<String, Pair<String, Long>> allocatedVMMap = new HashMap<String, Pair<String, Long>>();
7777

78-
Map<String, VMInfo> vmServiceOfferingMap = new HashMap<String, VMInfo>();
78+
Map<String, VMInfo> vmInfosMap = new HashMap<String, VMInfo>();
7979

8080
// loop through all the usage instances, create a usage record for each
8181
for (UsageVMInstanceVO usageInstance : usageInstances) {
@@ -84,10 +84,13 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
8484
long zoneId = usageInstance.getZoneId();
8585
long tId = usageInstance.getTemplateId();
8686
int usageType = usageInstance.getUsageType();
87-
String key = vmId + "-" + soId + "-" + usageType;
87+
Long cpuCores = usageInstance.getCpuCores();
88+
Long cpuSpeed = usageInstance.getCpuSpeed();
89+
Long memory = usageInstance.getMemory();
90+
String key = StringUtils.join("-", vmId, soId, usageType, cpuCores, cpuSpeed, memory);
8891

89-
// store the info in the service offering map
90-
vmServiceOfferingMap.put(key, new VMInfo(vmId, zoneId, soId, tId, usageInstance.getHypervisorType()));
92+
// store the info in the VMs map
93+
vmInfosMap.put(key, new VMInfo(vmId, zoneId, soId, tId, usageInstance.getHypervisorType(), cpuCores, cpuSpeed, memory));
9194

9295
Date vmStartDate = usageInstance.getStartDate();
9396
Date vmEndDate = usageInstance.getEndDate();
@@ -119,9 +122,9 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
119122

120123
// Only create a usage record if we have a runningTime of bigger than zero.
121124
if (runningTime > 0L) {
122-
VMInfo info = vmServiceOfferingMap.get(vmIdKey);
125+
VMInfo info = vmInfosMap.get(vmIdKey);
123126
createUsageRecord(UsageTypes.RUNNING_VM, runningTime, startDate, endDate, account, info.getVirtualMachineId(), vmUptimeInfo.first(), info.getZoneId(),
124-
info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType());
127+
info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType(), info.getCpuCores(), info.getCpuSpeed(), info.getMemory());
125128
}
126129
}
127130

@@ -131,9 +134,9 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
131134

132135
// Only create a usage record if we have a runningTime of bigger than zero.
133136
if (allocatedTime > 0L) {
134-
VMInfo info = vmServiceOfferingMap.get(vmIdKey);
137+
VMInfo info = vmInfosMap.get(vmIdKey);
135138
createUsageRecord(UsageTypes.ALLOCATED_VM, allocatedTime, startDate, endDate, account, info.getVirtualMachineId(), vmAllocInfo.first(), info.getZoneId(),
136-
info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType());
139+
info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType(), info.getCpuCores(), info.getCpuSpeed(), info.getMemory());
137140
}
138141
}
139142

@@ -153,7 +156,7 @@ private static void updateVmUsageData(Map<String, Pair<String, Long>> usageDataM
153156
}
154157

155158
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId,
156-
long serviceOfferingId, long templateId, String hypervisorType) {
159+
long serviceOfferingId, long templateId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) {
157160
// Our smallest increment is hourly for now
158161
if (s_logger.isDebugEnabled()) {
159162
s_logger.debug("Total running time " + runningTime + "ms");
@@ -179,23 +182,29 @@ private static void createUsageRecord(int type, long runningTime, Date startDate
179182
usageDesc += " (ServiceOffering: " + serviceOfferingId + ") (Template: " + templateId + ")";
180183
UsageVO usageRecord =
181184
new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), Long.valueOf(vmId),
182-
vmName, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
185+
vmName, cpuCores, cpuSpeed, memory, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
183186
s_usageDao.persist(usageRecord);
184187
}
185188

186189
private static class VMInfo {
187-
private long virtualMachineId;
188-
private long zoneId;
189-
private long serviceOfferingId;
190-
private long templateId;
191-
private String hypervisorType;
192-
193-
public VMInfo(long vmId, long zId, long soId, long tId, String hypervisorType) {
190+
private final long virtualMachineId;
191+
private final long zoneId;
192+
private final long serviceOfferingId;
193+
private final long templateId;
194+
private final String hypervisorType;
195+
private final Long cpuCores;
196+
private final Long cpuSpeed;
197+
private final Long memory;
198+
199+
public VMInfo(long vmId, long zId, long soId, long tId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) {
194200
virtualMachineId = vmId;
195201
zoneId = zId;
196202
serviceOfferingId = soId;
197203
templateId = tId;
198204
this.hypervisorType = hypervisorType;
205+
this.cpuCores = cpuCores;
206+
this.cpuSpeed = cpuSpeed;
207+
this.memory = memory;
199208
}
200209

201210
public long getZoneId() {
@@ -217,5 +226,17 @@ public long getTemplateId() {
217226
private String getHypervisorType() {
218227
return hypervisorType;
219228
}
229+
230+
public Long getCpuCores() {
231+
return cpuCores;
232+
}
233+
234+
public Long getCpuSpeed() {
235+
return cpuSpeed;
236+
}
237+
238+
public Long getMemory() {
239+
return memory;
240+
}
220241
}
221242
}

0 commit comments

Comments
 (0)