PM / EM: Expose the Energy Model in sysfs
authorQuentin Perret <quentin.perret@arm.com>
Thu, 17 May 2018 14:50:07 +0000 (15:50 +0100)
committerDouglas RAILLARD <douglas.raillard@arm.com>
Tue, 14 Aug 2018 15:32:15 +0000 (16:32 +0100)
Expose the Energy Model (read-only) of all frequency domains in sysfs
for convenience. To do so, add a kobject to the CPU subsystem under the
umbrella of which a kobject for each frequency domain is attached.

The resulting hierarchy is as follows for a platform with two frequency
domains for example:

   /sys/devices/system/cpu/energy_model
   ├── fd0
   │   ├── cost
   │   ├── cpus
   │   ├── frequency
   │   └── power
   └── fd4
       ├── cost
       ├── cpus
       ├── frequency
       └── power

In this implementation, the kobject abstraction is only used as a
convenient way of exposing data to sysfs. However, it could also be
used in the future to allocate and release frequency domains in a more
dynamic way using reference counting.

Cc: Peter Zijlstra <peterz@infradead.org>
Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
Signed-off-by: Quentin Perret <quentin.perret@arm.com>
include/linux/energy_model.h
kernel/power/energy_model.c

index be822ce05c17ec4f9467db5da8d93984af6ee85a..4c3a98bd1df4f0df8d520e2113627aeada08de3c 100644 (file)
@@ -19,6 +19,7 @@ struct em_cap_state {
 struct em_freq_domain {
        struct em_cap_state *table; /* Capacity states, in ascending order. */
        int nr_cap_states;
+       struct kobject kobj;
        unsigned long cpus[0]; /* CPUs of the frequency domain. */
 };
 
index 39740fe728ea76a33f62b6d26cf8162fcbbb9216..aa6c0bc5e784cf00afd58c1189f7bc370da1f730 100644 (file)
@@ -23,6 +23,82 @@ static DEFINE_PER_CPU(struct em_freq_domain *, em_data);
  */
 static DEFINE_MUTEX(em_fd_mutex);
 
+static struct kobject *em_kobject;
+
+/* Getters for the attributes of em_freq_domain objects */
+struct em_fd_attr {
+       struct attribute attr;
+       ssize_t (*show)(struct em_freq_domain *fd, char *buf);
+       ssize_t (*store)(struct em_freq_domain *fd, const char *buf, size_t s);
+};
+
+#define EM_ATTR_LEN 13
+#define show_table_attr(_attr) \
+static ssize_t show_##_attr(struct em_freq_domain *fd, char *buf) \
+{ \
+       ssize_t cnt = 0; \
+       int i; \
+       for (i = 0; i < fd->nr_cap_states; i++) { \
+               if (cnt >= (ssize_t) (PAGE_SIZE / sizeof(char) \
+                                     - (EM_ATTR_LEN + 2))) \
+                       goto out; \
+               cnt += scnprintf(&buf[cnt], EM_ATTR_LEN + 1, "%lu ", \
+                                fd->table[i]._attr); \
+       } \
+out: \
+       cnt += sprintf(&buf[cnt], "\n"); \
+       return cnt; \
+}
+
+show_table_attr(power);
+show_table_attr(frequency);
+show_table_attr(cost);
+
+static ssize_t show_cpus(struct em_freq_domain *fd, char *buf)
+{
+       return sprintf(buf, "%*pbl\n", cpumask_pr_args(to_cpumask(fd->cpus)));
+}
+
+#define fd_attr(_name) em_fd_##_name##_attr
+#define define_fd_attr(_name) static struct em_fd_attr fd_attr(_name) = \
+               __ATTR(_name, 0444, show_##_name, NULL)
+
+define_fd_attr(power);
+define_fd_attr(frequency);
+define_fd_attr(cost);
+define_fd_attr(cpus);
+
+static struct attribute *em_fd_default_attrs[] = {
+       &fd_attr(power).attr,
+       &fd_attr(frequency).attr,
+       &fd_attr(cost).attr,
+       &fd_attr(cpus).attr,
+       NULL
+};
+
+#define to_fd(k) container_of(k, struct em_freq_domain, kobj)
+#define to_fd_attr(a) container_of(a, struct em_fd_attr, attr)
+
+static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
+{
+       struct em_freq_domain *fd = to_fd(kobj);
+       struct em_fd_attr *fd_attr = to_fd_attr(attr);
+       ssize_t ret;
+
+       ret = fd_attr->show(fd, buf);
+
+       return ret;
+}
+
+static const struct sysfs_ops em_fd_sysfs_ops = {
+       .show   = show,
+};
+
+static struct kobj_type ktype_em_fd = {
+       .sysfs_ops      = &em_fd_sysfs_ops,
+       .default_attrs  = em_fd_default_attrs,
+};
+
 static struct em_freq_domain *em_create_fd(cpumask_t *span, int nr_states,
                                                struct em_data_callback *cb)
 {
@@ -102,6 +178,11 @@ static struct em_freq_domain *em_create_fd(cpumask_t *span, int nr_states,
        fd->nr_cap_states = nr_states;
        cpumask_copy(to_cpumask(fd->cpus), span);
 
+       ret = kobject_init_and_add(&fd->kobj, &ktype_em_fd, em_kobject,
+                                  "fd%u", cpu);
+       if (ret)
+               pr_err("fd%d: failed kobject_init_and_add(): %d\n", cpu, ret);
+
        return fd;
 
 free_cs_table:
@@ -155,6 +236,15 @@ int em_register_freq_domain(cpumask_t *span, unsigned int nr_states,
         */
        mutex_lock(&em_fd_mutex);
 
+       if (!em_kobject) {
+               em_kobject = kobject_create_and_add("energy_model",
+                                               &cpu_subsys.dev_root->kobj);
+               if (!em_kobject) {
+                       ret = -ENODEV;
+                       goto unlock;
+               }
+       }
+
        for_each_cpu(cpu, span) {
                /* Make sure we don't register again an existing domain. */
                if (READ_ONCE(per_cpu(em_data, cpu))) {