summaryrefslogtreecommitdiff
path: root/lib/igt_rapl.c
blob: 03e49226024d92556930be605fbdd206cc15a3c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
}