Hi Vinay,

Reviewing this since it's also in Riana's engine activity series. Mostly recommendations.
On Tue, Feb 04, 2025 at 06:10:55PM -0800, Vinay Belgaumkar wrote:
Functions to parse event ID and GT bit shift for PMU events.

v2: Review comments (Riana)
v3: Review comments (Lucas)

Cc: Riana Tauro <riana.ta...@intel.com>
Cc: Lucas De Marchi <lucas.demar...@intel.com>
Cc: Kamil Konieczny <kamil.koniec...@linux.intel.com>
Cc: Rodrigo Vivi <rodrigo.v...@intel.com>
Reviewed-by: Riana Tauro <riana.ta...@intel.com>
Signed-off-by: Vinay Belgaumkar <vinay.belgaum...@intel.com>
---
lib/igt_perf.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++
lib/igt_perf.h |  2 ++
2 files changed, 72 insertions(+)

diff --git a/lib/igt_perf.c b/lib/igt_perf.c
index 3866c6d77..f021fc3ec 100644
--- a/lib/igt_perf.c
+++ b/lib/igt_perf.c
@@ -92,6 +92,76 @@ const char *xe_perf_device(int xe, char *buf, int buflen)
        return buf;
}

+/**
+ * perf_event_format: Returns the start/end positions of an event format param
+ * @device: PMU device
+ * @param: Parameter for which you need the format start/end bits
+ * Returns: 0 on success or negative error code
+ */
+int perf_event_format(const char *device, const char *param, uint32_t *start, 
uint32_t *end)
+{
+       char buf[NAME_MAX];
+       ssize_t bytes;
+       int ret;
+       int fd;

You could cache the format definitions after reading them once.

+
+       snprintf(buf, sizeof(buf),
+                "/sys/bus/event_source/devices/%s/format/%s",
+                device, param);
+
+       fd = open(buf, O_RDONLY | O_CLOEXEC);
+       if (fd < 0)
+               return -EINVAL;

I would do an igt_assert for these since that will also print the errno automatically since errno will point to what went wrong.

igt_assert(fd >= 0);

+
+       bytes = read(fd, buf, sizeof(buf) - 1);
+       close(fd);
+       if (bytes < 1)
+               return -EINVAL;

Same here. Are you expecting a specific number of bytes? an assert should be helpful.

+
+       buf[bytes] = '\0';
+       ret = sscanf(buf, "config:%u-%u", start, end);
+       if (ret != 2)
+               return -EINVAL;

In short, I would recommend asserts in this helper so that caller is less concerned about error checking and the asserts will also dump the errno value for debug.

Same comments for perf_event_config below.

Thanks,
Umesh
+
+       return ret;
+}
+
+/**
+ * perf_event_config:
+ * @device: Device string in driver:pci format
+ * @event: The event name
+ * @config: Pointer to the config
+ * Returns: 0 for success, negative value on error
+ */
+int perf_event_config(const char *device, const char *event, uint64_t *config)
+{
+       char buf[NAME_MAX];
+       ssize_t bytes;
+       int ret;
+       int fd;
+
+       snprintf(buf, sizeof(buf),
+                "/sys/bus/event_source/devices/%s/events/%s",
+                device,
+                event);
+
+       fd = open(buf, O_RDONLY);
+       if (fd < 0)
+               return -EINVAL;
+
+       bytes = read(fd, buf, sizeof(buf) - 1);
+       close(fd);
+       if (bytes < 1)
+               return ret;
+
+       buf[bytes] = '\0';
+       ret = sscanf(buf, "event=0x%lx", config);
+       if (ret != 1)
+               return -EINVAL;
+
+       return 0;
+}
+
uint64_t xe_perf_type_id(int xe)
{
        char buf[80];
diff --git a/lib/igt_perf.h b/lib/igt_perf.h
index 3d9ba2917..69f7a3d74 100644
--- a/lib/igt_perf.h
+++ b/lib/igt_perf.h
@@ -71,5 +71,7 @@ int perf_i915_open(int i915, uint64_t config);
int perf_i915_open_group(int i915, uint64_t config, int group);

int perf_xe_open(int xe, uint64_t config);
+int perf_event_config(const char *device, const char *event, uint64_t *config);
+int perf_event_format(const char *device, const char *param, uint32_t *start, 
uint32_t *end);

#endif /* I915_PERF_H */
--
2.38.1

Reply via email to