Debian Codegraph @

src/libsysprof/sysprof-muxer-source.c

raw
1 /*
2 * sysprof-muxer-source.c
3 *
4 * Copyright 2024 Christian Hergert <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GPL-3.0-or-later
20 */
21
22 #include "config.h"
23
24 #include <glib/gstdio.h>
25 #include <glib-unix.h>
26
27 #include "sysprof-muxer-source.h"
28
29 #define DEFAULT_BUFFER_SIZE (4096*16)
30
31 typedef struct _SysprofMuxerSource
32 {
33 GSource gsource;
34 int capture_fd;
35 SysprofCaptureWriter *writer;
36 struct {
37 guint8 *allocation;
38 guint8 *begin;
39 guint8 *end;
40 guint8 *capacity;
41 gsize to_skip;
42 } buffer;
43 } SysprofMuxerSource;
44
45 static gboolean
46 sysprof_muxer_source_size (SysprofMuxerSource *source)
47 {
48 return source->buffer.end - source->buffer.begin;
49 }
50
51 static gboolean
52 sysprof_muxer_source_dispatch (GSource *gsource,
53 GSourceFunc callback,
54 gpointer user_data)
55 {
56 SysprofMuxerSource *source = (SysprofMuxerSource *)gsource;
57 gssize n_read;
58
59 g_assert (source != NULL);
60 g_assert (source->writer != NULL);
61
62 /* Try to read the next chunk */
63 n_read = read (source->capture_fd, source->buffer.end, source->buffer.capacity - source->buffer.end);
64
65 if (n_read > 0)
66 {
67 const SysprofCaptureFrame *frame;
68
69 /* Advance tail to what was filled */
70 source->buffer.end += n_read;
71
72 /* Get to next alignment */
73 if (source->buffer.to_skip)
74 {
75 gsize amount = MIN (source->buffer.to_skip, source->buffer.end - source->buffer.begin);
76 source->buffer.begin += amount;
77 source->buffer.to_skip -= amount;
78 }
79
80 /* If there is enough to read the frame header, try to read and dispatch
81 * it in raw form. We assume we're the same endianness here because this
82 * is coming from the same host (live-unwinder currently).
83 */
84 while (sysprof_muxer_source_size (source) >= sizeof *frame)
85 {
86 frame = (const SysprofCaptureFrame *)source->buffer.begin;
87
88 if (frame->len <= sysprof_muxer_source_size (source))
89 {
90 source->buffer.begin += frame->len;
91
92 if (frame->len % sizeof (guint64) != 0)
93 source->buffer.to_skip = sizeof (guint64) - (frame->len % sizeof (guint64));
94
95 /* TODO: Technically for counters/JIT map we need to translate them. */
96
97 _sysprof_capture_writer_add_raw (source->writer, frame);
98 }
99
100 if (source->buffer.to_skip > 0 &&
101 source->buffer.to_skip <= sysprof_muxer_source_size (source))
102 {
103 source->buffer.begin += source->buffer.to_skip;
104 source->buffer.to_skip = 0;
105 continue;
106 }
107
108 break;
109 }
110
111 /* Move anything left to the head of the buffer so we can
112 * fill in the entire next frame of data.
113 */
114 if (source->buffer.begin < source->buffer.end)
115 {
116 /* TODO: Should we adjust for alignment here? */
117
118 memmove (source->buffer.allocation,
119 source->buffer.begin,
120 source->buffer.end - source->buffer.begin);
121 source->buffer.end = source->buffer.allocation + (source->buffer.end - source->buffer.begin);
122 source->buffer.begin = source->buffer.allocation;
123 }
124 else
125 {
126 source->buffer.end = source->buffer.allocation;
127 source->buffer.begin = source->buffer.allocation;
128 }
129 }
130
131
132 return G_SOURCE_CONTINUE;
133 }
134
135 static void
136 sysprof_muxer_source_finalize (GSource *gsource)
137 {
138 SysprofMuxerSource *source = (SysprofMuxerSource *)gsource;
139
140 g_clear_fd (&source->capture_fd, NULL);
141 g_clear_pointer (&source->writer, sysprof_capture_writer_unref);
142 g_clear_pointer (&source->buffer.begin, g_free);
143 }
144
145 static const GSourceFuncs source_funcs = {
146 .dispatch = sysprof_muxer_source_dispatch,
147 .finalize = sysprof_muxer_source_finalize,
148 };
149
150 GSource *
151 sysprof_muxer_source_new (int capture_fd,
152 SysprofCaptureWriter *writer)
153 {
154 SysprofMuxerSource *source;
155
156 g_return_val_if_fail (capture_fd > -1, NULL);
157 g_return_val_if_fail (writer != NULL, NULL);
158
159 source = (SysprofMuxerSource *)g_source_new ((GSourceFuncs *)&source_funcs, sizeof (SysprofMuxerSource));
160 source->capture_fd = capture_fd;
161 source->writer = sysprof_capture_writer_ref (writer);
162 source->buffer.allocation = g_malloc (DEFAULT_BUFFER_SIZE);
163 source->buffer.begin = source->buffer.allocation;
164 source->buffer.end = source->buffer.allocation;
165 source->buffer.capacity = source->buffer.allocation + DEFAULT_BUFFER_SIZE;
166 source->buffer.to_skip = sizeof (SysprofCaptureFileHeader);
167
168 g_unix_set_fd_nonblocking (capture_fd, TRUE, NULL);
169
170 g_source_add_unix_fd ((GSource *)source, capture_fd, G_IO_IN);
171
172 return (GSource *)source;
173 }