libsysprof-analyze: start on sysprof-analyze library

The goal here is to break up libsysprof into a library for recording
profiles (using libsysprof-capture) and a library for analyzing profiles
(both used by the sysprof UI).
This commit is contained in:
Christian Hergert
2023-04-25 11:57:26 -07:00
parent e888d0602e
commit efab045006
13 changed files with 111 additions and 13 deletions

View File

@ -0,0 +1,45 @@
libsysprof_analyze_public_sources = [
'sysprof-capture-model.c',
'sysprof-capture-frame-object.c',
]
libsysprof_analyze_public_headers = [
'sysprof-analyze.h',
'sysprof-capture-model.h',
'sysprof-capture-frame-object.h',
]
libsysprof_analyze_deps = [
dependency('gio-2.0', version: glib_req_version),
libsysprof_capture_deps,
]
libsysprof_analyze = library('sysprof-analyze',
libsysprof_analyze_public_sources,
include_directories: [include_directories('.'),
ipc_include_dirs,
libsysprof_capture_include_dirs],
dependencies: libsysprof_analyze_deps,
gnu_symbol_visibility: 'hidden',
version: '@0@.0.0'.format(soname_major_version),
darwin_versions: '@0@.0'.format(soname_major_version),
)
libsysprof_analyze_dep = declare_dependency(
link_with: libsysprof_analyze,
dependencies: libsysprof_analyze_deps,
include_directories: [include_directories('.'), libsysprof_capture_include_dirs],
)
meson.override_dependency('sysprof-analyze-@0@'.format(soname_major_version), libsysprof_analyze_dep)
pkgconfig.generate(libsysprof_analyze,
subdirs: [sysprof_header_subdir],
description: 'A library for processing and analyzing Sysprof captures',
install_dir: join_paths(get_option('libdir'), 'pkgconfig'),
requires: ['gio-2.0'],
variables: ['datadir=' + datadir_for_pc_file],
)
install_headers(libsysprof_analyze_public_headers, subdir: sysprof_header_subdir)
subdir('tests')

View File

@ -0,0 +1,32 @@
/* sysprof-analyze.h
*
* Copyright 2023 Christian Hergert <chergert@redhat.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <glib.h>
G_BEGIN_DECLS
#define SYSPROF_ANALYZE_INSIDE
# include "sysprof-capture-frame-object.h"
# include "sysprof-capture-model.h"
#undef SYSPROF_ANALYZE_INSIDE
G_END_DECLS

View File

@ -0,0 +1,29 @@
/* sysprof-capture-frame-object-private.h
*
* Copyright 2023 Christian Hergert <chergert@redhat.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "sysprof-capture-frame-object.h"
G_BEGIN_DECLS
SysprofCaptureFrameObject *sysprof_capture_frame_object_new (GMappedFile *mapped,
gconstpointer data,
gboolean is_native);
G_END_DECLS

View File

@ -0,0 +1,72 @@
/* sysprof-capture-frame-object.c
*
* Copyright 2023 Christian Hergert <chergert@redhat.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "config.h"
#include "sysprof-capture-frame-object-private.h"
struct _SysprofCaptureFrameObject
{
GObject parent_instance;
GMappedFile *mapped_file;
const SysprofCaptureFrame *frame;
guint is_native : 1;
};
G_DEFINE_FINAL_TYPE (SysprofCaptureFrameObject, sysprof_capture_frame_object, G_TYPE_OBJECT)
static void
sysprof_capture_frame_object_finalize (GObject *object)
{
SysprofCaptureFrameObject *self = (SysprofCaptureFrameObject *)object;
g_clear_pointer (&self->mapped_file, g_mapped_file_unref);
self->frame = NULL;
G_OBJECT_CLASS (sysprof_capture_frame_object_parent_class)->finalize (object);
}
static void
sysprof_capture_frame_object_class_init (SysprofCaptureFrameObjectClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = sysprof_capture_frame_object_finalize;
}
static void
sysprof_capture_frame_object_init (SysprofCaptureFrameObject *self)
{
}
SysprofCaptureFrameObject *
sysprof_capture_frame_object_new (GMappedFile *mapped_file,
gconstpointer data,
gboolean is_native)
{
SysprofCaptureFrameObject *self;
self = g_object_new (SYSPROF_TYPE_CAPTURE_FRAME_OBJECT, NULL);
self->mapped_file = g_mapped_file_ref (mapped_file);
self->frame = data;
self->is_native = !!is_native;
return self;
}

View File

@ -0,0 +1,37 @@
/* sysprof-capture-frame-object.h
*
* Copyright 2023 Christian Hergert <chergert@redhat.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <gio/gio.h>
#include <sysprof-capture.h>
G_BEGIN_DECLS
#define SYSPROF_TYPE_CAPTURE_FRAME_OBJECT (sysprof_capture_frame_object_get_type())
SYSPROF_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (SysprofCaptureFrameObject, sysprof_capture_frame_object, SYSPROF, CAPTURE_FRAME_OBJECT, GObject)
SYSPROF_AVAILABLE_IN_ALL
SysprofCaptureFrame *sysprof_capture_frame_object_get_frame (SysprofCaptureFrameObject *self);
G_END_DECLS

View File

@ -0,0 +1,165 @@
/* sysprof-capture-model.c
*
* Copyright 2023 Christian Hergert <chergert@redhat.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "config.h"
#include "sysprof-capture-frame-object-private.h"
#include "sysprof-capture-model.h"
struct _SysprofCaptureModel
{
GObject parent_instance;
GPtrArray *frames;
GMappedFile *mapped_file;
SysprofCaptureFileHeader header;
guint is_native : 1;
};
static GType
sysprof_capture_model_get_item_type (GListModel *model)
{
return SYSPROF_TYPE_CAPTURE_FRAME_OBJECT;
}
static guint
sysprof_capture_model_get_n_items (GListModel *model)
{
return SYSPROF_CAPTURE_MODEL (model)->frames->len;
}
static gpointer
sysprof_capture_model_get_item (GListModel *model,
guint position)
{
SysprofCaptureModel *self = SYSPROF_CAPTURE_MODEL (model);
if (position >= self->frames->len)
return NULL;
return sysprof_capture_frame_object_new (self->mapped_file,
g_ptr_array_index (self->frames, position),
self->is_native);
}
static void
list_model_iface_init (GListModelInterface *iface)
{
iface->get_item_type = sysprof_capture_model_get_item_type;
iface->get_n_items = sysprof_capture_model_get_n_items;
iface->get_item = sysprof_capture_model_get_item;
}
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofCaptureModel, sysprof_capture_model, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
static void
sysprof_capture_model_finalize (GObject *object)
{
SysprofCaptureModel *self = (SysprofCaptureModel *)object;
g_clear_pointer (&self->mapped_file, g_mapped_file_unref);
g_clear_pointer (&self->frames, g_ptr_array_unref);
G_OBJECT_CLASS (sysprof_capture_model_parent_class)->finalize (object);
}
static void
sysprof_capture_model_class_init (SysprofCaptureModelClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = sysprof_capture_model_finalize;
}
static void
sysprof_capture_model_init (SysprofCaptureModel *self)
{
self->frames = g_ptr_array_new ();
}
static gboolean
sysprof_capture_model_load (SysprofCaptureModel *self,
int capture_fd,
GError **error)
{
const guint8 *data;
goffset pos;
gsize len;
g_assert (SYSPROF_IS_CAPTURE_MODEL (self));
g_assert (capture_fd > -1);
if (!(self->mapped_file = g_mapped_file_new_from_fd (capture_fd, FALSE, error)))
return FALSE;
data = (const guint8 *)g_mapped_file_get_contents (self->mapped_file);
len = g_mapped_file_get_length (self->mapped_file);
if (len < sizeof self->header)
return FALSE;
/* Keep a copy of our header */
memcpy (&self->header, data, sizeof self->header);
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
self->is_native = !!self->header.little_endian;
#else
self->is_native = !self->header.little_endian;
#endif
if (!self->is_native)
{
self->header.time = GUINT64_SWAP_LE_BE (self->header.time);
self->header.end_time = GUINT64_SWAP_LE_BE (self->header.end_time);
}
pos = sizeof self->header;
while (pos < (len - sizeof(guint16)))
{
guint16 frame_len;
memcpy (&frame_len, &data[pos], sizeof frame_len);
if (!self->is_native)
frame_len = GUINT16_SWAP_LE_BE (self->is_native);
if (frame_len < sizeof (SysprofCaptureFrame))
break;
g_ptr_array_add (self->frames, (gpointer)&data[pos]);
pos += frame_len;
}
return TRUE;
}
SysprofCaptureModel *
sysprof_capture_model_new_from_fd (int capture_fd,
GError **error)
{
g_autoptr(SysprofCaptureModel) self = NULL;
g_return_val_if_fail (capture_fd > -1, NULL);
self = g_object_new (SYSPROF_TYPE_CAPTURE_MODEL, NULL);
if (!sysprof_capture_model_load (self, capture_fd, error))
return NULL;
return g_steal_pointer (&self);
}

View File

@ -0,0 +1,38 @@
/* sysprof-capture-model.h
*
* Copyright 2023 Christian Hergert <chergert@redhat.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <gio/gio.h>
#include <sysprof-capture.h>
G_BEGIN_DECLS
#define SYSPROF_TYPE_CAPTURE_MODEL (sysprof_capture_model_get_type())
SYSPROF_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (SysprofCaptureModel, sysprof_capture_model, SYSPROF, CAPTURE_MODEL, GObject)
SYSPROF_AVAILABLE_IN_ALL
SysprofCaptureModel *sysprof_capture_model_new_from_fd (int capture_fd,
GError **error);
G_END_DECLS

View File

@ -0,0 +1,31 @@
libsysprof_analyze_test_env = [
'G_DEBUG=gc-friendly',
'GSETTINGS_BACKEND=memory',
'MALLOC_CHECK_=2',
]
libsysprof_analyze_testsuite_c_args = [
'-DG_LOG_DOMAIN="libdex"',
'-DG_ENABLE_DEBUG',
'-UG_DISABLE_ASSERT',
'-UG_DISABLE_CAST_CHECKS',
]
libsysprof_analyze_testsuite = {
'test-capture-model': {'skip': true},
}
libsysprof_analyze_testsuite_deps = [
libsysprof_analyze_dep,
libsysprof_capture_dep,
]
foreach test, params: libsysprof_analyze_testsuite
test_exe = executable(test, '@0@.c'.format(test),
c_args: libsysprof_analyze_testsuite_c_args,
dependencies: libsysprof_analyze_testsuite_deps,
)
if not params.get('skip', false)
test(test, test_exe, env: libsysprof_analyze_test_env)
endif
endforeach

View File

@ -0,0 +1,55 @@
#include <errno.h>
#include <fcntl.h>
#include <sysprof-analyze.h>
int
main (int argc,
char *argv[])
{
SysprofCaptureModel *model;
const char *filename;
GError *error = NULL;
guint n_items;
int fd;
sysprof_clock_init ();
if (argc < 2)
{
g_printerr ("usage: %s FILENAME\n", argv[0]);
return 1;
}
filename = argv[1];
fd = open (filename, O_RDONLY|O_CLOEXEC);
if (fd == -1)
{
g_printerr ("Failed to open %s: %s\n",
filename, g_strerror (errno));
return 1;
}
if (!(model = sysprof_capture_model_new_from_fd (fd, &error)))
{
g_printerr ("Failed to load %s: %s\n",
filename, error->message);
return 1;
}
n_items = g_list_model_get_n_items (G_LIST_MODEL (model));
g_print ("%u frames\n", n_items);
for (guint i = 0; i < n_items; i++)
{
SysprofCaptureFrameObject *obj = g_list_model_get_item (G_LIST_MODEL (model), i);
g_clear_object (&obj);
}
close (fd);
g_clear_object (&model);
return 0;
}