2019-11-26 16:39:36

by Sudarikov, Roman

[permalink] [raw]
Subject: [PATCH 5/6] perf tools: Add feature check for libpci

From: Roman Sudarikov <[email protected]>

Add feature check for libpci to show device name in --iiostat mode.
libpci support allows device name to b:d:f notion.

Signed-off-by: Roman Sudarikov <[email protected]>
Co-developed-by: Alexander Antonov <[email protected]>
Signed-off-by: Alexander Antonov <[email protected]>
---
tools/build/Makefile.feature | 2 ++
tools/build/feature/Makefile | 4 ++++
tools/build/feature/test-all.c | 5 +++++
tools/build/feature/test-libpci.c | 10 ++++++++++
tools/perf/Makefile.config | 10 ++++++++++
tools/perf/builtin-version.c | 1 +
tools/perf/tests/make | 1 +
7 files changed, 33 insertions(+)
create mode 100644 tools/build/feature/test-libpci.c

diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature
index 8a19753cc26a..bebdfb99607c 100644
--- a/tools/build/Makefile.feature
+++ b/tools/build/Makefile.feature
@@ -50,6 +50,7 @@ FEATURE_TESTS_BASIC := \
libelf-mmap \
libnuma \
numa_num_possible_cpus \
+ libpci \
libperl \
libpython \
libpython-version \
@@ -115,6 +116,7 @@ FEATURE_DISPLAY ?= \
libelf \
libnuma \
numa_num_possible_cpus \
+ libpci \
libperl \
libpython \
libcrypto \
diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index 8499385365c0..f0d5f886602d 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -28,6 +28,7 @@ FILES= \
test-libelf-mmap.bin \
test-libnuma.bin \
test-numa_num_possible_cpus.bin \
+ test-libpci.bin \
test-libperl.bin \
test-libpython.bin \
test-libpython-version.bin \
@@ -210,6 +211,9 @@ PERL_EMBED_LIBADD = $(call grep-libs,$(PERL_EMBED_LDOPTS))
PERL_EMBED_CCOPTS = `perl -MExtUtils::Embed -e ccopts 2>/dev/null`
FLAGS_PERL_EMBED=$(PERL_EMBED_CCOPTS) $(PERL_EMBED_LDOPTS)

+$(OUTPUT)test-libpci.bin:
+ $(BUILD) -lpci
+
$(OUTPUT)test-libperl.bin:
$(BUILD) $(FLAGS_PERL_EMBED)

diff --git a/tools/build/feature/test-all.c b/tools/build/feature/test-all.c
index 88145e8cde1a..c61d34804a06 100644
--- a/tools/build/feature/test-all.c
+++ b/tools/build/feature/test-all.c
@@ -74,6 +74,10 @@
# include "test-libunwind.c"
#undef main

+#define main main_test_libpci
+# include "test-libpci.c"
+#undef
+
#define main main_test_libaudit
# include "test-libaudit.c"
#undef main
@@ -210,6 +214,7 @@ int main(int argc, char *argv[])
main_test_libunwind();
main_test_libaudit();
main_test_libslang();
+ main_test_libpci();
main_test_gtk2(argc, argv);
main_test_gtk2_infobar(argc, argv);
main_test_libbfd();
diff --git a/tools/build/feature/test-libpci.c b/tools/build/feature/test-libpci.c
new file mode 100644
index 000000000000..4bbeb9ffd687
--- /dev/null
+++ b/tools/build/feature/test-libpci.c
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "pci/pci.h"
+
+int main(void)
+{
+ struct pci_access *pacc = pci_alloc();
+
+ pci_cleanup(pacc);
+ return 0;
+}
diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 46f7fba2306c..1b9d341492c8 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -839,6 +839,16 @@ ifndef NO_LIBCAP
endif
endif

+ifndef NO_PCILIB
+ ifeq ($(feature-libpci), 1)
+ CFLAGS += -DHAVE_LIBPCI_SUPPORT
+ EXTLIBS += -lpci
+ else
+ msg := $(warning No libpci found, show pci devices without names in iiostat mode, please install libpci-dev/pciutils-devel);
+ NO_PCILIB := 1
+ endif
+endif
+
ifndef NO_BACKTRACE
ifeq ($(feature-backtrace), 1)
CFLAGS += -DHAVE_BACKTRACE_SUPPORT
diff --git a/tools/perf/builtin-version.c b/tools/perf/builtin-version.c
index 05cf2af9e2c2..ec4e0eb07825 100644
--- a/tools/perf/builtin-version.c
+++ b/tools/perf/builtin-version.c
@@ -76,6 +76,7 @@ static void library_status(void)
STATUS(HAVE_LIBUNWIND_SUPPORT, libunwind);
STATUS(HAVE_DWARF_SUPPORT, libdw-dwarf-unwind);
STATUS(HAVE_ZLIB_SUPPORT, zlib);
+ STATUS(HAVE_LIBPCI_SUPPORT, libpci);
STATUS(HAVE_LZMA_SUPPORT, lzma);
STATUS(HAVE_AUXTRACE_SUPPORT, get_cpuid);
STATUS(HAVE_LIBBPF_SUPPORT, bpf);
diff --git a/tools/perf/tests/make b/tools/perf/tests/make
index c850d1664c56..0b78cf6e8377 100644
--- a/tools/perf/tests/make
+++ b/tools/perf/tests/make
@@ -109,6 +109,7 @@ make_minimal += NO_LIBNUMA=1 NO_LIBAUDIT=1 NO_LIBBIONIC=1
make_minimal += NO_LIBDW_DWARF_UNWIND=1 NO_AUXTRACE=1 NO_LIBBPF=1
make_minimal += NO_LIBCRYPTO=1 NO_SDT=1 NO_JVMTI=1 NO_LIBZSTD=1
make_minimal += NO_LIBCAP=1
+make_minimal += NO_LIBPCI=1

# $(run) contains all available tests
run := make_pure
--
2.19.1


2019-11-26 16:41:53

by Sudarikov, Roman

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

From: Roman Sudarikov <[email protected]>

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 <[email protected]>
Co-developed-by: Alexander Antonov <[email protected]>
Signed-off-by: Alexander Antonov <[email protected]>
---
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 <[email protected]>
*/
#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