virtcontainers: improve algorithm to check Large bar devices

Instead of iterate in a loop dividing bytes by 1024, use right shift
to convert Bytes to GBytes and check if that number is greater than 4G

Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2020-03-20 19:28:23 +00:00
parent 7aff546655
commit 3b53114ad1

View File

@ -102,7 +102,6 @@ func isLargeBarSpace(resourcePath string) (bool, error) {
// Refer: // Refer:
// resource format: https://github.com/torvalds/linux/blob/63623fd44972d1ed2bfb6e0fb631dfcf547fd1e7/drivers/pci/pci-sysfs.c#L145 // resource format: https://github.com/torvalds/linux/blob/63623fd44972d1ed2bfb6e0fb631dfcf547fd1e7/drivers/pci/pci-sysfs.c#L145
// calculate size : https://github.com/pciutils/pciutils/blob/61ecc14a327de030336f1ff3fea9c7e7e55a90ca/lspci.c#L388 // calculate size : https://github.com/pciutils/pciutils/blob/61ecc14a327de030336f1ff3fea9c7e7e55a90ca/lspci.c#L388
suffix := []string{"", "K", "M", "G", "T"}
for rIdx, line := range strings.Split(string(buf), "\n") { for rIdx, line := range strings.Split(string(buf), "\n") {
cols := strings.Fields(line) cols := strings.Fields(line)
// start and end columns are required to calculate the size // start and end columns are required to calculate the size
@ -119,25 +118,18 @@ func isLargeBarSpace(resourcePath string) (bool, error) {
}).Debug("start is greater than end") }).Debug("start is greater than end")
continue continue
} }
size := end - start + 1 // Use right shift to convert Bytes to GBytes
sIdx := 0 // This is equivalent to ((end - start + 1) / 1024 / 1024 / 1024)
for i := range suffix { gbSize := (end - start + 1) >> 30
if size/1024 < 1 {
break
}
size /= 1024
sIdx = i + 1
}
deviceLogger().WithFields(logrus.Fields{ deviceLogger().WithFields(logrus.Fields{
"resource": resourcePath, "resource": resourcePath,
"region": rIdx, "region": rIdx,
"start": cols[0], "start": cols[0],
"end": cols[1], "end": cols[1],
"size": size, "gb-size": gbSize,
"suffix": suffix[sIdx],
}).Debug("Check large bar space device") }).Debug("Check large bar space device")
//size is large than 4G //size is large than 4G
if (sIdx == 3 && size > 4) || sIdx > 3 { if gbSize > 4 {
return true, nil return true, nil
} }
} }