rrtrace 0.1.0
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 +7 -0
- data/.github/workflows/release.yml +137 -0
- data/Cargo.lock +2477 -0
- data/Cargo.toml +23 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +16 -0
- data/ext/rrtrace/extconf.rb +16 -0
- data/ext/rrtrace/process_manager_posix.h +25 -0
- data/ext/rrtrace/process_manager_windows.h +40 -0
- data/ext/rrtrace/rrtrace.c +192 -0
- data/ext/rrtrace/rrtrace.h +8 -0
- data/ext/rrtrace/rrtrace_event.h +111 -0
- data/ext/rrtrace/rrtrace_event_ringbuffer.h +45 -0
- data/ext/rrtrace/rust_build_helper.rb +41 -0
- data/ext/rrtrace/shared_memory_posix.h +26 -0
- data/ext/rrtrace/shared_memory_windows.h +34 -0
- data/lib/rrtrace/version.rb +5 -0
- data/lib/rrtrace.rb +12 -0
- data/libexec/rrtrace +0 -0
- data/mise.toml +8 -0
- data/sig/rrtrace.rbs +4 -0
- data/src/main.rs +197 -0
- data/src/renderer/vertex_arena.rs +305 -0
- data/src/renderer.rs +751 -0
- data/src/ringbuffer.rs +134 -0
- data/src/shader.wgsl +115 -0
- data/src/shm_unix.rs +47 -0
- data/src/shm_windows.rs +44 -0
- data/src/trace_state.rs +275 -0
- metadata +86 -0
data/Cargo.toml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "rrtrace"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2024"
|
|
5
|
+
|
|
6
|
+
[profile.release]
|
|
7
|
+
overflow-checks = true
|
|
8
|
+
|
|
9
|
+
[dependencies]
|
|
10
|
+
bytemuck = { version = "1.24.0", features = ["derive"] }
|
|
11
|
+
crossbeam-queue = "0.3.12"
|
|
12
|
+
glam = "0.30.9"
|
|
13
|
+
pollster = "0.4.0"
|
|
14
|
+
rayon-core = "1.13.0"
|
|
15
|
+
smallvec = "1.15.1"
|
|
16
|
+
wgpu = "28.0.0"
|
|
17
|
+
winit = "0.30.12"
|
|
18
|
+
|
|
19
|
+
[target.'cfg(unix)'.dependencies]
|
|
20
|
+
libc = "0.2.178"
|
|
21
|
+
|
|
22
|
+
[target.'cfg(windows)'.dependencies]
|
|
23
|
+
windows-sys = { version = "0.61.2", features = ["Win32_System_Memory", "Win32_Foundation", "Win32_System_Diagnostics_Debug"] }
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 White-Green
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Rrtrace
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rrtrace`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rrtrace.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "standard/rake"
|
|
5
|
+
|
|
6
|
+
require "rake/extensiontask"
|
|
7
|
+
|
|
8
|
+
task build: :compile
|
|
9
|
+
|
|
10
|
+
GEMSPEC = Gem::Specification.load("rrtrace.gemspec")
|
|
11
|
+
|
|
12
|
+
Rake::ExtensionTask.new("rrtrace", GEMSPEC) do |ext|
|
|
13
|
+
ext.lib_dir = "lib/rrtrace"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
task default: %i[clobber compile standard]
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mkmf"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
|
|
6
|
+
require_relative "rust_build_helper"
|
|
7
|
+
|
|
8
|
+
# Makes all symbols private by default to avoid unintended conflict
|
|
9
|
+
# with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
|
|
10
|
+
# selectively, or entirely remove this flag.
|
|
11
|
+
append_cflags("-fvisibility=hidden")
|
|
12
|
+
|
|
13
|
+
root_dir = File.expand_path("../../", __dir__)
|
|
14
|
+
RustBuildHelper.build(root_dir)
|
|
15
|
+
|
|
16
|
+
create_makefile("rrtrace/rrtrace")
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#ifndef PROCESS_MANAGER_POSIX_H
|
|
2
|
+
#define PROCESS_MANAGER_POSIX_H
|
|
3
|
+
|
|
4
|
+
#include <sys/types.h>
|
|
5
|
+
#include <sys/wait.h>
|
|
6
|
+
#include <spawn.h>
|
|
7
|
+
|
|
8
|
+
typedef pid_t process_id;
|
|
9
|
+
|
|
10
|
+
extern char **environ;
|
|
11
|
+
|
|
12
|
+
static inline process_id spawn_process(const char *cmd, char * const* restrict argv) {
|
|
13
|
+
pid_t pid;
|
|
14
|
+
int status = posix_spawn(&pid, cmd, NULL, NULL, argv, environ);
|
|
15
|
+
if (status == 0) return pid;
|
|
16
|
+
else return 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static inline int is_process_running(process_id pid) {
|
|
20
|
+
int status;
|
|
21
|
+
waitpid(pid, &status, WNOHANG);
|
|
22
|
+
return !WIFEXITED(status);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#endif /* PROCESS_MANAGER_POSIX_H */
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#ifndef PROCESS_MANAGER_WIN_H
|
|
2
|
+
#define PROCESS_MANAGER_WIN_H
|
|
3
|
+
|
|
4
|
+
#include <windows.h>
|
|
5
|
+
#include <string.h>
|
|
6
|
+
|
|
7
|
+
typedef HANDLE process_id;
|
|
8
|
+
|
|
9
|
+
static inline process_id spawn_process(const char *cmd, char * const* argv) {
|
|
10
|
+
STARTUPINFOA si;
|
|
11
|
+
PROCESS_INFORMATION pi;
|
|
12
|
+
ZeroMemory(&si, sizeof(si));
|
|
13
|
+
si.cb = sizeof(si);
|
|
14
|
+
ZeroMemory(&pi, sizeof(pi));
|
|
15
|
+
|
|
16
|
+
char cmdline[4096] = {0};
|
|
17
|
+
for (int i = 0; argv[i] != NULL; i++) {
|
|
18
|
+
if (i > 0) strncat(cmdline, " ", sizeof(cmdline) - strlen(cmdline) - 1);
|
|
19
|
+
strncat(cmdline, "\"", sizeof(cmdline) - strlen(cmdline) - 1);
|
|
20
|
+
strncat(cmdline, argv[i], sizeof(cmdline) - strlen(cmdline) - 1);
|
|
21
|
+
strncat(cmdline, "\"", sizeof(cmdline) - strlen(cmdline) - 1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
CloseHandle(pi.hThread);
|
|
28
|
+
return pi.hProcess;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static inline int is_process_running(process_id pid) {
|
|
32
|
+
if (pid == NULL) return 0;
|
|
33
|
+
DWORD exitCode;
|
|
34
|
+
if (GetExitCodeProcess(pid, &exitCode)) {
|
|
35
|
+
return exitCode == STILL_ACTIVE;
|
|
36
|
+
}
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
#endif /* PROCESS_MANAGER_WIN_H */
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
#include "rrtrace.h"
|
|
2
|
+
#include "rrtrace_event_ringbuffer.h"
|
|
3
|
+
|
|
4
|
+
#ifdef _WIN32
|
|
5
|
+
#include "process_manager_windows.h"
|
|
6
|
+
#include "shared_memory_windows.h"
|
|
7
|
+
#else
|
|
8
|
+
#include "process_manager_posix.h"
|
|
9
|
+
#include "shared_memory_posix.h"
|
|
10
|
+
#endif
|
|
11
|
+
|
|
12
|
+
// #define RRTRACE_WRITE_DEBUG_LOG
|
|
13
|
+
|
|
14
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
15
|
+
#include<stdio.h>
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
typedef struct {
|
|
19
|
+
uint32_t thread_id;
|
|
20
|
+
} ThreadData;
|
|
21
|
+
|
|
22
|
+
typedef struct {
|
|
23
|
+
RRTraceEventRingBuffer *event_ringbuffer;
|
|
24
|
+
process_id visualizer_process_id;
|
|
25
|
+
rb_internal_thread_specific_key_t thread_data_key;
|
|
26
|
+
atomic_uint_fast32_t next_thread_id;
|
|
27
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
28
|
+
FILE *log;
|
|
29
|
+
#endif
|
|
30
|
+
} TraceContext;
|
|
31
|
+
|
|
32
|
+
static inline void push_event(TraceContext *context, RRTraceEvent event) {
|
|
33
|
+
if (context->event_ringbuffer == NULL) return;
|
|
34
|
+
while (!rrtrace_event_ringbuffer_push(context->event_ringbuffer, event)) {
|
|
35
|
+
if (!is_process_running(context->visualizer_process_id)) {
|
|
36
|
+
context->event_ringbuffer = NULL;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static uint32_t get_thread_id(TraceContext *context, VALUE thread) {
|
|
43
|
+
ThreadData *data = rb_internal_thread_specific_get(thread, context->thread_data_key);
|
|
44
|
+
if (data == NULL) {
|
|
45
|
+
data = malloc(sizeof(ThreadData));
|
|
46
|
+
data->thread_id = atomic_fetch_add_explicit(&context->next_thread_id, 1, memory_order_relaxed);
|
|
47
|
+
rb_internal_thread_specific_set(thread, context->thread_data_key, data);
|
|
48
|
+
}
|
|
49
|
+
return data->thread_id;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static void tracepoint_call_handler(VALUE tpval, void *data) {
|
|
53
|
+
TraceContext *context = (TraceContext *)data;
|
|
54
|
+
struct rb_trace_arg_struct *tracearg = rb_tracearg_from_tracepoint(tpval);
|
|
55
|
+
uint64_t method_id = RB_SYM2ID(rb_tracearg_method_id(tracearg));
|
|
56
|
+
push_event(context, event_call(method_id));
|
|
57
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
58
|
+
const char *method_name = rb_id2name(method_id);
|
|
59
|
+
fprintf(context->log, "CALL: %s\n", method_name);
|
|
60
|
+
fflush(context->log);
|
|
61
|
+
#endif
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static void tracepoint_return_handler(VALUE tpval, void *data) {
|
|
65
|
+
TraceContext *context = (TraceContext *)data;
|
|
66
|
+
struct rb_trace_arg_struct *tracearg = rb_tracearg_from_tracepoint(tpval);
|
|
67
|
+
uint64_t method_id = RB_SYM2ID(rb_tracearg_method_id(tracearg));
|
|
68
|
+
push_event(context, event_return(method_id));
|
|
69
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
70
|
+
const char *method_name = rb_id2name(method_id);
|
|
71
|
+
fprintf(context->log, "RETURN: %s\n", method_name);
|
|
72
|
+
fflush(context->log);
|
|
73
|
+
#endif
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static void tracepoint_gc_start_handler(VALUE tpval, void *data) {
|
|
77
|
+
TraceContext *context = (TraceContext *)data;
|
|
78
|
+
push_event(context, event_gc_start());
|
|
79
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
80
|
+
fprintf(context->log, "GC START\n");
|
|
81
|
+
fflush(context->log);
|
|
82
|
+
#endif
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static void tracepoint_gc_end_handler(VALUE tpval, void *data) {
|
|
86
|
+
TraceContext *context = (TraceContext *)data;
|
|
87
|
+
push_event(context, event_gc_end());
|
|
88
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
89
|
+
fprintf(context->log, "GC END\n");
|
|
90
|
+
fflush(context->log);
|
|
91
|
+
#endif
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static void thread_start_handler(rb_event_flag_t event, const rb_internal_thread_event_data_t *event_data, void *data) {
|
|
95
|
+
TraceContext *context = (TraceContext *)data;
|
|
96
|
+
push_event(context, event_thread_start(get_thread_id(context, event_data->thread)));
|
|
97
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
98
|
+
fprintf(context->log, "THREAD START\n");
|
|
99
|
+
fflush(context->log);
|
|
100
|
+
#endif
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static void thread_ready_handler(rb_event_flag_t event, const rb_internal_thread_event_data_t *event_data, void *data) {
|
|
104
|
+
TraceContext *context = (TraceContext *)data;
|
|
105
|
+
push_event(context, event_thread_ready(get_thread_id(context, event_data->thread)));
|
|
106
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
107
|
+
fprintf(context->log, "THREAD READY\n");
|
|
108
|
+
fflush(context->log);
|
|
109
|
+
#endif
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static void thread_suspended_handler(rb_event_flag_t event, const rb_internal_thread_event_data_t *event_data, void *data) {
|
|
113
|
+
TraceContext *context = (TraceContext *)data;
|
|
114
|
+
push_event(context, event_thread_suspended(get_thread_id(context, event_data->thread)));
|
|
115
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
116
|
+
fprintf(context->log, "THREAD SUSPENDED\n");
|
|
117
|
+
fflush(context->log);
|
|
118
|
+
#endif
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static void thread_resume_handler(rb_event_flag_t event, const rb_internal_thread_event_data_t *event_data, void *data) {
|
|
122
|
+
TraceContext *context = (TraceContext *)data;
|
|
123
|
+
push_event(context, event_thread_resume(get_thread_id(context, event_data->thread)));
|
|
124
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
125
|
+
fprintf(context->log, "THREAD RESUME\n");
|
|
126
|
+
fflush(context->log);
|
|
127
|
+
#endif
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static void thread_exit_handler(rb_event_flag_t event, const rb_internal_thread_event_data_t *event_data, void *data) {
|
|
131
|
+
TraceContext *context = (TraceContext *)data;
|
|
132
|
+
push_event(context, event_thread_exit(get_thread_id(context, event_data->thread)));
|
|
133
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
134
|
+
fprintf(context->log, "THREAD EXIT\n");
|
|
135
|
+
fflush(context->log);
|
|
136
|
+
#endif
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
RUBY_FUNC_EXPORTED void
|
|
140
|
+
Init_rrtrace(void)
|
|
141
|
+
{
|
|
142
|
+
TraceContext *context = malloc(sizeof(TraceContext));
|
|
143
|
+
context->thread_data_key = rb_internal_thread_specific_key_create();
|
|
144
|
+
atomic_init(&context->next_thread_id, 1);
|
|
145
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
146
|
+
context->log = fopen("rrtrace.log", "w");
|
|
147
|
+
#endif
|
|
148
|
+
|
|
149
|
+
char shm_name[64];
|
|
150
|
+
generate_shared_memory_name(shm_name, sizeof(shm_name));
|
|
151
|
+
RRTraceEventRingBuffer *ringbuffer = open_shared_memory(shm_name, sizeof(RRTraceEventRingBuffer));
|
|
152
|
+
if (ringbuffer == NULL) {
|
|
153
|
+
rb_raise(rb_eRuntimeError, "Failed to create shared memory for rrtrace");
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
rrtrace_event_ringbuffer_init(ringbuffer);
|
|
157
|
+
context->event_ringbuffer = ringbuffer;
|
|
158
|
+
|
|
159
|
+
VALUE mMyGem = rb_const_get(rb_cObject, rb_intern("Rrtrace"));
|
|
160
|
+
VALUE visualizer = rb_funcall(mMyGem, rb_intern("visualizer_path"), 0);
|
|
161
|
+
char *visualizer_path = rb_string_value_cstr(&visualizer);
|
|
162
|
+
#ifdef RRTRACE_WRITE_DEBUG_LOG
|
|
163
|
+
fprintf(context->log, "Visualizer: %s\n", visualizer_path);
|
|
164
|
+
fprintf(context->log, "Shared Memory: %s\n", shm_name);
|
|
165
|
+
#endif
|
|
166
|
+
process_id pid = spawn_process(visualizer_path, (char * const[]){visualizer_path, shm_name, NULL});
|
|
167
|
+
if (pid == 0) {
|
|
168
|
+
rb_raise(rb_eRuntimeError, "Failed to spawn visualizer process");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
context->visualizer_process_id = pid;
|
|
172
|
+
|
|
173
|
+
ThreadData *main_thread_data = malloc(sizeof(ThreadData));
|
|
174
|
+
main_thread_data->thread_id = 0;
|
|
175
|
+
VALUE thread = rb_thread_current();
|
|
176
|
+
rb_internal_thread_specific_set(thread, context->thread_data_key, main_thread_data);
|
|
177
|
+
|
|
178
|
+
VALUE trace_call = rb_tracepoint_new(RUBY_Qnil, RUBY_EVENT_CALL | RUBY_EVENT_C_CALL, tracepoint_call_handler, context);
|
|
179
|
+
VALUE trace_return = rb_tracepoint_new(RUBY_Qnil, RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN, tracepoint_return_handler, context);
|
|
180
|
+
VALUE trace_gc_start = rb_tracepoint_new(RUBY_Qnil, RUBY_INTERNAL_EVENT_GC_ENTER, tracepoint_gc_start_handler, context);
|
|
181
|
+
VALUE trace_gc_end = rb_tracepoint_new(RUBY_Qnil, RUBY_INTERNAL_EVENT_GC_EXIT, tracepoint_gc_end_handler, context);
|
|
182
|
+
rb_internal_thread_add_event_hook(thread_start_handler, RUBY_INTERNAL_THREAD_EVENT_STARTED, context);
|
|
183
|
+
rb_internal_thread_add_event_hook(thread_ready_handler, RUBY_INTERNAL_THREAD_EVENT_READY, context);
|
|
184
|
+
rb_internal_thread_add_event_hook(thread_suspended_handler, RUBY_INTERNAL_THREAD_EVENT_SUSPENDED, context);
|
|
185
|
+
rb_internal_thread_add_event_hook(thread_resume_handler, RUBY_INTERNAL_THREAD_EVENT_RESUMED, context);
|
|
186
|
+
rb_internal_thread_add_event_hook(thread_exit_handler, RUBY_INTERNAL_THREAD_EVENT_EXITED, context);
|
|
187
|
+
|
|
188
|
+
rb_tracepoint_enable(trace_call);
|
|
189
|
+
rb_tracepoint_enable(trace_return);
|
|
190
|
+
rb_tracepoint_enable(trace_gc_start);
|
|
191
|
+
rb_tracepoint_enable(trace_gc_end);
|
|
192
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#ifndef RRTRACE_EVENT_H
|
|
2
|
+
#define RRTRACE_EVENT_H
|
|
3
|
+
|
|
4
|
+
#include <time.h>
|
|
5
|
+
#include <stdatomic.h>
|
|
6
|
+
#include <stdint.h>
|
|
7
|
+
|
|
8
|
+
#define EVENT_TYPE_CALL 0x0000000000000000ull
|
|
9
|
+
#define EVENT_TYPE_RETURN 0x1000000000000000ull
|
|
10
|
+
#define EVENT_TYPE_GC_START 0x2000000000000000ull
|
|
11
|
+
#define EVENT_TYPE_GC_END 0x3000000000000000ull
|
|
12
|
+
#define EVENT_TYPE_THREAD_START 0x4000000000000000ull
|
|
13
|
+
#define EVENT_TYPE_THREAD_READY 0x5000000000000000ull
|
|
14
|
+
#define EVENT_TYPE_THREAD_SUSPENDED 0x6000000000000000ull
|
|
15
|
+
#define EVENT_TYPE_THREAD_RESUME 0x7000000000000000ull
|
|
16
|
+
#define EVENT_TYPE_THREAD_EXIT 0x8000000000000000ull
|
|
17
|
+
|
|
18
|
+
#define EVENT_TYPE_MASK 0xF000000000000000ull
|
|
19
|
+
|
|
20
|
+
typedef struct {
|
|
21
|
+
uint64_t timestamp_and_event_type;
|
|
22
|
+
uint64_t data;
|
|
23
|
+
} RRTraceEvent;
|
|
24
|
+
|
|
25
|
+
static inline uint64_t now(void) {
|
|
26
|
+
struct timespec ts;
|
|
27
|
+
timespec_get(&ts, TIME_UTC);
|
|
28
|
+
uint64_t timestamp = (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
|
|
29
|
+
|
|
30
|
+
static atomic_uint_fast64_t base_timestamp = 0;
|
|
31
|
+
uint64_t expected = 0;
|
|
32
|
+
if (atomic_compare_exchange_strong_explicit(&base_timestamp, &expected, timestamp, memory_order_relaxed, memory_order_relaxed)) {
|
|
33
|
+
return 0;
|
|
34
|
+
} else {
|
|
35
|
+
return timestamp - expected;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static inline RRTraceEvent event_call(uint64_t method_id) {
|
|
40
|
+
RRTraceEvent event;
|
|
41
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_CALL;
|
|
42
|
+
event.data = method_id;
|
|
43
|
+
return event;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static inline RRTraceEvent event_return(uint64_t method_id) {
|
|
47
|
+
RRTraceEvent event;
|
|
48
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_RETURN;
|
|
49
|
+
event.data = method_id;
|
|
50
|
+
return event;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static inline RRTraceEvent event_gc_start(void) {
|
|
54
|
+
RRTraceEvent event;
|
|
55
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_GC_START;
|
|
56
|
+
return event;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static inline RRTraceEvent event_gc_end(void) {
|
|
60
|
+
RRTraceEvent event;
|
|
61
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_GC_END;
|
|
62
|
+
return event;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static inline RRTraceEvent event_thread_start(uint32_t thread_id) {
|
|
66
|
+
RRTraceEvent event;
|
|
67
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_THREAD_START;
|
|
68
|
+
event.data = thread_id;
|
|
69
|
+
return event;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static inline RRTraceEvent event_thread_ready(uint32_t thread_id) {
|
|
73
|
+
RRTraceEvent event;
|
|
74
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_THREAD_READY;
|
|
75
|
+
event.data = thread_id;
|
|
76
|
+
return event;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static inline RRTraceEvent event_thread_suspended(uint32_t thread_id) {
|
|
80
|
+
RRTraceEvent event;
|
|
81
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_THREAD_SUSPENDED;
|
|
82
|
+
event.data = thread_id;
|
|
83
|
+
return event;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static inline RRTraceEvent event_thread_resume(uint32_t thread_id) {
|
|
87
|
+
RRTraceEvent event;
|
|
88
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_THREAD_RESUME;
|
|
89
|
+
event.data = thread_id;
|
|
90
|
+
return event;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static inline RRTraceEvent event_thread_exit(uint32_t thread_id) {
|
|
94
|
+
RRTraceEvent event;
|
|
95
|
+
event.timestamp_and_event_type = now() | EVENT_TYPE_THREAD_EXIT;
|
|
96
|
+
event.data = thread_id;
|
|
97
|
+
return event;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#undef EVENT_TYPE_CALL
|
|
101
|
+
#undef EVENT_TYPE_RETURN
|
|
102
|
+
#undef EVENT_TYPE_GC_START
|
|
103
|
+
#undef EVENT_TYPE_GC_END
|
|
104
|
+
#undef EVENT_TYPE_THREAD_START
|
|
105
|
+
#undef EVENT_TYPE_THREAD_READY
|
|
106
|
+
#undef EVENT_TYPE_THREAD_SUSPENDED
|
|
107
|
+
#undef EVENT_TYPE_THREAD_RESUME
|
|
108
|
+
#undef EVENT_TYPE_THREAD_EXIT
|
|
109
|
+
#undef EVENT_TYPE_MASK
|
|
110
|
+
|
|
111
|
+
#endif /* RRTRACE_EVENT_H */
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#ifndef RRTRACE_EVENT_RINGBUFFER_H
|
|
2
|
+
#define RRTRACE_EVENT_RINGBUFFER_H
|
|
3
|
+
|
|
4
|
+
#include "rrtrace_event.h"
|
|
5
|
+
|
|
6
|
+
#define SIZE 65536
|
|
7
|
+
#define MASK (SIZE - 1)
|
|
8
|
+
|
|
9
|
+
typedef struct {
|
|
10
|
+
RRTraceEvent buffer[SIZE];
|
|
11
|
+
alignas(64) struct {
|
|
12
|
+
atomic_uint_fast64_t write_index;
|
|
13
|
+
uint64_t read_index_cache;
|
|
14
|
+
} writer;
|
|
15
|
+
alignas(64) struct {
|
|
16
|
+
atomic_uint_fast64_t read_index;
|
|
17
|
+
uint64_t write_index_cache;
|
|
18
|
+
} reader;
|
|
19
|
+
} RRTraceEventRingBuffer;
|
|
20
|
+
|
|
21
|
+
static inline void rrtrace_event_ringbuffer_init(RRTraceEventRingBuffer *rb) {
|
|
22
|
+
atomic_store_explicit(&rb->writer.write_index, 0, memory_order_relaxed);
|
|
23
|
+
rb->writer.read_index_cache = 0;
|
|
24
|
+
atomic_store_explicit(&rb->reader.read_index, 0, memory_order_relaxed);
|
|
25
|
+
rb->reader.write_index_cache = 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static inline int rrtrace_event_ringbuffer_push(RRTraceEventRingBuffer *rb, RRTraceEvent event) {
|
|
29
|
+
if (rb == NULL) return 1;
|
|
30
|
+
uint64_t write_index = atomic_load_explicit(&rb->writer.write_index, memory_order_relaxed);
|
|
31
|
+
uint64_t read_index_cache = rb->writer.read_index_cache;
|
|
32
|
+
if (write_index - read_index_cache >= SIZE) {
|
|
33
|
+
read_index_cache = atomic_load_explicit(&rb->reader.read_index, memory_order_acquire);
|
|
34
|
+
rb->writer.read_index_cache = read_index_cache;
|
|
35
|
+
if (write_index - read_index_cache >= SIZE) return 0;
|
|
36
|
+
}
|
|
37
|
+
rb->buffer[write_index & MASK] = event;
|
|
38
|
+
atomic_store_explicit(&rb->writer.write_index, write_index + 1, memory_order_release);
|
|
39
|
+
return 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
#undef MASK
|
|
43
|
+
#undef SIZE
|
|
44
|
+
|
|
45
|
+
#endif /* RRTRACE_EVENT_RINGBUFFER_H */
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "rbconfig"
|
|
5
|
+
|
|
6
|
+
module RustBuildHelper
|
|
7
|
+
def self.build(root_dir)
|
|
8
|
+
puts "--- Building Rust visualizer ---"
|
|
9
|
+
cargo_toml = File.join(root_dir, "Cargo.toml")
|
|
10
|
+
|
|
11
|
+
unless File.exist?(cargo_toml)
|
|
12
|
+
puts "Cargo.toml not found at #{cargo_toml}. Skipping rust build (maybe pre-compiled gem?)"
|
|
13
|
+
return
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
unless system("cargo --version")
|
|
17
|
+
puts "Cargo not found. Please install Rust to build this gem from source."
|
|
18
|
+
exit 1
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Build rust binary
|
|
22
|
+
# We use --release for performance of the visualizer
|
|
23
|
+
sh_cmd = "cargo build --release --locked"
|
|
24
|
+
puts "Running: #{sh_cmd} in #{root_dir}"
|
|
25
|
+
unless system(sh_cmd, chdir: root_dir)
|
|
26
|
+
puts "Failed to build Rust visualizer."
|
|
27
|
+
exit 1
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Copy to libexec
|
|
31
|
+
exe = "rrtrace#{RbConfig::CONFIG["EXEEXT"]}"
|
|
32
|
+
src_exe = File.join(root_dir, "target", "release", exe)
|
|
33
|
+
dest_dir = File.join(root_dir, "libexec")
|
|
34
|
+
FileUtils.mkdir_p(dest_dir)
|
|
35
|
+
dest_exe = File.join(dest_dir, exe)
|
|
36
|
+
|
|
37
|
+
puts "Copying #{src_exe} to #{dest_exe}"
|
|
38
|
+
FileUtils.cp(src_exe, dest_exe)
|
|
39
|
+
FileUtils.chmod(0755, dest_exe) unless Gem.win_platform?
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#ifndef SHARED_MEMORY_POSIX_H
|
|
2
|
+
#define SHARED_MEMORY_POSIX_H
|
|
3
|
+
|
|
4
|
+
#include <sys/mman.h>
|
|
5
|
+
#include <sys/stat.h>
|
|
6
|
+
#include <fcntl.h>
|
|
7
|
+
#include <unistd.h>
|
|
8
|
+
#include <time.h>
|
|
9
|
+
#include <stdio.h>
|
|
10
|
+
|
|
11
|
+
static inline void generate_shared_memory_name(char *buffer, size_t size) {
|
|
12
|
+
struct timespec ts;
|
|
13
|
+
timespec_get(&ts, TIME_UTC);
|
|
14
|
+
snprintf(buffer, size, "/rrtrace_shm_%d_%d", getpid(), (int)ts.tv_nsec);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static inline void* open_shared_memory(const char *name, int size) {
|
|
18
|
+
int fd = shm_open(name, O_CREAT | O_RDWR, 0666);
|
|
19
|
+
if (fd == -1) return NULL;
|
|
20
|
+
if (ftruncate(fd, size) == -1) return NULL;
|
|
21
|
+
void *ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
22
|
+
if (ptr == MAP_FAILED) return NULL;
|
|
23
|
+
return ptr;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#endif /* SHARED_MEMORY_POSIX_H */
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#ifndef SHARED_MEMORY_WIN_H
|
|
2
|
+
#define SHARED_MEMORY_WIN_H
|
|
3
|
+
|
|
4
|
+
#include <windows.h>
|
|
5
|
+
#include <stdio.h>
|
|
6
|
+
|
|
7
|
+
static inline void generate_shared_memory_name(char *buffer, size_t size) {
|
|
8
|
+
snprintf(buffer, size, "Local\\rrtrace_shm_%lu_%lu", GetCurrentProcessId(), GetTickCount());
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static inline void* open_shared_memory(const char *name, int size) {
|
|
12
|
+
HANDLE hMapFile = CreateFileMappingA(
|
|
13
|
+
INVALID_HANDLE_VALUE,
|
|
14
|
+
NULL,
|
|
15
|
+
PAGE_READWRITE,
|
|
16
|
+
0,
|
|
17
|
+
size,
|
|
18
|
+
name);
|
|
19
|
+
|
|
20
|
+
if (hMapFile == NULL) {
|
|
21
|
+
return NULL;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
void *ptr = MapViewOfFile(
|
|
25
|
+
hMapFile,
|
|
26
|
+
FILE_MAP_ALL_ACCESS,
|
|
27
|
+
0,
|
|
28
|
+
0,
|
|
29
|
+
size);
|
|
30
|
+
|
|
31
|
+
return ptr;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#endif /* SHARED_MEMORY_WIN_H */
|
data/lib/rrtrace.rb
ADDED
data/libexec/rrtrace
ADDED
|
Binary file
|