| 1 |
/* sysprof-descendants-model.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 "sysprof-allocator-private.h" |
| 24 |
#include "sysprof-callgraph-private.h" |
| 25 |
#include "sysprof-callgraph-frame-private.h" |
| 26 |
#include "sysprof-descendants-model-private.h" |
| 27 |
#include "sysprof-document-private.h" |
| 28 |
#include "sysprof-symbol-private.h" |
| 29 |
|
| 30 |
#define MAX_STACK_DEPTH 128 |
| 31 |
|
| 32 |
struct _SysprofDescendantsModel |
| 33 |
{ |
| 34 |
GObject parent_instance; |
| 35 |
SysprofAllocator *allocator; |
| 36 |
SysprofCallgraph *callgraph; |
| 37 |
SysprofSymbol *symbol; |
| 38 |
SysprofCallgraphNode root; |
| 39 |
}; |
| 40 |
|
| 41 |
static GType |
| 42 |
sysprof_descendants_model_get_item_type (GListModel *model) |
| 43 |
{ |
| 44 |
return SYSPROF_TYPE_CALLGRAPH_FRAME; |
| 45 |
} |
| 46 |
|
| 47 |
static guint |
| 48 |
sysprof_descendants_model_get_n_items (GListModel *model) |
| 49 |
{ |
| 50 |
return 1; |
| 51 |
} |
| 52 |
|
| 53 |
static gpointer |
| 54 |
sysprof_descendants_model_get_item (GListModel *model, |
| 55 |
guint position) |
| 56 |
{ |
| 57 |
SysprofDescendantsModel *self = SYSPROF_DESCENDANTS_MODEL (model); |
| 58 |
|
| 59 |
if (position != 0) |
| 60 |
return NULL; |
| 61 |
|
| 62 |
return _sysprof_callgraph_frame_new_for_node (self->callgraph, G_OBJECT (self), &self->root); |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
list_model_iface_init (GListModelInterface *iface) |
| 67 |
{ |
| 68 |
iface->get_item_type = sysprof_descendants_model_get_item_type; |
| 69 |
iface->get_n_items = sysprof_descendants_model_get_n_items; |
| 70 |
iface->get_item = sysprof_descendants_model_get_item; |
| 71 |
} |
| 72 |
|
| 73 |
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDescendantsModel, sysprof_descendants_model, G_TYPE_OBJECT, |
| 74 |
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init)) |
| 75 |
|
| 76 |
static void |
| 77 |
sysprof_descendants_model_finalize (GObject *object) |
| 78 |
{ |
| 79 |
SysprofDescendantsModel *self = (SysprofDescendantsModel *)object; |
| 80 |
|
| 81 |
g_clear_object (&self->callgraph); |
| 82 |
g_clear_object (&self->symbol); |
| 83 |
|
| 84 |
g_clear_pointer (&self->allocator, sysprof_allocator_unref); |
| 85 |
|
| 86 |
G_OBJECT_CLASS (sysprof_descendants_model_parent_class)->finalize (object); |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
sysprof_descendants_model_class_init (SysprofDescendantsModelClass *klass) |
| 91 |
{ |
| 92 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 93 |
|
| 94 |
object_class->finalize = sysprof_descendants_model_finalize; |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
sysprof_descendants_model_init (SysprofDescendantsModel *self) |
| 99 |
{ |
| 100 |
self->allocator = sysprof_allocator_new (); |
| 101 |
} |
| 102 |
|
| 103 |
static SysprofCallgraphNode * |
| 104 |
sysprof_descendants_model_add_trace (SysprofDescendantsModel *self, |
| 105 |
SysprofSymbol **symbols, |
| 106 |
guint n_symbols) |
| 107 |
{ |
| 108 |
SysprofCallgraphNode *parent = NULL; |
| 109 |
|
| 110 |
g_assert (SYSPROF_IS_DESCENDANTS_MODEL (self)); |
| 111 |
g_assert (symbols != NULL); |
| 112 |
g_assert (n_symbols > 0); |
| 113 |
|
| 114 |
parent = &self->root; |
| 115 |
|
| 116 |
for (guint i = n_symbols; i > 0; i--) |
| 117 |
{ |
| 118 |
SysprofSymbol *symbol = symbols[i-1]; |
| 119 |
SysprofCallgraphNode *node = NULL; |
| 120 |
SysprofCallgraphSummary *summary; |
| 121 |
|
| 122 |
/* Try to find @symbol within the children of @parent */ |
| 123 |
for (SysprofCallgraphNode *iter = parent->children; |
| 124 |
iter != NULL; |
| 125 |
iter = iter->next) |
| 126 |
{ |
| 127 |
g_assert (iter != NULL); |
| 128 |
g_assert (iter->summary != NULL); |
| 129 |
g_assert (iter->summary->symbol != NULL); |
| 130 |
g_assert (symbol != NULL); |
| 131 |
|
| 132 |
if (_sysprof_symbol_equal (iter->summary->symbol, symbol)) |
| 133 |
{ |
| 134 |
node = iter; |
| 135 |
node->count++; |
| 136 |
goto next_symbol; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (!(summary = g_hash_table_lookup (self->callgraph->symbol_to_summary, symbol))) |
| 141 |
{ |
| 142 |
node = parent; |
| 143 |
goto next_symbol; |
| 144 |
} |
| 145 |
|
| 146 |
/* Otherwise create a new node */ |
| 147 |
node = sysprof_allocator_new0 (self->allocator, SysprofCallgraphNode); |
| 148 |
node->summary = summary; |
| 149 |
node->parent = parent; |
| 150 |
node->next = parent->children; |
| 151 |
node->count = 1; |
| 152 |
if (parent->children) |
| 153 |
parent->children->prev = node; |
| 154 |
parent->children = node; |
| 155 |
|
| 156 |
g_assert (node->summary != NULL); |
| 157 |
|
| 158 |
next_symbol: |
| 159 |
parent = node; |
| 160 |
} |
| 161 |
|
| 162 |
return parent; |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
sysprof_descendants_model_add_traceable (SysprofDescendantsModel *self, |
| 167 |
SysprofDocument *document, |
| 168 |
SysprofDocumentTraceable *traceable, |
| 169 |
SysprofSymbol *from_symbol, |
| 170 |
gboolean include_threads, |
| 171 |
gboolean merge_similar_processes) |
| 172 |
{ |
| 173 |
SysprofAddressContext final_context; |
| 174 |
SysprofSymbol **symbols; |
| 175 |
SysprofSymbolKind kind; |
| 176 |
guint stack_depth; |
| 177 |
guint n_symbols; |
| 178 |
|
| 179 |
g_assert (SYSPROF_IS_DESCENDANTS_MODEL (self)); |
| 180 |
g_assert (SYSPROF_IS_DOCUMENT (document)); |
| 181 |
g_assert (SYSPROF_IS_DOCUMENT_TRACEABLE (traceable)); |
| 182 |
g_assert (SYSPROF_IS_SYMBOL (from_symbol)); |
| 183 |
|
| 184 |
stack_depth = MIN (MAX_STACK_DEPTH, sysprof_document_traceable_get_stack_depth (traceable)); |
| 185 |
symbols = g_alloca (sizeof (SysprofSymbol *) * (stack_depth + 2)); |
| 186 |
n_symbols = sysprof_document_symbolize_traceable (document, traceable, symbols, stack_depth, &final_context); |
| 187 |
|
| 188 |
kind = sysprof_symbol_get_kind (from_symbol); |
| 189 |
|
| 190 |
if (kind == SYSPROF_SYMBOL_KIND_PROCESS || kind == SYSPROF_SYMBOL_KIND_THREAD) |
| 191 |
{ |
| 192 |
int pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (traceable)); |
| 193 |
|
| 194 |
if (include_threads) |
| 195 |
{ |
| 196 |
int thread_id = sysprof_document_traceable_get_thread_id (traceable); |
| 197 |
symbols[n_symbols++] = _sysprof_document_thread_symbol (document, pid, thread_id); |
| 198 |
} |
| 199 |
|
| 200 |
symbols[n_symbols++] = _sysprof_document_process_symbol (document, pid, merge_similar_processes); |
| 201 |
} |
| 202 |
|
| 203 |
for (guint i = n_symbols; i > 0; i--) |
| 204 |
{ |
| 205 |
SysprofSymbol *symbol = symbols[i-1]; |
| 206 |
|
| 207 |
n_symbols--; |
| 208 |
|
| 209 |
if (_sysprof_symbol_equal (symbol, from_symbol)) |
| 210 |
break; |
| 211 |
} |
| 212 |
|
| 213 |
if (n_symbols > 0) |
| 214 |
{ |
| 215 |
SysprofCallgraphNode *node; |
| 216 |
|
| 217 |
node = sysprof_descendants_model_add_trace (self, symbols, n_symbols); |
| 218 |
|
| 219 |
node->is_toplevel = TRUE; |
| 220 |
|
| 221 |
if (node && self->callgraph->augment_func) |
| 222 |
self->callgraph->augment_func (self->callgraph, |
| 223 |
node, |
| 224 |
SYSPROF_DOCUMENT_FRAME (traceable), |
| 225 |
FALSE, |
| 226 |
self->callgraph->augment_func_data); |
| 227 |
|
| 228 |
if ((self->callgraph->flags & SYSPROF_CALLGRAPH_FLAGS_CATEGORIZE_FRAMES) != 0) |
| 229 |
_sysprof_callgraph_categorize (self->callgraph, node); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
GListModel * |
| 234 |
_sysprof_descendants_model_new (SysprofCallgraph *callgraph, |
| 235 |
SysprofSymbol *symbol) |
| 236 |
{ |
| 237 |
SysprofDescendantsModel *self; |
| 238 |
g_autoptr(SysprofDocument) document = NULL; |
| 239 |
g_autoptr(GListModel) model = NULL; |
| 240 |
gboolean include_threads; |
| 241 |
gboolean merge_similar_processes; |
| 242 |
guint n_items; |
| 243 |
|
| 244 |
g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (callgraph), NULL); |
| 245 |
g_return_val_if_fail (SYSPROF_IS_SYMBOL (symbol), NULL); |
| 246 |
|
| 247 |
model = sysprof_callgraph_list_traceables_for_symbol (callgraph, symbol); |
| 248 |
document = g_object_ref (callgraph->document); |
| 249 |
|
| 250 |
self = g_object_new (SYSPROF_TYPE_DESCENDANTS_MODEL, NULL); |
| 251 |
self->callgraph = g_object_ref (callgraph); |
| 252 |
self->symbol = g_object_ref (symbol); |
| 253 |
self->root.summary = g_hash_table_lookup (callgraph->symbol_to_summary, symbol); |
| 254 |
|
| 255 |
g_assert (self->root.summary != NULL); |
| 256 |
g_assert (_sysprof_symbol_equal (self->root.summary->symbol, symbol)); |
| 257 |
|
| 258 |
include_threads = (callgraph->flags & SYSPROF_CALLGRAPH_FLAGS_INCLUDE_THREADS) != 0; |
| 259 |
merge_similar_processes = (callgraph->flags & SYSPROF_CALLGRAPH_FLAGS_MERGE_SIMILAR_PROCESSES) != 0; |
| 260 |
|
| 261 |
n_items = g_list_model_get_n_items (model); |
| 262 |
|
| 263 |
for (guint i = 0; i < n_items; i++) |
| 264 |
{ |
| 265 |
g_autoptr(SysprofDocumentTraceable) traceable = g_list_model_get_item (model, i); |
| 266 |
|
| 267 |
sysprof_descendants_model_add_traceable (self, |
| 268 |
document, |
| 269 |
traceable, |
| 270 |
symbol, |
| 271 |
include_threads, |
| 272 |
merge_similar_processes); |
| 273 |
} |
| 274 |
|
| 275 |
return G_LIST_MODEL (self); |
| 276 |
} |