Debian Codegraph @

src/sysprofd/ipc-rapl-profiler.c

raw
1 /* ipc-rapl-profiler.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-Identifier: GPL-3.0-or-later
19 */
20
21 #define G_LOG_DOMAIN "ipc-rapl-profiler"
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <gio/gunixfdlist.h>
28 #include <polkit/polkit.h>
29 #include <sysprof-capture.h>
30 #include <unistd.h>
31
32 #include "ipc-rapl-profiler.h"
33 #include "sysprof-turbostat.h"
34
35 #define DEFAULT_POLL_FREQ_SECONDS 1
36
37 struct _IpcRaplProfiler
38 {
39 IpcProfilerSkeleton parent_instance;
40
41 GMutex mutex;
42 GArray *counter_ids;
43 SysprofTurbostat *turbostat;
44 SysprofCaptureWriter *writer;
45 guint poll_source;
46 };
47
48 typedef struct
49 {
50 gint core;
51 gint cpu;
52 guint counter_base;
53 } CounterId;
54
55 static void profiler_iface_init (IpcProfilerIface *iface);
56
57 G_DEFINE_TYPE_WITH_CODE (IpcRaplProfiler, ipc_rapl_profiler, IPC_TYPE_PROFILER_SKELETON,
58 G_IMPLEMENT_INTERFACE (IPC_TYPE_PROFILER, profiler_iface_init))
59
60 enum {
61 ACTIVITY,
62 N_SIGNALS
63 };
64
65 static guint signals [N_SIGNALS];
66
67 static void
68 ipc_rapl_profiler_activity (IpcRaplProfiler *self)
69 {
70 g_assert (IPC_IS_RAPL_PROFILER (self));
71
72 g_signal_emit (self, signals [ACTIVITY], 0);
73 }
74
75 static void
76 ipc_rapl_profiler_stop_locked (IpcRaplProfiler *self)
77 {
78 g_assert (IPC_IS_RAPL_PROFILER (self));
79
80 g_message ("Stopping RAPL monitor");
81
82 g_clear_handle_id (&self->poll_source, g_source_remove);
83
84 if (self->turbostat != NULL)
85 sysprof_turbostat_stop (self->turbostat);
86
87 g_clear_pointer (&self->turbostat, sysprof_turbostat_unref);
88 g_clear_pointer (&self->counter_ids, g_array_unref);
89
90 if (self->writer != NULL)
91 {
92 sysprof_capture_writer_flush (self->writer);
93 sysprof_capture_writer_unref (self->writer);
94 self->writer = NULL;
95 }
96 }
97
98 static guint
99 add_counter_base (SysprofCaptureWriter *writer,
100 GArray *counters,
101 gint core,
102 gint cpu)
103 {
104 static const gchar *names[] = { "Package", "Core", "GFX", "RAM" };
105 g_autofree gchar *desc = NULL;
106 CounterId id;
107
108 g_assert (writer != NULL);
109 g_assert (counters != NULL);
110
111 id.core = core;
112 id.cpu = cpu;
113 id.counter_base = sysprof_capture_writer_request_counter (writer, G_N_ELEMENTS (names));
114
115 g_array_append_val (counters, id);
116
117 desc = g_strdup_printf ("Core:%u CPU:%u", core, cpu);
118
119 {
120 SysprofCaptureCounter ctrs[4] = {
121 { .name = "Package Watt",
122 .id = id.counter_base,
123 .type = SYSPROF_CAPTURE_COUNTER_DOUBLE },
124
125 { .name = "Core Watt",
126 .id = id.counter_base + 1,
127 .type = SYSPROF_CAPTURE_COUNTER_DOUBLE },
128
129 { .name = "GFX Watt",
130 .id = id.counter_base + 2,
131 .type = SYSPROF_CAPTURE_COUNTER_DOUBLE },
132
133 { .name = "RAM Watt",
134 .id = id.counter_base + 3,
135 .type = SYSPROF_CAPTURE_COUNTER_DOUBLE },
136 };
137
138 for (guint j = 0; j < G_N_ELEMENTS (ctrs); j++)
139 {
140 if (cpu == -1)
141 {
142 g_snprintf (ctrs[j].category, sizeof ctrs[j].category, "RAPL");
143 g_snprintf (ctrs[j].name, sizeof ctrs[j].name, "%s Watt", names[j]);
144 }
145 else
146 {
147 g_snprintf (ctrs[j].category, sizeof ctrs[j].category, "RAPL %d:%d", core, cpu);
148 g_snprintf (ctrs[j].name, sizeof ctrs[j].name, "%s Watt (%d:%d)", names[j], core, cpu);
149 }
150 }
151
152 sysprof_capture_writer_define_counters (writer,
153 SYSPROF_CAPTURE_CURRENT_TIME,
154 -1,
155 -1,
156 ctrs,
157 G_N_ELEMENTS (ctrs));
158 }
159
160 return id.counter_base;
161 }
162
163 static guint
164 get_counter_base (SysprofCaptureWriter *writer,
165 GArray *counters,
166 gint core,
167 gint cpu)
168 {
169 for (guint i = 0; i < counters->len; i++)
170 {
171 const CounterId *ele = &g_array_index (counters, CounterId, i);
172
173 if (core == ele->core && cpu == ele->cpu)
174 return ele->counter_base;
175 }
176
177 return add_counter_base (writer, counters, core, cpu);
178 }
179
180 static gboolean
181 ipc_rapl_profiler_g_authorize_method (GDBusInterfaceSkeleton *skeleton,
182 GDBusMethodInvocation *invocation)
183 {
184 PolkitAuthorizationResult *res = NULL;
185 PolkitAuthority *authority = NULL;
186 PolkitSubject *subject = NULL;
187 const gchar *peer_name;
188 gboolean ret = TRUE;
189
190 g_assert (IPC_IS_RAPL_PROFILER (skeleton));
191 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
192
193 ipc_rapl_profiler_activity (IPC_RAPL_PROFILER (skeleton));
194
195 peer_name = g_dbus_method_invocation_get_sender (invocation);
196
197 if (!(authority = polkit_authority_get_sync (NULL, NULL)) ||
198 !(subject = polkit_system_bus_name_new (peer_name)) ||
199 !(res = polkit_authority_check_authorization_sync (authority,
200 POLKIT_SUBJECT (subject),
201 "org.gnome.sysprof3.profile",
202 NULL,
203 POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
204 NULL,
205 NULL)) ||
206 !polkit_authorization_result_get_is_authorized (res))
207 {
208 g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
209 G_DBUS_ERROR,
210 G_DBUS_ERROR_ACCESS_DENIED,
211 "Not authorized to make request");
212 ret = FALSE;
213 }
214
215 g_clear_object (&authority);
216 g_clear_object (&subject);
217 g_clear_object (&res);
218
219 return ret;
220 }
221
222 static void
223 ipc_rapl_profiler_finalize (GObject *object)
224 {
225 IpcRaplProfiler *self = (IpcRaplProfiler *)object;
226
227 ipc_rapl_profiler_stop_locked (self);
228 g_mutex_clear (&self->mutex);
229
230 G_OBJECT_CLASS (ipc_rapl_profiler_parent_class)->finalize (object);
231 }
232
233 static void
234 ipc_rapl_profiler_class_init (IpcRaplProfilerClass *klass)
235 {
236 GObjectClass *object_class = G_OBJECT_CLASS (klass);
237 GDBusInterfaceSkeletonClass *skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (klass);
238
239 object_class->finalize = ipc_rapl_profiler_finalize;
240
241 skeleton_class->g_authorize_method = ipc_rapl_profiler_g_authorize_method;
242
243 /**
244 * IpcRaplProfiler::activity:
245 *
246 * The "activity" signal is used to denote that some amount of activity
247 * has occurred and therefore the process should be kept alive longer.
248 */
249 signals [ACTIVITY] =
250 g_signal_new ("activity",
251 G_TYPE_FROM_CLASS (klass),
252 G_SIGNAL_RUN_LAST,
253 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
254 }
255
256 static void
257 ipc_rapl_profiler_init (IpcRaplProfiler *self)
258 {
259 g_mutex_init (&self->mutex);
260 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (self),
261 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
262 }
263
264 IpcProfiler *
265 ipc_rapl_profiler_new (void)
266 {
267 return g_object_new (IPC_TYPE_RAPL_PROFILER, NULL);
268 }
269
270 static gboolean
271 ipc_rapl_profiler_poll_cb (gpointer data)
272 {
273 IpcRaplProfiler *self = data;
274 g_autoptr(GMutexLocker) locker = NULL;
275 g_autoptr(GError) error = NULL;
276
277 g_assert (IPC_IS_RAPL_PROFILER (self));
278
279 ipc_rapl_profiler_activity (self);
280
281 locker = g_mutex_locker_new (&self->mutex);
282
283 if (self->turbostat == NULL)
284 goto failure;
285
286 g_assert (self->counter_ids != NULL);
287 g_assert (self->writer != NULL);
288
289 if (!sysprof_turbostat_sample (self->turbostat, &error))
290 {
291 ipc_rapl_profiler_stop_locked (self);
292 goto failure;
293 }
294
295 return G_SOURCE_CONTINUE;
296
297 failure:
298 self->poll_source = 0;
299
300 return G_SOURCE_REMOVE;
301 }
302
303 static void
304 on_sample_cb (gpointer data,
305 gpointer user_data)
306 {
307 IpcRaplProfiler *self = user_data;
308 GArray *samples = data;
309
310 g_assert (IPC_IS_RAPL_PROFILER (self));
311 g_assert (samples != NULL);
312 g_assert (samples->len > 0);
313
314 for (guint i = 0; i < samples->len; i++)
315 {
316 const SysprofTurbostatSample *sample = &g_array_index (samples, SysprofTurbostatSample, i);
317 guint base = get_counter_base (self->writer, self->counter_ids, sample->core, sample->cpu);
318 const guint counter_ids[4] = { base, base + 1, base + 2, base + 3 };
319 const SysprofCaptureCounterValue values[4] = {
320 { .vdbl = sample->pkg_watt },
321 { .vdbl = sample->core_watt },
322 { .vdbl = sample->gfx_watt },
323 { .vdbl = sample->ram_watt },
324 };
325 gboolean r;
326
327 r = sysprof_capture_writer_set_counters (self->writer,
328 SYSPROF_CAPTURE_CURRENT_TIME,
329 sample->cpu,
330 -1,
331 counter_ids,
332 values,
333 G_N_ELEMENTS (values));
334
335 if (r == FALSE)
336 {
337 ipc_rapl_profiler_stop_locked (self);
338 return;
339 }
340 }
341
342 sysprof_capture_writer_flush (self->writer);
343 }
344
345 static gboolean
346 ipc_rapl_profiler_handle_start (IpcProfiler *profiler,
347 GDBusMethodInvocation *invocation,
348 GUnixFDList *in_fd_list,
349 GVariant *arg_options,
350 GVariant *arg_fd)
351 {
352 IpcRaplProfiler *self = (IpcRaplProfiler *)profiler;
353 g_autoptr(SysprofTurbostat) turbostat = NULL;
354 g_autoptr(GMutexLocker) locker = NULL;
355 g_autoptr(GError) error = NULL;
356 g_autofree gchar *path = NULL;
357 gint fd = -1;
358 gint handle;
359
360 g_assert (IPC_IS_RAPL_PROFILER (self));
361 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
362 g_assert (arg_options != NULL);
363 g_assert (g_variant_is_of_type (arg_options, G_VARIANT_TYPE_VARDICT));
364 g_assert (arg_fd != NULL);
365 g_assert (g_variant_is_of_type (arg_fd, G_VARIANT_TYPE_HANDLE));
366
367 if (!(path = g_find_program_in_path ("turbostat")))
368 {
369 g_dbus_method_invocation_return_dbus_error (g_steal_pointer (&invocation),
370 "org.freedesktop.DBus.Error.Failed",
371 "Program `turbostat` is not available. Try installing kernel-tools.");
372 return TRUE;
373 }
374
375 /* Get the FD for capture writing */
376 if (in_fd_list != NULL &&
377 (handle = g_variant_get_handle (arg_fd)) > -1)
378 fd = g_unix_fd_list_get (in_fd_list, handle, NULL);
379
380 /* Make sure the FD is reasonable */
381 if (fd < 0)
382 {
383 g_dbus_method_invocation_return_dbus_error (g_steal_pointer (&invocation),
384 "org.freedesktop.DBus.Error.Failed",
385 "Invalid param 'fd'");
386 return TRUE;
387 }
388
389 locker = g_mutex_locker_new (&self->mutex);
390
391 if (self->turbostat != NULL)
392 {
393 close (fd);
394 g_dbus_method_invocation_return_dbus_error (g_steal_pointer (&invocation),
395 "org.freedesktop.DBus.Error.Failed",
396 "RAPL collector already running");
397 return TRUE;
398 }
399
400 turbostat = sysprof_turbostat_new (on_sample_cb, self);
401
402 if (!sysprof_turbostat_start (turbostat, &error))
403 {
404 close (fd);
405 g_dbus_method_invocation_return_dbus_error (g_steal_pointer (&invocation),
406 "org.freedesktop.DBus.Error.Failed",
407 error->message);
408 return TRUE;
409 }
410
411 self->writer = sysprof_capture_writer_new_from_fd (fd, 0);
412 self->counter_ids = g_array_new (FALSE, FALSE, sizeof (CounterId));
413
414 g_message ("Starting RAPL monitor");
415
416 self->turbostat = g_steal_pointer (&turbostat);
417 self->poll_source = g_timeout_add_seconds (DEFAULT_POLL_FREQ_SECONDS,
418 ipc_rapl_profiler_poll_cb,
419 self);
420
421 ipc_profiler_complete_start (profiler, g_steal_pointer (&invocation), NULL);
422
423 return TRUE;
424 }
425
426 static gboolean
427 ipc_rapl_profiler_handle_stop (IpcProfiler *profiler,
428 GDBusMethodInvocation *invocation)
429 {
430 IpcRaplProfiler *self = (IpcRaplProfiler *)profiler;
431 g_autoptr(GMutexLocker) locker = NULL;
432
433 g_assert (IPC_IS_RAPL_PROFILER (self));
434 g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
435
436 locker = g_mutex_locker_new (&self->mutex);
437
438 ipc_rapl_profiler_stop_locked (self);
439
440 ipc_profiler_complete_stop (profiler, g_steal_pointer (&invocation));
441
442 return TRUE;
443 }
444
445 static void
446 profiler_iface_init (IpcProfilerIface *iface)
447 {
448 iface->handle_start = ipc_rapl_profiler_handle_start;
449 iface->handle_stop = ipc_rapl_profiler_handle_stop;
450 }