| 1 |
/* test-callgraph.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 toplevel : 1; |
| 31 |
guint64 size : 31; |
| 32 |
guint32 total : 32; |
| 33 |
} Augment; |
| 34 |
|
| 35 |
static char *kallsyms_path; |
| 36 |
static gboolean include_threads; |
| 37 |
static GEnumClass *category_class; |
| 38 |
static gboolean summary_only; |
| 39 |
static const GOptionEntry entries[] = { |
| 40 |
{ "kallsyms", 'k', 0, G_OPTION_ARG_FILENAME, &kallsyms_path, "The path to kallsyms to use for decoding", "PATH" }, |
| 41 |
{ "threads", 't', 0, G_OPTION_ARG_NONE, &include_threads, "Include threads in the stack traces" }, |
| 42 |
{ "summary", 's', 0, G_OPTION_ARG_NONE, &summary_only, "Only show summary" }, |
| 43 |
{ 0 } |
| 44 |
}; |
| 45 |
|
| 46 |
static void |
| 47 |
print_callgraph (GListModel *model, |
| 48 |
guint depth, |
| 49 |
guint total) |
| 50 |
{ |
| 51 |
guint n_items = g_list_model_get_n_items (model); |
| 52 |
|
| 53 |
for (guint i = 0; i < n_items; i++) |
| 54 |
{ |
| 55 |
g_autoptr(SysprofCallgraphFrame) frame = g_list_model_get_item (model, i); |
| 56 |
SysprofSymbol *symbol = sysprof_callgraph_frame_get_symbol (frame); |
| 57 |
Augment *aug = sysprof_callgraph_frame_get_augment (frame); |
| 58 |
SysprofCallgraphCategory category = sysprof_callgraph_frame_get_category (frame); |
| 59 |
const char*category_name = g_enum_get_value (category_class, category)->value_nick; |
| 60 |
char tstr[16]; |
| 61 |
|
| 62 |
g_snprintf (tstr, sizeof tstr, "%.2lf%%", 100. * (aug->total / (double)total)); |
| 63 |
|
| 64 |
g_print (" [%6u] [%8s] ", aug->total, tstr); |
| 65 |
for (guint j = 0; j < depth; j++) |
| 66 |
g_print (" "); |
| 67 |
g_print ("%s [%s]\n", sysprof_symbol_get_name (symbol), category_name); |
| 68 |
|
| 69 |
print_callgraph (G_LIST_MODEL (frame), depth+1, total); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
summarize_cb (GObject *object, |
| 75 |
GAsyncResult *result, |
| 76 |
gpointer user_data) |
| 77 |
{ |
| 78 |
SysprofCallgraphFrame *frame = SYSPROF_CALLGRAPH_FRAME (object); |
| 79 |
GMainLoop *main_loop = user_data; |
| 80 |
g_autoptr(GError) error = NULL; |
| 81 |
g_autoptr(GListModel) model = NULL; |
| 82 |
guint n_items; |
| 83 |
|
| 84 |
model = sysprof_callgraph_frame_summarize_finish (frame, result, &error); |
| 85 |
g_assert_no_error (error); |
| 86 |
g_assert_nonnull (model); |
| 87 |
g_assert_true (G_IS_LIST_MODEL (model)); |
| 88 |
|
| 89 |
g_print ("\n\nSummary\n"); |
| 90 |
|
| 91 |
n_items = g_list_model_get_n_items (model); |
| 92 |
|
| 93 |
for (guint i = 0; i < n_items; i++) |
| 94 |
{ |
| 95 |
g_autoptr(SysprofCategorySummary) summary = g_list_model_get_item (model, i); |
| 96 |
|
| 97 |
g_assert_nonnull (summary); |
| 98 |
g_assert_true (SYSPROF_IS_CATEGORY_SUMMARY (summary)); |
| 99 |
|
| 100 |
g_print ("%s: %lf\n", |
| 101 |
g_enum_to_string (SYSPROF_TYPE_CALLGRAPH_CATEGORY, |
| 102 |
sysprof_category_summary_get_category (summary)), |
| 103 |
sysprof_category_summary_get_fraction (summary)); |
| 104 |
} |
| 105 |
|
| 106 |
g_main_loop_quit (main_loop); |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
callgraph_cb (GObject *object, |
| 111 |
GAsyncResult *result, |
| 112 |
gpointer user_data) |
| 113 |
{ |
| 114 |
SysprofDocument *document = SYSPROF_DOCUMENT (object); |
| 115 |
GMainLoop *main_loop = user_data; |
| 116 |
g_autoptr(GError) error = NULL; |
| 117 |
g_autoptr(SysprofCallgraph) callgraph = sysprof_document_callgraph_finish (document, result, &error); |
| 118 |
g_autoptr(SysprofCallgraphFrame) root = NULL; |
| 119 |
Augment *aug; |
| 120 |
|
| 121 |
g_assert_no_error (error); |
| 122 |
g_assert_true (SYSPROF_IS_CALLGRAPH (callgraph)); |
| 123 |
|
| 124 |
root = g_list_model_get_item (G_LIST_MODEL (callgraph), 0); |
| 125 |
aug = sysprof_callgraph_frame_get_augment (root); |
| 126 |
|
| 127 |
if (!summary_only) |
| 128 |
{ |
| 129 |
g_print (" Hits Percent\n"); |
| 130 |
print_callgraph (G_LIST_MODEL (callgraph), 0, aug->total); |
| 131 |
} |
| 132 |
|
| 133 |
sysprof_callgraph_frame_summarize_async (root, NULL, summarize_cb, main_loop); |
| 134 |
} |
| 135 |
|
| 136 |
static void |
| 137 |
augment_cb (SysprofCallgraph *callgraph, |
| 138 |
SysprofCallgraphNode *node, |
| 139 |
SysprofDocumentFrame *frame, |
| 140 |
gboolean summarize, |
| 141 |
gpointer user_data) |
| 142 |
{ |
| 143 |
Augment *aug; |
| 144 |
|
| 145 |
g_assert (SYSPROF_IS_CALLGRAPH (callgraph)); |
| 146 |
g_assert (node != NULL); |
| 147 |
g_assert (SYSPROF_IS_DOCUMENT_SAMPLE (frame)); |
| 148 |
g_assert (user_data == NULL); |
| 149 |
|
| 150 |
aug = sysprof_callgraph_get_augment (callgraph, node); |
| 151 |
aug->toplevel = TRUE; |
| 152 |
|
| 153 |
for (; node ; node = sysprof_callgraph_node_parent (node)) |
| 154 |
{ |
| 155 |
aug = sysprof_callgraph_get_augment (callgraph, node); |
| 156 |
aug->total += 1; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
int |
| 161 |
main (int argc, |
| 162 |
char *argv[]) |
| 163 |
{ |
| 164 |
g_autoptr(GOptionContext) context = g_option_context_new ("- test callgraph generation"); |
| 165 |
g_autoptr(GMainLoop) main_loop = g_main_loop_new (NULL, FALSE); |
| 166 |
g_autoptr(SysprofDocumentLoader) loader = NULL; |
| 167 |
g_autoptr(SysprofDocument) document = NULL; |
| 168 |
g_autoptr(SysprofMultiSymbolizer) multi = NULL; |
| 169 |
g_autoptr(GListModel) samples = NULL; |
| 170 |
g_autoptr(GError) error = NULL; |
| 171 |
SysprofCallgraphFlags flags = 0; |
| 172 |
|
| 173 |
setlocale (LC_ALL, ""); |
| 174 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 175 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 176 |
textdomain (GETTEXT_PACKAGE); |
| 177 |
|
| 178 |
g_option_context_add_main_entries (context, entries, NULL); |
| 179 |
if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 180 |
g_error ("%s", error->message); |
| 181 |
|
| 182 |
if (argc < 2) |
| 183 |
g_error ("usage: %s CAPTURE_FILE", argv[0]); |
| 184 |
|
| 185 |
category_class = g_type_class_ref (SYSPROF_TYPE_CALLGRAPH_CATEGORY); |
| 186 |
|
| 187 |
multi = sysprof_multi_symbolizer_new (); |
| 188 |
|
| 189 |
if (kallsyms_path) |
| 190 |
{ |
| 191 |
g_autoptr(GFile) kallsyms_file = g_file_new_for_path (kallsyms_path); |
| 192 |
GFileInputStream *kallsyms_stream = g_file_read (kallsyms_file, NULL, NULL); |
| 193 |
|
| 194 |
sysprof_multi_symbolizer_take (multi, sysprof_kallsyms_symbolizer_new_for_symbols (G_INPUT_STREAM (kallsyms_stream))); |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
sysprof_multi_symbolizer_take (multi, sysprof_kallsyms_symbolizer_new ()); |
| 199 |
} |
| 200 |
|
| 201 |
sysprof_multi_symbolizer_take (multi, sysprof_elf_symbolizer_new ()); |
| 202 |
sysprof_multi_symbolizer_take (multi, sysprof_jitmap_symbolizer_new ()); |
| 203 |
|
| 204 |
loader = sysprof_document_loader_new (argv[1]); |
| 205 |
sysprof_document_loader_set_symbolizer (loader, SYSPROF_SYMBOLIZER (multi)); |
| 206 |
if (!(document = sysprof_document_loader_load (loader, NULL, &error))) |
| 207 |
g_error ("Failed to load document: %s", error->message); |
| 208 |
|
| 209 |
samples = sysprof_document_list_samples (document); |
| 210 |
|
| 211 |
if (include_threads) |
| 212 |
flags |= SYSPROF_CALLGRAPH_FLAGS_INCLUDE_THREADS; |
| 213 |
|
| 214 |
flags |= SYSPROF_CALLGRAPH_FLAGS_CATEGORIZE_FRAMES; |
| 215 |
|
| 216 |
sysprof_document_callgraph_async (document, |
| 217 |
flags, |
| 218 |
samples, |
| 219 |
sizeof (Augment), |
| 220 |
augment_cb, NULL, NULL, |
| 221 |
NULL, |
| 222 |
callgraph_cb, |
| 223 |
main_loop); |
| 224 |
|
| 225 |
g_main_loop_run (main_loop); |
| 226 |
|
| 227 |
return 0; |
| 228 |
} |