Skip to content

Commit

Permalink
Include the currently active GC in RUBY_DESCRIPTION
Browse files Browse the repository at this point in the history
This will add +MOD_GC to the version string and Ruby description when
Ruby is compiled with shared gc support.

When shared GC support is compiled in and a GC module has been loaded
using RUBY_GC_LIBRARY, the version string will include the name of
the currently active GC as reported by the rb_gc_active_gc_name function
in the form

+MOD_GC[gc_name]
  • Loading branch information
eightbitraptor committed Oct 10, 2024
1 parent 9b243ba commit 582a166
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19516,6 +19516,7 @@ version.$(OBJEXT): $(CCAN_DIR)/str/str.h
version.$(OBJEXT): $(hdrdir)/ruby.h
version.$(OBJEXT): $(hdrdir)/ruby/ruby.h
version.$(OBJEXT): $(hdrdir)/ruby/version.h
version.$(OBJEXT): $(top_srcdir)/gc/gc.h
version.$(OBJEXT): $(top_srcdir)/internal/array.h
version.$(OBJEXT): $(top_srcdir)/internal/basic_operators.h
version.$(OBJEXT): $(top_srcdir)/internal/cmdlineopt.h
Expand Down
13 changes: 12 additions & 1 deletion gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2761,7 +2761,18 @@ rb_gc_copy_attributes(VALUE dest, VALUE obj)
const char *
rb_gc_active_gc_name(void)
{
return rb_gc_impl_active_gc_name();
const char *gc_name = rb_gc_impl_active_gc_name();
if (strlen(gc_name) > RB_GC_MAX_NAME_LEN) {
char *truncated_gc_name = ruby_xmalloc(RB_GC_MAX_NAME_LEN + 1);

rb_warn("GC module %s has a name larger than %d chars, it will be truncated\n",
gc_name, RB_GC_MAX_NAME_LEN);

strncpy(truncated_gc_name, gc_name, RB_GC_MAX_NAME_LEN);
return (const char *)truncated_gc_name;
}
return gc_name;

}

// TODO: rearchitect this function to work for a generic GC
Expand Down
3 changes: 3 additions & 0 deletions gc/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ uint32_t rb_gc_get_shape(VALUE obj);
void rb_gc_set_shape(VALUE obj, uint32_t shape_id);
uint32_t rb_gc_rebuild_shape(VALUE obj, size_t heap_id);
size_t rb_obj_memsize_of(VALUE obj);
const char * rb_gc_active_gc_name(void);
RUBY_SYMBOL_EXPORT_END

#define RB_GC_MAX_NAME_LEN 20

void rb_ractor_finish_marking(void);

// -------------------Private section begin------------------------
Expand Down
12 changes: 11 additions & 1 deletion test/ruby/test_rubyoptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_debug
VERSION_PATTERN_WITH_RJIT =
case RUBY_ENGINE
when 'ruby'
/^ruby #{q[RUBY_VERSION]}(?:[p ]|dev|rc).*? \+RJIT (\+MN )?(\+PRISM )?\[#{q[RUBY_PLATFORM]}\]$/
/^ruby #{q[RUBY_VERSION]}(?:[p ]|dev|rc).*? \+RJIT (\+MN )?(\+PRISM )?(\+MOD_GC)?(\[\w+\]\s)?\[#{q[RUBY_PLATFORM]}\]$/
else
VERSION_PATTERN
end
Expand Down Expand Up @@ -307,6 +307,16 @@ def test_rjit_version
end
end

def test_enabled_gc
omit unless /linux|darwin/ =~ RUBY_PLATFORM

if RbConfig::CONFIG['shared_gc_dir'].length > 0
assert_match(/\+MOD_GC/, RUBY_DESCRIPTION)
else
assert_match(/^((?!\+MOD_GC(\[\w+\])?).)*$/, RUBY_DESCRIPTION)
end
end

def test_parser_flag
assert_in_out_err(%w(--parser=prism -e) + ["puts :hi"], "", %w(hi), [])
assert_in_out_err(%w(--parser=prism --dump=parsetree -e _=:hi), "", /"hi"/, [])
Expand Down
22 changes: 22 additions & 0 deletions version.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "vm_core.h"
#include "rjit.h"
#include "yjit.h"
#include "gc/gc.h"
#include <stdio.h>

#ifndef EXIT_SUCCESS
Expand Down Expand Up @@ -61,6 +62,11 @@ const int ruby_api_version[] = {
#else
#define YJIT_DESCRIPTION " +YJIT"
#endif
#if USE_SHARED_GC
#define GC_DESCRIPTION " +MOD_GC"
#else
#define GC_DESCRIPTION ""
#endif
const char ruby_version[] = RUBY_VERSION;
const char ruby_revision[] = RUBY_FULL_REVISION;
const char ruby_release_date[] = RUBY_RELEASE_DATE;
Expand Down Expand Up @@ -167,6 +173,14 @@ define_ruby_description(const char *const jit_opt)
+ rb_strlen_lit(YJIT_DESCRIPTION)
+ rb_strlen_lit(" +MN")
+ rb_strlen_lit(" +PRISM")
#if USE_SHARED_GC
+ rb_strlen_lit(GC_DESCRIPTION)
// Assume the active GC name can not be longer than 20 chars
// so that we don't have to use strlen and remove the static
// qualifier from desc.
+ RB_GC_MAX_NAME_LEN + 3
#endif

];

int n = ruby_description_opt_point;
Expand All @@ -176,6 +190,14 @@ define_ruby_description(const char *const jit_opt)
RUBY_ASSERT(n <= ruby_description_opt_point + (int)rb_strlen_lit(YJIT_DESCRIPTION));
if (ruby_mn_threads_enabled) append(" +MN");
if (rb_ruby_prism_p()) append(" +PRISM");
#if USE_SHARED_GC
append(GC_DESCRIPTION);
if (getenv("RUBY_GC_LIBRARY")) {
append("[");
append(rb_gc_active_gc_name());
append("]");
}
#endif
append(ruby_description + ruby_description_opt_point);
# undef append

Expand Down

0 comments on commit 582a166

Please sign in to comment.