[i-g-t,2/2] overlay: Show total package power
diff mbox series

Message ID 20191013135021.31957-2-chris@chris-wilson.co.uk
State New
Headers show
Series
  • [i-g-t,1/2] lib: Generalise rapl interface
Related show

Commit Message

Chris Wilson Oct. 13, 2019, 1:50 p.m. UTC
Add the total package power after the GPU package power, for reference.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
---
 overlay/overlay.c | 18 ++++++----
 overlay/power.c   | 88 +++++++++++++++++++----------------------------
 overlay/power.h   | 21 +++++------
 3 files changed, 59 insertions(+), 68 deletions(-)

Patch
diff mbox series

diff --git a/overlay/overlay.c b/overlay/overlay.c
index eae5ddfa8..dd4fca29e 100644
--- a/overlay/overlay.c
+++ b/overlay/overlay.c
@@ -609,12 +609,12 @@  static void show_gpu_freq(struct overlay_context *ctx, struct overlay_gpu_freq *
 	}
 
 	if (has_power) {
-		chart_add_sample(&gf->power_chart, gf->power.power_mW);
-		if (gf->power.new_sample) {
-			if (gf->power.power_mW > gf->power_max)
-				gf->power_max = gf->power.power_mW;
+		chart_add_sample(&gf->power_chart, gf->power.gpu.power_mW);
+		if (gf->power.gpu.new_sample) {
+			if (gf->power.gpu.power_mW > gf->power_max)
+				gf->power_max = gf->power.gpu.power_mW;
 			chart_set_range(&gf->power_chart, 0, gf->power_max);
-			gf->power.new_sample = 0;
+			gf->power.gpu.new_sample = 0;
 		}
 		chart_draw(&gf->power_chart, ctx->cr);
 	}
@@ -700,8 +700,14 @@  static void show_gpu_freq(struct overlay_context *ctx, struct overlay_gpu_freq *
 	}
 
 	if (has_power) {
-		sprintf(buf, "Power: %llumW", (long long unsigned)gf->power.power_mW);
 		cairo_set_source_rgba(ctx->cr, 1, 1, 1, 1);
+
+		sprintf(buf, "Power: %llumW", (long long unsigned)gf->power.gpu.power_mW);
+		cairo_move_to(ctx->cr, PAD, y);
+		cairo_show_text(ctx->cr, buf);
+		y += 14;
+
+		sprintf(buf, "Package: %llumW", (long long unsigned)gf->power.pkg.power_mW);
 		cairo_move_to(ctx->cr, PAD, y);
 		cairo_show_text(ctx->cr, buf);
 		y += 14;
diff --git a/overlay/power.c b/overlay/power.c
index 0f99e2a4a..76fafea91 100644
--- a/overlay/power.c
+++ b/overlay/power.c
@@ -77,15 +77,6 @@  static uint64_t filename_to_u64(const char *filename, int base)
 	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);
@@ -96,6 +87,11 @@  static uint64_t rapl_gpu_power(void)
 	return filename_to_u64("/sys/devices/power/events/energy-gpu", 0);
 }
 
+static uint64_t rapl_pkg_power(void)
+{
+	return filename_to_u64("/sys/devices/power/events/energy-pkg", 0);
+}
+
 static double filename_to_double(const char *filename)
 {
 	char *oldlocale;
@@ -117,70 +113,58 @@  static double rapl_gpu_power_scale(void)
 	return filename_to_double("/sys/devices/power/events/energy-gpu.scale");
 }
 
-int power_init(struct power *power)
+static double rapl_pkg_power_scale(void)
 {
-	uint64_t val;
+	return filename_to_double("/sys/devices/power/events/energy-pkg.scale");
+}
 
+int power_init(struct power *power)
+{
 	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;
-		}
-	}
+	power->gpu.fd = igt_perf_open(rapl_type_id(), rapl_gpu_power());
+	if (power->gpu.fd < 0)
+		return power->error = ENOENT;
+	power->gpu.scale = rapl_gpu_power_scale() * 1e3; /* to milli */
 
-	val = debugfs_file_to_u64("i915_energy_uJ");
-	if (val == 0)
-		return power->error = EINVAL;
+	power->pkg.fd = igt_perf_open(rapl_type_id(), rapl_pkg_power());
+	power->pkg.scale = rapl_pkg_power_scale() *1e3; /* to milli */
 
 	return 0;
 }
 
-static uint64_t clock_ms_to_u64(void)
+static void __power_update(struct power_domain *pd, int count)
 {
-	struct timespec tv;
+	struct power_stat *s = &pd->stat[count & 1];
+	struct power_stat *d = &pd->stat[(count + 1) & 1];
+	uint64_t data[2], d_time;
+	int len;
 
-	if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0)
-		return 0;
+	len = read(pd->fd, data, sizeof(data));
+	if (len != sizeof(data))
+		return;
 
-	return (uint64_t)tv.tv_sec * 1e3 + tv.tv_nsec / 1e6;
+	s->energy = llround((double)data[0] * pd->scale);
+	s->timestamp = data[1];
+
+	if (!count)
+		return;
+
+	d_time = s->timestamp - d->timestamp;
+	pd->power_mW = round((s->energy - d->energy) * 1e9 / d_time);
+	pd->new_sample = 1;
 }
 
 int power_update(struct power *power)
 {
-	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 >= 0) {
-		uint64_t data[2];
-		int len;
-
-		len = read(power->fd, data, sizeof(data));
-		if (len != sizeof(data))
-			return power->error = errno;
-
-		s->energy = llround((double)data[0] * power->rapl_scale);
-		s->timestamp = data[1] / 1e6;
-	} else {
-		s->energy = debugfs_file_to_u64("i915_energy_uJ") / 1e3;
-		s->timestamp = clock_ms_to_u64();
-	}
+	__power_update(&power->gpu, power->count);
+	__power_update(&power->pkg, power->count);
 
-	if (power->count == 1)
+	if (!power->count++)
 		return EAGAIN;
 
-	d_time = s->timestamp - d->timestamp;
-	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 28abfc322..be9f1e219 100644
--- a/overlay/power.h
+++ b/overlay/power.h
@@ -28,19 +28,20 @@ 
 #include <stdint.h>
 
 struct power {
-	struct power_stat {
-		uint64_t energy;
-		uint64_t timestamp;
-	} stat[2];
+	struct power_domain {
+		double scale;
+		uint64_t power_mW;
+		int new_sample;
+		int fd;
+
+		struct power_stat {
+			uint64_t energy;
+			uint64_t timestamp;
+		} stat[2];
+	} gpu, pkg;
 
-	int fd;
 	int error;
 	int count;
-	int new_sample;
-
-	uint64_t power_mW;
-
-	double rapl_scale;
 };
 
 int power_init(struct power *power);