From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list <caml-list@inria.fr>
Subject: Re: Random segfaults / out of memory
Date: Thu, 18 Mar 2010 11:56:40 +0100 [thread overview]
Message-ID: <874okd2907.fsf@frosties.localdomain> (raw)
In-Reply-To: <87hboehphv.fsf_-_@frosties.localdomain> (Goswin von Brederlow's message of "Wed, 17 Mar 2010 17:39:08 +0100")
Goswin von Brederlow <goswin-v-b@web.de> writes:
> Goswin von Brederlow <goswin-v-b@web.de> writes:
>
>>> On Wed, Mar 17, 2010 at 09:27:30AM +0100, Goswin von Brederlow wrote:
>>>> I want to rewrite the Digest module to expose a more lowlevel interface
>>>> to the md5 digest and add support to digest Bigarrays. I've patched the
>>>> respective files involved and it all looks alright but when I try to
>>>> build ocaml I get the following error:
>
> Ok, so I managed to bootstrap the compiler properly and build debian
> packages with my new Digest interface. But something is still wrong as I
> randomly get segfaults or
>
> <No room for growing heap
> Fatal error: out of memory.
>
> The more threads I use to compute digests in parallel the more likely
> the error becomes. But that might just be an issue with more allocations
> hapening and not a race condition between threads.
I finaly tracked down the issue with the help of Erkki Seppala.
First problem:
I had the function declared as "noalloc" but used CAMLparam2() in
it. That seems to cause random segfaults. I don't understand why but if
I remove the "noalloc" then it works.
Second problem:
When I remove the CAMLparam2() the finalizer is called too early:
CAMLprim value md5_update_bigarray(value context, value vb)
{
//CAMLparam2(context, vb);
struct helper *helper = (struct helper*)Data_custom_val(context);
struct MD5Context *ctx = helper->ctx;
fprintf(stderr, "update_bigarray: helper = %p, ctx = %p\n", helper, ctx);
struct caml_ba_array * b = Caml_ba_array_val(vb);
unsigned char *data = b->data;
uintnat len = caml_ba_byte_size(b);
caml_enter_blocking_section();
caml_MD5Update(ctx, data, len);
caml_leave_blocking_section();
//CAMLreturn(Val_unit);
return Val_unit;
}
let rec loop () =
Mutex.lock mutex;
if !num = 0
then Mutex.unlock mutex
else begin
decr num;
Mutex.unlock mutex;
let context = context () in
let () = update_bigarray context buf
in
loop ()
end
in
loop ()
This sometimes results in the following code flow:
context () <- allocates memory
update_bigarray context buf
caml_enter_blocking_section();
THREAD SWITCH
GC runs
context is finalized <- frees memory
THREAD SWITCH BACK
caml_MD5Update(ctx, data, len); <- writes to ctx which is freeed
Looks like ocamlopt really is so smart that is sees that context is
never used after the call to update_bigarray and removes it from the
root set before calling update_bigarray. It assumes the update_bigarray
will hold all its arguments alive itself, which is a valid assumption.
This is a tricky situation. The md5_update_bigarray() on its own is a
"noalloc" function. But due to the caml_enter_blocking_section() another
thread can alloc and trigger a GC run in parallel. So I guess that makes
the function actually not "noalloc".
Well, problem solved, lesson learned. :)
MfG
Goswin
next prev parent reply other threads:[~2010-03-18 10:58 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-17 8:27 Rewriting the Digest module causes linking errors Goswin von Brederlow
2010-03-17 8:39 ` [Caml-list] " Mark Shinwell
2010-03-17 9:53 ` Goswin von Brederlow
2010-03-17 16:39 ` Random segfaults / out of memory [Was: Re: [Caml-list] Rewriting the Digest module causes linking errors] Goswin von Brederlow
2010-03-18 10:56 ` Goswin von Brederlow [this message]
2010-03-30 7:14 ` [Caml-list] Re: Random segfaults / out of memory David Baelde
2010-03-30 15:57 ` Xavier Leroy
2010-03-30 16:19 ` Markus Mottl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874okd2907.fsf@frosties.localdomain \
--to=goswin-v-b@web.de \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox