Debian Codegraph @

src/libsysprof/sysprof-scheduler-details.c

raw
1 /* sysprof-scheduler-details.c
2 *
3 * Copyright 2023 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 #include "config.h"
22
23 #include "sysprof-instrument-private.h"
24 #include "sysprof-perf-event-stream-private.h"
25 #include "sysprof-scheduler-details.h"
26 #include "sysprof-recording-private.h"
27 #include "sysprof-util-private.h"
28
29 #include "line-reader-private.h"
30
31 struct _SysprofSchedulerDetails
32 {
33 SysprofInstrument parent_instance;
34
35 GDBusConnection *connection;
36 SysprofRecording *recording;
37 DexFuture *cancellable;
38 GPtrArray *streams;
39 gint64 *last_switch_times;
40
41 gint64 started_at;
42 gint64 ended_at;
43
44 gint64 sched_switch_id;
45 gint64 prev_comm_offset;
46 gint64 prev_pid_offset;
47 gint64 prev_state_offset;
48 };
49
50 struct _SysprofSchedulerDetailsClass
51 {
52 SysprofInstrumentClass parent_class;
53 };
54
55 G_DEFINE_FINAL_TYPE (SysprofSchedulerDetails, sysprof_scheduler_details, SYSPROF_TYPE_INSTRUMENT)
56
57 static void
58 sysprof_scheduler_details_event_cb (const SysprofPerfEvent *event,
59 guint cpu,
60 gpointer user_data)
61 {
62 SysprofSchedulerDetails *self = user_data;
63 const guint8 *raw;
64 gint64 begin, end;
65 glong prev_state;
66 char prev_comm[16];
67 char name[8];
68 GPid prev_pid;
69
70 g_assert (SYSPROF_IS_SCHEDULER_DETAILS (self));
71
72 if (event->header.type != PERF_RECORD_SAMPLE)
73 return;
74
75 begin = self->last_switch_times[cpu];
76 end = event->tracepoint.time;
77
78 if (begin == 0)
79 goto finish;
80
81 if (self->started_at == 0 || end < self->started_at)
82 goto finish;
83
84 if (self->ended_at != 0 && begin > self->ended_at)
85 goto finish;
86
87 raw = event->tracepoint.raw;
88
89 memcpy (&prev_pid, raw + self->prev_pid_offset, sizeof prev_pid);
90 memcpy (&prev_state, raw + self->prev_state_offset, sizeof prev_state);
91 memcpy (&prev_comm[0], raw + self->prev_comm_offset, sizeof prev_comm);
92
93 prev_comm[sizeof prev_comm-1] = 0;
94
95 /* Ignore the kswapper events because they just clutter up the
96 * timeline from things that would otherwise stand out.
97 */
98 if (memcmp (prev_comm, "swapper/", strlen ("swapper/")) == 0)
99 goto finish;
100
101 g_snprintf (name, sizeof name, "CPU %u", cpu);
102
103 sysprof_capture_writer_add_mark (self->recording->writer,
104 begin,
105 cpu,
106 event->tracepoint.pid,
107 end - begin,
108 "Scheduler",
109 name,
110 prev_comm);
111
112 finish:
113 self->last_switch_times[cpu] = end;
114 }
115
116 static void
117 sysprof_scheduler_details_parse_field (SysprofSchedulerDetails *self,
118 char **parts)
119 {
120 G_GNUC_UNUSED gboolean _signed = FALSE;
121 gint64 offset = -1;
122 gint64 size = 0;
123
124 g_assert (SYSPROF_IS_SCHEDULER_DETAILS (self));
125 g_assert (parts != NULL);
126
127 for (guint i = 1; parts[i]; i++)
128 {
129 if (g_str_has_prefix (parts[i], "offset:"))
130 offset = g_ascii_strtoll (parts[i] + strlen ("offset:"), NULL, 10);
131 else if (g_str_has_prefix (parts[i], "size:"))
132 size = g_ascii_strtoll (parts[i] + strlen ("size:"), NULL, 10);
133 else if (g_str_has_prefix (parts[i], "signed:"))
134 _signed = parts[i][strlen ("signed:")] == '1';
135 }
136
137 if (offset == -1)
138 return;
139
140 if (g_str_equal (parts[0], "field:char prev_comm[16];"))
141 {
142 if (size == 16)
143 self->prev_comm_offset = offset;
144 }
145 else if (g_str_equal (parts[0], "field:pid_t prev_pid;"))
146 {
147 if (size == 4)
148 self->prev_pid_offset = offset;
149 }
150 else if (g_str_equal (parts[0], "field:long prev_state;"))
151 {
152 if (size == sizeof (glong))
153 self->prev_state_offset = offset;
154 }
155 }
156
157 static gboolean
158 sysprof_scheduler_details_parse_format (SysprofSchedulerDetails *self,
159 char *format)
160 {
161 LineReader reader;
162 char *line;
163 gsize line_len;
164
165 g_assert (SYSPROF_IS_SCHEDULER_DETAILS (self));
166 g_assert (format != NULL);
167
168 line_reader_init (&reader, format, -1);
169
170 while ((line = line_reader_next (&reader, &line_len)))
171 {
172 line[line_len] = 0;
173
174 if (g_str_has_prefix (line, "ID: "))
175 {
176 if (!(self->sched_switch_id = g_ascii_strtoll (line + strlen ("ID: "), NULL, 10)))
177 return FALSE;
178 }
179
180 if (g_str_has_prefix (line, "\t"))
181 {
182 g_auto(GStrv) parts = g_strsplit (g_strstrip (line), "\t", 0);
183
184 sysprof_scheduler_details_parse_field (self, parts);
185 }
186 }
187
188 return self->sched_switch_id > 0 &&
189 self->prev_comm_offset > 0 &&
190 self->prev_state_offset > 0 &&
191 self->prev_pid_offset > 0;
192 }
193
194 static DexFuture *
195 sysprof_scheduler_details_prepare_fiber (gpointer user_data)
196 {
197 SysprofSchedulerDetails *self = user_data;
198 g_autoptr(GPtrArray) futures = NULL;
199 g_autoptr(GBytes) format_bytes = NULL;
200 g_autoptr(GError) error = NULL;
201 g_autofree char *format = NULL;
202 int n_cpu;
203
204 g_assert (SYSPROF_IS_SCHEDULER_DETAILS (self));
205 g_assert (SYSPROF_IS_RECORDING (self->recording));
206
207 if (!(format_bytes = dex_await_boxed (sysprof_get_proc_file_bytes (self->connection,
208 "/sys/kernel/debug/tracing/events/sched/sched_switch/format"),
209 &error)))
210 goto handle_error;
211
212 format = g_strndup (g_bytes_get_data (format_bytes, NULL),
213 g_bytes_get_size (format_bytes));
214
215 if (!sysprof_scheduler_details_parse_format (self, format))
216 {
217 error = g_error_new_literal (G_IO_ERROR,
218 G_IO_ERROR_INVALID_DATA,
219 "Could not parse data in tracepoint format");
220 goto handle_error;
221 }
222
223 futures = g_ptr_array_new_with_free_func (dex_unref);
224 n_cpu = g_get_num_processors ();
225
226 self->last_switch_times = g_new0 (gint64, n_cpu);
227
228 for (int cpu = 0; cpu < n_cpu; cpu++)
229 {
230 struct perf_event_attr attr = {0};
231
232 attr.type = PERF_TYPE_TRACEPOINT;
233 attr.type = PERF_TYPE_TRACEPOINT;
234 attr.sample_type = PERF_SAMPLE_RAW |
235 PERF_SAMPLE_IP |
236 PERF_SAMPLE_TID |
237 PERF_SAMPLE_IDENTIFIER |
238 PERF_SAMPLE_TIME;
239 attr.config = self->sched_switch_id;
240 attr.sample_period = 1;
241
242 #ifdef HAVE_PERF_CLOCKID
243 attr.clockid = sysprof_clock;
244 attr.use_clockid = 1;
245 #endif
246
247 attr.size = sizeof attr;
248
249 g_ptr_array_add (futures,
250 sysprof_perf_event_stream_new (self->connection,
251 &attr,
252 cpu,
253 -1,
254 0,
255 sysprof_scheduler_details_event_cb,
256 self,
257 NULL));
258 }
259
260 /* Create perf stream per-cpu to get sched:sched_switch */
261 if (!dex_await (dex_future_allv ((DexFuture **)futures->pdata, futures->len), &error))
262 goto handle_error;
263
264 for (int i = 0; i < futures->len; i++)
265 {
266 DexFuture *future = g_ptr_array_index (futures, i);
267 g_autoptr(SysprofPerfEventStream) stream = dex_await_object (dex_ref (future), NULL);
268
269 g_assert (stream != NULL);
270 g_assert (SYSPROF_IS_PERF_EVENT_STREAM (stream));
271
272 /* We'll start processing these once start_time is set
273 * for recording. That way we don't miss anything when
274 * racing to record.
275 */
276 if (sysprof_perf_event_stream_enable (stream, NULL))
277 g_ptr_array_add (self->streams, g_object_ref (stream));
278 }
279
280 return dex_future_new_for_boolean (TRUE);
281
282 handle_error:
283 g_assert (error != NULL);
284
285 _sysprof_recording_diagnostic (self->recording,
286 "Scheduler",
287 "Failed to register scheduler tracepoints: %s",
288 error->message);
289
290 return dex_future_new_for_error (g_steal_pointer (&error));
291 }
292
293 static DexFuture *
294 sysprof_scheduler_details_prepare (SysprofInstrument *instrument,
295 SysprofRecording *recording)
296 {
297 SysprofSchedulerDetails *self = (SysprofSchedulerDetails *)instrument;
298
299 g_assert (SYSPROF_IS_SCHEDULER_DETAILS (self));
300 g_assert (SYSPROF_IS_RECORDING (recording));
301
302 g_set_object (&self->recording, recording);
303
304 return dex_scheduler_spawn (NULL, 0,
305 sysprof_scheduler_details_prepare_fiber,
306 g_object_ref (self),
307 g_object_unref);
308 }
309
310 static DexFuture *
311 sysprof_scheduler_details_record_fiber (gpointer user_data)
312 {
313 SysprofSchedulerDetails *self = user_data;
314
315 g_assert (SYSPROF_IS_SCHEDULER_DETAILS (self));
316 g_assert (SYSPROF_IS_RECORDING (self->recording));
317 g_assert (DEX_IS_FUTURE (self->cancellable));
318
319 self->started_at = SYSPROF_CAPTURE_CURRENT_TIME;
320 dex_await (dex_ref (self->cancellable), NULL);
321 self->ended_at = SYSPROF_CAPTURE_CURRENT_TIME;
322
323 for (guint i = 0; i < self->streams->len; i++)
324 {
325 SysprofPerfEventStream *stream = g_ptr_array_index (self->streams, i);
326
327 sysprof_perf_event_stream_disable (stream, NULL);
328 }
329
330 if (self->streams->len)
331 g_ptr_array_remove_range (self->streams, 0, self->streams->len);
332
333 return dex_future_new_for_boolean (TRUE);
334 }
335
336 static DexFuture *
337 sysprof_scheduler_details_record (SysprofInstrument *instrument,
338 SysprofRecording *recording,
339 GCancellable *cancellable)
340 {
341 SysprofSchedulerDetails *self = (SysprofSchedulerDetails *)instrument;
342
343 g_assert (SYSPROF_IS_SCHEDULER_DETAILS (self));
344 g_assert (SYSPROF_IS_RECORDING (recording));
345 g_assert (recording == self->recording);
346 g_assert (self->cancellable == NULL);
347
348 self->cancellable = dex_cancellable_new_from_cancellable (cancellable);
349
350 return dex_scheduler_spawn (NULL, 0,
351 sysprof_scheduler_details_record_fiber,
352 g_object_ref (self),
353 g_object_unref);
354 }
355
356 static void
357 sysprof_scheduler_details_set_connection (SysprofInstrument *instrument,
358 GDBusConnection *connection)
359 {
360 SysprofSchedulerDetails *self = SYSPROF_SCHEDULER_DETAILS (instrument);
361
362 g_set_object (&self->connection, connection);
363 }
364
365 static void
366 sysprof_scheduler_details_dispose (GObject *object)
367 {
368 SysprofSchedulerDetails *self = (SysprofSchedulerDetails *)object;
369
370 if (self->streams != NULL)
371 {
372 for (guint i = 0; i < self->streams->len; i++)
373 {
374 SysprofPerfEventStream *stream = g_ptr_array_index (self->streams, i);
375 sysprof_perf_event_stream_disable (stream, NULL);
376 }
377
378 g_clear_pointer (&self->streams, g_ptr_array_unref);
379 }
380
381 g_clear_object (&self->connection);
382 g_clear_object (&self->recording);
383 g_clear_pointer (&self->last_switch_times, g_free);
384 dex_clear (&self->cancellable);
385
386 G_OBJECT_CLASS (sysprof_scheduler_details_parent_class)->dispose (object);
387 }
388
389 static void
390 sysprof_scheduler_details_class_init (SysprofSchedulerDetailsClass *klass)
391 {
392 GObjectClass *object_class = G_OBJECT_CLASS (klass);
393 SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass);
394
395 object_class->dispose = sysprof_scheduler_details_dispose;
396
397 instrument_class->prepare = sysprof_scheduler_details_prepare;
398 instrument_class->record = sysprof_scheduler_details_record;
399 instrument_class->set_connection = sysprof_scheduler_details_set_connection;
400 }
401
402 static void
403 sysprof_scheduler_details_init (SysprofSchedulerDetails *self)
404 {
405 self->sched_switch_id = -1;
406 self->prev_comm_offset = -1;
407 self->prev_pid_offset = -1;
408 self->prev_state_offset = -1;
409 self->streams = g_ptr_array_new_with_free_func (g_object_unref);
410 }
411
412 SysprofInstrument *
413 sysprof_scheduler_details_new (void)
414 {
415 return g_object_new (SYSPROF_TYPE_SCHEDULER_DETAILS, NULL);
416 }