| 1 |
/* test-profiler.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-unix.h> |
| 24 |
#include <glib/gstdio.h> |
| 25 |
|
| 26 |
#include <sysprof.h> |
| 27 |
|
| 28 |
static GMainLoop *main_loop; |
| 29 |
static char *capture_file; |
| 30 |
static SysprofRecording *active_recording; |
| 31 |
static gboolean memprof; |
| 32 |
static gboolean tracer; |
| 33 |
static gboolean gnome_shell; |
| 34 |
static gboolean bundle_symbols; |
| 35 |
static gboolean session_bus; |
| 36 |
static gboolean system_bus; |
| 37 |
static gboolean gjs; |
| 38 |
static char *power_profile; |
| 39 |
static const GOptionEntry entries[] = { |
| 40 |
{ "capture", 'c', 0, G_OPTION_ARG_FILENAME, &capture_file, "The file to capture into", "CAPTURE" }, |
| 41 |
{ "memprof", 'm', 0, G_OPTION_ARG_NONE, &memprof, "Do memory allocation tracking on subprocess" }, |
| 42 |
{ "tracer", 't', 0, G_OPTION_ARG_NONE, &tracer, "Enable tracing with __cyg_profile_enter" }, |
| 43 |
{ "gjs", 'g', 0, G_OPTION_ARG_NONE, &gjs, "export GJS_TRACE_FD" }, |
| 44 |
{ "gnome-shell", 's', 0, G_OPTION_ARG_NONE, &gnome_shell, "Request GNOME Shell to provide profiler data" }, |
| 45 |
{ "power-profile", 'p', 0, G_OPTION_ARG_STRING, &power_profile, "Use POWER_PROFILE for duration of recording", "power-saver|balanced|performance" }, |
| 46 |
{ "session-bus", 0, 0, G_OPTION_ARG_NONE, &session_bus, "Record D-Bus messages on the session bus" }, |
| 47 |
{ "system-bus", 0, 0, G_OPTION_ARG_NONE, &system_bus, "Record D-Bus messages on the system bus" }, |
| 48 |
{ "bundle-symbols", 'b', 0, G_OPTION_ARG_STRING, &bundle_symbols, "Bundle symbols with the capture" }, |
| 49 |
{ 0 } |
| 50 |
}; |
| 51 |
|
| 52 |
static void |
| 53 |
wait_cb (GObject *object, |
| 54 |
GAsyncResult *result, |
| 55 |
gpointer user_data) |
| 56 |
{ |
| 57 |
SysprofRecording *recording = (SysprofRecording *)object; |
| 58 |
g_autoptr(GError) error = NULL; |
| 59 |
gboolean r; |
| 60 |
|
| 61 |
r = sysprof_recording_wait_finish (recording, result, &error); |
| 62 |
g_assert_no_error (error); |
| 63 |
g_assert_true (r); |
| 64 |
|
| 65 |
g_main_loop_quit (main_loop); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
diagnostics_items_changed_cb (GListModel *model, |
| 70 |
guint position, |
| 71 |
guint removed, |
| 72 |
guint added, |
| 73 |
gpointer user_data) |
| 74 |
{ |
| 75 |
for (guint i = 0; i < added; i++) |
| 76 |
{ |
| 77 |
g_autoptr(SysprofDiagnostic) diagnostic = g_list_model_get_item (model, position+i); |
| 78 |
|
| 79 |
g_printerr ("%s: %s\n", |
| 80 |
sysprof_diagnostic_get_domain (diagnostic), |
| 81 |
sysprof_diagnostic_get_message (diagnostic)); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
record_cb (GObject *object, |
| 87 |
GAsyncResult *result, |
| 88 |
gpointer user_data) |
| 89 |
{ |
| 90 |
g_autoptr(GError) error = NULL; |
| 91 |
g_autoptr(SysprofRecording) recording = sysprof_profiler_record_finish (SYSPROF_PROFILER (object), result, &error); |
| 92 |
g_autoptr(GListModel) diagnostics = NULL; |
| 93 |
|
| 94 |
g_assert_no_error (error); |
| 95 |
g_assert_nonnull (recording); |
| 96 |
g_assert_true (SYSPROF_IS_RECORDING (recording)); |
| 97 |
|
| 98 |
diagnostics = sysprof_recording_list_diagnostics (recording); |
| 99 |
g_signal_connect (diagnostics, |
| 100 |
"items-changed", |
| 101 |
G_CALLBACK (diagnostics_items_changed_cb), |
| 102 |
NULL); |
| 103 |
diagnostics_items_changed_cb (diagnostics, |
| 104 |
0, |
| 105 |
0, |
| 106 |
g_list_model_get_n_items (diagnostics), |
| 107 |
NULL); |
| 108 |
|
| 109 |
sysprof_recording_wait_async (recording, NULL, wait_cb, NULL); |
| 110 |
|
| 111 |
active_recording = recording; |
| 112 |
} |
| 113 |
|
| 114 |
static gboolean |
| 115 |
sigint_handler (gpointer user_data) |
| 116 |
{ |
| 117 |
static int count; |
| 118 |
|
| 119 |
if (count >= 2) |
| 120 |
{ |
| 121 |
g_main_loop_quit (main_loop); |
| 122 |
return G_SOURCE_REMOVE; |
| 123 |
} |
| 124 |
|
| 125 |
g_printerr ("\n"); |
| 126 |
|
| 127 |
if (count == 0) |
| 128 |
{ |
| 129 |
g_printerr ("%s\n", "Stopping profiler. Press twice more ^C to force exit."); |
| 130 |
sysprof_recording_stop_async (active_recording, NULL, NULL, NULL); |
| 131 |
} |
| 132 |
|
| 133 |
count++; |
| 134 |
|
| 135 |
return G_SOURCE_CONTINUE; |
| 136 |
} |
| 137 |
|
| 138 |
int |
| 139 |
main (int argc, |
| 140 |
char *argv[]) |
| 141 |
{ |
| 142 |
g_autoptr(GOptionContext) context = g_option_context_new ("[-- [COMMAND...]]"); |
| 143 |
g_autoptr(SysprofProfiler) profiler = NULL; |
| 144 |
g_autoptr(GError) error = NULL; |
| 145 |
g_auto(GStrv) argv_copy = NULL; |
| 146 |
SysprofCaptureWriter *writer = NULL; |
| 147 |
SysprofCaptureReader *reader = NULL; |
| 148 |
g_autofd int trace_fd = -1; |
| 149 |
g_autofd int gjs_trace_fd = -1; |
| 150 |
int argv_copy_len = 0; |
| 151 |
|
| 152 |
sysprof_clock_init (); |
| 153 |
|
| 154 |
main_loop = g_main_loop_new (NULL, FALSE); |
| 155 |
|
| 156 |
argv_copy = g_new0 (char *, argc+1); |
| 157 |
for (guint i = 0; i < argc; i++) |
| 158 |
{ |
| 159 |
if (strcmp ("--", argv[i]) == 0) |
| 160 |
{ |
| 161 |
argv_copy[i] = NULL; |
| 162 |
break; |
| 163 |
} |
| 164 |
|
| 165 |
argv_copy[i] = g_strdup (argv[i]); |
| 166 |
argv_copy_len++; |
| 167 |
} |
| 168 |
|
| 169 |
g_option_context_add_main_entries (context, entries, NULL); |
| 170 |
|
| 171 |
if (!g_option_context_parse (context, &argv_copy_len, &argv_copy, &error)) |
| 172 |
{ |
| 173 |
g_printerr ("%s\n", error->message); |
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
if (capture_file == NULL) |
| 178 |
writer = sysprof_capture_writer_new ("capture.syscap", 0); |
| 179 |
else |
| 180 |
writer = sysprof_capture_writer_new (capture_file, 0); |
| 181 |
|
| 182 |
profiler = sysprof_profiler_new (); |
| 183 |
|
| 184 |
sysprof_profiler_add_instrument (profiler, sysprof_battery_charge_new ()); |
| 185 |
sysprof_profiler_add_instrument (profiler, sysprof_cpu_usage_new ()); |
| 186 |
sysprof_profiler_add_instrument (profiler, sysprof_disk_usage_new ()); |
| 187 |
sysprof_profiler_add_instrument (profiler, sysprof_energy_usage_new ()); |
| 188 |
sysprof_profiler_add_instrument (profiler, sysprof_memory_usage_new ()); |
| 189 |
sysprof_profiler_add_instrument (profiler, sysprof_network_usage_new ()); |
| 190 |
sysprof_profiler_add_instrument (profiler, sysprof_system_logs_new ()); |
| 191 |
|
| 192 |
if (bundle_symbols) |
| 193 |
sysprof_profiler_add_instrument (profiler, sysprof_symbols_bundle_new ()); |
| 194 |
|
| 195 |
if (power_profile) |
| 196 |
sysprof_profiler_add_instrument (profiler, sysprof_power_profile_new (power_profile)); |
| 197 |
|
| 198 |
if (gnome_shell) |
| 199 |
sysprof_profiler_add_instrument (profiler, |
| 200 |
sysprof_proxied_instrument_new (G_BUS_TYPE_SESSION, |
| 201 |
"org.gnome.Shell", |
| 202 |
"/org/gnome/Sysprof3/Profiler")); |
| 203 |
if (memprof) |
| 204 |
sysprof_profiler_add_instrument (profiler, sysprof_malloc_tracing_new ()); |
| 205 |
|
| 206 |
sysprof_profiler_add_instrument (profiler, sysprof_scheduler_details_new ()); |
| 207 |
|
| 208 |
if (tracer) |
| 209 |
sysprof_profiler_add_instrument (profiler, sysprof_tracer_new ()); |
| 210 |
else |
| 211 |
sysprof_profiler_add_instrument (profiler, sysprof_sampler_new ()); |
| 212 |
|
| 213 |
if (session_bus) |
| 214 |
sysprof_profiler_add_instrument (profiler, sysprof_dbus_monitor_new (G_BUS_TYPE_SESSION)); |
| 215 |
|
| 216 |
if (system_bus) |
| 217 |
sysprof_profiler_add_instrument (profiler, sysprof_dbus_monitor_new (G_BUS_TYPE_SYSTEM)); |
| 218 |
|
| 219 |
for (int i = 1; i < argc; i++) |
| 220 |
{ |
| 221 |
if (strcmp (argv[i], "--") == 0 && i+1 < argc) |
| 222 |
{ |
| 223 |
g_autoptr(SysprofSpawnable) spawnable = sysprof_spawnable_new (); |
| 224 |
|
| 225 |
sysprof_spawnable_append_args (spawnable, (const char * const *)&argv[i+1]); |
| 226 |
sysprof_spawnable_set_cwd (spawnable, g_get_current_dir ()); |
| 227 |
|
| 228 |
sysprof_profiler_set_spawnable (profiler, spawnable); |
| 229 |
|
| 230 |
trace_fd = sysprof_spawnable_add_trace_fd (spawnable, NULL); |
| 231 |
|
| 232 |
if (gjs) |
| 233 |
gjs_trace_fd = sysprof_spawnable_add_trace_fd (spawnable, "GJS_TRACE_FD"); |
| 234 |
|
| 235 |
break; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
sysprof_profiler_add_instrument (profiler, sysprof_tracefd_consumer_new (g_steal_fd (&gjs_trace_fd))); |
| 240 |
sysprof_profiler_add_instrument (profiler, sysprof_tracefd_consumer_new (g_steal_fd (&trace_fd))); |
| 241 |
|
| 242 |
sysprof_profiler_add_instrument (profiler, |
| 243 |
g_object_new (SYSPROF_TYPE_SUBPROCESS_OUTPUT, |
| 244 |
"stdout-path", "__zoneinfo__", |
| 245 |
"command-argv", (const char * const[]) { "cat", "/proc/zoneinfo", NULL }, |
| 246 |
NULL)); |
| 247 |
|
| 248 |
sysprof_profiler_record_async (profiler, writer, NULL, record_cb, NULL); |
| 249 |
|
| 250 |
g_unix_signal_add (SIGINT, sigint_handler, main_loop); |
| 251 |
g_unix_signal_add (SIGTERM, sigint_handler, main_loop); |
| 252 |
|
| 253 |
g_main_loop_run (main_loop); |
| 254 |
|
| 255 |
if (trace_fd != -1) |
| 256 |
{ |
| 257 |
if ((reader = sysprof_capture_reader_new_from_fd (g_steal_fd (&trace_fd)))) |
| 258 |
sysprof_capture_writer_cat (writer, reader); |
| 259 |
} |
| 260 |
|
| 261 |
sysprof_capture_writer_flush (writer); |
| 262 |
|
| 263 |
g_clear_pointer (&reader, sysprof_capture_reader_unref); |
| 264 |
g_clear_pointer (&writer, sysprof_capture_writer_unref); |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |