From b5eec21fbcd8efa52f4972d418bde1fcdc159041 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Fri, 14 Jul 2023 17:09:49 -0700 Subject: [PATCH] sysprof: start on CPU section --- src/sysprof/meson.build | 1 + src/sysprof/sysprof-cpu-section.c | 141 +++++++++++++++++++++++++++++ src/sysprof/sysprof-cpu-section.h | 31 +++++++ src/sysprof/sysprof-cpu-section.ui | 62 +++++++++++++ src/sysprof/sysprof-window.c | 2 + src/sysprof/sysprof-window.ui | 2 +- src/sysprof/sysprof.gresource.xml | 1 + 7 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 src/sysprof/sysprof-cpu-section.c create mode 100644 src/sysprof/sysprof-cpu-section.h create mode 100644 src/sysprof/sysprof-cpu-section.ui diff --git a/src/sysprof/meson.build b/src/sysprof/meson.build index 7eb71919..e22c3317 100644 --- a/src/sysprof/meson.build +++ b/src/sysprof/meson.build @@ -3,6 +3,7 @@ sysprof_sources = [ 'sysprof-application.c', 'sysprof-counters-section.c', 'sysprof-cpu-info-dialog.c', + 'sysprof-cpu-section.c', 'sysprof-files-section.c', 'sysprof-frame-utility.c', 'sysprof-greeter.c', diff --git a/src/sysprof/sysprof-cpu-section.c b/src/sysprof/sysprof-cpu-section.c new file mode 100644 index 00000000..a13cbe75 --- /dev/null +++ b/src/sysprof/sysprof-cpu-section.c @@ -0,0 +1,141 @@ +/* sysprof-cpu-section.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 + +#include "sysprof-cpu-section.h" +#include "sysprof-time-filter-model.h" +#include "sysprof-time-scrubber.h" +#include "sysprof-value-axis.h" + +struct _SysprofCpuSection +{ + SysprofSection parent_instance; + SysprofTimeScrubber *scrubber; +}; + +G_DEFINE_FINAL_TYPE (SysprofCpuSection, sysprof_cpu_section, SYSPROF_TYPE_SECTION) + +static GtkWidget * +create_counter_chart (SysprofDocumentCounter *counter, + SysprofAxis *x_axis) +{ + g_autoptr(SysprofValueAxis) y_axis = NULL; + g_autoptr(SysprofSeries) series = NULL; + SysprofChartLayer *layer; + SysprofChart *chart; + + g_assert (SYSPROF_IS_DOCUMENT_COUNTER (counter)); + + series = sysprof_xy_series_new (NULL, + g_object_ref (G_LIST_MODEL (counter)), + gtk_property_expression_new (SYSPROF_TYPE_DOCUMENT_COUNTER_VALUE, NULL, "time"), + gtk_property_expression_new (SYSPROF_TYPE_DOCUMENT_COUNTER_VALUE, NULL, "value-double")); + y_axis = g_object_new (SYSPROF_TYPE_VALUE_AXIS, + "min-value", 0., + "max-value", 100., + NULL); + layer = g_object_new (SYSPROF_TYPE_LINE_LAYER, + "fill", TRUE, + "series", series, + "x-axis", x_axis, + "y-axis", y_axis, + "spline", TRUE, + NULL); + chart = g_object_new (SYSPROF_TYPE_CHART, + "height-request", 20, + NULL); + sysprof_chart_add_layer (chart, layer); + + return GTK_WIDGET (chart); +} + +static void +sysprof_cpu_section_session_set (SysprofSection *section, + SysprofSession *session) +{ + SysprofCpuSection *self = (SysprofCpuSection *)section; + g_autoptr(SysprofDocumentCounter) combined_counter = NULL; + SysprofDocument *document; + SysprofAxis *x_axis; + + g_assert (SYSPROF_IS_CPU_SECTION (self)); + g_assert (!session || SYSPROF_IS_SESSION (session)); + + if (!session) + return; + + document = sysprof_session_get_document (session); + x_axis = sysprof_session_get_visible_time_axis (session); + + if ((combined_counter = sysprof_document_find_counter (document, "CPU Percent", "Combined"))) + { + g_autoptr(SysprofTimeFilterModel) filter = sysprof_time_filter_model_new (NULL, NULL); + + sysprof_time_filter_model_set_model (filter, G_LIST_MODEL (combined_counter)); + g_object_bind_property (session, "visible-time", filter, "time-span", G_BINDING_SYNC_CREATE); + + sysprof_time_scrubber_add_chart (self->scrubber, + create_counter_chart (combined_counter, x_axis)); + } + +} + +static void +sysprof_cpu_section_dispose (GObject *object) +{ + SysprofCpuSection *self = (SysprofCpuSection *)object; + + gtk_widget_dispose_template (GTK_WIDGET (self), SYSPROF_TYPE_CPU_SECTION); + + G_OBJECT_CLASS (sysprof_cpu_section_parent_class)->dispose (object); +} + +static void +sysprof_cpu_section_class_init (SysprofCpuSectionClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + SysprofSectionClass *section_class = SYSPROF_SECTION_CLASS (klass); + + object_class->dispose = sysprof_cpu_section_dispose; + + section_class->session_set = sysprof_cpu_section_session_set; + + gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/sysprof/sysprof-cpu-section.ui"); + gtk_widget_class_bind_template_child (widget_class, SysprofCpuSection, scrubber); + + g_type_ensure (SYSPROF_TYPE_CHART); + g_type_ensure (SYSPROF_TYPE_DOCUMENT_MARK); + g_type_ensure (SYSPROF_TYPE_DOCUMENT_COUNTER); + g_type_ensure (SYSPROF_TYPE_DOCUMENT_COUNTER_VALUE); + g_type_ensure (SYSPROF_TYPE_LINE_LAYER); + g_type_ensure (SYSPROF_TYPE_TIME_SERIES); + g_type_ensure (SYSPROF_TYPE_TIME_SPAN_LAYER); + g_type_ensure (SYSPROF_TYPE_XY_SERIES); +} + +static void +sysprof_cpu_section_init (SysprofCpuSection *self) +{ + gtk_widget_init_template (GTK_WIDGET (self)); +} diff --git a/src/sysprof/sysprof-cpu-section.h b/src/sysprof/sysprof-cpu-section.h new file mode 100644 index 00000000..995eb0a0 --- /dev/null +++ b/src/sysprof/sysprof-cpu-section.h @@ -0,0 +1,31 @@ +/* sysprof-cpu-section.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-section.h" + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_CPU_SECTION (sysprof_cpu_section_get_type()) + +G_DECLARE_FINAL_TYPE (SysprofCpuSection, sysprof_cpu_section, SYSPROF, CPU_SECTION, SysprofSection) + +G_END_DECLS diff --git a/src/sysprof/sysprof-cpu-section.ui b/src/sysprof/sysprof-cpu-section.ui new file mode 100644 index 00000000..abe3f577 --- /dev/null +++ b/src/sysprof/sysprof-cpu-section.ui @@ -0,0 +1,62 @@ + + + + + + + diff --git a/src/sysprof/sysprof-window.c b/src/sysprof/sysprof-window.c index 4c27792d..5b19d252 100644 --- a/src/sysprof/sysprof-window.c +++ b/src/sysprof/sysprof-window.c @@ -26,6 +26,7 @@ #include "sysprof-counters-section.h" #include "sysprof-cpu-info-dialog.h" +#include "sysprof-cpu-section.h" #include "sysprof-files-section.h" #include "sysprof-greeter.h" #include "sysprof-logs-section.h" @@ -414,6 +415,7 @@ sysprof_window_class_init (SysprofWindowClass *klass) gtk_widget_class_add_binding_action (widget_class, GDK_KEY_bracketright, GDK_CONTROL_MASK, "session.seek-forward", NULL); g_type_ensure (SYSPROF_TYPE_COUNTERS_SECTION); + g_type_ensure (SYSPROF_TYPE_CPU_SECTION); g_type_ensure (SYSPROF_TYPE_DOCUMENT); g_type_ensure (SYSPROF_TYPE_FILES_SECTION); g_type_ensure (SYSPROF_TYPE_LOGS_SECTION); diff --git a/src/sysprof/sysprof-window.ui b/src/sysprof/sysprof-window.ui index 79af664a..f2475f4b 100644 --- a/src/sysprof/sysprof-window.ui +++ b/src/sysprof/sysprof-window.ui @@ -144,7 +144,7 @@ - + CPU Usage counters system-run-symbolic diff --git a/src/sysprof/sysprof.gresource.xml b/src/sysprof/sysprof.gresource.xml index 032728d2..2f2078c6 100644 --- a/src/sysprof/sysprof.gresource.xml +++ b/src/sysprof/sysprof.gresource.xml @@ -5,6 +5,7 @@ gtk/menus.ui sysprof-counters-section.ui sysprof-cpu-info-dialog.ui + sysprof-cpu-section.ui sysprof-files-section.ui sysprof-frame-utility.ui sysprof-greeter.ui