Debian Codegraph @

src/libsysprof/sysprof-document-frame.c

raw
1 /* sysprof-document-frame.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 <glib/gi18n.h>
24
25 #include "sysprof-document-frame-private.h"
26
27 #include "sysprof-document-allocation.h"
28 #include "sysprof-document-ctrdef.h"
29 #include "sysprof-document-ctrset.h"
30 #include "sysprof-document-dbus-message.h"
31 #include "sysprof-document-exit.h"
32 #include "sysprof-document-file-chunk.h"
33 #include "sysprof-document-fork.h"
34 #include "sysprof-document-log.h"
35 #include "sysprof-document-jitmap.h"
36 #include "sysprof-document-mark.h"
37 #include "sysprof-document-metadata.h"
38 #include "sysprof-document-mmap.h"
39 #include "sysprof-document-overlay.h"
40 #include "sysprof-document-process.h"
41 #include "sysprof-document-sample.h"
42
43 G_DEFINE_TYPE (SysprofDocumentFrame, sysprof_document_frame, G_TYPE_OBJECT)
44
45 enum {
46 PROP_0,
47 PROP_CPU,
48 PROP_PID,
49 PROP_TIME,
50 PROP_TIME_OFFSET,
51 PROP_TIME_STRING,
52 PROP_TOOLTIP,
53 PROP_TYPE_NAME,
54 N_PROPS
55 };
56
57 static GParamSpec *properties[N_PROPS];
58
59 static char *
60 sysprof_document_frame_real_dup_tooltip (SysprofDocumentFrame *self)
61 {
62 g_autofree char *time_string = sysprof_document_frame_dup_time_string (self);
63
64 return g_strdup_printf ("%s: %s",
65 time_string,
66 SYSPROF_DOCUMENT_FRAME_GET_CLASS (self)->type_name);
67 }
68
69 static const char *
70 sysprof_document_frame_get_type_name (SysprofDocumentFrame *self)
71 {
72 return g_dgettext (GETTEXT_PACKAGE, SYSPROF_DOCUMENT_FRAME_GET_CLASS (self)->type_name);
73 }
74
75 static void
76 sysprof_document_frame_finalize (GObject *object)
77 {
78 SysprofDocumentFrame *self = (SysprofDocumentFrame *)object;
79
80 g_clear_pointer (&self->mapped_file, g_mapped_file_unref);
81
82 self->frame = NULL;
83 self->needs_swap = 0;
84
85 G_OBJECT_CLASS (sysprof_document_frame_parent_class)->finalize (object);
86 }
87
88 static void
89 sysprof_document_frame_get_property (GObject *object,
90 guint prop_id,
91 GValue *value,
92 GParamSpec *pspec)
93 {
94 SysprofDocumentFrame *self = SYSPROF_DOCUMENT_FRAME (object);
95
96 switch (prop_id)
97 {
98 case PROP_CPU:
99 g_value_set_int (value, sysprof_document_frame_get_cpu (self));
100 break;
101
102 case PROP_PID:
103 g_value_set_int (value, sysprof_document_frame_get_pid (self));
104 break;
105
106 case PROP_TIME:
107 g_value_set_int64 (value, sysprof_document_frame_get_time (self));
108 break;
109
110 case PROP_TIME_OFFSET:
111 g_value_set_int64 (value, sysprof_document_frame_get_time_offset (self));
112 break;
113
114 case PROP_TIME_STRING:
115 g_value_take_string (value, sysprof_document_frame_dup_time_string (self));
116 break;
117
118 case PROP_TOOLTIP:
119 g_value_take_string (value, sysprof_document_frame_dup_tooltip (self));
120 break;
121
122 case PROP_TYPE_NAME:
123 g_value_set_static_string (value, sysprof_document_frame_get_type_name (self));
124 break;
125
126 default:
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 }
129 }
130
131 static void
132 sysprof_document_frame_class_init (SysprofDocumentFrameClass *klass)
133 {
134 GObjectClass *object_class = G_OBJECT_CLASS (klass);
135
136 object_class->finalize = sysprof_document_frame_finalize;
137 object_class->get_property = sysprof_document_frame_get_property;
138
139 klass->type_name = N_("Frame");
140 klass->dup_tooltip = sysprof_document_frame_real_dup_tooltip;
141
142 properties[PROP_CPU] =
143 g_param_spec_int ("cpu", NULL, NULL,
144 G_MININT32,
145 G_MAXINT32,
146 -1,
147 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
148
149 properties[PROP_PID] =
150 g_param_spec_int ("pid", NULL, NULL,
151 G_MININT32,
152 G_MAXINT32,
153 -1,
154 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
155
156 properties[PROP_TIME] =
157 g_param_spec_int64 ("time", NULL, NULL,
158 G_MININT64,
159 G_MAXINT64,
160 0,
161 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
162
163 properties[PROP_TIME_OFFSET] =
164 g_param_spec_int64 ("time-offset", NULL, NULL,
165 G_MININT64,
166 G_MAXINT64,
167 0,
168 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
169
170 properties[PROP_TIME_STRING] =
171 g_param_spec_string ("time-string", NULL, NULL,
172 NULL,
173 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
174
175 properties[PROP_TYPE_NAME] =
176 g_param_spec_string ("type-name", NULL, NULL,
177 NULL,
178 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
179
180 properties[PROP_TOOLTIP] =
181 g_param_spec_string ("tooltip", NULL, NULL,
182 NULL,
183 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
184
185 g_object_class_install_properties (object_class, N_PROPS, properties);
186 }
187
188 static void
189 sysprof_document_frame_init (SysprofDocumentFrame *self)
190 {
191 }
192
193 SysprofDocumentFrame *
194 _sysprof_document_frame_new (GMappedFile *mapped_file,
195 const SysprofCaptureFrame *frame,
196 guint16 frame_len,
197 gboolean needs_swap,
198 gint64 begin_time,
199 gint64 end_time)
200 {
201 SysprofDocumentFrame *self;
202 GType gtype;
203
204 switch (frame->type)
205 {
206 case SYSPROF_CAPTURE_FRAME_SAMPLE:
207 gtype = SYSPROF_TYPE_DOCUMENT_SAMPLE;
208 break;
209
210 case SYSPROF_CAPTURE_FRAME_MAP:
211 gtype = SYSPROF_TYPE_DOCUMENT_MMAP;
212 break;
213
214 case SYSPROF_CAPTURE_FRAME_LOG:
215 gtype = SYSPROF_TYPE_DOCUMENT_LOG;
216 break;
217
218 case SYSPROF_CAPTURE_FRAME_MARK:
219 gtype = SYSPROF_TYPE_DOCUMENT_MARK;
220 break;
221
222 case SYSPROF_CAPTURE_FRAME_METADATA:
223 gtype = SYSPROF_TYPE_DOCUMENT_METADATA;
224 break;
225
226 case SYSPROF_CAPTURE_FRAME_PROCESS:
227 gtype = SYSPROF_TYPE_DOCUMENT_PROCESS;
228 break;
229
230 case SYSPROF_CAPTURE_FRAME_EXIT:
231 gtype = SYSPROF_TYPE_DOCUMENT_EXIT;
232 break;
233
234 case SYSPROF_CAPTURE_FRAME_FORK:
235 gtype = SYSPROF_TYPE_DOCUMENT_FORK;
236 break;
237
238 case SYSPROF_CAPTURE_FRAME_ALLOCATION:
239 gtype = SYSPROF_TYPE_DOCUMENT_ALLOCATION;
240 break;
241
242 case SYSPROF_CAPTURE_FRAME_FILE_CHUNK:
243 gtype = SYSPROF_TYPE_DOCUMENT_FILE_CHUNK;
244 break;
245
246 case SYSPROF_CAPTURE_FRAME_OVERLAY:
247 gtype = SYSPROF_TYPE_DOCUMENT_OVERLAY;
248 break;
249
250 case SYSPROF_CAPTURE_FRAME_JITMAP:
251 gtype = SYSPROF_TYPE_DOCUMENT_JITMAP;
252 break;
253
254 case SYSPROF_CAPTURE_FRAME_CTRDEF:
255 gtype = SYSPROF_TYPE_DOCUMENT_CTRDEF;
256 break;
257
258 case SYSPROF_CAPTURE_FRAME_CTRSET:
259 gtype = SYSPROF_TYPE_DOCUMENT_CTRSET;
260 break;
261
262 case SYSPROF_CAPTURE_FRAME_DBUS_MESSAGE:
263 gtype = SYSPROF_TYPE_DOCUMENT_DBUS_MESSAGE;
264 break;
265
266 default:
267 gtype = SYSPROF_TYPE_DOCUMENT_FRAME;
268 break;
269 }
270
271
272 self = g_object_new (gtype, NULL);
273 self->mapped_file = g_mapped_file_ref (mapped_file);
274 self->frame = frame;
275 self->frame_len = frame_len;
276 self->needs_swap = !!needs_swap;
277
278 self->time_offset = CLAMP (sysprof_document_frame_get_time (self) - begin_time, 0, G_MAXINT64);
279
280 return self;
281 }
282
283 int
284 sysprof_document_frame_get_cpu (SysprofDocumentFrame *self)
285 {
286 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FRAME (self), 0);
287
288 return SYSPROF_DOCUMENT_FRAME_INT32 (self, self->frame->cpu);
289 }
290
291 int
292 sysprof_document_frame_get_pid (SysprofDocumentFrame *self)
293 {
294 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FRAME (self), 0);
295
296 return SYSPROF_DOCUMENT_FRAME_INT32 (self, self->frame->pid);
297 }
298
299 gint64
300 sysprof_document_frame_get_time (SysprofDocumentFrame *self)
301 {
302 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FRAME (self), 0);
303
304 return SYSPROF_DOCUMENT_FRAME_INT64 (self, self->frame->time);
305 }
306
307 gint64
308 sysprof_document_frame_get_time_offset (SysprofDocumentFrame *self)
309 {
310 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FRAME (self), 0);
311
312 return self->time_offset;
313 }
314
315 gboolean
316 sysprof_document_frame_equal (const SysprofDocumentFrame *a,
317 const SysprofDocumentFrame *b)
318 {
319 return a->frame == b->frame;
320 }
321
322 /**
323 * sysprof_document_frame_dup_time_string:
324 * @self: a #SysprofDocumentFrame
325 *
326 * Gets the time formatted as a string such as `00:00:00.1234`.
327 *
328 * Returns: (transfer full): a new string
329 */
330 char *
331 sysprof_document_frame_dup_time_string (SysprofDocumentFrame *self)
332 {
333 int hours;
334 int minutes;
335 int seconds;
336 double time;
337
338 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FRAME (self), NULL);
339
340 time = self->time_offset / (double)SYSPROF_NSEC_PER_SEC;
341
342 hours = time / (60 * 60);
343 time -= hours * (60 * 60);
344
345 minutes = time / 60;
346 time -= minutes * 60;
347
348 seconds = time / SYSPROF_NSEC_PER_SEC;
349 time -= seconds * SYSPROF_NSEC_PER_SEC;
350
351 return g_strdup_printf ("%02d:%02d:%02d.%04d", hours, minutes, seconds, (int)(time * 10000));
352 }
353
354 /**
355 * sysprof_document_frame_dup_tooltip:
356 * @self: a #SysprofDocumentFrame
357 *
358 * Returns a new string containing suggested tooltip text.
359 *
360 * Returns: (transfer full): a string
361 */
362 char *
363 sysprof_document_frame_dup_tooltip (SysprofDocumentFrame *self)
364 {
365 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FRAME (self), NULL);
366
367 return SYSPROF_DOCUMENT_FRAME_GET_CLASS (self)->dup_tooltip (self);
368 }