| 1 |
/* sysprof-recording.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 <stdlib.h> |
| 24 |
#include <sys/sysinfo.h> |
| 25 |
#include <sys/utsname.h> |
| 26 |
|
| 27 |
#include <libdex.h> |
| 28 |
|
| 29 |
#include "sysprof-recording-private.h" |
| 30 |
#include "sysprof-util-private.h" |
| 31 |
|
| 32 |
enum { |
| 33 |
PROP_0, |
| 34 |
PROP_DURATION, |
| 35 |
PROP_EVENT_COUNT, |
| 36 |
N_PROPS |
| 37 |
}; |
| 38 |
|
| 39 |
G_DEFINE_FINAL_TYPE (SysprofRecording, sysprof_recording, G_TYPE_OBJECT) |
| 40 |
|
| 41 |
static GParamSpec *properties[N_PROPS]; |
| 42 |
|
| 43 |
static DexFuture * |
| 44 |
_sysprof_recording_spawn (SysprofSpawnable *spawnable, |
| 45 |
GSubprocess **subprocess) |
| 46 |
{ |
| 47 |
g_autoptr(GError) error = NULL; |
| 48 |
DexFuture *ret; |
| 49 |
|
| 50 |
g_assert (SYSPROF_IS_SPAWNABLE (spawnable)); |
| 51 |
g_assert (subprocess != NULL); |
| 52 |
g_assert (*subprocess == NULL); |
| 53 |
|
| 54 |
if (!(*subprocess = sysprof_spawnable_spawn (spawnable, &error))) |
| 55 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 56 |
|
| 57 |
ret = dex_subprocess_wait_check (*subprocess); |
| 58 |
dex_async_pair_set_cancel_on_discard (DEX_ASYNC_PAIR (ret), FALSE); |
| 59 |
return ret; |
| 60 |
} |
| 61 |
|
| 62 |
static inline void |
| 63 |
add_metadata (SysprofRecording *self, |
| 64 |
const char *id, |
| 65 |
const char *value) |
| 66 |
{ |
| 67 |
if (value == NULL) |
| 68 |
return; |
| 69 |
|
| 70 |
sysprof_capture_writer_add_metadata (self->writer, |
| 71 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 72 |
-1, -1, id, value, -1); |
| 73 |
} |
| 74 |
|
| 75 |
static inline void |
| 76 |
add_metadata_int (SysprofRecording *self, |
| 77 |
const char *id, |
| 78 |
int value) |
| 79 |
{ |
| 80 |
char str[16]; |
| 81 |
g_snprintf (str, sizeof str, "%d", value); |
| 82 |
sysprof_capture_writer_add_metadata (self->writer, |
| 83 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 84 |
-1, -1, id, str, -1); |
| 85 |
} |
| 86 |
|
| 87 |
static inline void |
| 88 |
add_metadata_int64 (SysprofRecording *self, |
| 89 |
const char *id, |
| 90 |
gint64 value) |
| 91 |
{ |
| 92 |
char str[32]; |
| 93 |
g_snprintf (str, sizeof str, "%"G_GINT64_FORMAT, value); |
| 94 |
sysprof_capture_writer_add_metadata (self->writer, |
| 95 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 96 |
-1, -1, id, str, -1); |
| 97 |
} |
| 98 |
|
| 99 |
static DexFuture * |
| 100 |
sysprof_recording_fiber (gpointer user_data) |
| 101 |
{ |
| 102 |
SysprofRecording *self = user_data; |
| 103 |
g_autoptr(GDBusConnection) connection = NULL; |
| 104 |
g_autoptr(GCancellable) cancellable = NULL; |
| 105 |
g_autoptr(DexFuture) record = NULL; |
| 106 |
g_autoptr(DexFuture) monitor = NULL; |
| 107 |
g_autoptr(DexFuture) message = NULL; |
| 108 |
g_autoptr(GError) error = NULL; |
| 109 |
const char *debuginfod_urls = NULL; |
| 110 |
struct utsname uts; |
| 111 |
struct sysinfo si; |
| 112 |
gint64 begin_time; |
| 113 |
gint64 end_time; |
| 114 |
char hostname[64] = {0}; |
| 115 |
|
| 116 |
g_assert (SYSPROF_IS_RECORDING (self)); |
| 117 |
|
| 118 |
cancellable = g_cancellable_new (); |
| 119 |
|
| 120 |
if (self->use_sysprofd) |
| 121 |
{ |
| 122 |
/* Get our D-Bus system connection if we're using sysprofd. */ |
| 123 |
if (!(connection = dex_await_object (dex_bus_get (G_BUS_TYPE_SYSTEM), NULL))) |
| 124 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 125 |
|
| 126 |
/* Let every instrument know what bus we're using so that they can |
| 127 |
* automatically enable/disable sysprofd/policy-kit integration. |
| 128 |
*/ |
| 129 |
_sysprof_instruments_set_connection (self->instruments, connection); |
| 130 |
|
| 131 |
/* First ensure that all our required policy have been acquired on |
| 132 |
* the bus so that we don't need to individually acquire them from |
| 133 |
* each of the instruments after the recording starts. |
| 134 |
*/ |
| 135 |
if (!dex_await (_sysprof_instruments_acquire_policy (self->instruments, self), &error)) |
| 136 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 137 |
} |
| 138 |
|
| 139 |
/* Now allow instruments to prepare for the recording */ |
| 140 |
if (!dex_await (_sysprof_instruments_prepare (self->instruments, self), &error)) |
| 141 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 142 |
|
| 143 |
/* Ask instruments to start recording and stop if cancelled. */ |
| 144 |
record = _sysprof_instruments_record (self->instruments, self, cancellable); |
| 145 |
|
| 146 |
/* Now take our begin time now that all instruments are notified */ |
| 147 |
begin_time = SYSPROF_CAPTURE_CURRENT_TIME; |
| 148 |
|
| 149 |
g_assert (self->subprocess == NULL); |
| 150 |
|
| 151 |
/* If we need to spawn a subprocess, do it now */ |
| 152 |
if (self->spawnable != NULL) |
| 153 |
monitor = _sysprof_recording_spawn (self->spawnable, &self->subprocess); |
| 154 |
else |
| 155 |
monitor = dex_future_new_infinite (); |
| 156 |
|
| 157 |
/* Track various app metadata */ |
| 158 |
add_metadata (self, "org.gnome.sysprof.app-id", APP_ID_S); |
| 159 |
add_metadata (self, "org.gnome.sysprof.version", PACKAGE_VERSION); |
| 160 |
|
| 161 |
/* Give a readable timestamp to the user */ |
| 162 |
{ |
| 163 |
g_autoptr(GDateTime) now = g_date_time_new_now_local (); |
| 164 |
g_autofree char *now_str = g_date_time_format_iso8601 (now); |
| 165 |
add_metadata (self, "capture-time", now_str); |
| 166 |
} |
| 167 |
|
| 168 |
/* Include some host/kernel/arch information */ |
| 169 |
add_metadata_int (self, "n-cpu", g_get_num_processors ()); |
| 170 |
add_metadata_int (self, "page-size", sysprof_getpagesize ()); |
| 171 |
add_metadata_int (self, "buffer-size", sysprof_capture_writer_get_buffer_size (self->writer)); |
| 172 |
if (uname (&uts) == 0) |
| 173 |
{ |
| 174 |
add_metadata (self, "uname.sysname", uts.sysname); |
| 175 |
add_metadata (self, "uname.release", uts.release); |
| 176 |
add_metadata (self, "uname.version", uts.version); |
| 177 |
add_metadata (self, "uname.machine", uts.machine); |
| 178 |
} |
| 179 |
|
| 180 |
if (gethostname (hostname, sizeof hostname-1) == 0) |
| 181 |
add_metadata (self, "hostname", hostname); |
| 182 |
|
| 183 |
/* More system information via sysinfo */ |
| 184 |
if (sysinfo (&si) == 0) |
| 185 |
{ |
| 186 |
add_metadata_int64 (self, "sysinfo.uptime", si.uptime); |
| 187 |
add_metadata_int64 (self, "sysinfo.totalram", si.totalram); |
| 188 |
add_metadata_int64 (self, "sysinfo.freeram", si.freeram); |
| 189 |
add_metadata_int64 (self, "sysinfo.sharedram", si.sharedram); |
| 190 |
add_metadata_int64 (self, "sysinfo.bufferram", si.bufferram); |
| 191 |
add_metadata_int64 (self, "sysinfo.totalswap", si.totalswap); |
| 192 |
add_metadata_int64 (self, "sysinfo.freeswap", si.freeswap); |
| 193 |
add_metadata_int64 (self, "sysinfo.procs", si.procs); |
| 194 |
add_metadata_int64 (self, "sysinfo.totalhigh", si.totalhigh); |
| 195 |
add_metadata_int64 (self, "sysinfo.freehigh", si.freehigh); |
| 196 |
add_metadata_int64 (self, "sysinfo.mem_unit", si.mem_unit); |
| 197 |
} |
| 198 |
|
| 199 |
/* Some environment variables/info for correlating */ |
| 200 |
add_metadata (self, "USER", g_get_user_name ()); |
| 201 |
add_metadata (self, "DISPLAY", g_getenv ("DISPLAY")); |
| 202 |
add_metadata (self, "WAYLAND_DISPLAY", g_getenv ("WAYLAND_DISPLAY")); |
| 203 |
add_metadata (self, "DESKTOP_SESSION", g_getenv ("DESKTOP_SESSION")); |
| 204 |
add_metadata (self, "HOSTTYPE", g_getenv ("HOSTTYPE")); |
| 205 |
add_metadata (self, "OSTYPE", g_getenv ("OSTYPE")); |
| 206 |
|
| 207 |
if ((debuginfod_urls = g_getenv ("DEBUGINFOD_URLS"))) |
| 208 |
add_metadata (self, "DEBUGINFOD_URLS", debuginfod_urls); |
| 209 |
|
| 210 |
/* Log information about the spawning process */ |
| 211 |
if (self->spawnable != NULL) |
| 212 |
{ |
| 213 |
const char * const *argv = sysprof_spawnable_get_argv (self->spawnable); |
| 214 |
const char * const *env = sysprof_spawnable_get_environ (self->spawnable); |
| 215 |
const char *cwd = sysprof_spawnable_get_cwd (self->spawnable); |
| 216 |
|
| 217 |
if (cwd) |
| 218 |
add_metadata (self, "spawnable.cwd", cwd); |
| 219 |
|
| 220 |
if (env != NULL) |
| 221 |
{ |
| 222 |
g_autoptr(GString) str = g_string_new (NULL); |
| 223 |
|
| 224 |
for (guint e = 0; env[e]; e++) |
| 225 |
{ |
| 226 |
g_autofree char *quoted = g_shell_quote (env[e]); |
| 227 |
|
| 228 |
g_string_append (str, quoted); |
| 229 |
g_string_append_c (str, ' '); |
| 230 |
} |
| 231 |
|
| 232 |
add_metadata (self, "spawnable.environ", str->str); |
| 233 |
} |
| 234 |
|
| 235 |
if (argv != NULL) |
| 236 |
{ |
| 237 |
g_autoptr(GString) str = g_string_new (NULL); |
| 238 |
|
| 239 |
for (guint a = 0; argv[a]; a++) |
| 240 |
{ |
| 241 |
g_autofree char *quoted = g_shell_quote (argv[a]); |
| 242 |
|
| 243 |
g_string_append (str, quoted); |
| 244 |
g_string_append_c (str, ' '); |
| 245 |
} |
| 246 |
|
| 247 |
add_metadata (self, "spawnable.argv", str->str); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/* Save a copy of os-release for troubleshooting */ |
| 252 |
dex_await (_sysprof_recording_add_file (self, "/etc/os-release", FALSE), NULL); |
| 253 |
|
| 254 |
self->start_time = g_get_monotonic_time (); |
| 255 |
|
| 256 |
/* Queue receive of first message */ |
| 257 |
message = dex_channel_receive (self->channel); |
| 258 |
|
| 259 |
/* Wait for messages on our channel or the recording to complete */ |
| 260 |
for (;;) |
| 261 |
{ |
| 262 |
g_autoptr(DexFuture) duration = dex_timeout_new_seconds (1); |
| 263 |
|
| 264 |
g_debug ("Recording loop iteration"); |
| 265 |
|
| 266 |
/* Wait for either recording of all instruments to complete or a |
| 267 |
* message from our channel with what to do next. |
| 268 |
*/ |
| 269 |
if (!dex_await (dex_future_first (dex_ref (record), |
| 270 |
dex_ref (message), |
| 271 |
dex_ref (monitor), |
| 272 |
dex_ref (duration), |
| 273 |
NULL), |
| 274 |
&error) && |
| 275 |
!g_error_matches (error, DEX_ERROR, DEX_ERROR_TIMED_OUT)) |
| 276 |
goto stop_recording; |
| 277 |
|
| 278 |
/* Clear any ignored error */ |
| 279 |
g_clear_error (&error); |
| 280 |
|
| 281 |
/* If record is not pending, then everything resolved/rejected */ |
| 282 |
if (dex_future_get_status (record) != DEX_FUTURE_STATUS_PENDING || |
| 283 |
dex_future_get_status (monitor) != DEX_FUTURE_STATUS_PENDING) |
| 284 |
goto stop_recording; |
| 285 |
|
| 286 |
/* If message resolved, then we got a command to process */ |
| 287 |
if (dex_future_get_status (message) == DEX_FUTURE_STATUS_RESOLVED) |
| 288 |
{ |
| 289 |
SysprofRecordingCommand command = dex_await_uint (dex_ref (message), NULL); |
| 290 |
|
| 291 |
switch (command) |
| 292 |
{ |
| 293 |
case SYSPROF_RECORDING_COMMAND_STOP: |
| 294 |
g_debug ("Recording received stop command"); |
| 295 |
goto stop_recording; |
| 296 |
|
| 297 |
default: |
| 298 |
break; |
| 299 |
} |
| 300 |
|
| 301 |
/* Queue receive of next message */ |
| 302 |
dex_clear (&message); |
| 303 |
message = dex_channel_receive (self->channel); |
| 304 |
} |
| 305 |
|
| 306 |
/* Update duration each pass through the loop */ |
| 307 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DURATION]); |
| 308 |
|
| 309 |
/* Update event count each pass through the loop */ |
| 310 |
sysprof_capture_writer_stat (self->writer, &self->stat); |
| 311 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EVENT_COUNT]); |
| 312 |
} |
| 313 |
|
| 314 |
stop_recording: |
| 315 |
g_debug ("Stopping recording"); |
| 316 |
|
| 317 |
end_time = SYSPROF_CAPTURE_CURRENT_TIME; |
| 318 |
|
| 319 |
self->end_time = g_get_monotonic_time (); |
| 320 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DURATION]); |
| 321 |
|
| 322 |
/* Signal cancellable so that anything lingering has a chance to be |
| 323 |
* cleaned up, cascading into other subsystems. |
| 324 |
*/ |
| 325 |
g_cancellable_cancel (cancellable); |
| 326 |
|
| 327 |
/* But we must still wait for instruments to respond to |
| 328 |
* the cancellation and clean up before we can move onto |
| 329 |
* the augmentation phase. |
| 330 |
*/ |
| 331 |
dex_await (dex_ref (record), NULL); |
| 332 |
|
| 333 |
/* Let instruments augment the capture. Some instruments may include |
| 334 |
* extra information about the capture such as symbol names and their |
| 335 |
* address ranges per-process. |
| 336 |
*/ |
| 337 |
dex_await (_sysprof_instruments_augment (self->instruments, self), NULL); |
| 338 |
|
| 339 |
/* Update start/end times to be the "running time" */ |
| 340 |
_sysprof_capture_writer_set_time_range (self->writer, begin_time, end_time); |
| 341 |
|
| 342 |
/* Clear buffers and ensure the disk layer has access to them */ |
| 343 |
sysprof_capture_writer_flush (self->writer); |
| 344 |
|
| 345 |
/* Aggressively release our instruments so if they have references back |
| 346 |
* to the recording any reference cycles are broken. |
| 347 |
*/ |
| 348 |
if (self->instruments->len > 0) |
| 349 |
g_ptr_array_remove_range (self->instruments, 0, self->instruments->len); |
| 350 |
|
| 351 |
/* Ignore error types we use to bail out of loops */ |
| 352 |
if (error != NULL && |
| 353 |
!g_error_matches (error, DEX_ERROR, DEX_ERROR_TIMED_OUT) && |
| 354 |
!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 355 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 356 |
|
| 357 |
return dex_future_new_for_boolean (TRUE); |
| 358 |
} |
| 359 |
|
| 360 |
static void |
| 361 |
sysprof_recording_finalize (GObject *object) |
| 362 |
{ |
| 363 |
SysprofRecording *self = (SysprofRecording *)object; |
| 364 |
|
| 365 |
if (self->channel) |
| 366 |
{ |
| 367 |
dex_channel_close_send (self->channel); |
| 368 |
dex_clear (&self->channel); |
| 369 |
} |
| 370 |
|
| 371 |
g_clear_pointer (&self->writer, sysprof_capture_writer_unref); |
| 372 |
g_clear_pointer (&self->instruments, g_ptr_array_unref); |
| 373 |
g_clear_object (&self->spawnable); |
| 374 |
g_clear_object (&self->diagnostics); |
| 375 |
g_clear_object (&self->subprocess); |
| 376 |
dex_clear (&self->fiber); |
| 377 |
|
| 378 |
G_OBJECT_CLASS (sysprof_recording_parent_class)->finalize (object); |
| 379 |
} |
| 380 |
|
| 381 |
static void |
| 382 |
sysprof_recording_get_property (GObject *object, |
| 383 |
guint prop_id, |
| 384 |
GValue *value, |
| 385 |
GParamSpec *pspec) |
| 386 |
{ |
| 387 |
SysprofRecording *self = SYSPROF_RECORDING (object); |
| 388 |
|
| 389 |
switch (prop_id) |
| 390 |
{ |
| 391 |
case PROP_DURATION: |
| 392 |
g_value_set_int64 (value, sysprof_recording_get_duration (self)); |
| 393 |
break; |
| 394 |
|
| 395 |
case PROP_EVENT_COUNT: |
| 396 |
g_value_set_int64 (value, sysprof_recording_get_event_count (self)); |
| 397 |
break; |
| 398 |
|
| 399 |
default: |
| 400 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
static void |
| 405 |
sysprof_recording_class_init (SysprofRecordingClass *klass) |
| 406 |
{ |
| 407 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 408 |
|
| 409 |
object_class->finalize = sysprof_recording_finalize; |
| 410 |
object_class->get_property = sysprof_recording_get_property; |
| 411 |
|
| 412 |
properties [PROP_DURATION] = |
| 413 |
g_param_spec_int64 ("duration", NULL, NULL, |
| 414 |
0, G_MAXINT64, 0, |
| 415 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 416 |
|
| 417 |
properties [PROP_EVENT_COUNT] = |
| 418 |
g_param_spec_int64 ("event-count", NULL, NULL, |
| 419 |
0, G_MAXINT64, 0, |
| 420 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 421 |
|
| 422 |
g_object_class_install_properties (object_class, N_PROPS, properties); |
| 423 |
} |
| 424 |
|
| 425 |
static void |
| 426 |
sysprof_recording_init (SysprofRecording *self) |
| 427 |
{ |
| 428 |
self->channel = dex_channel_new (0); |
| 429 |
self->instruments = g_ptr_array_new_with_free_func (g_object_unref); |
| 430 |
self->diagnostics = g_list_store_new (SYSPROF_TYPE_DIAGNOSTIC); |
| 431 |
} |
| 432 |
|
| 433 |
SysprofRecording * |
| 434 |
_sysprof_recording_new (SysprofCaptureWriter *writer, |
| 435 |
SysprofSpawnable *spawnable, |
| 436 |
SysprofInstrument **instruments, |
| 437 |
guint n_instruments, |
| 438 |
gboolean use_sysprofd) |
| 439 |
{ |
| 440 |
SysprofRecording *self; |
| 441 |
|
| 442 |
g_return_val_if_fail (writer != NULL, NULL); |
| 443 |
|
| 444 |
self = g_object_new (SYSPROF_TYPE_RECORDING, NULL); |
| 445 |
self->writer = sysprof_capture_writer_ref (writer); |
| 446 |
self->use_sysprofd = !!use_sysprofd; |
| 447 |
|
| 448 |
g_set_object (&self->spawnable, spawnable); |
| 449 |
|
| 450 |
for (guint i = 0; i < n_instruments; i++) |
| 451 |
g_ptr_array_add (self->instruments, g_object_ref (instruments[i])); |
| 452 |
|
| 453 |
return self; |
| 454 |
} |
| 455 |
|
| 456 |
void |
| 457 |
_sysprof_recording_start (SysprofRecording *self) |
| 458 |
{ |
| 459 |
|
| 460 |
g_return_if_fail (SYSPROF_IS_RECORDING (self)); |
| 461 |
g_return_if_fail (self->fiber == NULL); |
| 462 |
|
| 463 |
self->fiber = dex_scheduler_spawn (NULL, 0, |
| 464 |
sysprof_recording_fiber, |
| 465 |
g_object_ref (self), |
| 466 |
g_object_unref); |
| 467 |
} |
| 468 |
|
| 469 |
void |
| 470 |
sysprof_recording_stop_async (SysprofRecording *self, |
| 471 |
GCancellable *cancellable, |
| 472 |
GAsyncReadyCallback callback, |
| 473 |
gpointer user_data) |
| 474 |
{ |
| 475 |
g_autoptr(DexAsyncResult) result = NULL; |
| 476 |
|
| 477 |
g_return_if_fail (SYSPROF_IS_RECORDING (self)); |
| 478 |
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable)); |
| 479 |
|
| 480 |
result = dex_async_result_new (self, cancellable, callback, user_data); |
| 481 |
dex_async_result_await (result, |
| 482 |
dex_channel_send (self->channel, |
| 483 |
dex_future_new_for_uint (SYSPROF_RECORDING_COMMAND_STOP))); |
| 484 |
} |
| 485 |
|
| 486 |
gboolean |
| 487 |
sysprof_recording_stop_finish (SysprofRecording *self, |
| 488 |
GAsyncResult *result, |
| 489 |
GError **error) |
| 490 |
{ |
| 491 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), FALSE); |
| 492 |
g_return_val_if_fail (DEX_IS_ASYNC_RESULT (result), FALSE); |
| 493 |
|
| 494 |
return dex_async_result_propagate_boolean (DEX_ASYNC_RESULT (result), error); |
| 495 |
} |
| 496 |
|
| 497 |
void |
| 498 |
sysprof_recording_wait_async (SysprofRecording *self, |
| 499 |
GCancellable *cancellable, |
| 500 |
GAsyncReadyCallback callback, |
| 501 |
gpointer user_data) |
| 502 |
{ |
| 503 |
g_autoptr(DexAsyncResult) result = NULL; |
| 504 |
|
| 505 |
g_return_if_fail (SYSPROF_IS_RECORDING (self)); |
| 506 |
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable)); |
| 507 |
|
| 508 |
result = dex_async_result_new (self, cancellable, callback, user_data); |
| 509 |
dex_async_result_await (result, dex_ref (self->fiber)); |
| 510 |
} |
| 511 |
|
| 512 |
gboolean |
| 513 |
sysprof_recording_wait_finish (SysprofRecording *self, |
| 514 |
GAsyncResult *result, |
| 515 |
GError **error) |
| 516 |
{ |
| 517 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), FALSE); |
| 518 |
g_return_val_if_fail (DEX_IS_ASYNC_RESULT (result), FALSE); |
| 519 |
|
| 520 |
return dex_async_result_propagate_boolean (DEX_ASYNC_RESULT (result), error); |
| 521 |
} |
| 522 |
|
| 523 |
SysprofSpawnable * |
| 524 |
_sysprof_recording_get_spawnable (SysprofRecording *self) |
| 525 |
{ |
| 526 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), NULL); |
| 527 |
|
| 528 |
return self->spawnable; |
| 529 |
} |
| 530 |
|
| 531 |
typedef struct _AddFile |
| 532 |
{ |
| 533 |
SysprofCaptureWriter *writer; |
| 534 |
char *path; |
| 535 |
guint compress : 1; |
| 536 |
guint use_sysprofd : 1; |
| 537 |
} AddFile; |
| 538 |
|
| 539 |
static void |
| 540 |
add_file_free (AddFile *add_file) |
| 541 |
{ |
| 542 |
g_clear_pointer (&add_file->writer, sysprof_capture_writer_unref); |
| 543 |
g_clear_pointer (&add_file->path, g_free); |
| 544 |
g_free (add_file); |
| 545 |
} |
| 546 |
|
| 547 |
static DexFuture * |
| 548 |
sysprof_recording_add_file_fiber (gpointer user_data) |
| 549 |
{ |
| 550 |
AddFile *add_file = user_data; |
| 551 |
g_autoptr(GInputStream) input = NULL; |
| 552 |
g_autoptr(GOutputStream) memory_stream = NULL; |
| 553 |
g_autoptr(GOutputStream) zlib_stream = NULL; |
| 554 |
g_autoptr(GError) error = NULL; |
| 555 |
g_autoptr(GBytes) bytes = NULL; |
| 556 |
g_autoptr(GFile) proc = NULL; |
| 557 |
g_autoptr(GFile) file = NULL; |
| 558 |
g_autofree char *dest_path = NULL; |
| 559 |
const guint8 *data = NULL; |
| 560 |
GOutputStream *output; |
| 561 |
gsize len; |
| 562 |
|
| 563 |
g_assert (add_file != NULL); |
| 564 |
g_assert (add_file->path != NULL); |
| 565 |
g_assert (add_file->writer != NULL); |
| 566 |
|
| 567 |
/* If we are compressing the file, store it as ".gz" in the capture |
| 568 |
* so that the reader knows to decompress it automatically. |
| 569 |
*/ |
| 570 |
dest_path = add_file->compress ? g_strdup_printf ("%s.gz", add_file->path) |
| 571 |
: g_strdup (add_file->path); |
| 572 |
|
| 573 |
file = g_file_new_for_path (add_file->path); |
| 574 |
proc = g_file_new_for_path ("/proc"); |
| 575 |
|
| 576 |
/* If the file has a prefix of `/proc/` then we need to request the |
| 577 |
* file from sysprofd as our user is not guaranteed to be able to |
| 578 |
* read the file. Even if we can open the file, we may get data that |
| 579 |
* has been redacted (such as /proc/kallsyms). |
| 580 |
* |
| 581 |
* With `/proc/kallsyms` it's even worse in that if we get an FD back |
| 582 |
* from sysprofd and read it from our process, it *too* will be redacted. |
| 583 |
* That leaves us with the only option of letting sysprofd read the file |
| 584 |
* and transfer the contents to our process over D-Bus. |
| 585 |
* |
| 586 |
* We use g_file_has_prefix() for this as it will canonicalize the paths |
| 587 |
* of #GFile rather than us having to be careful here. |
| 588 |
*/ |
| 589 |
if (g_file_has_prefix (file, proc)) |
| 590 |
{ |
| 591 |
g_autoptr(GDBusConnection) connection = NULL; |
| 592 |
g_autoptr(GBytes) input_bytes = NULL; |
| 593 |
|
| 594 |
if (add_file->use_sysprofd) |
| 595 |
{ |
| 596 |
/* Only use D-Bus if sysprofd use is requested */ |
| 597 |
if (!(connection = dex_await_object (dex_bus_get (G_BUS_TYPE_SYSTEM), &error))) |
| 598 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 599 |
} |
| 600 |
|
| 601 |
if (!(input_bytes = dex_await_boxed (sysprof_get_proc_file_bytes (connection, g_file_get_path (file)), &error))) |
| 602 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 603 |
|
| 604 |
input = g_memory_input_stream_new_from_bytes (input_bytes); |
| 605 |
} |
| 606 |
else |
| 607 |
{ |
| 608 |
if (!(input = dex_await_object (dex_file_read (file, 0), &error))) |
| 609 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 610 |
} |
| 611 |
|
| 612 |
g_assert (input != NULL); |
| 613 |
g_assert (G_IS_INPUT_STREAM (input)); |
| 614 |
|
| 615 |
output = memory_stream = g_memory_output_stream_new_resizable (); |
| 616 |
|
| 617 |
if (add_file->compress) |
| 618 |
{ |
| 619 |
g_autoptr(GZlibCompressor) compressor = g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 6); |
| 620 |
output = zlib_stream = g_converter_output_stream_new (memory_stream, G_CONVERTER (compressor)); |
| 621 |
} |
| 622 |
|
| 623 |
g_assert (output != NULL); |
| 624 |
g_assert (G_IS_OUTPUT_STREAM (output)); |
| 625 |
g_assert (G_IS_MEMORY_OUTPUT_STREAM (output) || G_IS_CONVERTER_OUTPUT_STREAM (output)); |
| 626 |
g_assert (G_IS_MEMORY_OUTPUT_STREAM (memory_stream)); |
| 627 |
g_assert (!zlib_stream || G_IS_CONVERTER_OUTPUT_STREAM (zlib_stream)); |
| 628 |
|
| 629 |
if (!dex_await (dex_output_stream_splice (output, |
| 630 |
input, |
| 631 |
(G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | |
| 632 |
G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET), |
| 633 |
0), |
| 634 |
&error)) |
| 635 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 636 |
|
| 637 |
bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (memory_stream)); |
| 638 |
data = g_bytes_get_data (bytes, &len); |
| 639 |
|
| 640 |
while (len > 0) |
| 641 |
{ |
| 642 |
gsize to_write = MIN (len, ((4096*8)-sizeof (SysprofCaptureFileChunk))); |
| 643 |
|
| 644 |
if (!sysprof_capture_writer_add_file (add_file->writer, |
| 645 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 646 |
-1, |
| 647 |
-1, |
| 648 |
dest_path, |
| 649 |
to_write == len, |
| 650 |
data, |
| 651 |
to_write)) |
| 652 |
break; |
| 653 |
|
| 654 |
len -= to_write; |
| 655 |
data += to_write; |
| 656 |
} |
| 657 |
|
| 658 |
return dex_future_new_for_boolean (TRUE); |
| 659 |
} |
| 660 |
|
| 661 |
DexFuture * |
| 662 |
_sysprof_recording_add_file (SysprofRecording *self, |
| 663 |
const char *path, |
| 664 |
gboolean compress) |
| 665 |
{ |
| 666 |
AddFile *add_file; |
| 667 |
|
| 668 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), NULL); |
| 669 |
g_return_val_if_fail (path != NULL, NULL); |
| 670 |
|
| 671 |
add_file = g_new0 (AddFile, 1); |
| 672 |
add_file->writer = sysprof_capture_writer_ref (self->writer); |
| 673 |
add_file->path = g_strdup (path); |
| 674 |
add_file->compress = !!compress; |
| 675 |
add_file->use_sysprofd = self->use_sysprofd; |
| 676 |
|
| 677 |
return dex_scheduler_spawn (NULL, 0, |
| 678 |
sysprof_recording_add_file_fiber, |
| 679 |
g_steal_pointer (&add_file), |
| 680 |
(GDestroyNotify)add_file_free); |
| 681 |
} |
| 682 |
|
| 683 |
static char * |
| 684 |
do_compress (const char *data, |
| 685 |
gsize length, |
| 686 |
gsize *out_length) |
| 687 |
{ |
| 688 |
g_autoptr(GZlibCompressor) compressor = g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 6); |
| 689 |
g_autofree char *compressed = g_malloc (length); |
| 690 |
GConverterResult res; |
| 691 |
gsize n_read; |
| 692 |
gsize n_written; |
| 693 |
|
| 694 |
*out_length = 0; |
| 695 |
|
| 696 |
res = g_converter_convert (G_CONVERTER (compressor), data, length, compressed, length, |
| 697 |
G_CONVERTER_INPUT_AT_END | G_CONVERTER_FLUSH, |
| 698 |
&n_read, &n_written, NULL); |
| 699 |
|
| 700 |
if (res == G_CONVERTER_FINISHED) |
| 701 |
{ |
| 702 |
*out_length = n_written; |
| 703 |
return g_steal_pointer (&compressed); |
| 704 |
} |
| 705 |
|
| 706 |
return NULL; |
| 707 |
} |
| 708 |
|
| 709 |
void |
| 710 |
_sysprof_recording_add_file_data (SysprofRecording *self, |
| 711 |
const char *path, |
| 712 |
const char *contents, |
| 713 |
gssize length, |
| 714 |
gboolean compress) |
| 715 |
{ |
| 716 |
g_autofree char *compress_filename = NULL; |
| 717 |
g_autofree char *compress_bytes = NULL; |
| 718 |
gsize compress_len = 0; |
| 719 |
|
| 720 |
g_return_if_fail (SYSPROF_IS_RECORDING (self)); |
| 721 |
g_return_if_fail (path != NULL); |
| 722 |
g_return_if_fail (contents != NULL); |
| 723 |
|
| 724 |
if (length < 0) |
| 725 |
length = strlen (contents); |
| 726 |
|
| 727 |
if (length == 0) |
| 728 |
compress = FALSE; |
| 729 |
|
| 730 |
if (compress) |
| 731 |
{ |
| 732 |
compress_bytes = do_compress (contents, length, &compress_len); |
| 733 |
|
| 734 |
if (compress_bytes) |
| 735 |
{ |
| 736 |
compress_filename = g_strdup_printf ("%s.gz", path); |
| 737 |
path = compress_filename; |
| 738 |
contents = compress_bytes; |
| 739 |
length = compress_len; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
while (length > 0) |
| 744 |
{ |
| 745 |
gsize to_write = MIN (length, ((4096*8)-sizeof (SysprofCaptureFileChunk))); |
| 746 |
|
| 747 |
if (!sysprof_capture_writer_add_file (self->writer, |
| 748 |
SYSPROF_CAPTURE_CURRENT_TIME, |
| 749 |
-1, |
| 750 |
-1, |
| 751 |
path, |
| 752 |
to_write == length, |
| 753 |
(const guint8 *)contents, |
| 754 |
to_write)) |
| 755 |
break; |
| 756 |
|
| 757 |
length -= to_write; |
| 758 |
contents += to_write; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
G_GNUC_PRINTF(3,0) |
| 763 |
static void |
| 764 |
_sysprof_recording_message_internal (SysprofRecording *self, |
| 765 |
const char *domain, |
| 766 |
const char *format, |
| 767 |
va_list *args, |
| 768 |
gboolean fatal) |
| 769 |
{ |
| 770 |
g_autoptr(SysprofDiagnostic) diagnostic = NULL; |
| 771 |
|
| 772 |
g_assert (SYSPROF_IS_RECORDING (self)); |
| 773 |
g_assert (domain != NULL); |
| 774 |
g_assert (format != NULL); |
| 775 |
g_assert (args != NULL); |
| 776 |
|
| 777 |
diagnostic = _sysprof_diagnostic_new (g_strdup (domain), |
| 778 |
g_strdup_vprintf (format, *args), |
| 779 |
fatal); |
| 780 |
|
| 781 |
g_list_store_append (self->diagnostics, diagnostic); |
| 782 |
|
| 783 |
if (fatal) |
| 784 |
sysprof_recording_stop_async (self, NULL, NULL, NULL); |
| 785 |
} |
| 786 |
|
| 787 |
void |
| 788 |
_sysprof_recording_diagnostic (SysprofRecording *self, |
| 789 |
const char *domain, |
| 790 |
const char *format, |
| 791 |
...) |
| 792 |
{ |
| 793 |
va_list args; |
| 794 |
|
| 795 |
va_start (args, format); |
| 796 |
_sysprof_recording_message_internal (self, domain, format, &args, FALSE); |
| 797 |
va_end (args); |
| 798 |
} |
| 799 |
|
| 800 |
void |
| 801 |
_sysprof_recording_error (SysprofRecording *self, |
| 802 |
const char *domain, |
| 803 |
const char *format, |
| 804 |
...) |
| 805 |
{ |
| 806 |
va_list args; |
| 807 |
|
| 808 |
va_start (args, format); |
| 809 |
_sysprof_recording_message_internal (self, domain, format, &args, TRUE); |
| 810 |
va_end (args); |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* sysprof_recording_list_diagnostics: |
| 815 |
* @self: a #SysprofRecording |
| 816 |
* |
| 817 |
* Gets the diagnostics for the recording which may be updated as |
| 818 |
* instruments discover issues with the recording or configuration. |
| 819 |
* |
| 820 |
* Returns: (transfer full): a #GListModel of #SysprofDiagnostic |
| 821 |
*/ |
| 822 |
GListModel * |
| 823 |
sysprof_recording_list_diagnostics (SysprofRecording *self) |
| 824 |
{ |
| 825 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), NULL); |
| 826 |
|
| 827 |
return g_object_ref (G_LIST_MODEL (self->diagnostics)); |
| 828 |
} |
| 829 |
|
| 830 |
/** |
| 831 |
* sysprof_recording_get_duration: |
| 832 |
* @self: a #SysprofRecording |
| 833 |
* |
| 834 |
* Gets the recording duration in microseconds, which is the same |
| 835 |
* precision used by g_get_monotonic_time(). Use %G_USEC_PER_SEC to |
| 836 |
* get the time in seconds. |
| 837 |
* |
| 838 |
* Returns: the duration of the recording, or 0 |
| 839 |
*/ |
| 840 |
gint64 |
| 841 |
sysprof_recording_get_duration (SysprofRecording *self) |
| 842 |
{ |
| 843 |
gint64 start_time; |
| 844 |
gint64 end_time; |
| 845 |
|
| 846 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), 0); |
| 847 |
|
| 848 |
if (!(start_time = self->start_time)) |
| 849 |
return 0; |
| 850 |
|
| 851 |
if (!(end_time = self->end_time)) |
| 852 |
end_time = g_get_monotonic_time (); |
| 853 |
|
| 854 |
return end_time - start_time; |
| 855 |
} |
| 856 |
|
| 857 |
gint64 |
| 858 |
sysprof_recording_get_event_count (SysprofRecording *self) |
| 859 |
{ |
| 860 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), 0); |
| 861 |
|
| 862 |
return self->stat.frame_count[SYSPROF_CAPTURE_FRAME_SAMPLE] |
| 863 |
+ self->stat.frame_count[SYSPROF_CAPTURE_FRAME_ALLOCATION] |
| 864 |
+ self->stat.frame_count[SYSPROF_CAPTURE_FRAME_FORK] |
| 865 |
+ self->stat.frame_count[SYSPROF_CAPTURE_FRAME_EXIT] |
| 866 |
+ self->stat.frame_count[SYSPROF_CAPTURE_FRAME_CTRSET] |
| 867 |
+ self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MARK] |
| 868 |
+ self->stat.frame_count[SYSPROF_CAPTURE_FRAME_LOG]; |
| 869 |
} |
| 870 |
|
| 871 |
/** |
| 872 |
* sysprof_recording_create_reader: |
| 873 |
* @self: a #SysprofRecording |
| 874 |
* |
| 875 |
* Creates a new reader for the recording's writer. |
| 876 |
* |
| 877 |
* Returns: (transfer full): a #SysprofCaptureReader |
| 878 |
*/ |
| 879 |
SysprofCaptureReader * |
| 880 |
sysprof_recording_create_reader (SysprofRecording *self) |
| 881 |
{ |
| 882 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), NULL); |
| 883 |
|
| 884 |
return sysprof_capture_writer_create_reader (self->writer); |
| 885 |
} |
| 886 |
|
| 887 |
/** |
| 888 |
* sysprof_recording_dup_fd: |
| 889 |
* @self: a #SysprofRecording |
| 890 |
* |
| 891 |
* Duplicates the FD that is being used for writing the recording. This can |
| 892 |
* be useful if you want to open the recording using a |
| 893 |
* #SysprofDocumentLoader. |
| 894 |
* |
| 895 |
* Returns: a FD that the caller should `close()` when no longer in use. |
| 896 |
*/ |
| 897 |
int |
| 898 |
sysprof_recording_dup_fd (SysprofRecording *self) |
| 899 |
{ |
| 900 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), -1); |
| 901 |
|
| 902 |
return _sysprof_capture_writer_dup_fd (self->writer); |
| 903 |
} |
| 904 |
|
| 905 |
/** |
| 906 |
* sysprof_recording_get_subprocess: |
| 907 |
* @self: a #SysprofRecording |
| 908 |
* |
| 909 |
* Gets the #GSubprocess if one was spawned. |
| 910 |
* |
| 911 |
* Returns: (transfer none) (nullable): a #GSubprocess or %NULL |
| 912 |
*/ |
| 913 |
GSubprocess * |
| 914 |
sysprof_recording_get_subprocess (SysprofRecording *self) |
| 915 |
{ |
| 916 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (self), NULL); |
| 917 |
|
| 918 |
return self->subprocess; |
| 919 |
} |
| 920 |
|
| 921 |
void |
| 922 |
_sysprof_recording_follow_process (SysprofRecording *self, |
| 923 |
int pid, |
| 924 |
const char *comm) |
| 925 |
{ |
| 926 |
g_return_if_fail (SYSPROF_IS_RECORDING (self)); |
| 927 |
g_return_if_fail (pid > 0); |
| 928 |
|
| 929 |
dex_future_disown (_sysprof_instruments_process_started (self->instruments, self, pid, comm)); |
| 930 |
} |