[PATCH 6/6] perf stat: Add PCI device name to --iiostat output

From: roman . sudarikov
Date: Tue Nov 26 2019 - 11:37:05 EST


From: Roman Sudarikov <roman.sudarikov@xxxxxxxxxxxxxxx>

Example:
$ perf stat --iiostat=show

Sample output w/o libpci:

S0-RootPort0-uncore_iio_0<00:00.0>
S1-RootPort2-uncore_iio_2<af:00.0>

Sample output with libpci:

S0-RootPort0-uncore_iio_0<00:00.0 Sky Lake-E DMI3 Registers>
S1-RootPort2-uncore_iio_2<af:00.0 Omni-Path HFI Silicon 100 Series [discrete]>

Signed-off-by: Roman Sudarikov <roman.sudarikov@xxxxxxxxxxxxxxx>
Co-developed-by: Alexander Antonov <alexander.antonov@xxxxxxxxx>
Signed-off-by: Alexander Antonov <alexander.antonov@xxxxxxxxx>
---
tools/perf/arch/x86/util/iiostat.c | 15 ++++++++--
tools/perf/util/pci.c | 46 ++++++++++++++++++++++++++++++
tools/perf/util/pci.h | 4 +++
3 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/tools/perf/arch/x86/util/iiostat.c b/tools/perf/arch/x86/util/iiostat.c
index 058a01d3a93f..7aad994e4936 100644
--- a/tools/perf/arch/x86/util/iiostat.c
+++ b/tools/perf/arch/x86/util/iiostat.c
@@ -113,6 +113,7 @@ struct dev_info {
u8 die;
u8 pmu_idx;
u8 root_port_nr;
+ char *name;
};

struct iio_device {
@@ -210,7 +211,12 @@ static struct iio_device *iio_device_new(struct dev_info *info)
if (p) {
INIT_LIST_HEAD(&(p->node));
p->dev_info = *info;
+ p->dev_info.name = strdup(pci_device_name(info->bdf));
p->idx = -1;
+ if (!p->dev_info.name) {
+ free(p);
+ p = NULL;
+ }
}
return p;
}
@@ -219,6 +225,7 @@ static void iio_device_delete(struct iio_device *device)
{
if (device) {
list_del_init(&(device->node));
+ free(device->dev_info.name);
free(device);
}
}
@@ -227,11 +234,11 @@ static void iiostat_device_show(FILE *output,
const struct iio_device * const device)
{
if (output && device)
- fprintf(output, "S%d-RootPort%d-uncore_iio_%d<%02x:%02x.%x>\n",
+ fprintf(output, "S%d-RootPort%d-uncore_iio_%d<%02x:%02x.%x %s>\n",
device->dev_info.die,
device->dev_info.root_port_nr, device->dev_info.pmu_idx,
device->dev_info.bdf.busno, device->dev_info.bdf.devno,
- device->dev_info.bdf.funcno);
+ device->dev_info.bdf.funcno, device->dev_info.name);
}

static struct iio_devs_list *iio_devs_list_new(void)
@@ -426,9 +433,12 @@ static int iio_devs_scan(struct iio_devs_list **list)
if (!rp_nr)
return -ENOMEM;

+ pci_library_init();
+
interim = iio_devs_list_new();
if (!interim) {
free(rp_nr);
+ pci_library_cleanup();
return -ENOMEM;
}

@@ -457,6 +467,7 @@ static int iio_devs_scan(struct iio_devs_list **list)
iio_devs_list_free(interim);

free(rp_nr);
+ pci_library_cleanup();

return ret;
}
diff --git a/tools/perf/util/pci.c b/tools/perf/util/pci.c
index ba1a48e9d0cc..6ce05e6ba037 100644
--- a/tools/perf/util/pci.c
+++ b/tools/perf/util/pci.c
@@ -8,6 +8,9 @@
* Alexander Antonov <alexander.antonov@xxxxxxxxx>
*/
#include "pci.h"
+#ifdef HAVE_LIBPCI_SUPPORT
+#include <pci/pci.h>
+#endif
#include <api/fs/fs.h>
#include <linux/kernel.h>
#include <string.h>
@@ -16,6 +19,49 @@
#define PCI_DEVICE_PATH_TEMPLATE "bus/pci/devices/0000:%02x:%02x.0"
#define PCI_DEVICE_FILE_TEMPLATE PCI_DEVICE_PATH_TEMPLATE"/%s"

+#ifdef HAVE_LIBPCI_SUPPORT
+static struct pci_access *pacc;
+#endif
+
+void pci_library_init(void)
+{
+#ifdef HAVE_LIBPCI_SUPPORT
+ pacc = pci_alloc();
+ if (pacc) {
+ pci_init(pacc);
+ pci_scan_bus(pacc);
+ }
+#endif
+}
+
+void pci_library_cleanup(void)
+{
+#ifdef HAVE_LIBPCI_SUPPORT
+ pci_cleanup(pacc);
+#endif
+}
+
+char *pci_device_name(struct bdf bdf __maybe_unused)
+{
+#ifdef HAVE_LIBPCI_SUPPORT
+ struct pci_dev *device;
+ char namebuf[PATH_MAX];
+
+ if (pacc) {
+ device = pci_get_dev(pacc, 0, bdf.busno, bdf.devno, bdf.funcno);
+ if (device) {
+ pci_fill_info(device, PCI_FILL_IDENT);
+ return pci_lookup_name(pacc, namebuf, sizeof(namebuf),
+ PCI_LOOKUP_DEVICE, device->vendor_id,
+ device->device_id);
+ }
+ }
+ return (char *)"";
+#else
+ return (char *)"";
+#endif
+}
+
static bool directory_exists(const char * const path)
{
return (access(path, F_OK) == 0);
diff --git a/tools/perf/util/pci.h b/tools/perf/util/pci.h
index e963b12e10e7..8d8551360419 100644
--- a/tools/perf/util/pci.h
+++ b/tools/perf/util/pci.h
@@ -17,6 +17,10 @@ struct bdf {
u8 funcno;
};

+void pci_library_init(void);
+void pci_library_cleanup(void);
+
+char *pci_device_name(struct bdf bdf);
bool pci_device_probe(struct bdf bdf);
bool is_pci_device_root_port(struct bdf bdf, u8 *secondary, u8 *subordinate);

--
2.19.1