Example #1
0
func (d *VirDomain) GetVcpusCpuMap(maxInfo int, maxCPUs uint32) ([]VirVcpuInfo, error) {
	ptr := make([]C.virVcpuInfo, maxInfo)

	mapLen := virCpuMapLen(maxCPUs)                    // Length of CPUs bitmask in bytes
	bufSize := int(mapLen) * int(maxInfo)              // Length of the array of 'maxinfo' bitmasks
	cpuMaps := (*C.uchar)(C.malloc(C.size_t(bufSize))) // Array itself
	defer C.free(unsafe.Pointer(cpuMaps))

	result := C.virDomainGetVcpus(
		d.ptr, (C.virVcpuInfoPtr)(unsafe.Pointer(&ptr[0])),
		C.int(maxInfo), cpuMaps, mapLen)

	if result == -1 {
		return nil, GetLastError()
	}

	// Convert to golang []byte for easier handling
	bytesCpuMaps := C.GoBytes(unsafe.Pointer(cpuMaps), C.int(bufSize))

	out := make([]VirVcpuInfo, 0)
	for i := 0; i < int(result); i++ {
		out = append(out, VirVcpuInfo{
			Number:  uint32(ptr[i].number),
			State:   int32(ptr[i].state),
			CpuTime: uint64(ptr[i].cpuTime),
			Cpu:     int32(ptr[i].cpu),
			CpuMap:  extractCpuMask(bytesCpuMaps, i, int(mapLen)),
		})
	}

	return out, nil
}
Example #2
0
func (d *VirDomain) GetVcpus(maxInfo int32) ([]VirVcpuInfo, error) {
	ptr := make([]C.virVcpuInfo, maxInfo)

	result := C.virDomainGetVcpus(
		d.ptr, (C.virVcpuInfoPtr)(unsafe.Pointer(&ptr[0])),
		C.int(maxInfo), nil, C.int(0))

	if result == -1 {
		return []VirVcpuInfo{}, GetLastError()
	}

	out := make([]VirVcpuInfo, 0)
	for i := 0; i < int(result); i++ {
		out = append(out, VirVcpuInfo{
			Number:  uint32(ptr[i].number),
			State:   int32(ptr[i].state),
			CpuTime: uint64(ptr[i].cpuTime),
			Cpu:     int32(ptr[i].cpu),
		})
	}

	return out, nil
}