| 1 |
/* top-offenders.c |
| 2 |
* |
| 3 |
* Copyright 2026 Christian Hergert |
| 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 <glib/gi18n.h> |
| 24 |
#include <locale.h> |
| 25 |
|
| 26 |
#include <sysprof.h> |
| 27 |
|
| 28 |
#include "sysprof-callgraph-private.h" |
| 29 |
|
| 30 |
typedef struct _Augment |
| 31 |
{ |
| 32 |
guint64 hits; |
| 33 |
} Augment; |
| 34 |
|
| 35 |
typedef struct _Offender |
| 36 |
{ |
| 37 |
SysprofCallgraphFrame *frame; |
| 38 |
guint64 hits; |
| 39 |
GPtrArray *path; |
| 40 |
} Offender; |
| 41 |
|
| 42 |
static int filter_pid = -1; |
| 43 |
static int filter_tid = -1; |
| 44 |
static int top_n = 10; |
| 45 |
|
| 46 |
static const GOptionEntry entries[] = { |
| 47 |
{ "process", 'p', 0, G_OPTION_ARG_INT, &filter_pid, "Filter by process ID", "PID" }, |
| 48 |
{ "thread", 't', 0, G_OPTION_ARG_INT, &filter_tid, "Filter by thread ID", "TID" }, |
| 49 |
{ "num", 'n', 0, G_OPTION_ARG_INT, &top_n, "Number of top offenders to show", "N" }, |
| 50 |
{ 0 } |
| 51 |
}; |
| 52 |
|
| 53 |
static GListModel * |
| 54 |
filter_samples (SysprofDocument *document) |
| 55 |
{ |
| 56 |
g_autoptr(GListModel) all_samples = NULL; |
| 57 |
g_autoptr(GListStore) filtered = NULL; |
| 58 |
guint n_items; |
| 59 |
|
| 60 |
all_samples = sysprof_document_list_samples (document); |
| 61 |
filtered = g_list_store_new (SYSPROF_TYPE_DOCUMENT_SAMPLE); |
| 62 |
|
| 63 |
n_items = g_list_model_get_n_items (all_samples); |
| 64 |
|
| 65 |
for (guint i = 0; i < n_items; i++) |
| 66 |
{ |
| 67 |
g_autoptr(SysprofDocumentSample) sample = g_list_model_get_item (all_samples, i); |
| 68 |
int pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (sample)); |
| 69 |
int tid = sysprof_document_traceable_get_thread_id (SYSPROF_DOCUMENT_TRACEABLE (sample)); |
| 70 |
|
| 71 |
if (filter_pid != -1 && pid != filter_pid) |
| 72 |
continue; |
| 73 |
|
| 74 |
if (filter_tid != -1 && tid != filter_tid) |
| 75 |
continue; |
| 76 |
|
| 77 |
g_list_store_append (filtered, sample); |
| 78 |
} |
| 79 |
|
| 80 |
return G_LIST_MODEL (g_steal_pointer (&filtered)); |
| 81 |
} |
| 82 |
|
| 83 |
static void |
| 84 |
augment_cb (SysprofCallgraph *callgraph, |
| 85 |
SysprofCallgraphNode *node, |
| 86 |
SysprofDocumentFrame *frame, |
| 87 |
gboolean summarize, |
| 88 |
gpointer user_data) |
| 89 |
{ |
| 90 |
Augment *aug; |
| 91 |
|
| 92 |
g_assert (SYSPROF_IS_CALLGRAPH (callgraph)); |
| 93 |
g_assert (node != NULL); |
| 94 |
g_assert (SYSPROF_IS_DOCUMENT_SAMPLE (frame)); |
| 95 |
|
| 96 |
aug = sysprof_callgraph_get_augment (callgraph, node); |
| 97 |
aug->hits += 1; |
| 98 |
|
| 99 |
for (SysprofCallgraphNode *iter = node->parent; iter; iter = iter->parent) |
| 100 |
{ |
| 101 |
aug = sysprof_callgraph_get_augment (callgraph, iter); |
| 102 |
aug->hits += 1; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static void |
| 107 |
collect_leaf_nodes (SysprofCallgraphFrame *frame, |
| 108 |
GPtrArray *offenders, |
| 109 |
GPtrArray *current_path) |
| 110 |
{ |
| 111 |
SysprofSymbol *symbol; |
| 112 |
const char *symbol_name; |
| 113 |
|
| 114 |
if (frame == NULL) |
| 115 |
return; |
| 116 |
|
| 117 |
symbol = sysprof_callgraph_frame_get_symbol (frame); |
| 118 |
symbol_name = symbol ? sysprof_symbol_get_name (symbol) : NULL; |
| 119 |
|
| 120 |
/* Add current frame to path */ |
| 121 |
if (symbol_name) |
| 122 |
g_ptr_array_add (current_path, g_object_ref (frame)); |
| 123 |
|
| 124 |
/* Check if this is a leaf node */ |
| 125 |
if (sysprof_callgraph_frame_is_leaf (frame)) |
| 126 |
{ |
| 127 |
Augment *aug = sysprof_callgraph_frame_get_augment (frame); |
| 128 |
Offender *offender = g_new0 (Offender, 1); |
| 129 |
|
| 130 |
offender->frame = g_object_ref (frame); |
| 131 |
offender->hits = aug ? aug->hits : 0; |
| 132 |
offender->path = g_ptr_array_new_with_free_func (g_object_unref); |
| 133 |
|
| 134 |
/* Copy current path */ |
| 135 |
for (guint i = 0; i < current_path->len; i++) |
| 136 |
{ |
| 137 |
SysprofCallgraphFrame *path_frame = g_ptr_array_index (current_path, i); |
| 138 |
g_ptr_array_add (offender->path, g_object_ref (path_frame)); |
| 139 |
} |
| 140 |
|
| 141 |
g_ptr_array_add (offenders, offender); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
/* Recurse into children */ |
| 146 |
guint n_children = g_list_model_get_n_items (G_LIST_MODEL (frame)); |
| 147 |
for (guint i = 0; i < n_children; i++) |
| 148 |
{ |
| 149 |
g_autoptr(SysprofCallgraphFrame) child = g_list_model_get_item (G_LIST_MODEL (frame), i); |
| 150 |
collect_leaf_nodes (child, offenders, current_path); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/* Remove current frame from path when backtracking */ |
| 155 |
if (symbol_name && current_path->len > 0) |
| 156 |
{ |
| 157 |
/* g_ptr_array_remove_index will call the free_func (g_object_unref) automatically */ |
| 158 |
g_ptr_array_remove_index (current_path, current_path->len - 1); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
static gint |
| 163 |
compare_offenders (gconstpointer a, |
| 164 |
gconstpointer b) |
| 165 |
{ |
| 166 |
const Offender *off_a = *(const Offender **)a; |
| 167 |
const Offender *off_b = *(const Offender **)b; |
| 168 |
|
| 169 |
if (off_a->hits < off_b->hits) |
| 170 |
return 1; |
| 171 |
else if (off_a->hits > off_b->hits) |
| 172 |
return -1; |
| 173 |
else |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
static void |
| 178 |
offender_free (Offender *offender) |
| 179 |
{ |
| 180 |
if (offender) |
| 181 |
{ |
| 182 |
g_clear_object (&offender->frame); |
| 183 |
if (offender->path) |
| 184 |
g_ptr_array_unref (offender->path); |
| 185 |
g_free (offender); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static void |
| 190 |
print_stack_trace (Offender *offender) |
| 191 |
{ |
| 192 |
guint frame_num = 0; |
| 193 |
|
| 194 |
/* Print path from leaf to root (frame 00 is the leaf where sample occurred) */ |
| 195 |
for (gint i = offender->path->len - 1; i >= 0; i--) |
| 196 |
{ |
| 197 |
SysprofCallgraphFrame *path_frame = g_ptr_array_index (offender->path, i); |
| 198 |
SysprofSymbol *symbol = sysprof_callgraph_frame_get_symbol (path_frame); |
| 199 |
Augment *aug = sysprof_callgraph_frame_get_augment (path_frame); |
| 200 |
const char *symbol_name = symbol ? sysprof_symbol_get_name (symbol) : "?"; |
| 201 |
guint64 hits = aug ? aug->hits : 0; |
| 202 |
|
| 203 |
g_print ("%3u: %7" G_GUINT64_FORMAT ": %s\n", frame_num++, hits, symbol_name); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
static void |
| 208 |
callgraph_cb (GObject *object, |
| 209 |
GAsyncResult *result, |
| 210 |
gpointer user_data) |
| 211 |
{ |
| 212 |
SysprofDocument *document = SYSPROF_DOCUMENT (object); |
| 213 |
GMainLoop *main_loop = user_data; |
| 214 |
g_autoptr(GError) error = NULL; |
| 215 |
g_autoptr(SysprofCallgraph) callgraph = NULL; |
| 216 |
g_autoptr(GPtrArray) offenders = NULL; |
| 217 |
g_autoptr(SysprofCallgraphFrame) root_frame = NULL; |
| 218 |
guint n_to_print; |
| 219 |
|
| 220 |
callgraph = sysprof_document_callgraph_finish (document, result, &error); |
| 221 |
if (error) |
| 222 |
{ |
| 223 |
g_printerr ("Failed to build callgraph: %s\n", error->message); |
| 224 |
g_main_loop_quit (main_loop); |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
g_assert_true (SYSPROF_IS_CALLGRAPH (callgraph)); |
| 229 |
|
| 230 |
offenders = g_ptr_array_new_with_free_func ((GDestroyNotify)offender_free); |
| 231 |
|
| 232 |
root_frame = g_list_model_get_item (G_LIST_MODEL (callgraph), 0); |
| 233 |
if (root_frame) |
| 234 |
{ |
| 235 |
g_autoptr(GPtrArray) current_path = g_ptr_array_new_with_free_func (g_object_unref); |
| 236 |
collect_leaf_nodes (root_frame, offenders, current_path); |
| 237 |
} |
| 238 |
|
| 239 |
/* Sort by hits (descending) */ |
| 240 |
g_ptr_array_sort (offenders, compare_offenders); |
| 241 |
|
| 242 |
/* Print header */ |
| 243 |
g_print ("FRM: HITS: SYMBOL\n"); |
| 244 |
|
| 245 |
/* Print top N offenders */ |
| 246 |
n_to_print = MIN (top_n, offenders->len); |
| 247 |
for (guint i = 0; i < n_to_print; i++) |
| 248 |
{ |
| 249 |
Offender *offender = g_ptr_array_index (offenders, i); |
| 250 |
print_stack_trace (offender); |
| 251 |
if (i < n_to_print - 1) |
| 252 |
g_print ("\n\n\n"); |
| 253 |
} |
| 254 |
|
| 255 |
g_main_loop_quit (main_loop); |
| 256 |
} |
| 257 |
|
| 258 |
int |
| 259 |
main (int argc, |
| 260 |
char *argv[]) |
| 261 |
{ |
| 262 |
g_autoptr(GOptionContext) context = g_option_context_new ("- find top offenders in syscap file"); |
| 263 |
g_autoptr(GMainLoop) main_loop = g_main_loop_new (NULL, FALSE); |
| 264 |
g_autoptr(SysprofDocumentLoader) loader = NULL; |
| 265 |
g_autoptr(SysprofDocument) document = NULL; |
| 266 |
g_autoptr(SysprofMultiSymbolizer) multi = NULL; |
| 267 |
g_autoptr(GListModel) samples = NULL; |
| 268 |
g_autoptr(GError) error = NULL; |
| 269 |
SysprofCallgraphFlags flags = 0; |
| 270 |
|
| 271 |
setlocale (LC_ALL, ""); |
| 272 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 273 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 274 |
textdomain (GETTEXT_PACKAGE); |
| 275 |
|
| 276 |
g_option_context_add_main_entries (context, entries, NULL); |
| 277 |
if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 278 |
{ |
| 279 |
g_printerr ("%s\n", error->message); |
| 280 |
return 1; |
| 281 |
} |
| 282 |
|
| 283 |
if (argc < 2) |
| 284 |
{ |
| 285 |
g_printerr ("usage: %s CAPTURE_FILE\n", argv[0]); |
| 286 |
return 1; |
| 287 |
} |
| 288 |
|
| 289 |
multi = sysprof_multi_symbolizer_new (); |
| 290 |
sysprof_multi_symbolizer_take (multi, sysprof_kallsyms_symbolizer_new ()); |
| 291 |
sysprof_multi_symbolizer_take (multi, sysprof_elf_symbolizer_new ()); |
| 292 |
sysprof_multi_symbolizer_take (multi, sysprof_jitmap_symbolizer_new ()); |
| 293 |
|
| 294 |
loader = sysprof_document_loader_new (argv[1]); |
| 295 |
sysprof_document_loader_set_symbolizer (loader, SYSPROF_SYMBOLIZER (multi)); |
| 296 |
if (!(document = sysprof_document_loader_load (loader, NULL, &error))) |
| 297 |
{ |
| 298 |
g_printerr ("Failed to load document: %s\n", error->message); |
| 299 |
return 1; |
| 300 |
} |
| 301 |
|
| 302 |
/* Not very efficient, but easy for now */ |
| 303 |
samples = filter_samples (document); |
| 304 |
|
| 305 |
flags |= SYSPROF_CALLGRAPH_FLAGS_LEFT_HEAVY; |
| 306 |
flags |= SYSPROF_CALLGRAPH_FLAGS_INCLUDE_THREADS; |
| 307 |
flags |= SYSPROF_CALLGRAPH_FLAGS_IGNORE_PROCESS_0; |
| 308 |
|
| 309 |
sysprof_document_callgraph_async (document, |
| 310 |
flags, |
| 311 |
samples, |
| 312 |
sizeof (Augment), |
| 313 |
augment_cb, NULL, NULL, |
| 314 |
NULL, |
| 315 |
callgraph_cb, |
| 316 |
main_loop); |
| 317 |
|
| 318 |
g_main_loop_run (main_loop); |
| 319 |
|
| 320 |
return 0; |
| 321 |
} |