2021-01-26 20:12:00

by Alexander Antonov

[permalink] [raw]
Subject: [PATCH v3 3/5] perf stat: Helper functions for PCIe root ports list in iostat mode

Introduce helper functions to control PCIe root ports list.
These helpers will be used in the follow-up patch.

Signed-off-by: Alexander Antonov <[email protected]>
---
tools/perf/arch/x86/util/iostat.c | 127 ++++++++++++++++++++++++++++++
1 file changed, 127 insertions(+)
create mode 100644 tools/perf/arch/x86/util/iostat.c

diff --git a/tools/perf/arch/x86/util/iostat.c b/tools/perf/arch/x86/util/iostat.c
new file mode 100644
index 000000000000..3ef727f9da63
--- /dev/null
+++ b/tools/perf/arch/x86/util/iostat.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * perf iostat
+ *
+ * Copyright (C) 2020, Intel Corporation
+ *
+ * Authors: Alexander Antonov <[email protected]>
+ */
+
+#include <api/fs/fs.h>
+#include <linux/kernel.h>
+#include <linux/err.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <regex.h>
+#include "util/cpumap.h"
+#include "util/debug.h"
+#include "util/iostat.h"
+#include "util/counts.h"
+#include "path.h"
+
+struct iio_root_port {
+ u32 domain;
+ u8 bus;
+ u8 die;
+ u8 pmu_idx;
+ int idx;
+};
+
+struct iio_root_ports_list {
+ struct iio_root_port **rps;
+ int nr_entries;
+};
+
+static void iio_root_port_show(FILE *output,
+ const struct iio_root_port * const rp)
+{
+ if (output && rp)
+ fprintf(output, "S%d-uncore_iio_%d<%04x:%02x>\n",
+ rp->die, rp->pmu_idx, rp->domain, rp->bus);
+}
+
+static struct iio_root_port *iio_root_port_new(u32 domain, u8 bus,
+ u8 die, u8 pmu_idx)
+{
+ struct iio_root_port *p = calloc(1, sizeof(*p));
+
+ if (p) {
+ p->domain = domain;
+ p->bus = bus;
+ p->die = die;
+ p->pmu_idx = pmu_idx;
+ }
+ return p;
+}
+
+static struct iio_root_ports_list *iio_root_ports_list_new(void)
+{
+ struct iio_root_ports_list *list = calloc(1, sizeof(*list));
+
+ if (list) {
+ list->rps = calloc(1, sizeof(struct iio_root_port *));
+ if (!list->rps) {
+ free(list);
+ list = NULL;
+ }
+ }
+
+ return list;
+}
+
+static void iio_root_ports_list_free(struct iio_root_ports_list *list)
+{
+ int idx;
+
+ if (list) {
+ for (idx = 0; idx < list->nr_entries; idx++)
+ free(list->rps[idx]);
+ free(list->rps);
+ free(list);
+ }
+}
+
+static struct iio_root_port *iio_root_port_find_by_notation(
+ const struct iio_root_ports_list * const list, u32 domain, u8 bus)
+{
+ int idx;
+ struct iio_root_port *rp;
+
+ if (list) {
+ for (idx = 0; idx < list->nr_entries; idx++) {
+ rp = list->rps[idx];
+ if (rp && rp->domain == domain && rp->bus == bus)
+ return rp;
+ }
+ }
+ return NULL;
+}
+
+static int iio_root_ports_list_insert(struct iio_root_ports_list *list,
+ struct iio_root_port * const rp)
+{
+ struct iio_root_port **tmp_buf;
+
+ if (list && rp) {
+ rp->idx = list->nr_entries++;
+ /* One more for NULL.*/
+ tmp_buf = realloc(list->rps,
+ (list->nr_entries + 1) * sizeof(*list->rps));
+ if (!tmp_buf) {
+ pr_err("Failed to realloc memory\n");
+ return -ENOMEM;
+ }
+ tmp_buf[rp->idx] = rp;
+ tmp_buf[list->nr_entries] = NULL;
+ list->rps = tmp_buf;
+ }
+ return 0;
+}
--
2.19.1


2021-02-01 18:00:08

by Alexander Antonov

[permalink] [raw]
Subject: Re: [PATCH v3 3/5] perf stat: Helper functions for PCIe root ports list in iostat mode


On 1/29/2021 11:26 AM, Namhyung Kim wrote:
> Hello,
>
> On Tue, Jan 26, 2021 at 5:06 PM Alexander Antonov
> <[email protected]> wrote:
>> Introduce helper functions to control PCIe root ports list.
>> These helpers will be used in the follow-up patch.
>>
>> Signed-off-by: Alexander Antonov <[email protected]>
>> ---
> [SNIP]
>> +static int iio_root_ports_list_insert(struct iio_root_ports_list *list,
>> + struct iio_root_port * const rp)
>> +{
>> + struct iio_root_port **tmp_buf;
>> +
>> + if (list && rp) {
>> + rp->idx = list->nr_entries++;
>> + /* One more for NULL.*/
>> + tmp_buf = realloc(list->rps,
>> + (list->nr_entries + 1) * sizeof(*list->rps));
> Why is this +1 needed since you already have the number of
> entries in the list?
>
> Thanks,
> Namhyung

Hello,

My first approach for iteration through root ports list was using
NULL-terminated array.
And seems like I just forgot to remove this code. I will fix it.

Thank you,
Alexander

>
>> + if (!tmp_buf) {
>> + pr_err("Failed to realloc memory\n");
>> + return -ENOMEM;
>> + }
>> + tmp_buf[rp->idx] = rp;
>> + tmp_buf[list->nr_entries] = NULL;
>> + list->rps = tmp_buf;
>> + }
>> + return 0;
>> +}
>> --
>> 2.19.1
>>