From 756e647e212f29d1f7900112241e07213763a0e6 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 29 Jun 2023 10:36:40 -0700 Subject: [PATCH] libsysprof-analyze: add sysprof_document_find_counter() This allows finding a specific counter without having to traverse them all from consumer code. --- src/libsysprof-analyze/sysprof-document.c | 37 +++++++++++++++++++++++ src/libsysprof-analyze/sysprof-document.h | 6 ++++ 2 files changed, 43 insertions(+) diff --git a/src/libsysprof-analyze/sysprof-document.c b/src/libsysprof-analyze/sysprof-document.c index a65b9be9..4d872418 100644 --- a/src/libsysprof-analyze/sysprof-document.c +++ b/src/libsysprof-analyze/sysprof-document.c @@ -1738,3 +1738,40 @@ sysprof_document_get_time_span (SysprofDocument *self) return &self->time_span; } + +/** + * sysprof_document_find_counter: + * @self: a #SysprofDocument + * @category: the category of the counter + * @name: the name of the counter + * + * Finds the first counter matching @category and @name. + * + * Returns: (transfer full) (nullable): a #SysprofDocumentCounter or %NULL + */ +SysprofDocumentCounter * +sysprof_document_find_counter (SysprofDocument *self, + const char *category, + const char *name) +{ + g_autoptr(GListModel) counters = NULL; + guint n_items; + + g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL); + g_return_val_if_fail (category != NULL, NULL); + g_return_val_if_fail (name != NULL, NULL); + + counters = sysprof_document_list_counters (self); + n_items = g_list_model_get_n_items (counters); + + for (guint i = 0; i < n_items; i++) + { + g_autoptr(SysprofDocumentCounter) counter = g_list_model_get_item (counters, i); + + if (g_strcmp0 (category, sysprof_document_counter_get_category (counter)) == 0 && + g_strcmp0 (name, sysprof_document_counter_get_name (counter)) == 0) + return g_steal_pointer (&counter); + } + + return NULL; +} diff --git a/src/libsysprof-analyze/sysprof-document.h b/src/libsysprof-analyze/sysprof-document.h index b7abc9f4..7420648e 100644 --- a/src/libsysprof-analyze/sysprof-document.h +++ b/src/libsysprof-analyze/sysprof-document.h @@ -25,6 +25,7 @@ #include #include "sysprof-callgraph.h" +#include "sysprof-document-counter.h" #include "sysprof-document-file.h" #include "sysprof-document-traceable.h" #include "sysprof-mark-catalog.h" @@ -62,6 +63,11 @@ GListModel *sysprof_document_list_marks (SysprofDocume SYSPROF_AVAILABLE_IN_ALL GListModel *sysprof_document_catalog_marks (SysprofDocument *self); SYSPROF_AVAILABLE_IN_ALL +SysprofDocumentCounter + *sysprof_document_find_counter (SysprofDocument *self, + const char *category, + const char *name); +SYSPROF_AVAILABLE_IN_ALL GListModel *sysprof_document_list_symbols_in_traceable (SysprofDocument *self, SysprofDocumentTraceable *traceable); SYSPROF_AVAILABLE_IN_ALL