Add bin and edit workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s

This commit is contained in:
2026-09-16 13:11:16 -06:00
parent c8ac4fcae5
commit 4cee170d66
17576 changed files with 895740 additions and 2 deletions
@@ -0,0 +1,192 @@
// Released under the MIT License.
// Copyright, 2023, by Samuel Williams.
// Provides a simple implementation of unique pointers to elements of the given size.
#include <ruby.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
static const size_t IO_EVENT_ARRAY_MAXIMUM_COUNT = SIZE_MAX / sizeof(void*);
static const size_t IO_EVENT_ARRAY_DEFAULT_COUNT = 128;
struct IO_Event_Array {
// The array of pointers to elements:
void **base;
// The allocated size of the array:
size_t count;
// The biggest item we've seen so far:
size_t limit;
// The size of each element that is allocated:
size_t element_size;
void (*element_initialize)(void*);
void (*element_free)(void*);
};
inline static int IO_Event_Array_initialize(struct IO_Event_Array *array, size_t count, size_t element_size)
{
array->limit = 0;
array->element_size = element_size;
if (count) {
array->base = (void**)calloc(count, sizeof(void*));
if (array->base == NULL) {
return -1;
}
array->count = count;
return 1;
} else {
array->base = NULL;
array->count = 0;
return 0;
}
}
inline static size_t IO_Event_Array_memory_size(const struct IO_Event_Array *array)
{
// Upper bound.
return array->count * (sizeof(void*) + array->element_size);
}
inline static void IO_Event_Array_free(struct IO_Event_Array *array)
{
if (array->base) {
void **base = array->base;
size_t limit = array->limit;
array->base = NULL;
array->count = 0;
array->limit = 0;
for (size_t i = 0; i < limit; i += 1) {
void *element = base[i];
if (element) {
array->element_free(element);
free(element);
}
}
free(base);
}
}
inline static int IO_Event_Array_resize(struct IO_Event_Array *array, size_t count)
{
if (count <= array->count) {
// Already big enough:
return 0;
}
if (count > IO_EVENT_ARRAY_MAXIMUM_COUNT) {
errno = ENOMEM;
return -1;
}
size_t new_count = array->count;
// If the array is empty, we need to set the initial size:
if (new_count == 0) new_count = IO_EVENT_ARRAY_DEFAULT_COUNT;
else while (new_count < count) {
// Ensure we don't overflow:
if (new_count > (IO_EVENT_ARRAY_MAXIMUM_COUNT / 2)) {
new_count = IO_EVENT_ARRAY_MAXIMUM_COUNT;
break;
}
// Compute the next multiple (ideally a power of 2):
new_count *= 2;
}
void **new_base = (void**)realloc(array->base, new_count * sizeof(void*));
if (new_base == NULL) {
return -1;
}
// Zero out the new memory:
memset(new_base + array->count, 0, (new_count - array->count) * sizeof(void*));
array->base = (void**)new_base;
array->count = new_count;
// Resizing sucessful:
return 1;
}
inline static void* IO_Event_Array_lookup(struct IO_Event_Array *array, size_t index)
{
size_t count = index + 1;
// Resize the array if necessary:
if (count > array->count) {
if (IO_Event_Array_resize(array, count) == -1) {
return NULL;
}
}
// Get the element:
void **element = array->base + index;
// Allocate the element if it doesn't exist:
if (*element == NULL) {
*element = malloc(array->element_size);
assert(*element);
if (array->element_initialize) {
array->element_initialize(*element);
}
// Update the limit:
if (count > array->limit) array->limit = count;
}
return *element;
}
inline static void* IO_Event_Array_last(struct IO_Event_Array *array)
{
if (array->limit == 0) return NULL;
else return array->base[array->limit - 1];
}
inline static void IO_Event_Array_truncate(struct IO_Event_Array *array, size_t limit)
{
if (limit < array->limit) {
for (size_t i = limit; i < array->limit; i += 1) {
void **element = array->base + i;
if (*element) {
array->element_free(*element);
free(*element);
*element = NULL;
}
}
array->limit = limit;
}
}
// Push a new element onto the end of the array.
inline static void* IO_Event_Array_push(struct IO_Event_Array *array)
{
return IO_Event_Array_lookup(array, array->limit);
}
inline static void IO_Event_Array_each(struct IO_Event_Array *array, void (*callback)(void*))
{
for (size_t i = 0; i < array->limit; i += 1) {
void *element = array->base[i];
if (element) {
callback(element);
}
}
}
@@ -0,0 +1,35 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#include "event.h"
#include "fiber.h"
#include "profiler.h"
#include "selector/selector.h"
#include <complex.h>
void Init_IO_Event(void)
{
#ifdef HAVE_RB_EXT_RACTOR_SAFE
rb_ext_ractor_safe(true);
#endif
VALUE IO_Event = rb_define_module_under(rb_cIO, "Event");
Init_IO_Event_Fiber(IO_Event);
Init_IO_Event_Profiler(IO_Event);
VALUE IO_Event_Selector = rb_define_module_under(IO_Event, "Selector");
Init_IO_Event_Selector(IO_Event_Selector);
#ifdef IO_EVENT_SELECTOR_URING
Init_IO_Event_Selector_URing(IO_Event_Selector);
#endif
#ifdef IO_EVENT_SELECTOR_EPOLL
Init_IO_Event_Selector_EPoll(IO_Event_Selector);
#endif
#ifdef IO_EVENT_SELECTOR_KQUEUE
Init_IO_Event_Selector_KQueue(IO_Event_Selector);
#endif
}
@@ -0,0 +1,20 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#pragma once
#include <ruby.h>
void Init_IO_Event(void);
#ifdef HAVE_LIBURING_H
#include "selector/uring.h"
#endif
#ifdef HAVE_SYS_EPOLL_H
#include "selector/epoll.h"
#endif
#ifdef HAVE_SYS_EVENT_H
#include "selector/kqueue.h"
#endif
@@ -0,0 +1,63 @@
// Released under the MIT License.
// Copyright, 2025, by Samuel Williams.
#include "fiber.h"
static ID id_transfer, id_alive_p;
VALUE IO_Event_Fiber_transfer(VALUE fiber, int argc, VALUE *argv) {
// TODO Consider introducing something like `rb_fiber_scheduler_transfer(...)`.
#ifdef HAVE__RB_FIBER_TRANSFER
if (RTEST(rb_obj_is_fiber(fiber))) {
if (RTEST(rb_fiber_alive_p(fiber))) {
return rb_fiber_transfer(fiber, argc, argv);
}
// If it's a fiber, but dead, we are done.
return Qnil;
}
#endif
if (RTEST(rb_funcall(fiber, id_alive_p, 0))) {
return rb_funcallv(fiber, id_transfer, argc, argv);
}
return Qnil;
}
#ifndef HAVE__RB_FIBER_RAISE
static ID id_raise;
VALUE IO_Event_Fiber_raise(VALUE fiber, int argc, VALUE *argv) {
return rb_funcallv(fiber, id_raise, argc, argv);
}
#endif
#ifndef HAVE_RB_FIBER_CURRENT
static ID id_current;
static VALUE IO_Event_Fiber_current(void) {
return rb_funcall(rb_cFiber, id_current, 0);
}
#endif
// There is no public interface for this... yet.
static ID id_blocking_p;
int IO_Event_Fiber_blocking(VALUE fiber) {
return RTEST(rb_funcall(fiber, id_blocking_p, 0));
}
void Init_IO_Event_Fiber(VALUE IO_Event) {
id_transfer = rb_intern("transfer");
id_alive_p = rb_intern("alive?");
#ifndef HAVE__RB_FIBER_RAISE
id_raise = rb_intern("raise");
#endif
#ifndef HAVE_RB_FIBER_CURRENT
id_current = rb_intern("current");
#endif
id_blocking_p = rb_intern("blocking?");
}
@@ -0,0 +1,23 @@
// Released under the MIT License.
// Copyright, 2025, by Samuel Williams.
#pragma once
#include <ruby.h>
VALUE IO_Event_Fiber_transfer(VALUE fiber, int argc, VALUE *argv);
#ifdef HAVE__RB_FIBER_RAISE
#define IO_Event_Fiber_raise(fiber, argc, argv) rb_fiber_raise(fiber, argc, argv)
#else
VALUE IO_Event_Fiber_raise(VALUE fiber, int argc, VALUE *argv);
#endif
#ifdef HAVE_RB_FIBER_CURRENT
#define IO_Event_Fiber_current() rb_fiber_current()
#else
VALUE IO_Event_Fiber_current(void);
#endif
int IO_Event_Fiber_blocking(VALUE fiber);
void Init_IO_Event_Fiber(VALUE IO_Event);
@@ -0,0 +1,100 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#include "interrupt.h"
#include <unistd.h>
#include "selector/selector.h"
#ifdef HAVE_RUBY_WIN32_H
#include <ruby/win32.h>
#if !defined(HAVE_PIPE) && !defined(pipe)
#define pipe(p) rb_w32_pipe(p)
#endif
#endif
#ifdef HAVE_SYS_EVENTFD_H
#include <sys/eventfd.h>
void IO_Event_Interrupt_open(struct IO_Event_Interrupt *interrupt)
{
interrupt->descriptor = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
rb_update_max_fd(interrupt->descriptor);
}
void IO_Event_Interrupt_close(struct IO_Event_Interrupt *interrupt)
{
close(interrupt->descriptor);
}
void IO_Event_Interrupt_signal(struct IO_Event_Interrupt *interrupt)
{
uint64_t value = 1;
ssize_t result = write(interrupt->descriptor, &value, sizeof(value));
if (result == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return;
rb_sys_fail("IO_Event_Interrupt_signal:write");
}
}
void IO_Event_Interrupt_clear(struct IO_Event_Interrupt *interrupt)
{
uint64_t value = 0;
ssize_t result = read(interrupt->descriptor, &value, sizeof(value));
if (result == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return;
rb_sys_fail("IO_Event_Interrupt_clear:read");
}
}
#else
void IO_Event_Interrupt_open(struct IO_Event_Interrupt *interrupt)
{
#ifdef __linux__
pipe2(interrupt->descriptor, O_CLOEXEC | O_NONBLOCK);
#else
pipe(interrupt->descriptor);
IO_Event_Selector_nonblock_set(interrupt->descriptor[0]);
IO_Event_Selector_nonblock_set(interrupt->descriptor[1]);
#endif
rb_update_max_fd(interrupt->descriptor[0]);
rb_update_max_fd(interrupt->descriptor[1]);
}
void IO_Event_Interrupt_close(struct IO_Event_Interrupt *interrupt)
{
close(interrupt->descriptor[0]);
close(interrupt->descriptor[1]);
}
void IO_Event_Interrupt_signal(struct IO_Event_Interrupt *interrupt)
{
ssize_t result = write(interrupt->descriptor[1], ".", 1);
if (result == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// If we can't write to the pipe, it means the other end is full. In that case, we can be sure that the other end has already been woken up or is about to be woken up.
} else {
rb_sys_fail("IO_Event_Interrupt_signal:write");
}
}
}
void IO_Event_Interrupt_clear(struct IO_Event_Interrupt *interrupt)
{
char buffer[128];
ssize_t result = read(interrupt->descriptor[0], buffer, sizeof(buffer));
if (result == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// If we can't read from the pipe, it means the other end is empty. In that case, we can be sure that the other end is already clear.
} else {
rb_sys_fail("IO_Event_Interrupt_clear:read");
}
}
}
#endif
@@ -0,0 +1,30 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#pragma once
#include <ruby.h>
#ifdef HAVE_SYS_EVENTFD_H
struct IO_Event_Interrupt {
int descriptor;
};
static inline int IO_Event_Interrupt_descriptor(struct IO_Event_Interrupt *interrupt) {
return interrupt->descriptor;
}
#else
struct IO_Event_Interrupt {
int descriptor[2];
};
static inline int IO_Event_Interrupt_descriptor(struct IO_Event_Interrupt *interrupt) {
return interrupt->descriptor[0];
}
#endif
void IO_Event_Interrupt_open(struct IO_Event_Interrupt *interrupt);
void IO_Event_Interrupt_close(struct IO_Event_Interrupt *interrupt);
void IO_Event_Interrupt_signal(struct IO_Event_Interrupt *interrupt);
void IO_Event_Interrupt_clear(struct IO_Event_Interrupt *interrupt);
+107
View File
@@ -0,0 +1,107 @@
// Released under the MIT License.
// Copyright, 2023-2025, by Samuel Williams.
#include <ruby.h>
#include <stdio.h>
#include <assert.h>
struct IO_Event_List_Type {
};
struct IO_Event_List {
struct IO_Event_List *head, *tail;
struct IO_Event_List_Type *type;
};
inline static void IO_Event_List_initialize(struct IO_Event_List *list)
{
list->head = list->tail = list;
list->type = 0;
}
inline static void IO_Event_List_clear(struct IO_Event_List *list)
{
list->head = list->tail = NULL;
list->type = 0;
}
// Append an item to the end of the list.
inline static void IO_Event_List_append(struct IO_Event_List *list, struct IO_Event_List *node)
{
assert(node->head == NULL);
assert(node->tail == NULL);
struct IO_Event_List *head = list->head;
node->tail = list;
node->head = head;
list->head = node;
head->tail = node;
}
// Prepend an item to the beginning of the list.
inline static void IO_Event_List_prepend(struct IO_Event_List *list, struct IO_Event_List *node)
{
assert(node->head == NULL);
assert(node->tail == NULL);
struct IO_Event_List *tail = list->tail;
node->head = list;
node->tail = tail;
list->tail = node;
tail->head = node;
}
// Pop an item from the list.
inline static void IO_Event_List_pop(struct IO_Event_List *node)
{
assert(node->head != NULL);
assert(node->tail != NULL);
struct IO_Event_List *head = node->head;
struct IO_Event_List *tail = node->tail;
head->tail = tail;
tail->head = head;
node->head = node->tail = NULL;
}
// Remove an item from the list, if it is in a list.
inline static void IO_Event_List_free(struct IO_Event_List *node)
{
if (node->head && node->tail) {
IO_Event_List_pop(node);
}
}
// Calculate the memory size of the list nodes.
inline static size_t IO_Event_List_memory_size(const struct IO_Event_List *list)
{
size_t memsize = 0;
const struct IO_Event_List *node = list->tail;
while (node != list) {
memsize += sizeof(struct IO_Event_List);
node = node->tail;
}
return memsize;
}
// Return true if the list is empty.
inline static int IO_Event_List_empty(const struct IO_Event_List *list)
{
return list->head == list->tail;
}
// Enumerate all items in the list, assuming the list will not be modified during iteration.
inline static void IO_Event_List_immutable_each(struct IO_Event_List *list, void (*callback)(struct IO_Event_List *node))
{
struct IO_Event_List *node = list->tail;
while (node != list) {
if (node->type)
callback(node);
node = node->tail;
}
}
@@ -0,0 +1,505 @@
// Released under the MIT License.
// Copyright, 2025, by Samuel Williams.
#include "profiler.h"
#include "time.h"
#include "fiber.h"
#include "array.h"
#include <ruby/debug.h>
#include <stdio.h>
VALUE IO_Event_Profiler = Qnil;
struct IO_Event_Profiler_Call {
struct timespec enter_time;
struct timespec exit_time;
size_t nesting;
rb_event_flag_t event_flag;
ID id;
VALUE klass;
const char *path;
int line;
struct IO_Event_Profiler_Call *parent;
};
struct IO_Event_Profiler {
// Configuration:
float log_threshold;
int track_calls;
// Whether or not the profiler is currently running:
int running;
// Whether or not to capture call data:
int capture;
size_t stalls;
// From this point on, the state of any profile in progress:
struct timespec start_time;
struct timespec stop_time;
// The depth of the call stack:
size_t nesting;
// The current call frame:
struct IO_Event_Profiler_Call *current;
struct IO_Event_Array calls;
};
void IO_Event_Profiler_reset(struct IO_Event_Profiler *profiler) {
profiler->nesting = 0;
profiler->current = NULL;
IO_Event_Array_truncate(&profiler->calls, 0);
}
void IO_Event_Profiler_Call_initialize(struct IO_Event_Profiler_Call *call) {
call->enter_time.tv_sec = 0;
call->enter_time.tv_nsec = 0;
call->exit_time.tv_sec = 0;
call->exit_time.tv_nsec = 0;
call->nesting = 0;
call->event_flag = 0;
call->id = 0;
call->path = NULL;
call->line = 0;
}
void IO_Event_Profiler_Call_free(struct IO_Event_Profiler_Call *call) {
if (call->path) {
free((void*)call->path);
call->path = NULL;
}
}
static void IO_Event_Profiler_mark(void *ptr) {
struct IO_Event_Profiler *profiler = (struct IO_Event_Profiler*)ptr;
// If `klass` is stored as a VALUE in calls, we need to mark them here:
for (size_t i = 0; i < profiler->calls.limit; i += 1) {
struct IO_Event_Profiler_Call *call = profiler->calls.base[i];
rb_gc_mark_movable(call->klass);
}
}
static void IO_Event_Profiler_compact(void *ptr) {
struct IO_Event_Profiler *profiler = (struct IO_Event_Profiler*)ptr;
// If `klass` is stored as a VALUE in calls, we need to update their locations here:
for (size_t i = 0; i < profiler->calls.limit; i += 1) {
struct IO_Event_Profiler_Call *call = profiler->calls.base[i];
call->klass = rb_gc_location(call->klass);
}
}
static void IO_Event_Profiler_free(void *ptr) {
struct IO_Event_Profiler *profiler = (struct IO_Event_Profiler*)ptr;
IO_Event_Array_free(&profiler->calls);
free(profiler);
}
static size_t IO_Event_Profiler_memsize(const void *ptr) {
const struct IO_Event_Profiler *profiler = (const struct IO_Event_Profiler*)ptr;
return sizeof(*profiler) + IO_Event_Array_memory_size(&profiler->calls);
}
const rb_data_type_t IO_Event_Profiler_Type = {
.wrap_struct_name = "IO::Event::Profiler",
.function = {
.dmark = IO_Event_Profiler_mark,
.dcompact = IO_Event_Profiler_compact,
.dfree = IO_Event_Profiler_free,
.dsize = IO_Event_Profiler_memsize,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};
struct IO_Event_Profiler *IO_Event_Profiler_get(VALUE self) {
struct IO_Event_Profiler *profiler;
TypedData_Get_Struct(self, struct IO_Event_Profiler, &IO_Event_Profiler_Type, profiler);
return profiler;
}
VALUE IO_Event_Profiler_allocate(VALUE klass) {
struct IO_Event_Profiler *profiler = ALLOC(struct IO_Event_Profiler);
// Initialize the profiler state:
profiler->running = 0;
profiler->capture = 0;
profiler->stalls = 0;
profiler->nesting = 0;
profiler->current = NULL;
profiler->calls.element_initialize = (void (*)(void*))IO_Event_Profiler_Call_initialize;
profiler->calls.element_free = (void (*)(void*))IO_Event_Profiler_Call_free;
IO_Event_Array_initialize(&profiler->calls, 0, sizeof(struct IO_Event_Profiler_Call));
return TypedData_Wrap_Struct(klass, &IO_Event_Profiler_Type, profiler);
}
int IO_Event_Profiler_p(void) {
const char *enabled = getenv("IO_EVENT_PROFILER");
if (enabled && strcmp(enabled, "true") == 0) {
return 1;
}
return 0;
}
float IO_Event_Profiler_default_log_threshold(void) {
const char *log_threshold = getenv("IO_EVENT_PROFILER_LOG_THRESHOLD");
if (log_threshold) {
return strtof(log_threshold, NULL);
} else {
return 0.01;
}
}
int IO_Event_Profiler_default_track_calls(void) {
const char *track_calls = getenv("IO_EVENT_PROFILER_TRACK_CALLS");
if (track_calls && strcmp(track_calls, "false") == 0) {
return 0;
} else {
return 1;
}
}
VALUE IO_Event_Profiler_initialize(int argc, VALUE *argv, VALUE self) {
struct IO_Event_Profiler *profiler = IO_Event_Profiler_get(self);
VALUE log_threshold, track_calls;
rb_scan_args(argc, argv, "02", &log_threshold, &track_calls);
if (RB_NIL_P(log_threshold)) {
profiler->log_threshold = IO_Event_Profiler_default_log_threshold();
} else {
profiler->log_threshold = NUM2DBL(log_threshold);
}
if (RB_NIL_P(track_calls)) {
profiler->track_calls = IO_Event_Profiler_default_track_calls();
} else {
profiler->track_calls = RB_TEST(track_calls);
}
return self;
}
VALUE IO_Event_Profiler_default(VALUE klass) {
if (!IO_Event_Profiler_p()) {
return Qnil;
}
VALUE profiler = IO_Event_Profiler_allocate(klass);
struct IO_Event_Profiler *profiler_data = IO_Event_Profiler_get(profiler);
profiler_data->log_threshold = IO_Event_Profiler_default_log_threshold();
profiler_data->track_calls = IO_Event_Profiler_default_track_calls();
return profiler;
}
VALUE IO_Event_Profiler_new(float log_threshold, int track_calls) {
VALUE profiler = IO_Event_Profiler_allocate(IO_Event_Profiler);
struct IO_Event_Profiler *profiler_data = IO_Event_Profiler_get(profiler);
profiler_data->log_threshold = log_threshold;
profiler_data->track_calls = track_calls;
return profiler;
}
int event_flag_call_p(rb_event_flag_t event_flags) {
return event_flags & (RUBY_EVENT_CALL | RUBY_EVENT_C_CALL | RUBY_EVENT_B_CALL);
}
int event_flag_return_p(rb_event_flag_t event_flags) {
return event_flags & (RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN | RUBY_EVENT_B_RETURN);
}
const char *event_flag_name(rb_event_flag_t event_flag) {
switch (event_flag) {
case RUBY_EVENT_CALL: return "call";
case RUBY_EVENT_C_CALL: return "c-call";
case RUBY_EVENT_B_CALL: return "b-call";
case RUBY_EVENT_RETURN: return "return";
case RUBY_EVENT_C_RETURN: return "c-return";
case RUBY_EVENT_B_RETURN: return "b-return";
default: return "unknown";
}
}
static struct IO_Event_Profiler_Call* profiler_event_record_call(struct IO_Event_Profiler *profiler, rb_event_flag_t event_flag, ID id, VALUE klass) {
struct IO_Event_Profiler_Call *call = IO_Event_Array_push(&profiler->calls);
call->event_flag = event_flag;
call->parent = profiler->current;
profiler->current = call;
call->nesting = profiler->nesting;
profiler->nesting += 1;
if (id) {
call->id = id;
call->klass = klass;
} else {
rb_frame_method_id_and_class(&call->id, &call->klass);
}
const char *path = rb_sourcefile();
if (path) {
call->path = strdup(path);
}
call->line = rb_sourceline();
return call;
}
void IO_Event_Profiler_fiber_switch(struct IO_Event_Profiler *profiler);
static void IO_Event_Profiler_callback(rb_event_flag_t event_flag, VALUE data, VALUE self, ID id, VALUE klass) {
struct IO_Event_Profiler *profiler = IO_Event_Profiler_get(data);
if (event_flag & RUBY_EVENT_FIBER_SWITCH) {
IO_Event_Profiler_fiber_switch(profiler);
return;
}
// We don't want to capture data if we're not running:
if (!profiler->capture) return;
if (event_flag_call_p(event_flag)) {
struct IO_Event_Profiler_Call *call = profiler_event_record_call(profiler, event_flag, id, klass);
IO_Event_Time_current(&call->enter_time);
}
else if (event_flag_return_p(event_flag)) {
struct IO_Event_Profiler_Call *call = profiler->current;
// We may encounter returns without a preceeding call. This isn't an error, but we should pretend like the call started at the beginning of the profiling session:
if (call == NULL) {
struct IO_Event_Profiler_Call *last_call = IO_Event_Array_last(&profiler->calls);
call = profiler_event_record_call(profiler, event_flag, id, klass);
if (last_call) {
call->enter_time = last_call->enter_time;
} else {
call->enter_time = profiler->start_time;
}
}
IO_Event_Time_current(&call->exit_time);
profiler->current = call->parent;
// We may encounter returns without a preceeding call.
if (profiler->nesting > 0)
profiler->nesting -= 1;
}
}
VALUE IO_Event_Profiler_start(VALUE self) {
struct IO_Event_Profiler *profiler = IO_Event_Profiler_get(self);
if (profiler->running) return Qfalse;
profiler->running = 1;
IO_Event_Profiler_reset(profiler);
IO_Event_Time_current(&profiler->start_time);
rb_event_flag_t event_flags = RUBY_EVENT_FIBER_SWITCH;
if (profiler->track_calls) {
event_flags |= RUBY_EVENT_CALL | RUBY_EVENT_RETURN;
event_flags |= RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN;
// event_flags |= RUBY_EVENT_B_CALL | RUBY_EVENT_B_RETURN;
}
VALUE thread = rb_thread_current();
rb_thread_add_event_hook(thread, IO_Event_Profiler_callback, event_flags, self);
return self;
}
VALUE IO_Event_Profiler_stop(VALUE self) {
struct IO_Event_Profiler *profiler = IO_Event_Profiler_get(self);
if (!profiler->running) return Qfalse;
profiler->running = 0;
VALUE thread = rb_thread_current();
rb_thread_remove_event_hook_with_data(thread, IO_Event_Profiler_callback, self);
IO_Event_Time_current(&profiler->stop_time);
IO_Event_Profiler_reset(profiler);
return self;
}
static inline float IO_Event_Profiler_duration(struct IO_Event_Profiler *profiler) {
struct timespec duration;
IO_Event_Time_current(&profiler->stop_time);
IO_Event_Time_elapsed(&profiler->start_time, &profiler->stop_time, &duration);
return IO_Event_Time_duration(&duration);
}
void IO_Event_Profiler_print(struct IO_Event_Profiler *profiler, FILE *restrict stream);
void IO_Event_Profiler_finish(struct IO_Event_Profiler *profiler) {
profiler->capture = 0;
struct IO_Event_Profiler_Call *current = profiler->current;
while (current) {
IO_Event_Time_current(&current->exit_time);
current = current->parent;
}
}
void IO_Event_Profiler_fiber_switch(struct IO_Event_Profiler *profiler)
{
float duration = IO_Event_Profiler_duration(profiler);
if (profiler->capture) {
IO_Event_Profiler_finish(profiler);
if (duration > profiler->log_threshold) {
profiler->stalls += 1;
IO_Event_Profiler_print(profiler, stderr);
}
}
IO_Event_Profiler_reset(profiler);
if (!IO_Event_Fiber_blocking(IO_Event_Fiber_current())) {
// Reset the start time:
IO_Event_Time_current(&profiler->start_time);
profiler->capture = 1;
}
}
static const float IO_EVENT_PROFILER_PRINT_MINIMUM_PROPORTION = 0.01;
void IO_Event_Profiler_print_tty(struct IO_Event_Profiler *profiler, FILE *restrict stream) {
struct timespec total_duration = {};
IO_Event_Time_elapsed(&profiler->start_time, &profiler->stop_time, &total_duration);
fprintf(stderr, "Fiber stalled for %.3f seconds\n", IO_Event_Time_duration(&total_duration));
size_t skipped = 0;
for (size_t i = 0; i < profiler->calls.limit; i += 1) {
struct IO_Event_Profiler_Call *call = profiler->calls.base[i];
struct timespec duration = {};
IO_Event_Time_elapsed(&call->enter_time, &call->exit_time, &duration);
// Skip calls that are too short to be meaningful:
if (IO_Event_Time_proportion(&duration, &total_duration) < IO_EVENT_PROFILER_PRINT_MINIMUM_PROPORTION) {
skipped += 1;
continue;
}
for (size_t i = 0; i < call->nesting; i += 1) {
fputc('\t', stream);
}
VALUE class_inspect = rb_inspect(call->klass);
const char *name = rb_id2name(call->id);
fprintf(stream, "%s:%d in %s '%s#%s' (" IO_EVENT_TIME_PRINTF_TIMESPEC "s)\n", call->path, call->line, event_flag_name(call->event_flag), RSTRING_PTR(class_inspect), name, IO_EVENT_TIME_PRINTF_TIMESPEC_ARGUMENTS(duration));
}
if (skipped > 0) {
fprintf(stream, "Skipped %zu calls that were too short to be meaningful.\n", skipped);
}
}
void IO_Event_Profiler_print_json(struct IO_Event_Profiler *profiler, FILE *restrict stream) {
struct timespec total_duration = {};
IO_Event_Time_elapsed(&profiler->start_time, &profiler->stop_time, &total_duration);
fputc('{', stream);
fprintf(stream, "\"duration\":" IO_EVENT_TIME_PRINTF_TIMESPEC, IO_EVENT_TIME_PRINTF_TIMESPEC_ARGUMENTS(total_duration));
size_t skipped = 0;
fprintf(stream, ",\"calls\":[");
int first = 1;
for (size_t i = 0; i < profiler->calls.limit; i += 1) {
struct IO_Event_Profiler_Call *call = profiler->calls.base[i];
struct timespec duration = {};
IO_Event_Time_elapsed(&call->enter_time, &call->exit_time, &duration);
// Skip calls that are too short to be meaningful:
if (IO_Event_Time_proportion(&duration, &total_duration) < IO_EVENT_PROFILER_PRINT_MINIMUM_PROPORTION) {
skipped += 1;
continue;
}
VALUE class_inspect = rb_inspect(call->klass);
const char *name = rb_id2name(call->id);
fprintf(stream, "%s{\"path\":\"%s\",\"line\":%d,\"class\":\"%s\",\"method\":\"%s\",\"duration\":" IO_EVENT_TIME_PRINTF_TIMESPEC ",\"nesting\":%zu}", first ? "" : ",", call->path, call->line, RSTRING_PTR(class_inspect), name, IO_EVENT_TIME_PRINTF_TIMESPEC_ARGUMENTS(duration), call->nesting);
first = 0;
}
fprintf(stream, "]");
if (skipped > 0) {
fprintf(stream, ",\"skipped\":%zu", skipped);
}
fprintf(stream, "}\n");
}
void IO_Event_Profiler_print(struct IO_Event_Profiler *profiler, FILE *restrict stream) {
if (isatty(fileno(stream))) {
IO_Event_Profiler_print_tty(profiler, stream);
} else {
IO_Event_Profiler_print_json(profiler, stream);
}
}
VALUE IO_Event_Profiler_stalls(VALUE self) {
struct IO_Event_Profiler *profiler = IO_Event_Profiler_get(self);
return SIZET2NUM(profiler->stalls);
}
void Init_IO_Event_Profiler(VALUE IO_Event) {
IO_Event_Profiler = rb_define_class_under(IO_Event, "Profiler", rb_cObject);
rb_define_alloc_func(IO_Event_Profiler, IO_Event_Profiler_allocate);
rb_define_singleton_method(IO_Event_Profiler, "default", IO_Event_Profiler_default, 0);
rb_define_method(IO_Event_Profiler, "initialize", IO_Event_Profiler_initialize, -1);
rb_define_method(IO_Event_Profiler, "start", IO_Event_Profiler_start, 0);
rb_define_method(IO_Event_Profiler, "stop", IO_Event_Profiler_stop, 0);
rb_define_method(IO_Event_Profiler, "stalls", IO_Event_Profiler_stalls, 0);
}
@@ -0,0 +1,8 @@
// Released under the MIT License.
// Copyright, 2025, by Samuel Williams.
#pragma once
#include <ruby.h>
void Init_IO_Event_Profiler(VALUE IO_Event);
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,10 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#pragma once
#include <ruby.h>
#define IO_EVENT_SELECTOR_EPOLL
void Init_IO_Event_Selector_EPoll(VALUE IO_Event_Selector);
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,10 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#pragma once
#include <ruby.h>
#define IO_EVENT_SELECTOR_KQUEUE
void Init_IO_Event_Selector_KQueue(VALUE IO_Event_Selector);
@@ -0,0 +1,21 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#include <ruby.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef __NR_pidfd_open
#define __NR_pidfd_open 434 /* System call # on most architectures */
#endif
static int
pidfd_open(pid_t pid, unsigned int flags)
{
return syscall(__NR_pidfd_open, pid, flags);
}
@@ -0,0 +1,298 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#include "selector.h"
#include <fcntl.h>
#include <stdlib.h>
static const int DEBUG = 0;
#ifndef HAVE_RB_IO_DESCRIPTOR
static ID id_fileno;
int IO_Event_Selector_io_descriptor(VALUE io) {
return RB_NUM2INT(rb_funcall(io, id_fileno, 0));
}
#endif
#ifndef HAVE_RB_PROCESS_STATUS_WAIT
static ID id_wait;
static VALUE rb_Process_Status = Qnil;
VALUE IO_Event_Selector_process_status_wait(rb_pid_t pid, int flags)
{
return rb_funcall(rb_Process_Status, id_wait, 2, PIDT2NUM(pid), INT2NUM(flags | WNOHANG));
}
#endif
int IO_Event_Selector_nonblock_set(int file_descriptor)
{
#ifdef _WIN32
u_long nonblock = 1;
ioctlsocket(file_descriptor, FIONBIO, &nonblock);
// Windows does not provide any way to know this, so we always restore it back to unset:
return 0;
#else
// Get the current mode:
int flags = fcntl(file_descriptor, F_GETFL, 0);
// Set the non-blocking flag if it isn't already:
if (!(flags & O_NONBLOCK)) {
fcntl(file_descriptor, F_SETFL, flags | O_NONBLOCK);
}
return flags;
#endif
}
void IO_Event_Selector_nonblock_restore(int file_descriptor, int flags)
{
#ifdef _WIN32
// Yolo...
u_long nonblock = flags;
ioctlsocket(file_descriptor, FIONBIO, &nonblock);
#else
// The flags didn't have O_NONBLOCK set, so it would have been set, so we need to restore it:
if (!(flags & O_NONBLOCK)) {
fcntl(file_descriptor, F_SETFL, flags);
}
#endif
}
struct IO_Event_Selector_nonblock_arguments {
int file_descriptor;
int flags;
};
static VALUE IO_Event_Selector_nonblock_ensure(VALUE _arguments) {
struct IO_Event_Selector_nonblock_arguments *arguments = (struct IO_Event_Selector_nonblock_arguments *)_arguments;
IO_Event_Selector_nonblock_restore(arguments->file_descriptor, arguments->flags);
return Qnil;
}
static VALUE IO_Event_Selector_nonblock(VALUE class, VALUE io)
{
struct IO_Event_Selector_nonblock_arguments arguments = {
.file_descriptor = IO_Event_Selector_io_descriptor(io),
.flags = IO_Event_Selector_nonblock_set(arguments.file_descriptor)
};
return rb_ensure(rb_yield, io, IO_Event_Selector_nonblock_ensure, (VALUE)&arguments);
}
void Init_IO_Event_Selector(VALUE IO_Event_Selector) {
#ifndef HAVE_RB_IO_DESCRIPTOR
id_fileno = rb_intern("fileno");
#endif
#ifndef HAVE_RB_PROCESS_STATUS_WAIT
id_wait = rb_intern("wait");
rb_Process_Status = rb_const_get_at(rb_mProcess, rb_intern("Status"));
rb_gc_register_mark_object(rb_Process_Status);
#endif
rb_define_singleton_method(IO_Event_Selector, "nonblock", IO_Event_Selector_nonblock, 1);
}
void IO_Event_Selector_initialize(struct IO_Event_Selector *backend, VALUE self, VALUE loop) {
RB_OBJ_WRITE(self, &backend->self, self);
RB_OBJ_WRITE(self, &backend->loop, loop);
backend->waiting = NULL;
backend->ready = NULL;
}
VALUE IO_Event_Selector_loop_resume(struct IO_Event_Selector *backend, VALUE fiber, int argc, VALUE *argv) {
return IO_Event_Fiber_transfer(fiber, argc, argv);
}
VALUE IO_Event_Selector_loop_yield(struct IO_Event_Selector *backend)
{
// TODO Why is this assertion failing in async?
// RUBY_ASSERT(backend->loop != IO_Event_Fiber_current());
return IO_Event_Fiber_transfer(backend->loop, 0, NULL);
}
struct wait_and_transfer_arguments {
int argc;
VALUE *argv;
struct IO_Event_Selector *backend;
struct IO_Event_Selector_Queue *waiting;
};
static void queue_pop(struct IO_Event_Selector *backend, struct IO_Event_Selector_Queue *waiting) {
if (waiting->head) {
waiting->head->tail = waiting->tail;
} else {
// We must have been at the head of the queue:
backend->waiting = waiting->tail;
}
if (waiting->tail) {
waiting->tail->head = waiting->head;
} else {
// We must have been at the tail of the queue:
backend->ready = waiting->head;
}
waiting->head = NULL;
waiting->tail = NULL;
}
static void queue_push(struct IO_Event_Selector *backend, struct IO_Event_Selector_Queue *waiting) {
assert(waiting->head == NULL);
assert(waiting->tail == NULL);
if (backend->waiting) {
// If there was an item in the queue already, we shift it along:
backend->waiting->head = waiting;
waiting->tail = backend->waiting;
} else {
// If the queue was empty, we update the tail too:
backend->ready = waiting;
}
// We always push to the front/head:
backend->waiting = waiting;
}
static VALUE wait_and_transfer(VALUE _arguments) {
struct wait_and_transfer_arguments *arguments = (struct wait_and_transfer_arguments *)_arguments;
VALUE fiber = arguments->argv[0];
int argc = arguments->argc - 1;
VALUE *argv = arguments->argv + 1;
return IO_Event_Selector_loop_resume(arguments->backend, fiber, argc, argv);
}
static VALUE wait_and_transfer_ensure(VALUE _arguments) {
struct wait_and_transfer_arguments *arguments = (struct wait_and_transfer_arguments *)_arguments;
queue_pop(arguments->backend, arguments->waiting);
return Qnil;
}
VALUE IO_Event_Selector_resume(struct IO_Event_Selector *backend, int argc, VALUE *argv)
{
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
struct IO_Event_Selector_Queue waiting = {
.head = NULL,
.tail = NULL,
.flags = IO_EVENT_SELECTOR_QUEUE_FIBER,
.fiber = IO_Event_Fiber_current()
};
RB_OBJ_WRITTEN(backend->self, Qundef, waiting.fiber);
queue_push(backend, &waiting);
struct wait_and_transfer_arguments arguments = {
.argc = argc,
.argv = argv,
.backend = backend,
.waiting = &waiting,
};
return rb_ensure(wait_and_transfer, (VALUE)&arguments, wait_and_transfer_ensure, (VALUE)&arguments);
}
static VALUE wait_and_raise(VALUE _arguments) {
struct wait_and_transfer_arguments *arguments = (struct wait_and_transfer_arguments *)_arguments;
VALUE fiber = arguments->argv[0];
int argc = arguments->argc - 1;
VALUE *argv = arguments->argv + 1;
return IO_Event_Fiber_raise(fiber, argc, argv);
}
VALUE IO_Event_Selector_raise(struct IO_Event_Selector *backend, int argc, VALUE *argv)
{
rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS);
struct IO_Event_Selector_Queue waiting = {
.head = NULL,
.tail = NULL,
.flags = IO_EVENT_SELECTOR_QUEUE_FIBER,
.fiber = IO_Event_Fiber_current()
};
RB_OBJ_WRITTEN(backend->self, Qundef, waiting.fiber);
queue_push(backend, &waiting);
struct wait_and_transfer_arguments arguments = {
.argc = argc,
.argv = argv,
.backend = backend,
.waiting = &waiting,
};
return rb_ensure(wait_and_raise, (VALUE)&arguments, wait_and_transfer_ensure, (VALUE)&arguments);
}
void IO_Event_Selector_ready_push(struct IO_Event_Selector *backend, VALUE fiber)
{
struct IO_Event_Selector_Queue *waiting = malloc(sizeof(struct IO_Event_Selector_Queue));
assert(waiting);
waiting->head = NULL;
waiting->tail = NULL;
waiting->flags = IO_EVENT_SELECTOR_QUEUE_INTERNAL;
RB_OBJ_WRITE(backend->self, &waiting->fiber, fiber);
queue_push(backend, waiting);
}
static inline
void IO_Event_Selector_ready_pop(struct IO_Event_Selector *backend, struct IO_Event_Selector_Queue *ready)
{
if (DEBUG) fprintf(stderr, "IO_Event_Selector_ready_pop -> %p\n", (void*)ready->fiber);
VALUE fiber = ready->fiber;
if (ready->flags & IO_EVENT_SELECTOR_QUEUE_INTERNAL) {
// This means that the fiber was added to the ready queue by the selector itself, and we need to transfer control to it, but before we do that, we need to remove it from the queue, as there is no expectation that returning from `transfer` will remove it.
queue_pop(backend, ready);
free(ready);
} else if (ready->flags & IO_EVENT_SELECTOR_QUEUE_FIBER) {
// This means the fiber added itself to the ready queue, and we need to transfer control back to it. Transferring control back to the fiber will call `queue_pop` and remove it from the queue.
} else {
rb_raise(rb_eRuntimeError, "Unknown queue type!");
}
IO_Event_Selector_loop_resume(backend, fiber, 0, NULL);
}
int IO_Event_Selector_ready_flush(struct IO_Event_Selector *backend)
{
int count = 0;
// During iteration of the queue, the same item may be re-queued. If we don't handle this correctly, we may end up in an infinite loop. So, to avoid this situation, we keep note of the current head of the queue and break the loop if we reach the same item again.
// Get the current tail and head of the queue:
struct IO_Event_Selector_Queue *waiting = backend->waiting;
if (DEBUG) fprintf(stderr, "IO_Event_Selector_ready_flush waiting = %p\n", waiting);
// Process from head to tail in order:
// During this, more items may be appended to tail.
while (backend->ready) {
if (DEBUG) fprintf(stderr, "backend->ready = %p\n", backend->ready);
struct IO_Event_Selector_Queue *ready = backend->ready;
count += 1;
IO_Event_Selector_ready_pop(backend, ready);
if (ready == waiting) break;
}
return count;
}
@@ -0,0 +1,158 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#pragma once
#include <ruby.h>
#include <ruby/thread.h>
#include <ruby/io.h>
#include "../time.h"
#include "../fiber.h"
#ifdef HAVE_RUBY_IO_BUFFER_H
#include <ruby/io/buffer.h>
#include <ruby/fiber/scheduler.h>
#endif
#ifndef RUBY_FIBER_SCHEDULER_VERSION
#define RUBY_FIBER_SCHEDULER_VERSION 1
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
enum IO_Event {
IO_EVENT_READABLE = 1,
IO_EVENT_PRIORITY = 2,
IO_EVENT_WRITABLE = 4,
IO_EVENT_ERROR = 8,
IO_EVENT_HANGUP = 16,
// Used by kqueue to differentiate between process exit and file descriptor events:
IO_EVENT_EXIT = 32,
};
void Init_IO_Event_Selector(VALUE IO_Event_Selector);
static inline int IO_Event_try_again(int error) {
return error == EAGAIN || error == EWOULDBLOCK;
}
#ifdef HAVE_RB_IO_DESCRIPTOR
#define IO_Event_Selector_io_descriptor(io) rb_io_descriptor(io)
#else
int IO_Event_Selector_io_descriptor(VALUE io);
#endif
// Reap a process without hanging.
#ifdef HAVE_RB_PROCESS_STATUS_WAIT
#define IO_Event_Selector_process_status_wait(pid, flags) rb_process_status_wait(pid, flags | WNOHANG)
#else
VALUE IO_Event_Selector_process_status_wait(rb_pid_t pid, int flags);
#endif
int IO_Event_Selector_nonblock_set(int file_descriptor);
void IO_Event_Selector_nonblock_restore(int file_descriptor, int flags);
enum IO_Event_Selector_Queue_Flags {
IO_EVENT_SELECTOR_QUEUE_FIBER = 1,
IO_EVENT_SELECTOR_QUEUE_INTERNAL = 2,
};
struct IO_Event_Selector_Queue {
struct IO_Event_Selector_Queue *head;
struct IO_Event_Selector_Queue *tail;
enum IO_Event_Selector_Queue_Flags flags;
VALUE fiber;
};
// The internal state of the event selector.
// The event selector is responsible for managing the scheduling of fibers, as well as selecting for events.
struct IO_Event_Selector {
VALUE self;
VALUE loop;
// The ready queue is a list of fibers that are ready to be resumed from the event loop fiber.
// Append to waiting (front/head of queue).
struct IO_Event_Selector_Queue *waiting;
// Process from ready (back/tail of queue).
struct IO_Event_Selector_Queue *ready;
};
void IO_Event_Selector_initialize(struct IO_Event_Selector *backend, VALUE self, VALUE loop);
static inline
void IO_Event_Selector_mark(struct IO_Event_Selector *backend) {
rb_gc_mark_movable(backend->self);
rb_gc_mark_movable(backend->loop);
// Walk backwards through the ready queue:
struct IO_Event_Selector_Queue *ready = backend->ready;
while (ready) {
rb_gc_mark_movable(ready->fiber);
ready = ready->head;
}
}
static inline
void IO_Event_Selector_compact(struct IO_Event_Selector *backend) {
backend->self = rb_gc_location(backend->self);
backend->loop = rb_gc_location(backend->loop);
struct IO_Event_Selector_Queue *ready = backend->ready;
while (ready) {
ready->fiber = rb_gc_location(ready->fiber);
ready = ready->head;
}
}
// Transfer control from the event loop to a user fiber.
// This is used to transfer control to a user fiber when it may proceed.
// Strictly speaking, it's not a scheduling operation (does not schedule the current fiber).
VALUE IO_Event_Selector_loop_resume(struct IO_Event_Selector *backend, VALUE fiber, int argc, VALUE *argv);
// Transfer from a user fiber back to the event loop.
// This is used to transfer control back to the event loop in order to wait for events.
// Strictly speaking, it's not a scheduling operation (does not schedule the current fiber).
VALUE IO_Event_Selector_loop_yield(struct IO_Event_Selector *backend);
// Resume a specific fiber. This is a scheduling operation.
// The first argument is the fiber, the rest are the arguments to the resume.
//
// The implementation has two possible strategies:
// 1. Add the current fiber to the ready queue and transfer control to the target fiber.
// 2. Schedule the target fiber to be resumed by the event loop later on.
//
// We currently only implement the first strategy.
VALUE IO_Event_Selector_resume(struct IO_Event_Selector *backend, int argc, VALUE *argv);
// Raise an exception on a specific fiber.
// The first argument is the fiber, the rest are the arguments to the exception.
//
// The implementation has two possible strategies:
// 1. Add the current fiber to the ready queue and transfer control to the target fiber.
// 2. Schedule the target fiber to be resumed by the event loop with an exception later on.
//
// We currently only implement the first strategy.
VALUE IO_Event_Selector_raise(struct IO_Event_Selector *backend, int argc, VALUE *argv);
// Yield control to the event loop. This is a scheduling operation.
//
// The implementation adds the current fiber to the ready queue and transfers control to the event loop.
static inline
VALUE IO_Event_Selector_yield(struct IO_Event_Selector *backend)
{
return IO_Event_Selector_resume(backend, 1, &backend->loop);
}
// Append a specific fiber to the ready queue.
// The fiber can be an actual fiber or an object that responds to `alive?` and `transfer`.
// The implementation will transfer control to the fiber later on.
void IO_Event_Selector_ready_push(struct IO_Event_Selector *backend, VALUE fiber);
// Flush the ready queue by transferring control one at a time.
int IO_Event_Selector_ready_flush(struct IO_Event_Selector *backend);
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,10 @@
// Released under the MIT License.
// Copyright, 2021-2025, by Samuel Williams.
#pragma once
#include <ruby.h>
#define IO_EVENT_SELECTOR_URING
void Init_IO_Event_Selector_URing(VALUE IO_Event_Selector);
@@ -0,0 +1,35 @@
// Released under the MIT License.
// Copyright, 2025, by Samuel Williams.
#include "time.h"
void IO_Event_Time_elapsed(const struct timespec* start, const struct timespec* stop, struct timespec *duration)
{
if ((stop->tv_nsec - start->tv_nsec) < 0) {
duration->tv_sec = stop->tv_sec - start->tv_sec - 1;
duration->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000;
} else {
duration->tv_sec = stop->tv_sec - start->tv_sec;
duration->tv_nsec = stop->tv_nsec - start->tv_nsec;
}
}
float IO_Event_Time_duration(const struct timespec *duration)
{
return duration->tv_sec + duration->tv_nsec / 1000000000.0;
}
void IO_Event_Time_current(struct timespec *time) {
clock_gettime(CLOCK_MONOTONIC, time);
}
float IO_Event_Time_proportion(const struct timespec *duration, const struct timespec *total_duration) {
return IO_Event_Time_duration(duration) / IO_Event_Time_duration(total_duration);
}
float IO_Event_Time_delta(const struct timespec *start, const struct timespec *stop) {
struct timespec duration;
IO_Event_Time_elapsed(start, stop, &duration);
return IO_Event_Time_duration(&duration);
}
@@ -0,0 +1,17 @@
// Released under the MIT License.
// Copyright, 2025, by Samuel Williams.
#pragma once
#include <ruby.h>
#include <time.h>
void IO_Event_Time_elapsed(const struct timespec* start, const struct timespec* stop, struct timespec *duration);
float IO_Event_Time_duration(const struct timespec *duration);
void IO_Event_Time_current(struct timespec *time);
float IO_Event_Time_delta(const struct timespec *start, const struct timespec *stop);
float IO_Event_Time_proportion(const struct timespec *duration, const struct timespec *total_duration);
#define IO_EVENT_TIME_PRINTF_TIMESPEC "%.3g"
#define IO_EVENT_TIME_PRINTF_TIMESPEC_ARGUMENTS(ts) ((double)(ts).tv_sec + (ts).tv_nsec / 1e9)