summaryrefslogtreecommitdiff
path: root/overlay
diff options
context:
space:
mode:
authorTvrtko Ursulin <tvrtko.ursulin@intel.com>2017-10-10 08:22:30 +0100
committerTvrtko Ursulin <tvrtko.ursulin@intel.com>2017-11-22 11:19:14 +0000
commit273e66baef8bf930959c503041b68411bb42ff80 (patch)
treef5e331f8979540cd1488597c491c5380fcd55f4a /overlay
parent0d8385a7ad670e96dadef6e04e7541a64b637406 (diff)
intel-gpu-overlay: Use RAPL PMU for power reading
Wire up to the RAPL PMU for GPU energy readings. The only complication is that we have to add code to parse: # cat /sys/devices/power/events/energy-gpu.scale 2.3283064365386962890625e-10 v2: Link with -lm. v3: strtod can handle scientific notation, even though my initial reading of the man page did not spot that. (Chris Wilson) v4: Meson fix. Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'overlay')
-rw-r--r--overlay/Makefile.am2
-rw-r--r--overlay/meson.build2
-rw-r--r--overlay/power.c127
-rw-r--r--overlay/power.h2
4 files changed, 92 insertions, 41 deletions
diff --git a/overlay/Makefile.am b/overlay/Makefile.am
index cefde2d0..f49f54ac 100644
--- a/overlay/Makefile.am
+++ b/overlay/Makefile.am
@@ -63,7 +63,7 @@ intel_gpu_overlay_SOURCES += \
intel_gpu_overlay_SOURCES += $(both_x11_sources)
-intel_gpu_overlay_LDADD = $(LDADD) -lrt
+intel_gpu_overlay_LDADD = $(LDADD) -lrt -lm
EXTRA_DIST= \
README \
diff --git a/overlay/meson.build b/overlay/meson.build
index ffc011cc..6b479eb8 100644
--- a/overlay/meson.build
+++ b/overlay/meson.build
@@ -21,7 +21,7 @@ dri2proto = dependency('dri2proto', version : '>= 2.6', required : false)
cairo_xlib = dependency('cairo-xlib', required : false)
xrandr = dependency('xrandr', version : '>=1.3', required : false)
-gpu_overlay_deps = [ realtime, cairo, pciaccess, libdrm, libdrm_intel ]
+gpu_overlay_deps = [ realtime, math, cairo, pciaccess, libdrm, libdrm_intel ]
both_x11_src = ''
diff --git a/overlay/power.c b/overlay/power.c
index 805f4ca7..9ac90fde 100644
--- a/overlay/power.c
+++ b/overlay/power.c
@@ -30,60 +30,107 @@
#include <fcntl.h>
#include <time.h>
#include <errno.h>
+#include <ctype.h>
+#include <math.h>
#include "igt_perf.h"
#include "power.h"
#include "debugfs.h"
-/* XXX Is this exposed through RAPL? */
-
-int power_init(struct power *power)
+static int
+filename_to_buf(const char *filename, char *buf, unsigned int bufsize)
{
- char buf[4096];
- int fd, len;
-
- memset(power, 0, sizeof(*power));
-
- power->fd = -1;
+ int fd;
+ ssize_t ret;
- sprintf(buf, "%s/i915_energy_uJ", debugfs_dri_path);
- fd = open(buf, 0);
+ fd = open(filename, O_RDONLY);
if (fd < 0)
- return power->error = errno;
+ return -1;
- len = read(fd, buf, sizeof(buf));
+ ret = read(fd, buf, bufsize - 1);
close(fd);
+ if (ret < 1)
+ return -1;
- if (len < 0)
- return power->error = errno;
-
- buf[len] = '\0';
- if (strtoull(buf, 0, 0) == 0)
- return power->error = EINVAL;
+ buf[ret] = '\0';
return 0;
}
-static uint64_t file_to_u64(const char *name)
+static uint64_t filename_to_u64(const char *filename, int base)
{
- char buf[4096];
- int fd, len;
+ char buf[64], *b;
- sprintf(buf, "%s/%s", debugfs_dri_path, name);
- fd = open(buf, 0);
- if (fd < 0)
+ if (filename_to_buf(filename, buf, sizeof(buf)))
return 0;
- len = read(fd, buf, sizeof(buf)-1);
- close(fd);
+ /*
+ * Handle both single integer and key=value formats by skipping
+ * leading non-digits.
+ */
+ b = buf;
+ while (*b && !isdigit(*b))
+ b++;
+
+ return strtoull(b, NULL, base);
+}
+
+static uint64_t debugfs_file_to_u64(const char *name)
+{
+ char buf[1024];
+
+ snprintf(buf, sizeof(buf), "%s/%s", debugfs_dri_path, name);
+
+ return filename_to_u64(buf, 0);
+}
+
+static uint64_t rapl_type_id(void)
+{
+ return filename_to_u64("/sys/devices/power/type", 10);
+}
+
+static uint64_t rapl_gpu_power(void)
+{
+ return filename_to_u64("/sys/devices/power/events/energy-gpu", 0);
+}
- if (len < 0)
+static double filename_to_double(const char *filename)
+{
+ char buf[64];
+
+ if (filename_to_buf(filename, buf, sizeof(buf)))
return 0;
- buf[len] = '\0';
+ return strtod(buf, NULL);
+}
+
+static double rapl_gpu_power_scale(void)
+{
+ return filename_to_double("/sys/devices/power/events/energy-gpu.scale");
+}
+
+int power_init(struct power *power)
+{
+ uint64_t val;
+
+ memset(power, 0, sizeof(*power));
+
+ power->fd = igt_perf_open(rapl_type_id(), rapl_gpu_power());
+ if (power->fd >= 0) {
+ power->rapl_scale = rapl_gpu_power_scale();
+
+ if (power->rapl_scale != NAN) {
+ power->rapl_scale *= 1e3; /* from nano to micro */
+ return 0;
+ }
+ }
+
+ val = debugfs_file_to_u64("i915_energy_uJ");
+ if (val == 0)
+ return power->error = EINVAL;
- return strtoull(buf, 0, 0);
+ return 0;
}
static uint64_t clock_ms_to_u64(void)
@@ -93,30 +140,30 @@ static uint64_t clock_ms_to_u64(void)
if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0)
return 0;
- return (uint64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000;
+ return (uint64_t)tv.tv_sec * 1e3 + tv.tv_nsec / 1e6;
}
int power_update(struct power *power)
{
- struct power_stat *s = &power->stat[power->count++&1];
- struct power_stat *d = &power->stat[power->count&1];
+ struct power_stat *s = &power->stat[power->count++ & 1];
+ struct power_stat *d = &power->stat[power->count & 1];
uint64_t d_time;
if (power->error)
return power->error;
- if (power->fd != -1) {
+ if (power->fd >= 0) {
uint64_t data[2];
int len;
len = read(power->fd, data, sizeof(data));
- if (len < 0)
+ if (len != sizeof(data))
return power->error = errno;
- s->energy = data[0];
- s->timestamp = data[1] / (1000*1000);
+ s->energy = llround((double)data[0] * power->rapl_scale);
+ s->timestamp = data[1] / 1e6;
} else {
- s->energy = file_to_u64("i915_energy_uJ");
+ s->energy = debugfs_file_to_u64("i915_energy_uJ") / 1e3;
s->timestamp = clock_ms_to_u64();
}
@@ -124,7 +171,9 @@ int power_update(struct power *power)
return EAGAIN;
d_time = s->timestamp - d->timestamp;
- power->power_mW = (s->energy - d->energy) / d_time;
+ power->power_mW = round((double)(s->energy - d->energy) *
+ (1e3f / d_time));
power->new_sample = 1;
+
return 0;
}
diff --git a/overlay/power.h b/overlay/power.h
index bf8346ce..28abfc32 100644
--- a/overlay/power.h
+++ b/overlay/power.h
@@ -39,6 +39,8 @@ struct power {
int new_sample;
uint64_t power_mW;
+
+ double rapl_scale;
};
int power_init(struct power *power);