Skip to content

Commit 7216baf

Browse files
committed
Fixed rare crash bug when collecting Libvirt data
1 parent 8583d46 commit 7216baf

File tree

1 file changed

+67
-56
lines changed

1 file changed

+67
-56
lines changed

libvirt/driver_libvirt.go

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -87,71 +87,83 @@ func (d *DomainImpl) GetName() (string, error) {
8787
return d.domain.GetName()
8888
}
8989

90-
func (d *DomainImpl) CpuStats() (VirDomainCpuStats, error) {
91-
var res VirDomainCpuStats
92-
statSlice, err := d.domain.GetCPUStats(-1, 1, NO_FLAGS)
90+
func (d *DomainImpl) CpuStats() (res VirDomainCpuStats, err error) {
91+
var statSlice []lib.DomainCPUStats
92+
statSlice, err = d.domain.GetCPUStats(-1, 1, NO_FLAGS)
9393
if err == nil && len(statSlice) != 1 {
9494
err = fmt.Errorf("Libvirt returned %v CPU stats instead of 1: %v", len(statSlice), statSlice)
9595
}
96-
if err != nil {
97-
return res, err
96+
if err == nil {
97+
stats := statSlice[0]
98+
res = VirDomainCpuStats{
99+
CpuTime: stats.CpuTime,
100+
SystemTime: stats.SystemTime,
101+
UserTime: stats.UserTime,
102+
VcpuTime: stats.VcpuTime,
103+
}
98104
}
99-
stats := statSlice[0]
100-
res.CpuTime = stats.CpuTime
101-
res.SystemTime = stats.SystemTime
102-
res.UserTime = stats.UserTime
103-
res.VcpuTime = stats.VcpuTime
104-
return res, nil
105+
return
105106
}
106107

107-
func (d *DomainImpl) BlockStats(dev string) (VirDomainBlockStats, error) {
108-
stats, err := d.domain.BlockStats(dev)
109-
return VirDomainBlockStats{
110-
RdReq: stats.RdReq,
111-
WrReq: stats.WrReq,
112-
RdBytes: stats.RdBytes,
113-
WrBytes: stats.WrBytes,
114-
}, err
108+
func (d *DomainImpl) BlockStats(dev string) (res VirDomainBlockStats, err error) {
109+
var stats *lib.DomainBlockStats
110+
stats, err = d.domain.BlockStats(dev)
111+
if err == nil {
112+
res = VirDomainBlockStats{
113+
RdReq: stats.RdReq,
114+
WrReq: stats.WrReq,
115+
RdBytes: stats.RdBytes,
116+
WrBytes: stats.WrBytes,
117+
}
118+
}
119+
return
115120
}
116121

117-
func (d *DomainImpl) BlockInfo(dev string) (VirDomainBlockInfo, error) {
118-
stats, err := d.domain.GetBlockInfo(dev, NO_FLAGS)
119-
return VirDomainBlockInfo{
120-
Allocation: stats.Allocation,
121-
Capacity: stats.Capacity,
122-
Physical: stats.Physical,
123-
}, err
122+
func (d *DomainImpl) BlockInfo(dev string) (res VirDomainBlockInfo, err error) {
123+
var stats *lib.DomainBlockInfo
124+
stats, err = d.domain.GetBlockInfo(dev, NO_FLAGS)
125+
if err == nil {
126+
res = VirDomainBlockInfo{
127+
Allocation: stats.Allocation,
128+
Capacity: stats.Capacity,
129+
Physical: stats.Physical,
130+
}
131+
}
132+
return
124133
}
125134

126-
func (d *DomainImpl) MemoryStats() (VirDomainMemoryStat, error) {
127-
var res VirDomainMemoryStat
128-
stats, err := d.domain.MemoryStats(MAX_NUM_MEMORY_STATS, NO_FLAGS)
129-
if err != nil {
130-
return res, err
131-
}
132-
for _, stat := range stats {
133-
switch stat.Tag {
134-
case int32(lib.DOMAIN_MEMORY_STAT_UNUSED):
135-
res.Unused = stat.Val
136-
case int32(lib.DOMAIN_MEMORY_STAT_AVAILABLE):
137-
res.Available = stat.Val
135+
func (d *DomainImpl) MemoryStats() (res VirDomainMemoryStat, err error) {
136+
var stats []lib.DomainMemoryStat
137+
stats, err = d.domain.MemoryStats(MAX_NUM_MEMORY_STATS, NO_FLAGS)
138+
if err == nil {
139+
for _, stat := range stats {
140+
switch stat.Tag {
141+
case int32(lib.DOMAIN_MEMORY_STAT_UNUSED):
142+
res.Unused = stat.Val
143+
case int32(lib.DOMAIN_MEMORY_STAT_AVAILABLE):
144+
res.Available = stat.Val
145+
}
138146
}
139147
}
140-
return res, nil
148+
return
141149
}
142150

143-
func (d *DomainImpl) InterfaceStats(interfaceName string) (VirDomainInterfaceStats, error) {
144-
stats, err := d.domain.InterfaceStats(interfaceName)
145-
return VirDomainInterfaceStats{
146-
RxBytes: stats.RxBytes,
147-
RxPackets: stats.RxPackets,
148-
RxErrs: stats.RxErrs,
149-
RxDrop: stats.RxDrop,
150-
TxBytes: stats.TxBytes,
151-
TxPackets: stats.TxPackets,
152-
TxErrs: stats.TxErrs,
153-
TxDrop: stats.TxDrop,
154-
}, err
151+
func (d *DomainImpl) InterfaceStats(interfaceName string) (res VirDomainInterfaceStats, err error) {
152+
var stats *lib.DomainInterfaceStats
153+
stats, err = d.domain.InterfaceStats(interfaceName)
154+
if err == nil {
155+
res = VirDomainInterfaceStats{
156+
RxBytes: stats.RxBytes,
157+
RxPackets: stats.RxPackets,
158+
RxErrs: stats.RxErrs,
159+
RxDrop: stats.RxDrop,
160+
TxBytes: stats.TxBytes,
161+
TxPackets: stats.TxPackets,
162+
TxErrs: stats.TxErrs,
163+
TxDrop: stats.TxDrop,
164+
}
165+
}
166+
return
155167
}
156168

157169
func (d *DomainImpl) GetXML() (string, error) {
@@ -161,11 +173,10 @@ func (d *DomainImpl) GetXML() (string, error) {
161173
func (d *DomainImpl) GetInfo() (res DomainInfo, err error) {
162174
var info *lib.DomainInfo
163175
info, err = d.domain.GetInfo()
164-
if err != nil {
165-
return
176+
if err == nil {
177+
res.CpuTime = info.CpuTime
178+
res.MaxMem = info.MaxMem
179+
res.Mem = info.Memory
166180
}
167-
res.CpuTime = info.CpuTime
168-
res.MaxMem = info.MaxMem
169-
res.Mem = info.Memory
170181
return
171182
}

0 commit comments

Comments
 (0)