* [PATCH] lscpu: optimize query virt pci device
@ 2024-08-21 8:49 Guixin Liu
2024-08-22 11:30 ` Karel Zak
0 siblings, 1 reply; 2+ messages in thread
From: Guixin Liu @ 2024-08-21 8:49 UTC (permalink / raw)
To: qcai; +Cc: util-linux
The lscpu command needs to traverse the /proc/bus/pci/devices file
three times to check for any PCI devices related to virtualization.
If there are many PCI devices on the machine, this can lead to
increased execution time for lscpu. It would be beneficial to
consolidate these queries into a single check to optimize the
execution time of lscpu.
Use time command to test on a machine with 13000 PCI devices:
before:
real 0m13.506s
user 0m0.028s
sys 0m13.409s
after:
real 0m5.834s
user 0m0.018s
sys 0m5.771s
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---
sys-utils/lscpu-virt.c | 39 ++++++++++++++++++++++-----------------
1 file changed, 22 insertions(+), 17 deletions(-)
diff --git a/sys-utils/lscpu-virt.c b/sys-utils/lscpu-virt.c
index 051384622..f559d9c14 100644
--- a/sys-utils/lscpu-virt.c
+++ b/sys-utils/lscpu-virt.c
@@ -273,16 +273,15 @@ static int read_hypervisor_dmi(void)
return rc < 0 ? VIRT_VENDOR_NONE : rc;
}
-static int has_pci_device(struct lscpu_cxt *cxt,
- unsigned int vendor, unsigned int device)
+static int find_virt_pci_device(struct lscpu_cxt *cxt)
{
FILE *f;
- unsigned int num, fn, ven, dev;
- int res = 1;
+ int num, fn, ven, dev;
+ int vendor = VIRT_VENDOR_NONE;
f = ul_path_fopen(cxt->procfs, "r", "bus/pci/devices");
if (!f)
- return 0;
+ return vendor;
/* for more details about bus/pci/devices format see
* drivers/pci/proc.c in linux kernel
@@ -290,14 +289,28 @@ static int has_pci_device(struct lscpu_cxt *cxt,
while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]",
&num, &fn, &ven, &dev) == 4) {
- if (ven == vendor && dev == device)
+ if (ven == hv_vendor_pci[VIRT_VENDOR_XEN] &&
+ dev == hv_graphics_pci[VIRT_VENDOR_XEN]) {
+ vendor = VIRT_VENDOR_XEN;
+ goto found;
+ }
+
+ if (ven == hv_vendor_pci[VIRT_VENDOR_VMWARE] &&
+ dev == hv_graphics_pci[VIRT_VENDOR_VMWARE]) {
+ vendor = VIRT_VENDOR_VMWARE;
+ goto found;
+ }
+
+ if (ven == hv_vendor_pci[VIRT_VENDOR_VBOX] &&
+ dev == hv_graphics_pci[VIRT_VENDOR_VBOX]) {
+ vendor = VIRT_VENDOR_VBOX;
goto found;
+ }
}
- res = 0;
found:
fclose(f);
- return res;
+ return vendor;
}
#if defined(__x86_64__) || defined(__i386__)
@@ -597,16 +610,8 @@ struct lscpu_virt *lscpu_read_virtualization(struct lscpu_cxt *cxt)
virt->vendor = VIRT_VENDOR_XEN;
/* Xen full-virt on non-x86_64 */
- } else if (has_pci_device(cxt, hv_vendor_pci[VIRT_VENDOR_XEN], hv_graphics_pci[VIRT_VENDOR_XEN])) {
- virt->vendor = VIRT_VENDOR_XEN;
+ } else if ((virt->vendor = find_virt_pci_device(cxt))) {
virt->type = VIRT_TYPE_FULL;
- } else if (has_pci_device(cxt, hv_vendor_pci[VIRT_VENDOR_VMWARE], hv_graphics_pci[VIRT_VENDOR_VMWARE])) {
- virt->vendor = VIRT_VENDOR_VMWARE;
- virt->type = VIRT_TYPE_FULL;
- } else if (has_pci_device(cxt, hv_vendor_pci[VIRT_VENDOR_VBOX], hv_graphics_pci[VIRT_VENDOR_VBOX])) {
- virt->vendor = VIRT_VENDOR_VBOX;
- virt->type = VIRT_TYPE_FULL;
-
/* IBM PR/SM */
} else if ((fd = ul_path_fopen(cxt->procfs, "r", "sysinfo"))) {
--
2.30.1 (Apple Git-130)
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] lscpu: optimize query virt pci device
2024-08-21 8:49 [PATCH] lscpu: optimize query virt pci device Guixin Liu
@ 2024-08-22 11:30 ` Karel Zak
0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2024-08-22 11:30 UTC (permalink / raw)
To: Guixin Liu; +Cc: qcai, util-linux
On Wed, Aug 21, 2024 at 04:49:54PM GMT, Guixin Liu wrote:
> sys-utils/lscpu-virt.c | 39 ++++++++++++++++++++++-----------------
> 1 file changed, 22 insertions(+), 17 deletions(-)
Thanks, the patch looks good. I'll merge it later after it passes CI:
https://github.com/util-linux/util-linux/pull/3177.
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-08-22 11:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-21 8:49 [PATCH] lscpu: optimize query virt pci device Guixin Liu
2024-08-22 11:30 ` Karel Zak
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox