| 1 |
/* sysprof-user-sampler.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 <fcntl.h> |
| 24 |
|
| 25 |
#include <sys/ioctl.h> |
| 26 |
#include <sys/eventfd.h> |
| 27 |
|
| 28 |
#ifdef HAVE_PERF_REGS_H |
| 29 |
# include <asm/perf_regs.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <glib/gstdio.h> |
| 33 |
|
| 34 |
#include "sysprof-instrument-private.h" |
| 35 |
#include "sysprof-perf-event-stream-private.h" |
| 36 |
#include "sysprof-recording-private.h" |
| 37 |
#include "sysprof-user-sampler.h" |
| 38 |
#include "sysprof-muxer-source.h" |
| 39 |
#include "sysprof-fd-private.h" |
| 40 |
|
| 41 |
#include "ipc-unwinder.h" |
| 42 |
|
| 43 |
/* The following was provided to Sysprof by Serhei Makarov as part |
| 44 |
* of the eu-stacktrace prototype work. |
| 45 |
*/ |
| 46 |
#ifdef _ASM_X86_PERF_REGS_H |
| 47 |
/* #define SYSPROF_ARCH_PREFERRED_REGS PERF_REG_EXTENDED_MASK -- error on x86_64 due to including segment regs*/ |
| 48 |
#define REG(R) (1ULL << PERF_REG_X86_ ## R) |
| 49 |
#define DWARF_NEEDED_REGS (/* no FLAGS */ REG(IP) | REG(SP) | REG(AX) | REG(CX) | REG(DX) | REG(BX) | REG(SI) | REG(DI) | REG(SP) | REG(BP) | /* no segment regs */ REG(R8) | REG(R9) | REG(R10) | REG(R11) | REG(R12) | REG(R13) | REG(R14) | REG(R15)) |
| 50 |
/* XXX register ordering is defined in linux arch/x86/include/uapi/asm/perf_regs.h; |
| 51 |
see code in tools/perf/util/intel-pt.c intel_pt_add_gp_regs() |
| 52 |
and note how registers are added in the same order as the perf_regs.h enum */ |
| 53 |
#define SYSPROF_ARCH_PREFERRED_REGS DWARF_NEEDED_REGS |
| 54 |
/* TODO: add other architectures, imitating the linux tools/perf tree */ |
| 55 |
#else |
| 56 |
# ifdef PERF_REG_EXTENDED_MASK |
| 57 |
# define SYSPROF_ARCH_PREFERRED_REGS PERF_REG_EXTENDED_MASK |
| 58 |
# else |
| 59 |
# define SYSPROF_ARCH_PREFERRED_REGS 0 |
| 60 |
# endif |
| 61 |
#endif /* _ASM_{arch}_PERF_REGS_H */ |
| 62 |
|
| 63 |
#define N_WAKEUP_EVENTS 149 |
| 64 |
|
| 65 |
struct _SysprofUserSampler |
| 66 |
{ |
| 67 |
SysprofInstrument parent_instance; |
| 68 |
GArray *perf_fds; |
| 69 |
int capture_fd_read; |
| 70 |
int capture_fd_write; |
| 71 |
int event_fd; |
| 72 |
guint stack_size; |
| 73 |
}; |
| 74 |
|
| 75 |
struct _SysprofUserSamplerClass |
| 76 |
{ |
| 77 |
SysprofInstrumentClass parent_class; |
| 78 |
}; |
| 79 |
|
| 80 |
G_DEFINE_FINAL_TYPE (SysprofUserSampler, sysprof_user_sampler, SYSPROF_TYPE_INSTRUMENT) |
| 81 |
|
| 82 |
static void |
| 83 |
close_fd (gpointer data) |
| 84 |
{ |
| 85 |
int *fdp = data; |
| 86 |
|
| 87 |
if (*fdp != -1) |
| 88 |
{ |
| 89 |
close (*fdp); |
| 90 |
*fdp = -1; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
promise_resolve_fd (DexPromise *promise, |
| 96 |
int fd) |
| 97 |
{ |
| 98 |
GValue gvalue = {SYSPROF_TYPE_FD, {{.v_pointer = &fd}, {.v_int = 0}}}; |
| 99 |
dex_promise_resolve (promise, &gvalue); |
| 100 |
} |
| 101 |
|
| 102 |
static int |
| 103 |
await_fd (DexFuture *future, |
| 104 |
GError **error) |
| 105 |
{ |
| 106 |
SysprofFD *fd = dex_await_boxed (future, error); |
| 107 |
int ret = -1; |
| 108 |
|
| 109 |
if (fd != NULL) |
| 110 |
{ |
| 111 |
ret = sysprof_fd_steal (fd); |
| 112 |
sysprof_fd_free (fd); |
| 113 |
} |
| 114 |
|
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
static void |
| 119 |
sysprof_user_sampler_ioctl (SysprofUserSampler *self, |
| 120 |
gboolean enable) |
| 121 |
{ |
| 122 |
for (guint i = 0; i < self->perf_fds->len; i++) |
| 123 |
{ |
| 124 |
int perf_fd = g_array_index (self->perf_fds, int, i); |
| 125 |
|
| 126 |
if (0 != ioctl (perf_fd, enable ? PERF_EVENT_IOC_ENABLE : PERF_EVENT_IOC_DISABLE)) |
| 127 |
{ |
| 128 |
int errsv = errno; |
| 129 |
g_warning ("Failed to toggle perf_fd: %s", g_strerror (errsv)); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
static char ** |
| 135 |
sysprof_user_sampler_list_required_policy (SysprofInstrument *instrument) |
| 136 |
{ |
| 137 |
static const char *policy[] = {"org.gnome.sysprof3.profile", NULL}; |
| 138 |
|
| 139 |
return g_strdupv ((char **)policy); |
| 140 |
} |
| 141 |
|
| 142 |
typedef struct _Prepare |
| 143 |
{ |
| 144 |
SysprofRecording *recording; |
| 145 |
SysprofUserSampler *sampler; |
| 146 |
guint stack_size; |
| 147 |
} Prepare; |
| 148 |
|
| 149 |
static void |
| 150 |
prepare_free (Prepare *prepare) |
| 151 |
{ |
| 152 |
g_clear_object (&prepare->recording); |
| 153 |
g_clear_object (&prepare->sampler); |
| 154 |
g_free (prepare); |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
_perf_event_open_cb (GObject *object, |
| 159 |
GAsyncResult *result, |
| 160 |
gpointer user_data) |
| 161 |
{ |
| 162 |
GDBusConnection *connection = (GDBusConnection *)object; |
| 163 |
g_autoptr(DexPromise) promise = user_data; |
| 164 |
g_autoptr(GUnixFDList) fd_list = NULL; |
| 165 |
g_autoptr(GVariant) ret = NULL; |
| 166 |
g_autoptr(GError) error = NULL; |
| 167 |
|
| 168 |
g_assert (G_IS_DBUS_CONNECTION (connection)); |
| 169 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 170 |
g_assert (DEX_IS_PROMISE (promise)); |
| 171 |
|
| 172 |
if ((ret = g_dbus_connection_call_with_unix_fd_list_finish (connection, &fd_list, result, &error))) |
| 173 |
{ |
| 174 |
g_autofd int fd = -1; |
| 175 |
int handle; |
| 176 |
|
| 177 |
g_variant_get (ret, "(h)", &handle); |
| 178 |
|
| 179 |
if (-1 == (fd = g_unix_fd_list_get (fd_list, handle, &error))) |
| 180 |
goto failure; |
| 181 |
|
| 182 |
promise_resolve_fd (promise, g_steal_fd (&fd)); |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
failure: |
| 187 |
dex_promise_reject (promise, g_steal_pointer (&error)); |
| 188 |
} |
| 189 |
|
| 190 |
static int |
| 191 |
_perf_event_open (GDBusConnection *connection, |
| 192 |
int cpu, |
| 193 |
guint stack_size, |
| 194 |
GError **error) |
| 195 |
{ |
| 196 |
g_autoptr(DexPromise) promise = NULL; |
| 197 |
g_autoptr(GVariant) options = NULL; |
| 198 |
g_autofd int perf_fd = -1; |
| 199 |
struct perf_event_attr attr = {0}; |
| 200 |
gboolean with_mmap2 = TRUE; |
| 201 |
gboolean use_software = FALSE; |
| 202 |
|
| 203 |
g_assert (G_IS_DBUS_CONNECTION (connection)); |
| 204 |
|
| 205 |
try_again: |
| 206 |
attr.sample_type = PERF_SAMPLE_IP |
| 207 |
| PERF_SAMPLE_TID |
| 208 |
| PERF_SAMPLE_IDENTIFIER |
| 209 |
| PERF_SAMPLE_CALLCHAIN |
| 210 |
| PERF_SAMPLE_STACK_USER |
| 211 |
| PERF_SAMPLE_REGS_USER |
| 212 |
| PERF_SAMPLE_TIME; |
| 213 |
attr.wakeup_events = N_WAKEUP_EVENTS; |
| 214 |
attr.disabled = TRUE; |
| 215 |
attr.mmap = TRUE; |
| 216 |
attr.mmap2 = with_mmap2; |
| 217 |
attr.comm = 1; |
| 218 |
attr.task = 1; |
| 219 |
attr.exclude_idle = 1; |
| 220 |
attr.sample_id_all = 1; |
| 221 |
|
| 222 |
#ifdef HAVE_PERF_CLOCKID |
| 223 |
attr.clockid = sysprof_clock; |
| 224 |
attr.use_clockid = 1; |
| 225 |
#endif |
| 226 |
|
| 227 |
attr.sample_stack_user = stack_size; |
| 228 |
attr.sample_regs_user = SYSPROF_ARCH_PREFERRED_REGS; |
| 229 |
|
| 230 |
attr.size = sizeof attr; |
| 231 |
|
| 232 |
if (use_software) |
| 233 |
{ |
| 234 |
attr.type = PERF_TYPE_SOFTWARE; |
| 235 |
attr.config = PERF_COUNT_SW_CPU_CLOCK; |
| 236 |
attr.sample_period = 1000000; |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
attr.type = PERF_TYPE_HARDWARE; |
| 241 |
attr.config = PERF_COUNT_HW_CPU_CYCLES; |
| 242 |
attr.sample_period = 1200000; |
| 243 |
} |
| 244 |
|
| 245 |
options = _sysprof_perf_event_attr_to_variant (&attr); |
| 246 |
promise = dex_promise_new (); |
| 247 |
|
| 248 |
g_dbus_connection_call_with_unix_fd_list (connection, |
| 249 |
"org.gnome.Sysprof3", |
| 250 |
"/org/gnome/Sysprof3", |
| 251 |
"org.gnome.Sysprof3.Service", |
| 252 |
"PerfEventOpen", |
| 253 |
g_variant_new ("(@a{sv}iiht)", |
| 254 |
options, |
| 255 |
-1, |
| 256 |
cpu, |
| 257 |
-1, |
| 258 |
0), |
| 259 |
G_VARIANT_TYPE ("(h)"), |
| 260 |
G_DBUS_CALL_FLAGS_NONE, |
| 261 |
G_MAXUINT, |
| 262 |
NULL, |
| 263 |
NULL, |
| 264 |
_perf_event_open_cb, |
| 265 |
dex_ref (promise)); |
| 266 |
|
| 267 |
if (-1 == (perf_fd = await_fd (dex_ref (promise), error))) |
| 268 |
{ |
| 269 |
g_clear_pointer (&options, g_variant_unref); |
| 270 |
|
| 271 |
if (with_mmap2) |
| 272 |
{ |
| 273 |
with_mmap2 = FALSE; |
| 274 |
goto try_again; |
| 275 |
} |
| 276 |
|
| 277 |
if (use_software == FALSE) |
| 278 |
{ |
| 279 |
with_mmap2 = TRUE; |
| 280 |
use_software = TRUE; |
| 281 |
goto try_again; |
| 282 |
} |
| 283 |
|
| 284 |
return -1; |
| 285 |
} |
| 286 |
|
| 287 |
return g_steal_fd (&perf_fd); |
| 288 |
} |
| 289 |
|
| 290 |
static void |
| 291 |
call_unwind_cb (GObject *object, |
| 292 |
GAsyncResult *result, |
| 293 |
gpointer user_data) |
| 294 |
{ |
| 295 |
g_autoptr(DexPromise) promise = user_data; |
| 296 |
g_autoptr(GUnixFDList) out_fd_list = NULL; |
| 297 |
GError *error = NULL; |
| 298 |
|
| 299 |
g_assert (IPC_IS_UNWINDER (object)); |
| 300 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 301 |
g_assert (DEX_IS_PROMISE (promise)); |
| 302 |
|
| 303 |
if (ipc_unwinder_call_unwind_finish (IPC_UNWINDER (object), &out_fd_list, result, &error)) |
| 304 |
dex_promise_resolve_boolean (promise, TRUE); |
| 305 |
else |
| 306 |
dex_promise_reject (promise, error); |
| 307 |
} |
| 308 |
|
| 309 |
static void |
| 310 |
create_unwinder_cb (GObject *object, |
| 311 |
GAsyncResult *result, |
| 312 |
gpointer user_data) |
| 313 |
{ |
| 314 |
g_autoptr(DexPromise) promise = user_data; |
| 315 |
IpcUnwinder *unwinder; |
| 316 |
GError *error = NULL; |
| 317 |
|
| 318 |
if ((unwinder = ipc_unwinder_proxy_new_finish (result, &error))) |
| 319 |
dex_promise_resolve_object (promise, unwinder); |
| 320 |
else |
| 321 |
dex_promise_reject (promise, error); |
| 322 |
} |
| 323 |
|
| 324 |
static IpcUnwinder * |
| 325 |
create_unwinder (GDBusConnection *connection, |
| 326 |
GError **error) |
| 327 |
{ |
| 328 |
g_autoptr(DexPromise) promise = dex_promise_new (); |
| 329 |
ipc_unwinder_proxy_new (connection, G_DBUS_PROXY_FLAGS_NONE, |
| 330 |
"org.gnome.Sysprof3", |
| 331 |
"/org/gnome/Sysprof3/Unwinder", |
| 332 |
NULL, |
| 333 |
create_unwinder_cb, |
| 334 |
dex_ref (promise)); |
| 335 |
return dex_await_object (dex_ref (promise), error); |
| 336 |
} |
| 337 |
|
| 338 |
static DexFuture * |
| 339 |
sysprof_user_sampler_prepare_fiber (gpointer user_data) |
| 340 |
{ |
| 341 |
Prepare *prepare = user_data; |
| 342 |
g_autoptr(GDBusConnection) connection = NULL; |
| 343 |
g_autoptr(GUnixFDList) fd_list = NULL; |
| 344 |
g_autoptr(GError) error = NULL; |
| 345 |
GVariantBuilder builder; |
| 346 |
gboolean all_failed = TRUE; |
| 347 |
guint n_cpu; |
| 348 |
|
| 349 |
g_assert (prepare != NULL); |
| 350 |
g_assert (SYSPROF_IS_RECORDING (prepare->recording)); |
| 351 |
g_assert (SYSPROF_IS_USER_SAMPLER (prepare->sampler)); |
| 352 |
|
| 353 |
if (!(connection = dex_await_object (dex_bus_get (G_BUS_TYPE_SYSTEM), &error))) |
| 354 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 355 |
|
| 356 |
if (!dex_await (_sysprof_recording_add_file (prepare->recording, |
| 357 |
"/proc/kallsyms", |
| 358 |
TRUE), |
| 359 |
&error)) |
| 360 |
{ |
| 361 |
_sysprof_recording_diagnostic (prepare->recording, |
| 362 |
"Sampler", |
| 363 |
"Failed to record copy of “kallsyms” to capture: %s", |
| 364 |
error->message); |
| 365 |
g_clear_error (&error); |
| 366 |
} |
| 367 |
|
| 368 |
n_cpu = g_get_num_processors (); |
| 369 |
fd_list = g_unix_fd_list_new (); |
| 370 |
|
| 371 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(hi)")); |
| 372 |
|
| 373 |
for (guint i = 0; i < n_cpu; i++) |
| 374 |
{ |
| 375 |
g_autofd int fd = _perf_event_open (connection, i, prepare->stack_size, &error); |
| 376 |
|
| 377 |
if (fd == -1) |
| 378 |
{ |
| 379 |
_sysprof_recording_diagnostic (prepare->recording, |
| 380 |
"Sampler", |
| 381 |
"Failed to load Perf event stream for CPU %d with stack size %u: %s", |
| 382 |
i, prepare->stack_size, error->message); |
| 383 |
g_clear_error (&error); |
| 384 |
} |
| 385 |
else |
| 386 |
{ |
| 387 |
int handle = g_unix_fd_list_append (fd_list, fd, &error); |
| 388 |
|
| 389 |
if (handle == -1) |
| 390 |
{ |
| 391 |
_sysprof_recording_diagnostic (prepare->recording, |
| 392 |
"Sampler", |
| 393 |
"Out of FDs to add to FDList: %s", |
| 394 |
error->message); |
| 395 |
g_clear_error (&error); |
| 396 |
} |
| 397 |
else |
| 398 |
{ |
| 399 |
g_array_append_val (prepare->sampler->perf_fds, fd); |
| 400 |
fd = -1; |
| 401 |
|
| 402 |
g_variant_builder_add (&builder, "(hi)", handle, i); |
| 403 |
|
| 404 |
all_failed = FALSE; |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
if (!all_failed) |
| 410 |
{ |
| 411 |
g_autoptr(IpcUnwinder) unwinder = create_unwinder (connection, &error); |
| 412 |
|
| 413 |
if (unwinder == NULL) |
| 414 |
{ |
| 415 |
_sysprof_recording_diagnostic (prepare->recording, |
| 416 |
"Sampler", |
| 417 |
"Failed to locate unwinder service: %s", |
| 418 |
error->message); |
| 419 |
g_clear_error (&error); |
| 420 |
} |
| 421 |
else |
| 422 |
{ |
| 423 |
g_autoptr(DexPromise) promise = dex_promise_new (); |
| 424 |
int event_fd_handle = g_unix_fd_list_append (fd_list, prepare->sampler->event_fd, NULL); |
| 425 |
int capture_fd_handle = g_unix_fd_list_append (fd_list, prepare->sampler->capture_fd_write, NULL); |
| 426 |
|
| 427 |
ipc_unwinder_call_unwind (unwinder, |
| 428 |
prepare->stack_size, |
| 429 |
g_variant_builder_end (&builder), |
| 430 |
g_variant_new_handle (event_fd_handle), |
| 431 |
g_variant_new_handle (capture_fd_handle), |
| 432 |
fd_list, |
| 433 |
NULL, |
| 434 |
call_unwind_cb, |
| 435 |
dex_ref (promise)); |
| 436 |
|
| 437 |
if (!dex_await (dex_ref (promise), &error)) |
| 438 |
{ |
| 439 |
_sysprof_recording_diagnostic (prepare->recording, |
| 440 |
"Sampler", |
| 441 |
"Failed to setup thread unwinder: %s", |
| 442 |
error->message); |
| 443 |
g_clear_error (&error); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
g_variant_builder_clear (&builder); |
| 449 |
|
| 450 |
return dex_future_new_for_boolean (TRUE); |
| 451 |
} |
| 452 |
|
| 453 |
static DexFuture * |
| 454 |
sysprof_user_sampler_prepare (SysprofInstrument *instrument, |
| 455 |
SysprofRecording *recording) |
| 456 |
{ |
| 457 |
SysprofUserSampler *self = (SysprofUserSampler *)instrument; |
| 458 |
Prepare *prepare; |
| 459 |
|
| 460 |
g_assert (SYSPROF_IS_INSTRUMENT (instrument)); |
| 461 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 462 |
|
| 463 |
prepare = g_new0 (Prepare, 1); |
| 464 |
prepare->recording = g_object_ref (recording); |
| 465 |
prepare->sampler = g_object_ref (self); |
| 466 |
prepare->stack_size = self->stack_size; |
| 467 |
|
| 468 |
return dex_scheduler_spawn (NULL, 0, |
| 469 |
sysprof_user_sampler_prepare_fiber, |
| 470 |
prepare, |
| 471 |
(GDestroyNotify)prepare_free); |
| 472 |
} |
| 473 |
|
| 474 |
typedef struct _Record |
| 475 |
{ |
| 476 |
SysprofRecording *recording; |
| 477 |
SysprofUserSampler *sampler; |
| 478 |
DexFuture *cancellable; |
| 479 |
} Record; |
| 480 |
|
| 481 |
static void |
| 482 |
record_free (Record *record) |
| 483 |
{ |
| 484 |
g_clear_object (&record->recording); |
| 485 |
g_clear_object (&record->sampler); |
| 486 |
dex_clear (&record->cancellable); |
| 487 |
g_free (record); |
| 488 |
} |
| 489 |
|
| 490 |
static DexFuture * |
| 491 |
sysprof_user_sampler_record_fiber (gpointer user_data) |
| 492 |
{ |
| 493 |
SysprofCaptureWriter *writer; |
| 494 |
Record *record = user_data; |
| 495 |
g_autoptr(GError) error = NULL; |
| 496 |
g_autoptr(GSource) muxer_source = NULL; |
| 497 |
guint64 exiting = 1234; |
| 498 |
|
| 499 |
g_assert (record != NULL); |
| 500 |
g_assert (SYSPROF_IS_USER_SAMPLER (record->sampler)); |
| 501 |
g_assert (SYSPROF_IS_RECORDING (record->recording)); |
| 502 |
g_assert (DEX_IS_FUTURE (record->cancellable)); |
| 503 |
|
| 504 |
writer = _sysprof_recording_writer (record->recording); |
| 505 |
|
| 506 |
if (record->sampler->capture_fd_read != -1) |
| 507 |
{ |
| 508 |
sysprof_user_sampler_ioctl (record->sampler, TRUE); |
| 509 |
|
| 510 |
g_debug ("Staring muxer for capture_fd %d", record->sampler->capture_fd_read); |
| 511 |
muxer_source = sysprof_muxer_source_new (g_steal_fd (&record->sampler->capture_fd_read), writer); |
| 512 |
g_source_set_static_name (muxer_source, "[stack-muxer]"); |
| 513 |
g_source_attach (muxer_source, NULL); |
| 514 |
|
| 515 |
if (!dex_await (dex_ref (record->cancellable), &error)) |
| 516 |
g_debug ("UserSampler shutting down for reason: %s", error->message); |
| 517 |
|
| 518 |
write (record->sampler->event_fd, &exiting, sizeof exiting); |
| 519 |
|
| 520 |
g_source_destroy (muxer_source); |
| 521 |
|
| 522 |
sysprof_user_sampler_ioctl (record->sampler, FALSE); |
| 523 |
} |
| 524 |
else |
| 525 |
g_warning ("No capture FD available for muxing"); |
| 526 |
|
| 527 |
return dex_future_new_for_boolean (TRUE); |
| 528 |
} |
| 529 |
|
| 530 |
static DexFuture * |
| 531 |
sysprof_user_sampler_record (SysprofInstrument *instrument, |
| 532 |
SysprofRecording *recording, |
| 533 |
GCancellable *cancellable) |
| 534 |
{ |
| 535 |
SysprofUserSampler *self = (SysprofUserSampler *)instrument; |
| 536 |
Record *record; |
| 537 |
|
| 538 |
g_assert (SYSPROF_IS_INSTRUMENT (instrument)); |
| 539 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 540 |
g_assert (!cancellable || G_IS_CANCELLABLE (cancellable)); |
| 541 |
|
| 542 |
record = g_new0 (Record, 1); |
| 543 |
record->recording = g_object_ref (recording); |
| 544 |
record->sampler = g_object_ref (self); |
| 545 |
record->cancellable = dex_cancellable_new_from_cancellable (cancellable); |
| 546 |
|
| 547 |
return dex_scheduler_spawn (NULL, 0, |
| 548 |
sysprof_user_sampler_record_fiber, |
| 549 |
record, |
| 550 |
(GDestroyNotify)record_free); |
| 551 |
} |
| 552 |
|
| 553 |
static void |
| 554 |
sysprof_user_sampler_finalize (GObject *object) |
| 555 |
{ |
| 556 |
SysprofUserSampler *self = (SysprofUserSampler *)object; |
| 557 |
|
| 558 |
g_clear_pointer (&self->perf_fds, g_array_unref); |
| 559 |
|
| 560 |
g_clear_fd (&self->capture_fd_read, NULL); |
| 561 |
g_clear_fd (&self->capture_fd_write, NULL); |
| 562 |
g_clear_fd (&self->event_fd, NULL); |
| 563 |
|
| 564 |
G_OBJECT_CLASS (sysprof_user_sampler_parent_class)->finalize (object); |
| 565 |
} |
| 566 |
|
| 567 |
static void |
| 568 |
sysprof_user_sampler_class_init (SysprofUserSamplerClass *klass) |
| 569 |
{ |
| 570 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 571 |
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); |
| 572 |
|
| 573 |
object_class->finalize = sysprof_user_sampler_finalize; |
| 574 |
|
| 575 |
instrument_class->list_required_policy = sysprof_user_sampler_list_required_policy; |
| 576 |
instrument_class->prepare = sysprof_user_sampler_prepare; |
| 577 |
instrument_class->record = sysprof_user_sampler_record; |
| 578 |
} |
| 579 |
|
| 580 |
static void |
| 581 |
sysprof_user_sampler_init (SysprofUserSampler *self) |
| 582 |
{ |
| 583 |
int fds[2]; |
| 584 |
|
| 585 |
self->event_fd = eventfd (0, EFD_CLOEXEC); |
| 586 |
|
| 587 |
self->capture_fd_read = -1; |
| 588 |
self->capture_fd_write = -1; |
| 589 |
|
| 590 |
if (pipe2 (fds, O_CLOEXEC) == 0) |
| 591 |
{ |
| 592 |
self->capture_fd_read = fds[0]; |
| 593 |
self->capture_fd_write = fds[1]; |
| 594 |
} |
| 595 |
|
| 596 |
self->perf_fds = g_array_new (FALSE, FALSE, sizeof (int)); |
| 597 |
g_array_set_clear_func (self->perf_fds, close_fd); |
| 598 |
} |
| 599 |
|
| 600 |
SysprofInstrument * |
| 601 |
sysprof_user_sampler_new (guint stack_size) |
| 602 |
{ |
| 603 |
SysprofUserSampler *self; |
| 604 |
|
| 605 |
g_return_val_if_fail (stack_size > 0, NULL); |
| 606 |
g_return_val_if_fail (stack_size % sysprof_getpagesize () == 0, NULL); |
| 607 |
|
| 608 |
self = g_object_new (SYSPROF_TYPE_USER_SAMPLER, NULL); |
| 609 |
self->stack_size = stack_size; |
| 610 |
|
| 611 |
return SYSPROF_INSTRUMENT (self); |
| 612 |
} |