|
| 1 | +// Released under the MIT License. |
| 2 | +// Copyright, 2025, by Samuel Williams. |
| 3 | + |
| 4 | +#include "profile.h" |
| 5 | +#include "time.h" |
| 6 | + |
| 7 | +#include <ruby/debug.h> |
| 8 | + |
| 9 | +#include <stdio.h> |
| 10 | + |
| 11 | +void IO_Event_Profile_Event_initialize(struct IO_Event_Profile_Event *event) { |
| 12 | + event->time.tv_sec = 0; |
| 13 | + event->time.tv_nsec = 0; |
| 14 | + event->nesting = 0; |
| 15 | + |
| 16 | + event->event_flag = 0; |
| 17 | + event->id = 0; |
| 18 | + |
| 19 | + event->path = NULL; |
| 20 | + event->line = 0; |
| 21 | +} |
| 22 | + |
| 23 | +void IO_Event_Profile_Event_free(struct IO_Event_Profile_Event *event) { |
| 24 | + if (event->path) { |
| 25 | + free((void*)event->path); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +static const char *event_flag_name(rb_event_flag_t event_flag) { |
| 30 | + switch (event_flag) { |
| 31 | + case RUBY_EVENT_LINE: |
| 32 | + return "line"; |
| 33 | + case RUBY_EVENT_CALL: |
| 34 | + case RUBY_EVENT_C_CALL: |
| 35 | + return "call"; |
| 36 | + case RUBY_EVENT_RETURN: |
| 37 | + case RUBY_EVENT_C_RETURN: |
| 38 | + return "return"; |
| 39 | + default: |
| 40 | + return "unknown"; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +int event_flag_call_p(rb_event_flag_t event_flags) { |
| 45 | + return event_flags & (RUBY_EVENT_CALL | RUBY_EVENT_C_CALL); |
| 46 | +} |
| 47 | + |
| 48 | +int event_flag_return_p(rb_event_flag_t event_flags) { |
| 49 | + return event_flags & (RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN); |
| 50 | +} |
| 51 | + |
| 52 | +static void profile_event_callback(rb_event_flag_t event_flag, VALUE data, VALUE self, ID id, VALUE klass) { |
| 53 | + struct IO_Event_Profile *profile = (struct IO_Event_Profile*)data; |
| 54 | + struct IO_Event_Profile_Event *event = IO_Event_Array_push(&profile->events); |
| 55 | + |
| 56 | + IO_Event_Time_current(&event->time); |
| 57 | + |
| 58 | + event->event_flag = event_flag; |
| 59 | + |
| 60 | + if (event_flag_call_p(event_flag)) { |
| 61 | + event->parent = profile->current; |
| 62 | + profile->current = event; |
| 63 | + |
| 64 | + event->nesting = profile->nesting; |
| 65 | + profile->nesting += 1; |
| 66 | + |
| 67 | + if (id) { |
| 68 | + event->id = id; |
| 69 | + event->klass = klass; |
| 70 | + } else { |
| 71 | + rb_frame_method_id_and_class(&event->id, &event->klass); |
| 72 | + } |
| 73 | + |
| 74 | + const char *path = rb_sourcefile(); |
| 75 | + if (path) { |
| 76 | + event->path = strdup(path); |
| 77 | + } |
| 78 | + event->line = rb_sourceline(); |
| 79 | + } else if (event_flag_return_p(event_flag)) { |
| 80 | + // Set up the call/return pair: |
| 81 | + profile->current->pair = event; |
| 82 | + event->pair = profile->current; |
| 83 | + |
| 84 | + profile->current = profile->current->parent; |
| 85 | + event->parent = profile->current; |
| 86 | + |
| 87 | + profile->nesting -= 1; |
| 88 | + event->nesting = profile->nesting; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void IO_Event_Profile_initialize(struct IO_Event_Profile *profile, VALUE fiber) { |
| 93 | + profile->fiber = fiber; |
| 94 | + |
| 95 | + profile->events.element_initialize = (void (*)(void*))IO_Event_Profile_Event_initialize; |
| 96 | + profile->events.element_free = (void (*)(void*))IO_Event_Profile_Event_free; |
| 97 | + |
| 98 | + IO_Event_Array_initialize(&profile->events, 0, sizeof(struct IO_Event_Profile_Event)); |
| 99 | +} |
| 100 | + |
| 101 | +void IO_Event_Profile_start(struct IO_Event_Profile *profile) { |
| 102 | + IO_Event_Time_current(&profile->start_time); |
| 103 | + profile->nesting = 0; |
| 104 | + profile->current = NULL; |
| 105 | + |
| 106 | + // Since fibers are currently limited to a single thread, we use this in the hope that it's a little more efficient: |
| 107 | + VALUE thread = rb_thread_current(); |
| 108 | + rb_thread_add_event_hook(thread, profile_event_callback, RUBY_EVENT_CALL | RUBY_EVENT_C_CALL | RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN, (VALUE)profile); |
| 109 | +} |
| 110 | + |
| 111 | +void IO_Event_Profile_stop(struct IO_Event_Profile *profile) { |
| 112 | + IO_Event_Time_current(&profile->stop_time); |
| 113 | + |
| 114 | + VALUE thread = rb_thread_current(); |
| 115 | + rb_thread_remove_event_hook_with_data(thread, profile_event_callback, (VALUE)profile); |
| 116 | +} |
| 117 | + |
| 118 | +void IO_Event_Profile_free(struct IO_Event_Profile *profile) { |
| 119 | + IO_Event_Array_free(&profile->events); |
| 120 | +} |
| 121 | + |
| 122 | +static const float IO_EVENT_PROFILE_PRINT_MINIMUM_PROPORTION = 0.01; |
| 123 | + |
| 124 | +void IO_Event_Profile_print(FILE *restrict stream, struct IO_Event_Profile *profile) { |
| 125 | + struct timespec total_duration = {}; |
| 126 | + IO_Event_Time_elapsed(&profile->start_time, &profile->stop_time, &total_duration); |
| 127 | + |
| 128 | + size_t skipped = 0; |
| 129 | + |
| 130 | + for (size_t i = 0; i < profile->events.limit; i += 1) { |
| 131 | + struct IO_Event_Profile_Event *event = profile->events.base[i]; |
| 132 | + |
| 133 | + if (event_flag_call_p(event->event_flag)) { |
| 134 | + struct timespec duration = {}; |
| 135 | + |
| 136 | + if (event->pair) { |
| 137 | + IO_Event_Time_elapsed(&event->time, &event->pair->time, &duration); |
| 138 | + |
| 139 | + // Skip events that are too short to be meaningful: |
| 140 | + if (IO_Event_Time_proportion(&duration, &total_duration) < IO_EVENT_PROFILE_PRINT_MINIMUM_PROPORTION) { |
| 141 | + skipped += 1; |
| 142 | + continue; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + for (size_t i = 0; i < event->nesting; i += 1) { |
| 147 | + fputc('\t', stream); |
| 148 | + } |
| 149 | + |
| 150 | + const char *name = rb_id2name(event->id); |
| 151 | + fprintf(stream, "\t%s:%d in '%s#%s' (" IO_EVENT_TIME_PRINTF_TIMESPEC "s)\n", event->path, event->line, RSTRING_PTR(rb_inspect(event->klass)), name, IO_EVENT_TIME_PRINTF_TIMESPEC_ARGUMENTS(duration)); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments