From fc576b369f1542cde07b5ded316ad5d4d0efc7e4 Mon Sep 17 00:00:00 2001 From: Guixin Liu Date: Mon, 2 Sep 2024 14:56:19 +0800 Subject: [PATCH] lscpu: optimize find virt pci device 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. And also we can read the /proc/bus/pci/devices one time to avoid call many read(). Signed-off-by: Guixin Liu --- ...lscpu-optimize-query-virt-pci-device.patch | 98 +++++++++++++++++++ util-linux.spec | 6 +- 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 3001-lscpu-optimize-query-virt-pci-device.patch diff --git a/3001-lscpu-optimize-query-virt-pci-device.patch b/3001-lscpu-optimize-query-virt-pci-device.patch new file mode 100644 index 0000000..cdb4981 --- /dev/null +++ b/3001-lscpu-optimize-query-virt-pci-device.patch @@ -0,0 +1,98 @@ +diff -uNrp rpmbuild/BUILD/util-linux-2.32.1/sys-utils/lscpu.c util-linux-2.32.1.changed/sys-utils/lscpu.c +--- rpmbuild/BUILD/util-linux-2.32.1/sys-utils/lscpu.c 2024-09-02 13:57:18.865397140 +0800 ++++ util-linux-2.32.1.changed/sys-utils/lscpu.c 2024-09-02 14:41:47.756250698 +0800 +@@ -538,30 +538,62 @@ read_basicinfo(struct lscpu_desc *desc, + } + + static int +-has_pci_device(unsigned int vendor, unsigned int device) ++find_virt_pci_device(struct lscpu_desc *desc) + { + FILE *f; +- unsigned int num, fn, ven, dev; +- int res = 1; ++ int num, fn, ven, dev; ++ int vendor = HYPER_NONE; ++ int filesize; ++ char *fbuf, *line; + + f = path_fopen("r", 0, _PATH_PROC_PCIDEVS); + if (!f) +- return 0; ++ return vendor; + +- /* for more details about bus/pci/devices format see +- * drivers/pci/proc.c in linux kernel +- */ +- while(fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]", +- &num, &fn, &ven, &dev) == 4) { ++ /* for more details about bus/pci/devices format see ++ * drivers/pci/proc.c in linux kernel ++ */ ++ ++ fseek(f, 0, SEEK_END); ++ filesize = ftell(f); ++ fseek(f, 0, SEEK_SET); ++ ++ fbuf = (char *)malloc(filesize); ++ if (!fbuf) { ++ fclose(f); ++ return vendor; ++ } ++ fread(fbuf, sizeof(char), filesize, f); + +- if (ven == vendor && dev == device) +- goto found; ++ line = strtok(fbuf, "\n"); ++ while (line != NULL) { ++ sscanf(line, "%02x%02x\t%04x%04x\t%*[^\n]", ++ &num, &fn, &ven, &dev); ++ ++ if (ven == hv_vendor_pci[HYPER_XEN] && ++ dev == hv_graphics_pci[HYPER_XEN]) { ++ vendor = HYPER_XEN; ++ goto found; ++ } ++ ++ if (ven == hv_vendor_pci[HYPER_VMWARE] && ++ dev == hv_graphics_pci[HYPER_VMWARE]) { ++ vendor = HYPER_VMWARE; ++ goto found; ++ } ++ ++ if (ven == hv_vendor_pci[HYPER_VBOX] && ++ dev == hv_graphics_pci[HYPER_VBOX]) { ++ vendor = HYPER_VBOX; ++ goto found; ++ } ++ line = strtok(NULL, "\n"); + } + +- res = 0; + found: + fclose(f); +- return res; ++ free(fbuf); ++ return vendor; + } + + #if defined(__x86_64__) || defined(__i386__) +@@ -848,16 +880,8 @@ read_hypervisor(struct lscpu_desc *desc, + desc->hyper = HYPER_XEN; + + /* Xen full-virt on non-x86_64 */ +- } else if (has_pci_device( hv_vendor_pci[HYPER_XEN], hv_graphics_pci[HYPER_XEN])) { +- desc->hyper = HYPER_XEN; ++ } else if (desc->hyper = find_virt_pci_device(desc)) { + desc->virtype = VIRT_FULL; +- } else if (has_pci_device( hv_vendor_pci[HYPER_VMWARE], hv_graphics_pci[HYPER_VMWARE])) { +- desc->hyper = HYPER_VMWARE; +- desc->virtype = VIRT_FULL; +- } else if (has_pci_device( hv_vendor_pci[HYPER_VBOX], hv_graphics_pci[HYPER_VBOX])) { +- desc->hyper = HYPER_VBOX; +- desc->virtype = VIRT_FULL; +- + /* IBM PR/SM */ + } else if ((fd = path_fopen("r", 0, _PATH_PROC_SYSINFO))) { + char buf[BUFSIZ]; +Binary files rpmbuild/BUILD/util-linux-2.32.1/sys-utils/.lscpu.c.swp and util-linux-2.32.1.changed/sys-utils/.lscpu.c.swp differ diff --git a/util-linux.spec b/util-linux.spec index eee9819..62e573e 100644 --- a/util-linux.spec +++ b/util-linux.spec @@ -1,4 +1,4 @@ -%define anolis_release .0.2 +%define anolis_release .0.3 ### Header Summary: A collection of basic system utilities Name: util-linux @@ -319,6 +319,7 @@ Patch1004: 1004-util-linux-add-sw.patch Patch2001: 0001-prlimit-fix-optional-arguments-parsing.patch Patch3000: 3000-lscpu-add-alibaba-hyperv-vendor.patch +Patch3001: 3001-lscpu-optimize-query-virt-pci-device.patch %description The util-linux package contains a large variety of low-level system @@ -1166,6 +1167,9 @@ fi %{_libdir}/python*/site-packages/libmount/ %changelog +* Mon Sep 02 2024 Guixin Liu 2.23.1-46.0.3 +- [Patch] lscpu: optimize find virt pci device (kanie@linux.alibaba.com) + * Fri Aug 23 2024 Guixin Liu 2.23.1-46.0.2 - [Patch] lscpu: add alibaba hyperv vendor (kanie@linux.alibaba.com) -- Gitee