summaryrefslogtreecommitdiff
path: root/lib/igt_rapl.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2019-10-13 13:53:43 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2019-10-15 14:54:09 +0100
commite293051f8f99c72cb01d21e4b73a5928ea351eb3 (patch)
treef8d65c3f5c83a890bdc33e23428702b82edb7f04 /lib/igt_rapl.c
parent1a644320e76f6d7cb13462804b5a8897c9b7d057 (diff)
lib: Generalise rapl interface
We can use our existing rapl interface that monitors gpu power, to also sample the other rapl domains such as package, cores and ram. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: Andi Shyti <andi.shyti@intel.com> Reviewed-by: Andi Shyti <andi.shyti@intel.com>
Diffstat (limited to 'lib/igt_rapl.c')
-rw-r--r--lib/igt_rapl.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/igt_rapl.c b/lib/igt_rapl.c
new file mode 100644
index 00000000..03e49226
--- /dev/null
+++ b/lib/igt_rapl.c
@@ -0,0 +1,69 @@
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <locale.h>
+#include <math.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+#include "igt_perf.h"
+#include "igt_rapl.h"
+#include "igt_sysfs.h"
+
+static int rapl_parse(struct rapl *r, const char *str)
+{
+ locale_t locale, oldlocale;
+ bool result = true;
+ char buf[128];
+ int dir;
+
+ memset(r, 0, sizeof(*r));
+
+ dir = open("/sys/devices/power", O_RDONLY);
+ if (dir < 0)
+ return -errno;
+
+ /* Replace user environment with plain C to match kernel format */
+ locale = newlocale(LC_ALL, "C", 0);
+ oldlocale = uselocale(locale);
+
+ result &= igt_sysfs_scanf(dir, "type", "%"PRIu64, &r->type) == 1;
+
+ snprintf(buf, sizeof(buf), "events/energy-%s", str);
+ result &= igt_sysfs_scanf(dir, buf, "event=%"PRIx64, &r->power) == 1;
+
+ snprintf(buf, sizeof(buf), "events/energy-%s.scale", str);
+ result &= igt_sysfs_scanf(dir, buf, "%lf", &r->scale) == 1;
+
+ uselocale(oldlocale);
+ freelocale(locale);
+
+ close(dir);
+
+ if (!result)
+ return -EINVAL;
+
+ if (isnan(r->scale) || !r->scale)
+ return -ERANGE;
+
+ return 0;
+}
+
+int rapl_open(struct rapl *r, const char *domain)
+{
+ r->fd = rapl_parse(r, domain);
+ if (r->fd < 0)
+ goto err;
+
+ r->fd = igt_perf_open(r->type, r->power);
+ if (r->fd < 0) {
+ r->fd = -errno;
+ goto err;
+ }
+
+ return 0;
+
+err:
+ errno = 0;
+ return r->fd;
+}