Forum: Ruby-core Ruby 1.9.1 embedding and GC

E18b659ca76a4e6ed9ef9e7054473d65?d=identicon&s=25 Roman Shterenzon (Guest)
on 2009-10-21 14:44
(Received via mailing list)
Good day,

I'm trying to embed a Ruby 1.9.1 interpreter, and I'd like to disable to
GC, and only run it manually (say, every 1000 requests).
The problem is, that somehow it's not disabled, and after an attempt to
allocate memory the interpreter exits.
I tried to understand the code, but I'm a bit lost. What I'm doing wrong
here?

The ruby version is:
ruby 1.9.1p281 (2009-08-09 revision 24476) [i386-darwin10.0.0]

My code:
-----
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
ruby_script(service_file);
rb_define_module("Gem");
rb_gc_disable();
Init_prelude();
int error;
rb_protect((VALUE (*)(VALUE))rb_require, (VALUE)service_file, &error);
-----

The gdb backtrace (line 193 in ruby_handler.c is Array allocation):
(gdb) bt
#0  0x00007fff84fb5183 in exit ()
#1  0x0000000100233b0b in garbage_collect_with_gvl
(objspace=0x10036d9c0) at gc.c:602
#2  0x0000000100233b71 in vm_xmalloc (objspace=0x10036d9c0, size=80) at
gc.c:623
#3  0x0000000100204f3a in ary_new (klass=<value temporarily unavailable,
due to optimizations>, len=10) at array.c:301
#4  0x000000010006d03b in ruby_init_request_data (req=0x1028ff800) at
ruby_handler.c:193
...
(gdb) up
#1  0x0000000100233b0b in garbage_collect_with_gvl
(objspace=0x10036d9c0) at gc.c:602
602    exit(EXIT_FAILURE);
(gdb) list
597    return (int)rb_thread_call_with_gvl(gc_with_gvl, (void
*)objspace);
598}
599else {
600    /* no ruby thread */
601    fprintf(stderr, "[FATAL] failed to allocate memory\n");
602    exit(EXIT_FAILURE);
603}
604    }
605}
606
(gdb) up
#2  0x0000000100233b71 in vm_xmalloc (objspace=0x10036d9c0, size=80) at
gc.c:623
623garbage_collect_with_gvl(objspace);
(gdb) list
618    size += sizeof(size_t);
619#endif
620
621    if ((ruby_gc_stress && !ruby_disable_gc_stress) ||
622(malloc_increase+size) > malloc_limit) {
623garbage_collect_with_gvl(objspace);
624    }
625    mem = malloc(size);
626    if (!mem) {
627if (garbage_collect_with_gvl(objspace)) {
(gdb)


Another question, which might be related, is that if I don't disable GC,
then I see this error:
[BUG] object allocation during garbage collection phase

And I assumed, that if I control the allocation and GC, then I can avoid
triggering this bug, but perhaps there's a better workaround or
solution.

Thank you very much in advance,

--Roman
9887130ab70f6e66b50f2c5e19272c67?d=identicon&s=25 Roger Pack (Guest)
on 2009-10-23 23:13
(Received via mailing list)
> My code:
> -----
> RUBY_INIT_STACK;
> ruby_init();
> ruby_init_loadpath();
> ruby_script(service_file);
> rb_define_module("Gem");
> rb_gc_disable();

I'd add an output to garbage_collect so you can see if it *is* GC'ing
[which apparently it is?]  if so then try to see why.
GL.
-r
E18b659ca76a4e6ed9ef9e7054473d65?d=identicon&s=25 Roman Shterenzon (Guest)
on 2009-10-24 14:38
(Received via mailing list)
It obviously does.
What happens, is the following, when some object (in my case an array)
is to be allocated, the vm_xmalloc is called.
(The GC should be off, as I called gc_disable, remember).
Then, in vm_xmalloc there a condition (malloc_increase+size) >
malloc_limit) which holds true, and then
garbage_collect_with_gvl(objspace) is called (though the GC was
disabled.. the check for this is postponed).
The garbage_collect_with_gvl check if it's a ruby native thread
(ruby_native_thread_p()) and if it's not - bails out with exit. I
believe that this is a reasonable thing, when the main program is the
ruby interpreter, but it's probably wrong for an embedded interpreter.
So either I need to run my code in "native ruby thread", but I didn't
quite understand yet from reading the code how to do that and if it's
feasible. On the other hand, if I change the vm_xmalloc to check the
dont_gc earlier, there still will be a problem if the GC is NOT
disabled, and it'll follow the same path.

--Roman



----- Original Message ----
From: Roger Pack <rogerdpack@gmail.com>
To: ruby-core@ruby-lang.org
Sent: Fri, October 23, 2009 11:12:49 PM
Subject: [ruby-core:26261] Re: Ruby 1.9.1 embedding and GC

> My code:
> -----
> RUBY_INIT_STACK;
> ruby_init();
> ruby_init_loadpath();
> ruby_script(service_file);
> rb_define_module("Gem");
> rb_gc_disable();

I'd add an output to garbage_collect so you can see if it *is* GC'ing
[which apparently it is?]  if so then try to see why.
GL.
-r
This topic is locked and can not be replied to.