Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSX compatibility #4

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
/experiments/
gems.locked
*.so
*.bundle
example*json
15 changes: 13 additions & 2 deletions ext/gvl_tracing_native_extension/gvl_tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@

#include <ruby/ruby.h>
#include <ruby/thread.h>
#include <ruby/atomic.h>
#include <errno.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>

static VALUE tracing_start(VALUE _self, VALUE output_path);
static VALUE tracing_stop(VALUE _self);
static double timestamp_microseconds(void);
static void render_event(const char *event_name);
static void on_event(rb_event_flag_t event, const rb_internal_thread_event_data_t *_unused1, void *_unused2);
static rb_atomic_t thread_serial = 0;
static _Thread_local bool current_thread_serial_set = false;
static _Thread_local unsigned int current_thread_serial = 0;

// Global mutable state
static FILE *output_file = NULL;
Expand All @@ -49,6 +52,14 @@ void Init_gvl_tracing_native_extension(void) {
rb_define_singleton_method(gvl_tracing_module, "stop", tracing_stop, 0);
}

static inline unsigned int current_thread_id(void) {
if (!current_thread_serial_set) {
current_thread_serial_set = true;
current_thread_serial = RUBY_ATOMIC_FETCH_ADD(thread_serial, 1);
}
return (unsigned int)current_thread_serial;
}

static VALUE tracing_start(VALUE _self, VALUE output_path) {
Check_Type(output_path, T_STRING);

Expand Down Expand Up @@ -103,7 +114,7 @@ static double timestamp_microseconds(void) {
static void render_event(const char *event_name) {
// Event data
double now_microseconds = timestamp_microseconds() - started_tracing_at_microseconds;
pid_t thread_id = gettid();
unsigned int thread_id = current_thread_id();

// Each event is converted into two events in the output: one that signals the end of the previous event
// (whatever it was), and one that signals the start of the actual event we're processing.
Expand Down