gvl-tracing 1.5.0 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/gvl_tracing_native_extension/gvl_tracing.c +27 -9
- data/lib/gvl_tracing/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f2845379f02ef9e8cd317a293b0c24c92dbd9aebad15ccb9b378ea1c41cf894
|
4
|
+
data.tar.gz: ce639fb1bffb10262e1dee5f5bf7a80f4425d9db7b690a494b6cefedae507265
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '01659c02734923610f4c771afcd1a863de369781a7c21cf42c46e5d956f897c65643f0ae6a5a1eebb7fcb81895556271f57f7e164b3ac3581177509d31381db7'
|
7
|
+
data.tar.gz: f0e0894e7d3dcc5c7f00485b8e741be31f832142ef0436524f26e5996da9f901dc17b46080b26f2ecf5bcfc0ca37b6bdaae48b014eaeb00518dfa56b6c892167
|
@@ -31,7 +31,7 @@
|
|
31
31
|
#include <inttypes.h>
|
32
32
|
#include <stdbool.h>
|
33
33
|
#include <sys/types.h>
|
34
|
-
#include <
|
34
|
+
#include <pthread.h>
|
35
35
|
|
36
36
|
#include "extconf.h"
|
37
37
|
|
@@ -77,7 +77,7 @@ static VALUE gc_tracepoint = Qnil;
|
|
77
77
|
#pragma GCC diagnostic ignored "-Wunused-variable"
|
78
78
|
static int thread_storage_key = 0;
|
79
79
|
static VALUE all_seen_threads = Qnil;
|
80
|
-
static
|
80
|
+
static pthread_mutex_t all_seen_threads_mutex = PTHREAD_MUTEX_INITIALIZER;
|
81
81
|
|
82
82
|
static VALUE tracing_init_local_storage(VALUE, VALUE);
|
83
83
|
static VALUE tracing_start(VALUE _self, VALUE output_path);
|
@@ -128,8 +128,6 @@ void Init_gvl_tracing_native_extension(void) {
|
|
128
128
|
|
129
129
|
all_seen_threads = rb_ary_new();
|
130
130
|
|
131
|
-
if (mtx_init(&all_seen_threads_mutex, mtx_plain) != thrd_success) rb_raise(rb_eRuntimeError, "Failed to initialize GvlTracing mutex");
|
132
|
-
|
133
131
|
VALUE gvl_tracing_module = rb_define_module("GvlTracing");
|
134
132
|
|
135
133
|
rb_define_singleton_method(gvl_tracing_module, "_init_local_storage", tracing_init_local_storage, 1);
|
@@ -148,7 +146,12 @@ static inline void initialize_thread_local_state(thread_local_state *state) {
|
|
148
146
|
uint32_t native_thread_id = 0;
|
149
147
|
|
150
148
|
#ifdef HAVE_PTHREAD_THREADID_NP
|
151
|
-
|
149
|
+
uint64_t full_native_thread_id;
|
150
|
+
pthread_threadid_np(pthread_self(), &full_native_thread_id);
|
151
|
+
// Note: `pthread_threadid_np` is declared as taking in a `uint64_t` but I don't think macOS uses such really
|
152
|
+
// high thread ids, and anyway perfetto doesn't like full 64-bit ids for threads so let's go with a simplification
|
153
|
+
// for now.
|
154
|
+
native_thread_id = (uint32_t) full_native_thread_id;
|
152
155
|
#elif HAVE_GETTID
|
153
156
|
native_thread_id = gettid();
|
154
157
|
#else
|
@@ -264,7 +267,9 @@ static void on_thread_event(rb_event_flag_t event_id, const rb_internal_thread_e
|
|
264
267
|
thread_local_state *state = GT_EVENT_LOCAL_STATE(event_data,
|
265
268
|
// These events are guaranteed to hold the GVL, so they can allocate
|
266
269
|
event_id & (RUBY_INTERNAL_THREAD_EVENT_STARTED | RUBY_INTERNAL_THREAD_EVENT_RESUMED));
|
270
|
+
|
267
271
|
if (!state) return;
|
272
|
+
|
268
273
|
#ifdef RUBY_3_3_PLUS
|
269
274
|
if (!state->thread) state->thread = event_data->thread;
|
270
275
|
#endif
|
@@ -299,6 +304,9 @@ static void on_thread_event(rb_event_flag_t event_id, const rb_internal_thread_e
|
|
299
304
|
static void on_gc_event(VALUE tpval, UNUSED_ARG void *_unused1) {
|
300
305
|
const char* event_name = "bug_unknown_event";
|
301
306
|
thread_local_state *state = GT_LOCAL_STATE(rb_thread_current(), false); // no alloc during GC
|
307
|
+
|
308
|
+
if (!state) return;
|
309
|
+
|
302
310
|
switch (rb_tracearg_event_flag(rb_tracearg_from_tracepoint(tpval))) {
|
303
311
|
case RUBY_INTERNAL_EVENT_GC_ENTER: event_name = "gc"; break;
|
304
312
|
// TODO: is it possible the thread wasn't running? Might need to save the last state.
|
@@ -319,6 +327,16 @@ static void thread_local_state_mark(void *data) {
|
|
319
327
|
rb_gc_mark(state->thread); // Marking thread to make sure it stays pinned
|
320
328
|
}
|
321
329
|
|
330
|
+
static inline void all_seen_threads_mutex_lock(void) {
|
331
|
+
int error = pthread_mutex_lock(&all_seen_threads_mutex);
|
332
|
+
if (error) rb_syserr_fail(error, "Failed to lock GvlTracing mutex");
|
333
|
+
}
|
334
|
+
|
335
|
+
static inline void all_seen_threads_mutex_unlock(void) {
|
336
|
+
int error = pthread_mutex_unlock(&all_seen_threads_mutex);
|
337
|
+
if (error) rb_syserr_fail(error, "Failed to unlock GvlTracing mutex");
|
338
|
+
}
|
339
|
+
|
322
340
|
#ifdef RUBY_3_3_PLUS
|
323
341
|
static inline thread_local_state *GT_LOCAL_STATE(VALUE thread, bool allocate) {
|
324
342
|
thread_local_state *state = rb_internal_thread_specific_get(thread, thread_storage_key);
|
@@ -333,9 +351,9 @@ static void thread_local_state_mark(void *data) {
|
|
333
351
|
// Keep thread around, to be able to extract names at the end
|
334
352
|
// We grab a lock here since thread creation can happen in multiple Ractors, and we want to make sure only one
|
335
353
|
// of them is mutating the array at a time. @ivoanjo: I think this is enough to make this safe....?
|
336
|
-
|
354
|
+
all_seen_threads_mutex_lock();
|
337
355
|
rb_ary_push(all_seen_threads, thread);
|
338
|
-
|
356
|
+
all_seen_threads_mutex_unlock();
|
339
357
|
}
|
340
358
|
return state;
|
341
359
|
}
|
@@ -380,7 +398,7 @@ static VALUE ruby_thread_id_for(UNUSED_ARG VALUE _self, VALUE thread) {
|
|
380
398
|
|
381
399
|
// Can only be called while GvlTracing is not active + while holding the GVL
|
382
400
|
static VALUE trim_all_seen_threads(UNUSED_ARG VALUE _self) {
|
383
|
-
|
401
|
+
all_seen_threads_mutex_lock();
|
384
402
|
|
385
403
|
VALUE alive_threads = rb_ary_new();
|
386
404
|
|
@@ -393,6 +411,6 @@ static VALUE trim_all_seen_threads(UNUSED_ARG VALUE _self) {
|
|
393
411
|
|
394
412
|
rb_ary_replace(all_seen_threads, alive_threads);
|
395
413
|
|
396
|
-
|
414
|
+
all_seen_threads_mutex_unlock();
|
397
415
|
return Qtrue;
|
398
416
|
}
|
data/lib/gvl_tracing/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gvl-tracing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivo Anjo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|