| 1 |
/* test-allocs-by-func.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 <glib/gi18n.h> |
| 24 |
#include <locale.h> |
| 25 |
|
| 26 |
#include <sysprof.h> |
| 27 |
|
| 28 |
typedef struct _Augment |
| 29 |
{ |
| 30 |
guint64 total_allocs; |
| 31 |
} Augment; |
| 32 |
|
| 33 |
static int |
| 34 |
sort_by_augment (gconstpointer a, |
| 35 |
gconstpointer b) |
| 36 |
{ |
| 37 |
SysprofCallgraphSymbol *sym_a = *(SysprofCallgraphSymbol **)a; |
| 38 |
SysprofCallgraphSymbol *sym_b = *(SysprofCallgraphSymbol **)b; |
| 39 |
Augment *aug_a = sysprof_callgraph_symbol_get_summary_augment (sym_a); |
| 40 |
Augment *aug_b = sysprof_callgraph_symbol_get_summary_augment (sym_b); |
| 41 |
|
| 42 |
if (aug_a->total_allocs > aug_b->total_allocs) |
| 43 |
return -1; |
| 44 |
if (aug_a->total_allocs < aug_b->total_allocs) |
| 45 |
return 1; |
| 46 |
|
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
callgraph_cb (GObject *object, |
| 52 |
GAsyncResult *result, |
| 53 |
gpointer user_data) |
| 54 |
{ |
| 55 |
SysprofDocument *document = SYSPROF_DOCUMENT (object); |
| 56 |
GMainLoop *main_loop = user_data; |
| 57 |
g_autoptr(GError) error = NULL; |
| 58 |
g_autoptr(SysprofCallgraph) callgraph = sysprof_document_callgraph_finish (document, result, &error); |
| 59 |
g_autoptr(GListModel) symbols = NULL; |
| 60 |
g_autoptr(GPtrArray) ar = NULL; |
| 61 |
guint n_items; |
| 62 |
|
| 63 |
g_assert_no_error (error); |
| 64 |
g_assert_true (SYSPROF_IS_CALLGRAPH (callgraph)); |
| 65 |
|
| 66 |
symbols = sysprof_callgraph_list_symbols (callgraph); |
| 67 |
n_items = g_list_model_get_n_items (symbols); |
| 68 |
ar = g_ptr_array_new_with_free_func (g_object_unref); |
| 69 |
|
| 70 |
g_printerr ("Sorting by total allocation...\n"); |
| 71 |
for (guint i = 0; i < n_items; i++) |
| 72 |
g_ptr_array_add (ar, g_list_model_get_item (symbols, i)); |
| 73 |
g_ptr_array_sort (ar, sort_by_augment); |
| 74 |
|
| 75 |
g_printerr ("Listing...\n"); |
| 76 |
|
| 77 |
for (guint i = 0; i < ar->len; i++) |
| 78 |
{ |
| 79 |
SysprofCallgraphSymbol *sym = g_ptr_array_index (ar, i); |
| 80 |
SysprofSymbol *symbol = sysprof_callgraph_symbol_get_symbol (sym); |
| 81 |
Augment *aug = sysprof_callgraph_symbol_get_summary_augment (sym); |
| 82 |
g_autofree char *total_size = g_format_size (aug->total_allocs); |
| 83 |
|
| 84 |
g_print ("%10s : %s\n", |
| 85 |
total_size, |
| 86 |
sysprof_symbol_get_name (symbol)); |
| 87 |
} |
| 88 |
|
| 89 |
g_main_loop_quit (main_loop); |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
augment_cb (SysprofCallgraph *callgraph, |
| 94 |
SysprofCallgraphNode *node, |
| 95 |
SysprofDocumentFrame *frame, |
| 96 |
gboolean summarize, |
| 97 |
gpointer user_data) |
| 98 |
{ |
| 99 |
Augment *aug; |
| 100 |
guint64 size; |
| 101 |
|
| 102 |
g_assert (SYSPROF_IS_CALLGRAPH (callgraph)); |
| 103 |
g_assert (node != NULL); |
| 104 |
g_assert (SYSPROF_IS_DOCUMENT_ALLOCATION (frame)); |
| 105 |
g_assert (user_data == NULL); |
| 106 |
|
| 107 |
size = sysprof_document_allocation_get_size (SYSPROF_DOCUMENT_ALLOCATION (frame)); |
| 108 |
if (size == 0) |
| 109 |
return; |
| 110 |
|
| 111 |
for (; node; node = sysprof_callgraph_node_parent (node)) |
| 112 |
{ |
| 113 |
aug = sysprof_callgraph_get_summary_augment (callgraph, node); |
| 114 |
aug->total_allocs += size; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
int |
| 119 |
main (int argc, |
| 120 |
char *argv[]) |
| 121 |
{ |
| 122 |
g_autoptr(GMainLoop) main_loop = g_main_loop_new (NULL, FALSE); |
| 123 |
g_autoptr(SysprofDocumentLoader) loader = NULL; |
| 124 |
g_autoptr(SysprofDocument) document = NULL; |
| 125 |
g_autoptr(GListModel) allocs = NULL; |
| 126 |
g_autoptr(GError) error = NULL; |
| 127 |
SysprofCallgraphFlags flags = 0; |
| 128 |
|
| 129 |
setlocale (LC_ALL, ""); |
| 130 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 131 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 132 |
textdomain (GETTEXT_PACKAGE); |
| 133 |
|
| 134 |
if (argc < 2) |
| 135 |
g_error ("usage: %s CAPTURE_FILE", argv[0]); |
| 136 |
|
| 137 |
g_printerr ("Loading document...\n"); |
| 138 |
loader = sysprof_document_loader_new (argv[1]); |
| 139 |
if (!(document = sysprof_document_loader_load (loader, NULL, &error))) |
| 140 |
g_error ("Failed to load document: %s", error->message); |
| 141 |
|
| 142 |
allocs = sysprof_document_list_allocations (document); |
| 143 |
|
| 144 |
g_printerr ("Generating callgraph...\n"); |
| 145 |
sysprof_document_callgraph_async (document, |
| 146 |
flags, |
| 147 |
allocs, |
| 148 |
sizeof (Augment), |
| 149 |
augment_cb, NULL, NULL, |
| 150 |
NULL, |
| 151 |
callgraph_cb, |
| 152 |
main_loop); |
| 153 |
|
| 154 |
g_main_loop_run (main_loop); |
| 155 |
|
| 156 |
return 0; |
| 157 |
} |