Debian Codegraph @

src/sysprofd/sysprof-turbostat.c

raw
1 /* sysprof-turbostat.c
2 *
3 * Copyright 2019 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-ntifier: GPL-3.0-or-later
19 */
20
21 #include "sysprof-turbostat.h"
22
23 #include <errno.h>
24 #include <glib-unix.h>
25 #include <math.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 struct _SysprofTurbostat
32 {
33 volatile gint ref_count;
34 GPid pid;
35 GIOChannel *channel;
36 guint channel_watch;
37 GFunc sample_func;
38 gpointer sample_data;
39 };
40
41 enum {
42 KIND_0,
43 KIND_DOUBLE,
44 KIND_INT,
45 };
46
47 SysprofTurbostat *
48 sysprof_turbostat_new (GFunc sample_func,
49 gpointer sample_data)
50 {
51 SysprofTurbostat *self;
52
53 self = g_slice_new0 (SysprofTurbostat);
54 self->ref_count = 1;
55 self->pid = 0;
56 self->channel = NULL;
57 self->sample_func = sample_func;
58 self->sample_data = sample_data;
59
60 return g_steal_pointer (&self);
61 }
62
63 static void
64 sysprof_turbostat_finalize (SysprofTurbostat *self)
65 {
66 if (self->pid != 0)
67 sysprof_turbostat_stop (self);
68
69 g_assert (self->pid == 0);
70 g_assert (self->channel == NULL);
71
72 g_slice_free (SysprofTurbostat, self);
73 }
74
75 SysprofTurbostat *
76 sysprof_turbostat_ref (SysprofTurbostat *self)
77 {
78 g_return_val_if_fail (self != NULL, NULL);
79 g_return_val_if_fail (self->ref_count > 0, NULL);
80
81 g_atomic_int_inc (&self->ref_count);
82
83 return self;
84 }
85
86 void
87 sysprof_turbostat_unref (SysprofTurbostat *self)
88 {
89 g_return_if_fail (self != NULL);
90 g_return_if_fail (self->ref_count > 0);
91
92 if (g_atomic_int_dec_and_test (&self->ref_count))
93 sysprof_turbostat_finalize (self);
94 }
95
96 static gboolean
97 sysprof_turbostat_watch_cb (GIOChannel *channel,
98 GIOCondition cond,
99 gpointer data)
100 {
101 SysprofTurbostat *self = data;
102 g_autoptr(GArray) ret = NULL;
103 g_autoptr(GString) str = NULL;
104 g_auto(GStrv) columns = NULL;
105 GIOStatus r;
106 gint lineno = 0;
107
108 g_assert (channel != NULL);
109 g_assert (cond & G_IO_IN);
110
111 ret = g_array_new (FALSE, FALSE, sizeof (SysprofTurbostatSample));
112 str = g_string_new (NULL);
113
114 for (;;)
115 {
116 SysprofTurbostatSample sample = {0};
117 g_autoptr(GError) lerror = NULL;
118 g_auto(GStrv) parts = NULL;
119 gsize pos = 0;
120
121 lineno++;
122
123 r = g_io_channel_read_line_string (self->channel, str, &pos, &lerror);
124 if (r != G_IO_STATUS_NORMAL || str->len == 0 || pos == 0)
125 break;
126
127 g_string_truncate (str, pos - 1);
128
129 parts = g_strsplit (str->str, "\t", 0);
130
131 if (lineno == 1)
132 {
133 columns = g_steal_pointer (&parts);
134 continue;
135 }
136
137 g_assert (columns != NULL);
138
139 for (guint i = 0; columns[i] != NULL && parts[i] != NULL; i++)
140 {
141 gdouble *addr = NULL;
142 gint *iaddr = NULL;
143 int kind = KIND_0;
144
145 if (g_strcmp0 (columns[i], "GFXWatt") == 0)
146 {
147 addr = &sample.gfx_watt;
148 kind = KIND_DOUBLE;
149 }
150 else if (g_strcmp0 (columns[i], "CorWatt") == 0)
151 {
152 addr = &sample.core_watt;
153 kind = KIND_DOUBLE;
154 }
155 else if (g_strcmp0 (columns[i], "PkgWatt") == 0)
156 {
157 addr = &sample.pkg_watt;
158 kind = KIND_DOUBLE;
159 }
160 else if (g_strcmp0 (columns[i], "RAMWatt") == 0)
161 {
162 addr = &sample.ram_watt;
163 kind = KIND_DOUBLE;
164 }
165 else if (g_strcmp0 (columns[i], "Core") == 0)
166 {
167 iaddr = &sample.core;
168 kind = KIND_INT;
169 }
170 else if (g_strcmp0 (columns[i], "CPU") == 0)
171 {
172 iaddr = &sample.cpu;
173 kind = KIND_INT;
174 }
175
176 if (kind == KIND_0)
177 continue;
178
179 g_assert ((kind == KIND_DOUBLE && addr != NULL) ||
180 (kind == KIND_INT && iaddr != NULL));
181
182 if (kind == KIND_DOUBLE)
183 {
184 *addr = g_ascii_strtod (parts[i], NULL);
185
186 /* Reset to zero if we failed to parse */
187 if (*addr == HUGE_VAL && errno == ERANGE)
188 *addr = 0.0;
189 }
190 else if (kind == KIND_INT)
191 {
192 /* Some columns are "-" and we use -1 to indicate that. */
193 if (parts[i][0] == '-' && parts[i][1] == 0)
194 {
195 *iaddr = -1;
196 }
197 else
198 {
199 gint64 v = g_ascii_strtoll (parts[i], NULL, 10);
200
201 /* Reset if we failed to parse */
202 if ((v == G_MAXINT64 || v == G_MININT64) && errno == ERANGE)
203 v = 0;
204
205 *iaddr = v;
206 }
207 }
208 }
209
210 g_array_append_val (ret, sample);
211 }
212
213 if (ret->len > 0)
214 self->sample_func (ret, self->sample_data);
215
216 return G_SOURCE_CONTINUE;
217 }
218
219 gboolean
220 sysprof_turbostat_start (SysprofTurbostat *self,
221 GError **error)
222 {
223 /* We use a long interval and kill(..., SIGUSR1) to force a sample */
224 static const gchar *argv[] = { "turbostat", "-T", "Celcius", "-i", "100000", NULL };
225 g_auto(GStrv) env = NULL;
226 gboolean ret;
227 gint stdout_fd = -1;
228
229 g_return_val_if_fail (self != NULL, FALSE);
230 g_return_val_if_fail (self->pid == 0, FALSE);
231 g_return_val_if_fail (self->channel == NULL, FALSE);
232
233 env = g_get_environ ();
234 env = g_environ_setenv (env, "LANG", "C", TRUE);
235
236 ret = g_spawn_async_with_pipes (NULL,
237 (gchar **)argv,
238 env,
239 (G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL),
240 NULL,
241 NULL,
242 &self->pid,
243 NULL,
244 &stdout_fd,
245 NULL,
246 error);
247
248
249 if (ret)
250 {
251 if (!g_unix_set_fd_nonblocking (stdout_fd, TRUE, error))
252 {
253 ret = FALSE;
254 close (stdout_fd);
255 }
256
257 self->channel = g_io_channel_unix_new (stdout_fd);
258 g_io_channel_set_close_on_unref (self->channel, TRUE);
259 g_io_channel_set_buffer_size (self->channel, 4096);
260 g_io_channel_set_flags (self->channel, G_IO_FLAG_NONBLOCK, NULL);
261 self->channel_watch =
262 g_io_add_watch (self->channel,
263 G_IO_IN,
264 sysprof_turbostat_watch_cb,
265 self);
266 }
267
268 return ret;
269 }
270
271 void
272 sysprof_turbostat_stop (SysprofTurbostat *self)
273 {
274 g_return_if_fail (self != NULL);
275
276 if (self->pid != 0)
277 {
278 GPid pid = self->pid;
279 self->pid = 0;
280 kill (pid, SIGTERM);
281 }
282
283 g_clear_handle_id (&self->channel_watch, g_source_remove);
284 g_clear_pointer (&self->channel, g_io_channel_unref);
285 }
286
287 gboolean
288 sysprof_turbostat_sample (SysprofTurbostat *self,
289 GError **error)
290 {
291 g_return_val_if_fail (self != NULL, FALSE);
292 g_return_val_if_fail (self->channel != NULL, FALSE);
293 g_return_val_if_fail (self->pid != 0, FALSE);
294
295 kill (self->pid, SIGUSR1);
296
297 return TRUE;
298 }