| 1 |
/* sysprof-diff.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 <libdex.h> |
| 24 |
|
| 25 |
#include <sysprof.h> |
| 26 |
|
| 27 |
#include "sysprof-callgraph-private.h" |
| 28 |
#include "sysprof-symbol-private.h" |
| 29 |
|
| 30 |
static GMainLoop *main_loop; |
| 31 |
static int exit_code = -1; |
| 32 |
static char empty[1024]; |
| 33 |
static const GOptionEntry options[] = { |
| 34 |
{ NULL } |
| 35 |
}; |
| 36 |
|
| 37 |
static void |
| 38 |
load_callgraph_cb (GObject *object, |
| 39 |
GAsyncResult *result, |
| 40 |
gpointer user_data) |
| 41 |
{ |
| 42 |
g_autoptr(DexPromise) promise = user_data; |
| 43 |
SysprofCallgraph *callgraph; |
| 44 |
GError *error = NULL; |
| 45 |
|
| 46 |
if ((callgraph = sysprof_document_callgraph_finish (SYSPROF_DOCUMENT (object), result, &error))) |
| 47 |
dex_promise_resolve_object (promise, callgraph); |
| 48 |
else |
| 49 |
dex_promise_reject (promise, error); |
| 50 |
} |
| 51 |
|
| 52 |
static DexFuture * |
| 53 |
load_callgraph (SysprofDocument *document, |
| 54 |
SysprofCallgraphFlags flags, |
| 55 |
GListModel *traceables, |
| 56 |
gsize augment_size, |
| 57 |
SysprofAugmentationFunc augment_func, |
| 58 |
gpointer augment_func_data, |
| 59 |
GDestroyNotify augment_func_data_destroy) |
| 60 |
{ |
| 61 |
DexPromise *promise; |
| 62 |
|
| 63 |
g_assert (SYSPROF_IS_DOCUMENT (document)); |
| 64 |
g_assert (G_IS_LIST_MODEL (traceables)); |
| 65 |
|
| 66 |
promise = dex_promise_new (); |
| 67 |
|
| 68 |
sysprof_document_callgraph_async (document, |
| 69 |
flags, |
| 70 |
traceables, |
| 71 |
augment_size, |
| 72 |
augment_func, |
| 73 |
augment_func_data, |
| 74 |
augment_func_data_destroy, |
| 75 |
dex_promise_get_cancellable (promise), |
| 76 |
load_callgraph_cb, |
| 77 |
dex_ref (promise)); |
| 78 |
|
| 79 |
return DEX_FUTURE (promise); |
| 80 |
} |
| 81 |
|
| 82 |
typedef struct |
| 83 |
{ |
| 84 |
guint size; |
| 85 |
guint total; |
| 86 |
} Augment; |
| 87 |
|
| 88 |
static void |
| 89 |
augment_cb (SysprofCallgraph *callgraph, |
| 90 |
SysprofCallgraphNode *node, |
| 91 |
SysprofDocumentFrame *frame, |
| 92 |
gboolean summarize, |
| 93 |
gpointer user_data) |
| 94 |
{ |
| 95 |
Augment *aug; |
| 96 |
|
| 97 |
g_assert (SYSPROF_IS_CALLGRAPH (callgraph)); |
| 98 |
g_assert (node != NULL); |
| 99 |
g_assert (SYSPROF_IS_DOCUMENT_SAMPLE (frame)); |
| 100 |
g_assert (user_data == NULL); |
| 101 |
|
| 102 |
aug = sysprof_callgraph_get_augment (callgraph, node); |
| 103 |
aug->size += 1; |
| 104 |
aug->total += 1; |
| 105 |
|
| 106 |
for (SysprofCallgraphNode *iter = node->parent; iter; iter = iter->parent) |
| 107 |
{ |
| 108 |
aug = sysprof_callgraph_get_augment (callgraph, iter); |
| 109 |
aug->total += 1; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
static int |
| 114 |
compare (SysprofCallgraphNode *a, |
| 115 |
SysprofCallgraphNode *b) |
| 116 |
{ |
| 117 |
if (a == b) |
| 118 |
return 0; |
| 119 |
|
| 120 |
if (a != NULL && b == NULL) |
| 121 |
return -1; |
| 122 |
|
| 123 |
if (a == NULL && b != NULL) |
| 124 |
return 1; |
| 125 |
|
| 126 |
return strcmp (a->summary->symbol->name, |
| 127 |
b->summary->symbol->name); |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
diff (SysprofCallgraphNode *left, |
| 132 |
SysprofCallgraphNode *right, |
| 133 |
guint depth) |
| 134 |
{ |
| 135 |
const Augment *l_aug = left ? (const Augment *)&left->augment[0] : NULL; |
| 136 |
const Augment *r_aug = right ? (const Augment *)&right->augment[0] : NULL; |
| 137 |
int l_total = l_aug ? l_aug->total : 0; |
| 138 |
int r_total = r_aug ? r_aug->total : 0; |
| 139 |
int change = r_total - l_total; |
| 140 |
const char *name = left ? left->summary->symbol->name : right->summary->symbol->name; |
| 141 |
SysprofCallgraphNode *l_iter; |
| 142 |
SysprofCallgraphNode *r_iter; |
| 143 |
|
| 144 |
g_print ("%6u | %6u | %+6d | ", l_total, r_total, change); |
| 145 |
if (depth > 0) |
| 146 |
write (STDOUT_FILENO, empty, MIN (sizeof empty, depth*2)); |
| 147 |
g_print ("%s\n", name); |
| 148 |
|
| 149 |
l_iter = left ? left->children : NULL; |
| 150 |
r_iter = right ? right->children : NULL; |
| 151 |
|
| 152 |
while (l_iter || r_iter) |
| 153 |
{ |
| 154 |
while (r_iter && compare (r_iter, l_iter) < 0) |
| 155 |
{ |
| 156 |
diff (NULL, r_iter, depth+1); |
| 157 |
r_iter = r_iter->next; |
| 158 |
} |
| 159 |
|
| 160 |
if (l_iter) |
| 161 |
{ |
| 162 |
if (r_iter && compare (l_iter, r_iter) == 0) |
| 163 |
diff (l_iter, r_iter, depth+1); |
| 164 |
else |
| 165 |
diff (l_iter, NULL, depth+1); |
| 166 |
|
| 167 |
l_iter = l_iter->next; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
load_document_cb (GObject *object, |
| 174 |
GAsyncResult *result, |
| 175 |
gpointer user_data) |
| 176 |
{ |
| 177 |
g_autoptr(DexPromise) promise = user_data; |
| 178 |
SysprofDocument *document; |
| 179 |
GError *error = NULL; |
| 180 |
|
| 181 |
if ((document = sysprof_document_loader_load_finish (SYSPROF_DOCUMENT_LOADER (object), result, &error))) |
| 182 |
dex_promise_resolve_object (promise, document); |
| 183 |
else |
| 184 |
dex_promise_reject (promise, error); |
| 185 |
} |
| 186 |
|
| 187 |
static DexFuture * |
| 188 |
load_document (SysprofDocumentLoader *loader) |
| 189 |
{ |
| 190 |
DexPromise *promise = dex_promise_new (); |
| 191 |
sysprof_document_loader_load_async (loader, NULL, load_document_cb, dex_ref (promise)); |
| 192 |
return DEX_FUTURE (promise); |
| 193 |
} |
| 194 |
|
| 195 |
static DexFuture * |
| 196 |
sysprof_diff_fiber (gpointer data) |
| 197 |
{ |
| 198 |
const char * const *filenames = data; |
| 199 |
g_autoptr(SysprofDocumentLoader) loader1 = NULL; |
| 200 |
g_autoptr(SysprofDocumentLoader) loader2 = NULL; |
| 201 |
g_autoptr(SysprofDocument) document1 = NULL; |
| 202 |
g_autoptr(SysprofDocument) document2 = NULL; |
| 203 |
g_autoptr(SysprofCallgraph) callgraph1 = NULL; |
| 204 |
g_autoptr(SysprofCallgraph) callgraph2 = NULL; |
| 205 |
g_autoptr(GListModel) traceables1 = NULL; |
| 206 |
g_autoptr(GListModel) traceables2 = NULL; |
| 207 |
g_autoptr(GError) error = NULL; |
| 208 |
SysprofCallgraphFlags flags = 0; |
| 209 |
|
| 210 |
g_assert (filenames != NULL); |
| 211 |
g_assert (filenames[0] != NULL); |
| 212 |
g_assert (filenames[1] != NULL); |
| 213 |
|
| 214 |
loader1 = sysprof_document_loader_new (filenames[0]); |
| 215 |
loader2 = sysprof_document_loader_new (filenames[1]); |
| 216 |
|
| 217 |
g_printerr ("Loading %s...\n", filenames[0]); |
| 218 |
if (!(document1 = dex_await_object (load_document (loader1), &error))) |
| 219 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 220 |
|
| 221 |
traceables1 = sysprof_document_list_samples (document1); |
| 222 |
|
| 223 |
if (!(callgraph1 = dex_await_object (load_callgraph (document1, flags, traceables1, sizeof (Augment), augment_cb, NULL, NULL), &error))) |
| 224 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 225 |
|
| 226 |
g_printerr ("Loading %s...\n", filenames[1]); |
| 227 |
if (!(document2 = dex_await_object (load_document (loader2), &error))) |
| 228 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 229 |
|
| 230 |
traceables2 = sysprof_document_list_samples (document2); |
| 231 |
|
| 232 |
if (!(callgraph2 = dex_await_object (load_callgraph (document2, flags, traceables2, sizeof (Augment), augment_cb, NULL, NULL), &error))) |
| 233 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 234 |
|
| 235 |
g_print ("BEFORE | AFTER | CHANGE | FUNCTION\n"); |
| 236 |
g_print ("-------|--------|--------|---------\n"); |
| 237 |
diff (&callgraph1->root, &callgraph2->root, 0); |
| 238 |
|
| 239 |
exit_code = EXIT_SUCCESS; |
| 240 |
|
| 241 |
g_main_loop_quit (main_loop); |
| 242 |
|
| 243 |
return dex_future_new_for_boolean (TRUE); |
| 244 |
} |
| 245 |
|
| 246 |
static DexFuture * |
| 247 |
sysprof_diff_catch_error_cb (DexFuture *future, |
| 248 |
gpointer user_data) |
| 249 |
{ |
| 250 |
g_autoptr(GError) error = NULL; |
| 251 |
|
| 252 |
dex_await (dex_ref (future), &error); |
| 253 |
|
| 254 |
g_printerr ("Error: %s\n", error->message); |
| 255 |
|
| 256 |
exit_code = EXIT_FAILURE; |
| 257 |
|
| 258 |
g_main_loop_quit (main_loop); |
| 259 |
|
| 260 |
return NULL; |
| 261 |
} |
| 262 |
|
| 263 |
int |
| 264 |
main (int argc, |
| 265 |
char *argv[]) |
| 266 |
{ |
| 267 |
g_autoptr(GOptionContext) context = NULL; |
| 268 |
g_autoptr(GError) error = NULL; |
| 269 |
const char *copy[3]; |
| 270 |
|
| 271 |
sysprof_clock_init (); |
| 272 |
dex_init (); |
| 273 |
|
| 274 |
g_set_prgname ("sysprof-diff"); |
| 275 |
g_set_application_name ("sysprof-diff"); |
| 276 |
|
| 277 |
main_loop = g_main_loop_new (NULL, FALSE); |
| 278 |
|
| 279 |
context = g_option_context_new ("BEFORE_CAPTURE AFTER_CAPTURE -- Generate a callgraph diff between two captures"); |
| 280 |
g_option_context_add_main_entries (context, options, NULL); |
| 281 |
|
| 282 |
if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 283 |
{ |
| 284 |
g_printerr ("%s\n", error->message); |
| 285 |
return EXIT_FAILURE; |
| 286 |
} |
| 287 |
|
| 288 |
if (argc < 3) |
| 289 |
{ |
| 290 |
g_printerr ("usage: %s BEFORE_CAPTURE AFTER_CAPTURE\n", argv[0]); |
| 291 |
return EXIT_FAILURE; |
| 292 |
} |
| 293 |
|
| 294 |
copy[0] = argv[1]; |
| 295 |
copy[1] = argv[2]; |
| 296 |
copy[2] = NULL; |
| 297 |
|
| 298 |
memset (empty, ' ', sizeof empty); |
| 299 |
|
| 300 |
dex_future_disown ( |
| 301 |
dex_future_catch ( |
| 302 |
dex_scheduler_spawn (NULL, 0, |
| 303 |
sysprof_diff_fiber, |
| 304 |
g_strdupv ((char **)copy), |
| 305 |
(GDestroyNotify)g_strfreev), |
| 306 |
sysprof_diff_catch_error_cb, |
| 307 |
NULL, NULL)); |
| 308 |
|
| 309 |
if (exit_code == -1) |
| 310 |
g_main_loop_run (main_loop); |
| 311 |
|
| 312 |
return exit_code; |
| 313 |
} |