[Intel-gfx] [PATCH i-g-t 6/9] intel-gpu-overlay: Use RAPL PMU for power reading

Tvrtko Ursulin tursulin at ursulin.net
Tue Oct 10 09:30:05 UTC 2017


From: Tvrtko Ursulin <tvrtko.ursulin at intel.com>

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

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin at intel.com>
---
 lib/igt_perf.c  |  16 ++++--
 lib/igt_perf.h  |   1 +
 overlay/power.c | 156 ++++++++++++++++++++++++++++++++++++++++++--------------
 overlay/power.h |   2 +
 4 files changed, 133 insertions(+), 42 deletions(-)

diff --git a/lib/igt_perf.c b/lib/igt_perf.c
index 208474302fcc..0221461e918f 100644
--- a/lib/igt_perf.c
+++ b/lib/igt_perf.c
@@ -27,11 +27,12 @@ uint64_t i915_type_id(void)
 	return strtoull(buf, NULL, 0);
 }
 
-static int _perf_open(uint64_t config, int group, uint64_t format)
+static int
+_perf_open(uint64_t type, uint64_t config, int group, uint64_t format)
 {
 	struct perf_event_attr attr = { };
 
-	attr.type = i915_type_id();
+	attr.type = type;
 	if (attr.type == 0)
 		return -ENOENT;
 
@@ -46,11 +47,18 @@ static int _perf_open(uint64_t config, int group, uint64_t format)
 
 int perf_i915_open(uint64_t config)
 {
-	return _perf_open(config, -1, PERF_FORMAT_TOTAL_TIME_ENABLED);
+	return _perf_open(i915_type_id(), config, -1,
+			  PERF_FORMAT_TOTAL_TIME_ENABLED);
 }
 
 int perf_i915_open_group(uint64_t config, int group)
 {
-	return _perf_open(config, group,
+	return _perf_open(i915_type_id(), config, group,
 			  PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_GROUP);
 }
+
+int igt_perf_open(uint64_t type, uint64_t config)
+{
+	return _perf_open(type, config, -1,
+			  PERF_FORMAT_TOTAL_TIME_ENABLED);
+}
diff --git a/lib/igt_perf.h b/lib/igt_perf.h
index 285823786324..b1f525739c69 100644
--- a/lib/igt_perf.h
+++ b/lib/igt_perf.h
@@ -99,5 +99,6 @@ perf_event_open(struct perf_event_attr *attr,
 uint64_t i915_type_id(void);
 int perf_i915_open(uint64_t config);
 int perf_i915_open_group(uint64_t config, int group);
+int igt_perf_open(uint64_t type, uint64_t config);
 
 #endif /* I915_PERF_H */
diff --git a/overlay/power.c b/overlay/power.c
index 805f4ca7805c..35e446e6bce5 100644
--- a/overlay/power.c
+++ b/overlay/power.c
@@ -30,60 +30,138 @@
 #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? */
+static uint64_t filename_to_u64(const char *filename, int base)
+{
+	char buf[64], *b;
+	ssize_t ret;
+	int fd;
 
-int power_init(struct power *power)
+	fd = open(filename, O_RDONLY);
+	if (fd < 0)
+		return 0;
+
+	ret = read(fd, buf, sizeof(buf) - 1);
+	close(fd);
+	if (ret < 1)
+		return 0;
+
+	buf[ret] = '\0';
+
+	b = buf;
+	while (*b && !isdigit(*b))
+		b++;
+
+	return strtoull(b, NULL, base);
+}
+
+static uint64_t debugfs_file_to_u64(const char *name)
 {
-	char buf[4096];
-	int fd, len;
+	char buf[1024];
 
-	memset(power, 0, sizeof(*power));
+	snprintf(buf, sizeof(buf), "%s/%s", debugfs_dri_path, name);
+
+	return filename_to_u64(buf, 0);
+}
 
-	power->fd = -1;
+static uint64_t rapl_type_id(void)
+{
+	return filename_to_u64("/sys/devices/power/type", 10);
+}
 
-	sprintf(buf, "%s/i915_energy_uJ", debugfs_dri_path);
-	fd = open(buf, 0);
+static uint64_t rapl_gpu_power(void)
+{
+	return filename_to_u64("/sys/devices/power/events/energy-gpu", 0);
+}
+
+static double filename_to_double(const char *filename)
+{
+	char *dot = NULL, *e = NULL;
+	unsigned long long int decimal;
+	char buf[64], *b;
+	long int val;
+	long int exponent;
+	double result;
+	ssize_t ret;
+	int fd;
+
+	fd = open(filename, O_RDONLY);
 	if (fd < 0)
-		return power->error = errno;
+		return NAN;
 
-	len = read(fd, buf, sizeof(buf));
+	ret = read(fd, buf, sizeof(buf) - 1);
 	close(fd);
+	if (ret < 1)
+		return NAN;
+
+	buf[ret] = '\0';
+
+	b = buf;
+	while (*b) {
+		if (*b == '.')
+			dot = b;
+		else if (*b == 'e')
+			e = b;
+		b++;
+	}
 
-	if (len < 0)
-		return power->error = errno;
+	if (!dot || !e)
+		return NAN;
 
-	buf[len] = '\0';
-	if (strtoull(buf, 0, 0) == 0)
-		return power->error = EINVAL;
+	*dot = '\0';
+	*e = '\0';
 
-	return 0;
+	/* Reduce precision to fit in long int. */
+	if ((e - dot) > 18)
+		dot[18] = '\0';
+
+	val = strtoll(buf, NULL, 10);
+	decimal = strtoull(++dot, NULL, 10);
+	exponent = strtoll(++e, NULL, 10);
+
+	result = (double)decimal;
+	result /= round(pow(10, strlen(dot)));
+	result += val;
+	result *= pow(10, exponent);
+
+	return result;
 }
 
-static uint64_t file_to_u64(const char *name)
+static double rapl_gpu_power_scale(void)
 {
-	char buf[4096];
-	int fd, len;
+	return filename_to_double("/sys/devices/power/events/energy-gpu.scale");
+}
 
-	sprintf(buf, "%s/%s", debugfs_dri_path, name);
-	fd = open(buf, 0);
-	if (fd < 0)
-		return 0;
+int power_init(struct power *power)
+{
+	uint64_t val;
 
-	len = read(fd, buf, sizeof(buf)-1);
-	close(fd);
+	memset(power, 0, sizeof(*power));
 
-	if (len < 0)
-		return 0;
+	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;
+		}
+	}
 
-	buf[len] = '\0';
+	val = debugfs_file_to_u64("i915_energy_uJ");
+	if (val == -1)
+		return power->error = errno;
+	else if (val == 0)
+		return power->error = EINVAL;
 
-	return strtoull(buf, 0, 0);
+	return 0;
 }
 
 static uint64_t clock_ms_to_u64(void)
@@ -93,30 +171,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 +202,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 bf8346ce46b4..28abfc32234b 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);
-- 
2.9.5



More information about the Intel-gfx mailing list