1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <locale.h>
35 #include <math.h>
36
37 #include "igt_perf.h"
38
39 #include "power.h"
40 #include "debugfs.h"
41
42 static int
filename_to_buf(const char * filename,char * buf,unsigned int bufsize)43 filename_to_buf(const char *filename, char *buf, unsigned int bufsize)
44 {
45 int fd;
46 ssize_t ret;
47
48 fd = open(filename, O_RDONLY);
49 if (fd < 0)
50 return -1;
51
52 ret = read(fd, buf, bufsize - 1);
53 close(fd);
54 if (ret < 1)
55 return -1;
56
57 buf[ret] = '\0';
58
59 return 0;
60 }
61
filename_to_u64(const char * filename,int base)62 static uint64_t filename_to_u64(const char *filename, int base)
63 {
64 char buf[64], *b;
65
66 if (filename_to_buf(filename, buf, sizeof(buf)))
67 return 0;
68
69 /*
70 * Handle both single integer and key=value formats by skipping
71 * leading non-digits.
72 */
73 b = buf;
74 while (*b && !isdigit(*b))
75 b++;
76
77 return strtoull(b, NULL, base);
78 }
79
debugfs_file_to_u64(const char * name)80 static uint64_t debugfs_file_to_u64(const char *name)
81 {
82 char buf[1024];
83
84 snprintf(buf, sizeof(buf), "%s/%s", debugfs_dri_path, name);
85
86 return filename_to_u64(buf, 0);
87 }
88
rapl_type_id(void)89 static uint64_t rapl_type_id(void)
90 {
91 return filename_to_u64("/sys/devices/power/type", 10);
92 }
93
rapl_gpu_power(void)94 static uint64_t rapl_gpu_power(void)
95 {
96 return filename_to_u64("/sys/devices/power/events/energy-gpu", 0);
97 }
98
filename_to_double(const char * filename)99 static double filename_to_double(const char *filename)
100 {
101 char *oldlocale;
102 char buf[80];
103 double v;
104
105 if (filename_to_buf(filename, buf, sizeof(buf)))
106 return 0;
107
108 oldlocale = setlocale(LC_ALL, "C");
109 v = strtod(buf, NULL);
110 setlocale(LC_ALL, oldlocale);
111
112 return v;
113 }
114
rapl_gpu_power_scale(void)115 static double rapl_gpu_power_scale(void)
116 {
117 return filename_to_double("/sys/devices/power/events/energy-gpu.scale");
118 }
119
power_init(struct power * power)120 int power_init(struct power *power)
121 {
122 uint64_t val;
123
124 memset(power, 0, sizeof(*power));
125
126 power->fd = igt_perf_open(rapl_type_id(), rapl_gpu_power());
127 if (power->fd >= 0) {
128 power->rapl_scale = rapl_gpu_power_scale();
129
130 if (power->rapl_scale != NAN) {
131 power->rapl_scale *= 1e3; /* from nano to micro */
132 return 0;
133 }
134 }
135
136 val = debugfs_file_to_u64("i915_energy_uJ");
137 if (val == 0)
138 return power->error = EINVAL;
139
140 return 0;
141 }
142
clock_ms_to_u64(void)143 static uint64_t clock_ms_to_u64(void)
144 {
145 struct timespec tv;
146
147 if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0)
148 return 0;
149
150 return (uint64_t)tv.tv_sec * 1e3 + tv.tv_nsec / 1e6;
151 }
152
power_update(struct power * power)153 int power_update(struct power *power)
154 {
155 struct power_stat *s = &power->stat[power->count++ & 1];
156 struct power_stat *d = &power->stat[power->count & 1];
157 uint64_t d_time;
158
159 if (power->error)
160 return power->error;
161
162 if (power->fd >= 0) {
163 uint64_t data[2];
164 int len;
165
166 len = read(power->fd, data, sizeof(data));
167 if (len != sizeof(data))
168 return power->error = errno;
169
170 s->energy = llround((double)data[0] * power->rapl_scale);
171 s->timestamp = data[1] / 1e6;
172 } else {
173 s->energy = debugfs_file_to_u64("i915_energy_uJ") / 1e3;
174 s->timestamp = clock_ms_to_u64();
175 }
176
177 if (power->count == 1)
178 return EAGAIN;
179
180 d_time = s->timestamp - d->timestamp;
181 power->power_mW = round((double)(s->energy - d->energy) *
182 (1e3f / d_time));
183 power->new_sample = 1;
184
185 return 0;
186 }
187