From ca83cd6b401356ac9ad617c034eb01f0fb708c8c Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Tue, 25 Apr 2023 17:10:12 -0700 Subject: [PATCH] libsysprof-analyze: add document type for samples --- src/libsysprof-analyze/meson.build | 2 + src/libsysprof-analyze/sysprof-analyze.h | 1 + .../sysprof-document-frame.c | 6 +- .../sysprof-document-sample.c | 133 ++++++++++++++++++ .../sysprof-document-sample.h | 42 ++++++ .../tests/test-capture-model.c | 5 +- 6 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 src/libsysprof-analyze/sysprof-document-sample.c create mode 100644 src/libsysprof-analyze/sysprof-document-sample.h diff --git a/src/libsysprof-analyze/meson.build b/src/libsysprof-analyze/meson.build index d803f25a..ba127f41 100644 --- a/src/libsysprof-analyze/meson.build +++ b/src/libsysprof-analyze/meson.build @@ -1,12 +1,14 @@ libsysprof_analyze_public_sources = [ 'sysprof-document.c', 'sysprof-document-frame.c', + 'sysprof-document-sample.c', ] libsysprof_analyze_public_headers = [ 'sysprof-analyze.h', 'sysprof-document.h', 'sysprof-document-frame.h', + 'sysprof-document-sample.h', ] libsysprof_analyze_deps = [ diff --git a/src/libsysprof-analyze/sysprof-analyze.h b/src/libsysprof-analyze/sysprof-analyze.h index 9f6a9860..ef81d837 100644 --- a/src/libsysprof-analyze/sysprof-analyze.h +++ b/src/libsysprof-analyze/sysprof-analyze.h @@ -27,6 +27,7 @@ G_BEGIN_DECLS #define SYSPROF_ANALYZE_INSIDE # include "sysprof-document.h" # include "sysprof-document-frame.h" +# include "sysprof-document-sample.h" #undef SYSPROF_ANALYZE_INSIDE G_END_DECLS diff --git a/src/libsysprof-analyze/sysprof-document-frame.c b/src/libsysprof-analyze/sysprof-document-frame.c index fa7180e4..cc976e4f 100644 --- a/src/libsysprof-analyze/sysprof-document-frame.c +++ b/src/libsysprof-analyze/sysprof-document-frame.c @@ -21,6 +21,7 @@ #include "config.h" #include "sysprof-document-frame-private.h" +#include "sysprof-document-sample.h" G_DEFINE_TYPE (SysprofDocumentFrame, sysprof_document_frame, G_TYPE_OBJECT) @@ -117,10 +118,13 @@ _sysprof_document_frame_new (GMappedFile *mapped_file, guint16 frame_len, gboolean needs_swap) { + GType gtype = SYSPROF_TYPE_DOCUMENT_FRAME; SysprofDocumentFrame *self; - self = g_object_new (SYSPROF_TYPE_DOCUMENT_FRAME, NULL); + if (frame->type == SYSPROF_CAPTURE_FRAME_SAMPLE) + gtype = SYSPROF_TYPE_DOCUMENT_SAMPLE; + self = g_object_new (gtype, NULL); self->mapped_file = g_mapped_file_ref (mapped_file); self->frame = frame; self->frame_len = frame_len; diff --git a/src/libsysprof-analyze/sysprof-document-sample.c b/src/libsysprof-analyze/sysprof-document-sample.c new file mode 100644 index 00000000..8fbbe2d4 --- /dev/null +++ b/src/libsysprof-analyze/sysprof-document-sample.c @@ -0,0 +1,133 @@ +/* sysprof-document-sample.c + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include "sysprof-document-frame-private.h" +#include "sysprof-document-sample.h" + +struct _SysprofDocumentSample +{ + SysprofDocumentFrame parent_instance; +}; + +struct _SysprofDocumentSampleClass +{ + SysprofDocumentFrameClass parent_class; +}; + +enum { + PROP_0, + PROP_DEPTH, + PROP_TID, + N_PROPS +}; + +G_DEFINE_FINAL_TYPE (SysprofDocumentSample, sysprof_document_sample, SYSPROF_TYPE_DOCUMENT_FRAME) + +static GParamSpec *properties [N_PROPS]; + +static void +sysprof_document_sample_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + SysprofDocumentSample *self = SYSPROF_DOCUMENT_SAMPLE (object); + + switch (prop_id) + { + case PROP_DEPTH: + g_value_set_uint (value, sysprof_document_sample_get_depth (self)); + break; + + case PROP_TID: + g_value_set_int (value, sysprof_document_sample_get_tid (self)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +sysprof_document_sample_class_init (SysprofDocumentSampleClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->get_property = sysprof_document_sample_get_property; + + /** + * SysprofDocumentSample:tid: + * + * The task-id or thread-id of the thread which was sampled. + * + * On Linux, this is generally set to the value of the gettid() syscall. + * + * Since: 45 + */ + properties [PROP_TID] = + g_param_spec_int ("tid", NULL, NULL, + G_MININT32, G_MAXINT32, 0, + (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + /** + * SysprofDocumentSample:depth: + * + * The depth of the stack trace. + * + * Since: 45 + */ + properties [PROP_DEPTH] = + g_param_spec_uint ("depth", NULL, NULL, + 0, G_MAXUINT32, 0, + (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, N_PROPS, properties); +} + +static void +sysprof_document_sample_init (SysprofDocumentSample *self) +{ +} + +guint +sysprof_document_sample_get_depth (SysprofDocumentSample *self) +{ + const SysprofCaptureSample *sample; + + g_return_val_if_fail (SYSPROF_IS_DOCUMENT_SAMPLE (self), 0); + + sample = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureSample); + + return SYSPROF_DOCUMENT_FRAME_UINT32 (self, sample->n_addrs); +} + +int +sysprof_document_sample_get_tid (SysprofDocumentSample *self) +{ + const SysprofCaptureSample *sample; + + g_return_val_if_fail (SYSPROF_IS_DOCUMENT_SAMPLE (self), 0); + + sample = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureSample); + + return SYSPROF_DOCUMENT_FRAME_INT32 (self, sample->tid); +} diff --git a/src/libsysprof-analyze/sysprof-document-sample.h b/src/libsysprof-analyze/sysprof-document-sample.h new file mode 100644 index 00000000..1155626e --- /dev/null +++ b/src/libsysprof-analyze/sysprof-document-sample.h @@ -0,0 +1,42 @@ +/* sysprof-document-sample.h + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include "sysprof-document-sample.h" + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_DOCUMENT_SAMPLE (sysprof_document_sample_get_type()) +#define SYSPROF_IS_DOCUMENT_SAMPLE(obj) G_TYPE_CHECK_INSTANCE_TYPE(obj, SYSPROF_TYPE_DOCUMENT_SAMPLE) +#define SYSPROF_DOCUMENT_SAMPLE(obj) G_TYPE_CHECK_INSTANCE_CAST(obj, SYSPROF_TYPE_DOCUMENT_SAMPLE, SysprofDocumentSample) +#define SYSPROF_DOCUMENT_SAMPLE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST(klass, SYSPROF_TYPE_DOCUMENT_SAMPLE, SysprofDocumentSampleClass) + +typedef struct _SysprofDocumentSample SysprofDocumentSample; +typedef struct _SysprofDocumentSampleClass SysprofDocumentSampleClass; + +SYSPROF_AVAILABLE_IN_ALL +GType sysprof_document_sample_get_type (void) G_GNUC_CONST; +SYSPROF_AVAILABLE_IN_ALL +guint sysprof_document_sample_get_depth (SysprofDocumentSample *self); +SYSPROF_AVAILABLE_IN_ALL +int sysprof_document_sample_get_tid (SysprofDocumentSample *self); + +G_END_DECLS diff --git a/src/libsysprof-analyze/tests/test-capture-model.c b/src/libsysprof-analyze/tests/test-capture-model.c index dba4918b..8fb3b1ad 100644 --- a/src/libsysprof-analyze/tests/test-capture-model.c +++ b/src/libsysprof-analyze/tests/test-capture-model.c @@ -36,10 +36,11 @@ main (int argc, { SysprofDocumentFrame *frame = g_list_model_get_item (G_LIST_MODEL (document), i); - g_print ("%"G_GINT64_FORMAT" [pid %d] [cpu %d]\n", + g_print ("%"G_GINT64_FORMAT" [pid %d] [cpu %d] (type %s)\n", sysprof_document_frame_get_time (frame), sysprof_document_frame_get_pid (frame), - sysprof_document_frame_get_cpu (frame)); + sysprof_document_frame_get_cpu (frame), + G_OBJECT_TYPE_NAME (frame)); g_clear_object (&frame); }