| 1 |
/* sysprof-agent.c |
| 2 |
* |
| 3 |
* Copyright 2022 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 <errno.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
#include <glib-unix.h> |
| 28 |
#include <glib/gstdio.h> |
| 29 |
|
| 30 |
#include <gio/gunixinputstream.h> |
| 31 |
#include <gio/gunixoutputstream.h> |
| 32 |
|
| 33 |
#include <sysprof.h> |
| 34 |
|
| 35 |
#include "ipc-agent.h" |
| 36 |
|
| 37 |
#define BUFFER_SIZE (4096L*16L) /* 64KB */ |
| 38 |
|
| 39 |
static gboolean forward_fd_func (const char *option_name, |
| 40 |
const char *option_value, |
| 41 |
gpointer data, |
| 42 |
GError **error); |
| 43 |
|
| 44 |
static GMainLoop *main_loop; |
| 45 |
static IpcAgent *service; |
| 46 |
static int read_fd = -1; |
| 47 |
static int write_fd = -1; |
| 48 |
static int pty_fd = -1; |
| 49 |
static char *directory; |
| 50 |
static char *capture_filename; |
| 51 |
static char *power_profile; |
| 52 |
static GArray *forward_fds; |
| 53 |
static char **env; |
| 54 |
static SysprofRecording *active_recording; |
| 55 |
static gboolean clear_env; |
| 56 |
static gboolean aid_battery; |
| 57 |
static gboolean aid_compositor; |
| 58 |
static gboolean aid_cpu; |
| 59 |
static gboolean aid_disk; |
| 60 |
static gboolean aid_energy; |
| 61 |
static gboolean aid_gjs; |
| 62 |
static gboolean aid_memory; |
| 63 |
static gboolean aid_memprof; |
| 64 |
static gboolean aid_net; |
| 65 |
static gboolean aid_perf; |
| 66 |
static gboolean aid_tracefd; |
| 67 |
static gboolean no_throttle; |
| 68 |
static gboolean decode; |
| 69 |
static gboolean do_system_bus; |
| 70 |
static gboolean do_session_bus; |
| 71 |
static gboolean scheduler_details; |
| 72 |
static const GOptionEntry options[] = { |
| 73 |
{ "read-fd", 0, 0, G_OPTION_ARG_INT, &read_fd, "The read side of the FD to use for D-Bus" }, |
| 74 |
{ "write-fd", 0, 0, G_OPTION_ARG_INT, &write_fd, "The write side of the FD to use for D-Bus" }, |
| 75 |
{ "forward-fd", 0, 0, G_OPTION_ARG_CALLBACK, forward_fd_func, "The FD to forward to the subprocess" }, |
| 76 |
{ "directory", 0, 0, G_OPTION_ARG_FILENAME, &directory, "The directory to run spawn the subprocess from", "PATH" }, |
| 77 |
{ "capture", 0, 0, G_OPTION_ARG_FILENAME, &capture_filename, "The filename to save the sysprof capture to", "PATH" }, |
| 78 |
{ "clear-env", 0, 0, G_OPTION_ARG_NONE, &clear_env, "Clear environment instead of inheriting" }, |
| 79 |
{ "decode", 0, 0, G_OPTION_ARG_NONE, &decode, "Decode symbols at the end of the recording" }, |
| 80 |
{ "env", 0, 0, G_OPTION_ARG_STRING_ARRAY, &env, "Add an environment variable to the spawned process", "KEY=VALUE" }, |
| 81 |
{ "cpu", 0, 0, G_OPTION_ARG_NONE, &aid_cpu, "Track CPU usage and frequency" }, |
| 82 |
{ "gjs", 0, 0, G_OPTION_ARG_NONE, &aid_gjs, "Record stack traces within GJS" }, |
| 83 |
{ "perf", 0, 0, G_OPTION_ARG_NONE, &aid_perf, "Record stack traces with perf" }, |
| 84 |
{ "memory", 0, 0, G_OPTION_ARG_NONE, &aid_memory, "Record basic system memory usage" }, |
| 85 |
{ "memprof", 0, 0, G_OPTION_ARG_NONE, &aid_memprof, "Record stack traces during memory allocations" }, |
| 86 |
{ "disk", 0, 0, G_OPTION_ARG_NONE, &aid_disk, "Record disk usage information" }, |
| 87 |
{ "net", 0, 0, G_OPTION_ARG_NONE, &aid_net, "Record network usage information" }, |
| 88 |
{ "energy", 0, 0, G_OPTION_ARG_NONE, &aid_energy, "Record energy usage using RAPL" }, |
| 89 |
{ "battery", 0, 0, G_OPTION_ARG_NONE, &aid_battery, "Record battery charge and discharge rates" }, |
| 90 |
{ "compositor", 0, 0, G_OPTION_ARG_NONE, &aid_compositor, "Record GNOME Shell compositor information" }, |
| 91 |
{ "no-throttle", 0, 0, G_OPTION_ARG_NONE, &no_throttle, "Disable CPU throttling [Deprecated for --power-profile]" }, |
| 92 |
{ "tracefd", 0, 0, G_OPTION_ARG_NONE, &aid_tracefd, "Provide TRACEFD to subprocess" }, |
| 93 |
{ "power-profile", 0, 0, G_OPTION_ARG_STRING, &power_profile, "Use POWER_PROFILE for duration of recording", "power-saver|balanced|performance" }, |
| 94 |
{ "scheduler", 0, 0, G_OPTION_ARG_NONE, &scheduler_details, "Track when processes are scheduled per CPU" }, |
| 95 |
{ "session-bus", 0, 0, G_OPTION_ARG_NONE, &do_session_bus, "Profile the D-Bus session bus" }, |
| 96 |
{ "system-bus", 0, 0, G_OPTION_ARG_NONE, &do_system_bus, "Profile the D-Bus system bus" }, |
| 97 |
{ NULL } |
| 98 |
}; |
| 99 |
|
| 100 |
G_GNUC_PRINTF (1, 2) |
| 101 |
static void |
| 102 |
message (const char *format, |
| 103 |
...) |
| 104 |
{ |
| 105 |
g_autofree char *formatted = NULL; |
| 106 |
va_list args; |
| 107 |
|
| 108 |
if (service == NULL) |
| 109 |
return; |
| 110 |
|
| 111 |
va_start (args, format); |
| 112 |
formatted = g_strdup_vprintf (format, args); |
| 113 |
va_end (args); |
| 114 |
|
| 115 |
ipc_agent_emit_log (service, formatted); |
| 116 |
} |
| 117 |
|
| 118 |
#define IPC_TYPE_AGENT_IMPL (ipc_agent_impl_get_type()) |
| 119 |
G_DECLARE_FINAL_TYPE (IpcAgentImpl, ipc_agent_impl, IPC, SYPSROF_IMPL, IpcAgentSkeleton) |
| 120 |
|
| 121 |
struct _IpcAgentImpl |
| 122 |
{ |
| 123 |
IpcAgentSkeleton parent_instance; |
| 124 |
}; |
| 125 |
|
| 126 |
static gboolean |
| 127 |
handle_force_exit (IpcAgent *sysprof, |
| 128 |
GDBusMethodInvocation *invocation) |
| 129 |
{ |
| 130 |
GSubprocess *subprocess; |
| 131 |
|
| 132 |
if (active_recording && |
| 133 |
(subprocess = sysprof_recording_get_subprocess (active_recording))) |
| 134 |
g_subprocess_force_exit (subprocess); |
| 135 |
|
| 136 |
ipc_agent_complete_force_exit (sysprof, invocation); |
| 137 |
|
| 138 |
return TRUE; |
| 139 |
} |
| 140 |
|
| 141 |
static gboolean |
| 142 |
handle_send_signal (IpcAgent *sysprof, |
| 143 |
GDBusMethodInvocation *invocation, |
| 144 |
int signum) |
| 145 |
{ |
| 146 |
GSubprocess *subprocess; |
| 147 |
|
| 148 |
if (active_recording && |
| 149 |
(subprocess = sysprof_recording_get_subprocess (active_recording))) |
| 150 |
g_subprocess_send_signal (subprocess, signum); |
| 151 |
|
| 152 |
ipc_agent_complete_send_signal (sysprof, invocation); |
| 153 |
|
| 154 |
return TRUE; |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
service_iface_init (IpcAgentIface *iface) |
| 159 |
{ |
| 160 |
iface->handle_force_exit = handle_force_exit; |
| 161 |
iface->handle_send_signal = handle_send_signal; |
| 162 |
} |
| 163 |
|
| 164 |
G_DEFINE_FINAL_TYPE_WITH_CODE (IpcAgentImpl, ipc_agent_impl, IPC_TYPE_AGENT_SKELETON, |
| 165 |
G_IMPLEMENT_INTERFACE (IPC_TYPE_AGENT, service_iface_init)) |
| 166 |
|
| 167 |
static void |
| 168 |
ipc_agent_impl_class_init (IpcAgentImplClass *klass) |
| 169 |
{ |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
ipc_agent_impl_init (IpcAgentImpl *self) |
| 174 |
{ |
| 175 |
} |
| 176 |
|
| 177 |
static gboolean |
| 178 |
forward_fd_func (const char *option_name, |
| 179 |
const char *option_value, |
| 180 |
gpointer data, |
| 181 |
GError **error) |
| 182 |
{ |
| 183 |
int fd; |
| 184 |
|
| 185 |
if (forward_fds == NULL) |
| 186 |
forward_fds = g_array_new (FALSE, FALSE, sizeof (int)); |
| 187 |
|
| 188 |
errno = 0; |
| 189 |
|
| 190 |
if (!(fd = atoi (option_value)) && errno != 0) |
| 191 |
{ |
| 192 |
int errsv = errno; |
| 193 |
g_set_error (error, |
| 194 |
G_IO_ERROR, |
| 195 |
g_io_error_from_errno (errsv), |
| 196 |
"--forward-fd must contain a file-descriptor: %s", |
| 197 |
g_strerror (errsv)); |
| 198 |
return FALSE; |
| 199 |
} |
| 200 |
|
| 201 |
if (fd < 0) |
| 202 |
{ |
| 203 |
g_set_error (error, |
| 204 |
G_IO_ERROR, |
| 205 |
G_IO_ERROR_INVAL, |
| 206 |
"--forward-fd must be 0 or a positive integer"); |
| 207 |
return FALSE; |
| 208 |
} |
| 209 |
|
| 210 |
g_array_append_val (forward_fds, fd); |
| 211 |
|
| 212 |
return TRUE; |
| 213 |
} |
| 214 |
|
| 215 |
static void |
| 216 |
split_argv (int argc, |
| 217 |
char **argv, |
| 218 |
int *our_argc, |
| 219 |
char ***our_argv, |
| 220 |
int *sub_argc, |
| 221 |
char ***sub_argv) |
| 222 |
{ |
| 223 |
gboolean found_split = FALSE; |
| 224 |
|
| 225 |
*our_argc = 0; |
| 226 |
*our_argv = g_new0 (char *, 1); |
| 227 |
|
| 228 |
*sub_argc = 0; |
| 229 |
*sub_argv = g_new0 (char *, 1); |
| 230 |
|
| 231 |
for (int i = 0; i < argc; i++) |
| 232 |
{ |
| 233 |
if (g_strcmp0 (argv[i], "--") == 0) |
| 234 |
{ |
| 235 |
found_split = TRUE; |
| 236 |
} |
| 237 |
else if (found_split) |
| 238 |
{ |
| 239 |
(*sub_argv) = g_realloc_n (*sub_argv, *sub_argc + 2, sizeof (char *)); |
| 240 |
(*sub_argv)[*sub_argc] = g_strdup (argv[i]); |
| 241 |
(*sub_argv)[*sub_argc+1] = NULL; |
| 242 |
(*sub_argc)++; |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
(*our_argv) = g_realloc_n (*our_argv, *our_argc + 2, sizeof (char *)); |
| 247 |
(*our_argv)[*our_argc] = g_strdup (argv[i]); |
| 248 |
(*our_argv)[*our_argc+1] = NULL; |
| 249 |
(*our_argc)++; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
static void |
| 255 |
warn_error (GError **error) |
| 256 |
{ |
| 257 |
if (*error) |
| 258 |
{ |
| 259 |
g_warning ("%s", (*error)->message); |
| 260 |
g_clear_error (error); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
static GDBusConnection * |
| 265 |
create_connection (GIOStream *stream, |
| 266 |
GError **error) |
| 267 |
{ |
| 268 |
GDBusConnection *ret; |
| 269 |
|
| 270 |
g_assert (G_IS_IO_STREAM (stream)); |
| 271 |
g_assert (main_loop != NULL); |
| 272 |
g_assert (error != NULL); |
| 273 |
|
| 274 |
if ((ret = g_dbus_connection_new_sync (stream, NULL, |
| 275 |
G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, |
| 276 |
NULL, NULL, error))) |
| 277 |
{ |
| 278 |
g_dbus_connection_set_exit_on_close (ret, FALSE); |
| 279 |
g_signal_connect_swapped (ret, "closed", G_CALLBACK (g_main_loop_quit), main_loop); |
| 280 |
} |
| 281 |
|
| 282 |
return ret; |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
sysprof_agent_recording_diagnostics_items_changed_cb (GListModel *model, |
| 287 |
guint position, |
| 288 |
guint removed, |
| 289 |
guint added, |
| 290 |
gpointer user_data) |
| 291 |
{ |
| 292 |
g_assert (G_IS_LIST_MODEL (model)); |
| 293 |
g_assert (user_data == NULL); |
| 294 |
|
| 295 |
for (guint i = 0; i < added; i++) |
| 296 |
{ |
| 297 |
g_autoptr(SysprofDiagnostic) diagnostic = g_list_model_get_item (model, position+i); |
| 298 |
|
| 299 |
message ("%s: %s", |
| 300 |
sysprof_diagnostic_get_domain (diagnostic), |
| 301 |
sysprof_diagnostic_get_message (diagnostic)); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
static gboolean |
| 306 |
sigint_handler (gpointer user_data) |
| 307 |
{ |
| 308 |
static int count; |
| 309 |
|
| 310 |
if (count >= 2) |
| 311 |
{ |
| 312 |
g_main_loop_quit (main_loop); |
| 313 |
return G_SOURCE_REMOVE; |
| 314 |
} |
| 315 |
|
| 316 |
g_printerr ("\n"); |
| 317 |
|
| 318 |
if (count == 0) |
| 319 |
{ |
| 320 |
g_printerr ("%s\n", "Stopping profiler. Press twice more ^C to force exit."); |
| 321 |
sysprof_recording_stop_async (active_recording, NULL, NULL, NULL); |
| 322 |
} |
| 323 |
|
| 324 |
count++; |
| 325 |
|
| 326 |
return G_SOURCE_CONTINUE; |
| 327 |
} |
| 328 |
|
| 329 |
static void |
| 330 |
sysprof_agent_wait_cb (GObject *object, |
| 331 |
GAsyncResult *result, |
| 332 |
gpointer user_data) |
| 333 |
{ |
| 334 |
SysprofRecording *recording = (SysprofRecording *)object; |
| 335 |
g_autoptr(GError) error = NULL; |
| 336 |
|
| 337 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 338 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 339 |
g_assert (user_data == NULL); |
| 340 |
|
| 341 |
if (!sysprof_recording_wait_finish (recording, result, &error)) |
| 342 |
g_error ("Failed to complete recording: %s", error->message); |
| 343 |
|
| 344 |
g_main_loop_quit (main_loop); |
| 345 |
} |
| 346 |
|
| 347 |
static void |
| 348 |
sysprof_agent_record_cb (GObject *object, |
| 349 |
GAsyncResult *result, |
| 350 |
gpointer user_data) |
| 351 |
{ |
| 352 |
SysprofProfiler *profiler = (SysprofProfiler *)object; |
| 353 |
g_autoptr(SysprofRecording) recording = NULL; |
| 354 |
g_autoptr(GListModel) diagnostics = NULL; |
| 355 |
g_autoptr(GError) error = NULL; |
| 356 |
|
| 357 |
g_assert (SYSPROF_IS_PROFILER (profiler)); |
| 358 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 359 |
g_assert (user_data == NULL); |
| 360 |
|
| 361 |
if (!(recording = sysprof_profiler_record_finish (profiler, result, &error))) |
| 362 |
g_error ("Failed to start profiling session: %s", error->message); |
| 363 |
|
| 364 |
diagnostics = sysprof_recording_list_diagnostics (recording); |
| 365 |
g_signal_connect (diagnostics, |
| 366 |
"items-changed", |
| 367 |
G_CALLBACK (sysprof_agent_recording_diagnostics_items_changed_cb), |
| 368 |
NULL); |
| 369 |
sysprof_agent_recording_diagnostics_items_changed_cb (diagnostics, |
| 370 |
0, |
| 371 |
0, |
| 372 |
g_list_model_get_n_items (diagnostics), |
| 373 |
NULL); |
| 374 |
|
| 375 |
sysprof_recording_wait_async (recording, |
| 376 |
NULL, |
| 377 |
sysprof_agent_wait_cb, |
| 378 |
NULL); |
| 379 |
|
| 380 |
g_set_object (&active_recording, recording); |
| 381 |
} |
| 382 |
|
| 383 |
int |
| 384 |
main (int argc, |
| 385 |
char *argv[]) |
| 386 |
{ |
| 387 |
SysprofCaptureWriter *writer = NULL; |
| 388 |
g_autoptr(SysprofProfiler) profiler = NULL; |
| 389 |
g_autoptr(GDBusConnection) connection = NULL; |
| 390 |
g_autoptr(GDBusConnection) session_bus = NULL; |
| 391 |
g_autoptr(GDBusConnection) system_bus = NULL; |
| 392 |
g_autoptr(GOptionContext) context = NULL; |
| 393 |
g_autoptr(GError) error = NULL; |
| 394 |
g_auto(GStrv) our_argv = NULL; |
| 395 |
g_auto(GStrv) sub_argv = NULL; |
| 396 |
g_autofd int gjs_trace_fd = -1; |
| 397 |
g_autofd int trace_fd = -1; |
| 398 |
GSubprocess *subprocess; |
| 399 |
int our_argc = -1; |
| 400 |
int sub_argc = -1; |
| 401 |
|
| 402 |
sysprof_clock_init (); |
| 403 |
|
| 404 |
g_set_prgname ("sysprof-agent"); |
| 405 |
g_set_application_name ("sysprof-agent"); |
| 406 |
|
| 407 |
/* Ignore SIGPIPE as we're using pipes to IPC */ |
| 408 |
signal (SIGPIPE, SIG_IGN); |
| 409 |
|
| 410 |
/* Split argv into pre/post -- command split */ |
| 411 |
split_argv (argc, argv, &our_argc, &our_argv, &sub_argc, &sub_argv); |
| 412 |
g_assert (our_argc >= 0); |
| 413 |
g_assert (sub_argc >= 0); |
| 414 |
|
| 415 |
/* Parse command line options pre -- */ |
| 416 |
context = g_option_context_new ("-- COMMAND"); |
| 417 |
g_option_context_add_main_entries (context, options, NULL); |
| 418 |
if (!g_option_context_parse (context, &our_argc, &our_argv, &error)) |
| 419 |
{ |
| 420 |
g_printerr ("%s\n", error->message); |
| 421 |
return EXIT_FAILURE; |
| 422 |
} |
| 423 |
|
| 424 |
/* Make sure we have a filename to capture to */ |
| 425 |
if (capture_filename == NULL) |
| 426 |
{ |
| 427 |
g_printerr ("You must provide --capture=PATH\n"); |
| 428 |
return EXIT_FAILURE; |
| 429 |
} |
| 430 |
|
| 431 |
/* Setup main loop, we'll need it going forward for things |
| 432 |
* like async D-Bus, waiting for child processes, etc. |
| 433 |
*/ |
| 434 |
main_loop = g_main_loop_new (NULL, FALSE); |
| 435 |
|
| 436 |
/* First spin up our bus connections */ |
| 437 |
if (!(session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error))) |
| 438 |
warn_error (&error); |
| 439 |
if (!(system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL))) |
| 440 |
warn_error (&error); |
| 441 |
|
| 442 |
/* Now setup our private p2p D-Bus connection to the controller */ |
| 443 |
if (read_fd != -1 || write_fd != -1) |
| 444 |
{ |
| 445 |
g_autoptr(GIOStream) stream = NULL; |
| 446 |
g_autoptr(GInputStream) in_stream = NULL; |
| 447 |
g_autoptr(GOutputStream) out_stream = NULL; |
| 448 |
|
| 449 |
/* Both must be set, not just one side */ |
| 450 |
if (read_fd == -1 || write_fd == -1) |
| 451 |
{ |
| 452 |
g_printerr ("You must specify both --read-fd and --write-fd\n"); |
| 453 |
return EXIT_FAILURE; |
| 454 |
} |
| 455 |
|
| 456 |
/* We need these FDs non-blocking for async IO */ |
| 457 |
if (!g_unix_set_fd_nonblocking (read_fd, TRUE, &error) || |
| 458 |
!g_unix_set_fd_nonblocking (write_fd, TRUE, &error)) |
| 459 |
{ |
| 460 |
g_printerr ("Failed to set FDs in nonblocking mode: %s\n", |
| 461 |
error->message); |
| 462 |
return EXIT_FAILURE; |
| 463 |
} |
| 464 |
|
| 465 |
/* Create stream using FDs provided to us */ |
| 466 |
in_stream = g_unix_input_stream_new (read_fd, FALSE); |
| 467 |
out_stream = g_unix_output_stream_new (write_fd, FALSE); |
| 468 |
stream = g_simple_io_stream_new (in_stream, out_stream); |
| 469 |
|
| 470 |
/* Create connection using our private stream from the controller */ |
| 471 |
if (!(connection = create_connection (stream, &error))) |
| 472 |
{ |
| 473 |
g_printerr ("Failed to setup P2P D-Bus connection: %s\n", |
| 474 |
error->message); |
| 475 |
return EXIT_FAILURE; |
| 476 |
} |
| 477 |
|
| 478 |
/* Now export our service at "/" (but don't start processing messages |
| 479 |
* until we start the profiler, further on. |
| 480 |
*/ |
| 481 |
service = g_object_new (IPC_TYPE_AGENT_IMPL, NULL); |
| 482 |
if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (service), |
| 483 |
connection, "/", &error)) |
| 484 |
{ |
| 485 |
g_printerr ("Failed to export service over D-Bus connection: %s", |
| 486 |
error->message); |
| 487 |
return EXIT_FAILURE; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
/* Now start setting up our profiler */ |
| 492 |
profiler = sysprof_profiler_new (); |
| 493 |
|
| 494 |
if (aid_battery) |
| 495 |
sysprof_profiler_add_instrument (profiler, sysprof_battery_charge_new ()); |
| 496 |
|
| 497 |
if (aid_cpu) |
| 498 |
sysprof_profiler_add_instrument (profiler, sysprof_cpu_usage_new ()); |
| 499 |
|
| 500 |
if (aid_disk) |
| 501 |
sysprof_profiler_add_instrument (profiler, sysprof_disk_usage_new ()); |
| 502 |
|
| 503 |
if (aid_energy) |
| 504 |
sysprof_profiler_add_instrument (profiler, sysprof_energy_usage_new ()); |
| 505 |
|
| 506 |
if (aid_memory) |
| 507 |
sysprof_profiler_add_instrument (profiler, sysprof_memory_usage_new ()); |
| 508 |
|
| 509 |
if (aid_net) |
| 510 |
sysprof_profiler_add_instrument (profiler, sysprof_network_usage_new ()); |
| 511 |
|
| 512 |
if (aid_perf) |
| 513 |
sysprof_profiler_add_instrument (profiler, sysprof_sampler_new ()); |
| 514 |
|
| 515 |
sysprof_profiler_add_instrument (profiler, sysprof_system_logs_new ()); |
| 516 |
|
| 517 |
if (power_profile) |
| 518 |
sysprof_profiler_add_instrument (profiler, sysprof_power_profile_new (power_profile)); |
| 519 |
else if (no_throttle) |
| 520 |
sysprof_profiler_add_instrument (profiler, sysprof_power_profile_new ("performance")); |
| 521 |
|
| 522 |
if (aid_compositor) |
| 523 |
sysprof_profiler_add_instrument (profiler, |
| 524 |
sysprof_proxied_instrument_new (G_BUS_TYPE_SESSION, |
| 525 |
"org.gnome.Shell", |
| 526 |
"/org/gnome/Sysprof3/Profiler")); |
| 527 |
|
| 528 |
if (do_session_bus) |
| 529 |
sysprof_profiler_add_instrument (profiler, sysprof_dbus_monitor_new (G_BUS_TYPE_SESSION)); |
| 530 |
|
| 531 |
if (do_system_bus) |
| 532 |
sysprof_profiler_add_instrument (profiler, sysprof_dbus_monitor_new (G_BUS_TYPE_SYSTEM)); |
| 533 |
|
| 534 |
if (scheduler_details) |
| 535 |
sysprof_profiler_add_instrument (profiler, sysprof_scheduler_details_new ()); |
| 536 |
|
| 537 |
if (decode) |
| 538 |
sysprof_profiler_add_instrument (profiler, sysprof_symbols_bundle_new ()); |
| 539 |
|
| 540 |
/* If -- was ommitted or there are no commands, just profile the entire |
| 541 |
* system without spawning anything. Really only useful when testing the |
| 542 |
* agent without a D-Bus service. |
| 543 |
*/ |
| 544 |
if (sub_argc >= 0) |
| 545 |
{ |
| 546 |
g_autoptr(SysprofSpawnable) spawnable = sysprof_spawnable_new (); |
| 547 |
g_auto(GStrv) current_env = g_get_environ (); |
| 548 |
|
| 549 |
if (directory != NULL) |
| 550 |
sysprof_spawnable_set_cwd (spawnable, directory); |
| 551 |
else |
| 552 |
sysprof_spawnable_set_cwd (spawnable, g_get_current_dir ()); |
| 553 |
|
| 554 |
sysprof_spawnable_append_args (spawnable, (const char * const *)sub_argv); |
| 555 |
|
| 556 |
if (clear_env) |
| 557 |
sysprof_spawnable_set_environ (spawnable, NULL); |
| 558 |
else |
| 559 |
sysprof_spawnable_set_environ (spawnable, (const char * const *)current_env); |
| 560 |
|
| 561 |
if (env != NULL) |
| 562 |
{ |
| 563 |
for (guint i = 0; env[i]; i++) |
| 564 |
{ |
| 565 |
g_autofree char *key = NULL; |
| 566 |
const char *eq; |
| 567 |
|
| 568 |
if (!(eq = strchr (env[i], '='))) |
| 569 |
{ |
| 570 |
sysprof_spawnable_setenv (spawnable, env[i], ""); |
| 571 |
continue; |
| 572 |
} |
| 573 |
|
| 574 |
key = g_strndup (env[i], eq - env[i]); |
| 575 |
sysprof_spawnable_setenv (spawnable, key, eq+1); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
sysprof_profiler_set_spawnable (profiler, spawnable); |
| 580 |
|
| 581 |
if (aid_gjs) |
| 582 |
gjs_trace_fd = sysprof_spawnable_add_trace_fd (spawnable, "GJS_TRACE_FD"); |
| 583 |
|
| 584 |
if (aid_tracefd) |
| 585 |
trace_fd = sysprof_spawnable_add_trace_fd (spawnable, NULL); |
| 586 |
|
| 587 |
if (aid_memprof) |
| 588 |
sysprof_spawnable_add_ld_preload (spawnable, PACKAGE_LIBDIR"/libsysprof-memory-"API_VERSION_S".so"); |
| 589 |
|
| 590 |
if (forward_fds != NULL) |
| 591 |
{ |
| 592 |
for (guint f = 0; f < forward_fds->len; f++) |
| 593 |
{ |
| 594 |
int fd = g_array_index (forward_fds, int, f); |
| 595 |
sysprof_spawnable_take_fd (spawnable, dup (fd), fd); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
if (pty_fd != -1) |
| 600 |
{ |
| 601 |
sysprof_spawnable_take_fd (spawnable, dup (pty_fd), STDIN_FILENO); |
| 602 |
sysprof_spawnable_take_fd (spawnable, dup (pty_fd), STDOUT_FILENO); |
| 603 |
sysprof_spawnable_take_fd (spawnable, dup (pty_fd), STDERR_FILENO); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
sysprof_profiler_add_instrument (profiler, sysprof_tracefd_consumer_new (g_steal_fd (&gjs_trace_fd))); |
| 608 |
sysprof_profiler_add_instrument (profiler, sysprof_tracefd_consumer_new (g_steal_fd (&trace_fd))); |
| 609 |
|
| 610 |
sysprof_profiler_add_instrument (profiler, |
| 611 |
g_object_new (SYSPROF_TYPE_SUBPROCESS_OUTPUT, |
| 612 |
"stdout-path", "eglinfo", |
| 613 |
"command-argv", (const char * const[]) {"eglinfo", NULL}, |
| 614 |
NULL)); |
| 615 |
sysprof_profiler_add_instrument (profiler, |
| 616 |
g_object_new (SYSPROF_TYPE_SUBPROCESS_OUTPUT, |
| 617 |
"stdout-path", "glxinfo", |
| 618 |
"command-argv", (const char * const[]) {"glxinfo", NULL}, |
| 619 |
NULL)); |
| 620 |
|
| 621 |
/* Now open the writer for our session */ |
| 622 |
if (!(writer = sysprof_capture_writer_new (capture_filename, BUFFER_SIZE))) |
| 623 |
{ |
| 624 |
int errsv = errno; |
| 625 |
g_printerr ("Failed to open capture writer: %s\n", |
| 626 |
g_strerror (errsv)); |
| 627 |
return EXIT_FAILURE; |
| 628 |
} |
| 629 |
|
| 630 |
sysprof_profiler_record_async (profiler, |
| 631 |
writer, |
| 632 |
NULL, |
| 633 |
sysprof_agent_record_cb, |
| 634 |
NULL); |
| 635 |
|
| 636 |
g_unix_signal_add (SIGINT, sigint_handler, main_loop); |
| 637 |
g_unix_signal_add (SIGTERM, sigint_handler, main_loop); |
| 638 |
|
| 639 |
/* Now tell the connection to start processing messages that are |
| 640 |
* delivered from the controller, or signals destined back. |
| 641 |
*/ |
| 642 |
if (connection != NULL) |
| 643 |
g_dbus_connection_start_message_processing (connection); |
| 644 |
|
| 645 |
g_main_loop_run (main_loop); |
| 646 |
|
| 647 |
sysprof_capture_writer_flush (writer); |
| 648 |
sysprof_capture_writer_unref (writer); |
| 649 |
|
| 650 |
subprocess = sysprof_recording_get_subprocess (active_recording); |
| 651 |
|
| 652 |
/* Try to exit the same way as the subprocess did to propagate that |
| 653 |
* back into Builder who is watching *this* process. |
| 654 |
*/ |
| 655 |
if (subprocess != NULL) |
| 656 |
{ |
| 657 |
if (g_subprocess_get_if_signaled (subprocess)) |
| 658 |
{ |
| 659 |
int signum = g_subprocess_get_term_sig (subprocess); |
| 660 |
/* Try to exit in the same manner, or SIGKILL if that doesn't |
| 661 |
* work, or just EXIT_FAILURE as last resort. |
| 662 |
*/ |
| 663 |
raise (signum); |
| 664 |
raise (SIGKILL); |
| 665 |
return EXIT_FAILURE; |
| 666 |
} |
| 667 |
|
| 668 |
if (g_subprocess_get_if_exited (subprocess)) |
| 669 |
return g_subprocess_get_exit_status (subprocess); |
| 670 |
} |
| 671 |
|
| 672 |
return EXIT_SUCCESS; |
| 673 |
} |