]> www.infradead.org Git - users/hch/misc.git/commitdiff
tools/power turbostat: Add PMT directory iterator helper
authorPatryk Wlazlyn <patryk.wlazlyn@linux.intel.com>
Thu, 12 Dec 2024 17:59:25 +0000 (18:59 +0100)
committerLen Brown <len.brown@intel.com>
Mon, 27 Jan 2025 17:35:22 +0000 (11:35 -0600)
PMT directories exposed in sysfs use the following pattern:
  telem%u
for example:
  telem0, telem2, telem3, ..., telem15, telem16

This naming scheme preserves the ordering from the PCIe discovery, which
is important to correctly map the telemetry directory to the specific
domain (cpu, core, package etc).

Because readdir() traverses the entries in alphabetical order, causing
for example "telem13" to be traversed before "telem3", it is necessary
to use scandir() with custom compare() callback to preserve the PCIe
ordering.

Signed-off-by: Patryk Wlazlyn <patryk.wlazlyn@linux.intel.com>
Signed-off-by: Len Brown <len.brown@intel.com>
tools/power/x86/turbostat/turbostat.c

index 14c495886746300b867fb1de157d9fe9095c8f3c..6104d5bcca5c15e8e8bce16a78afda0cbf8670fe 100644 (file)
@@ -1589,6 +1589,93 @@ struct pmt_counter {
        struct pmt_domain_info *domains;
 };
 
+/*
+ * PMT telemetry directory iterator.
+ * Used to iterate telemetry files in sysfs in correct order.
+ */
+struct pmt_diriter_t
+{
+       DIR *dir;
+       struct dirent **namelist;
+       unsigned int num_names;
+       unsigned int current_name_idx;
+};
+
+int pmt_telemdir_filter(const struct dirent *e)
+{
+       unsigned int dummy;
+       return sscanf(e->d_name, "telem%u", &dummy);
+}
+
+int pmt_telemdir_sort(const struct dirent **a, const struct dirent **b)
+{
+       unsigned int aidx = 0, bidx = 0;
+
+       sscanf((*a)->d_name, "telem%u", &aidx);
+       sscanf((*b)->d_name, "telem%u", &bidx);
+
+       return aidx >= bidx;
+}
+
+const struct dirent* pmt_diriter_next(struct pmt_diriter_t *iter)
+{
+       const struct dirent *ret = NULL;
+
+       if (!iter->dir)
+               return NULL;
+
+       if (iter->current_name_idx >= iter->num_names)
+               return NULL;
+
+       ret = iter->namelist[iter->current_name_idx];
+       ++iter->current_name_idx;
+
+       return ret;
+}
+
+const struct dirent* pmt_diriter_begin(struct pmt_diriter_t *iter, const char *pmt_root_path)
+{
+       int num_names = iter->num_names;
+
+       if (!iter->dir) {
+               iter->dir = opendir(pmt_root_path);
+               if (iter->dir == NULL)
+                       return NULL;
+
+               num_names = scandir(pmt_root_path, &iter->namelist, pmt_telemdir_filter, pmt_telemdir_sort);
+               if (num_names == -1)
+                       return NULL;
+       }
+
+       iter->current_name_idx = 0;
+       iter->num_names = num_names;
+
+       return pmt_diriter_next(iter);
+}
+
+void pmt_diriter_init(struct pmt_diriter_t *iter)
+{
+       memset(iter, 0, sizeof(*iter));
+}
+
+void pmt_diriter_remove(struct pmt_diriter_t *iter)
+{
+       if (iter->namelist) {
+               for (unsigned int i = 0; i < iter->num_names; i++) {
+                       free(iter->namelist[i]);
+                       iter->namelist[i] = NULL;
+               }
+       }
+
+       free(iter->namelist);
+       iter->namelist = NULL;
+       iter->num_names = 0;
+       iter->current_name_idx = 0;
+
+       closedir(iter->dir);
+       iter->dir = NULL;
+}
+
 unsigned int pmt_counter_get_width(const struct pmt_counter *p)
 {
        return (p->msb - p->lsb) + 1;