Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCAMLRUNPARAM does not affect stack size

I would like to change my stack size to allow a project with many non-tail-recursive functions to run on larger data. To do so, I tried to set OCAMLRUNPARAM="l=xxx" for varying values of xxx (in the range 0 through 10G), but it did not have any effect. Is setting OCAMLRUNPARAM even the right approach?

In case it is relevant: The project I am interested in is built using OCamlMakefile, target native-code.

Here is a minimal example where simply a large list is created without tail recursion. To quickly check whether the setting of OCAMLRUNPARAM has an effect, I compiled the program stacktest.ml:

let rec create l =
  match l with
| 0 -> []
| _ -> "00"::(create (l-1))

let l = create (int_of_string (Sys.argv.(1)))
let _ = print_endline("List of size " ^ string_of_int (List.length l) ^ " created.")

using the command

ocamlbuild stacktest.native

and found out roughly at which length of the list a stack overflow occurs by (more or less) binary search with the following bash script foo.sh:

#!/bin/bash
export OCAMLRUNPARAM="l=$1"
increment=1000000
length=1
while [[ $increment > 0 ]] ; do
    while [[ $(./stacktest.native $length) ]]; do
        length=$(($length+$increment))
    done
    length=$(($length-$increment))
    increment=$(($increment/2))
    length=$(($length+$increment))
done
length=$(($length-$increment))
echo "Largest list without overflow: $length"
echo $OCAMLRUNPARAM

The results vary between runs of this script (and the intermediate results are not even consistent within one run, but let's ignore that for now), but they are similar no matter whether I call

bash foo.sh 1

or

bash foo.sh 1G

i.e. whether the stack size is set to 1 or 2^30 words.

like image 296
klimpergeist Avatar asked Jun 24 '15 13:06

klimpergeist


1 Answers

Changing the stack limit via OCAMLRUNPARAM works only for bytecode executables, that are run by the OCaml interpreter. A native program is handled by an operating system and executed directly on CPU. Thus, in order to change the stack limit, you need to use facilities, provided by your operating system.

For example, on Linux there is the ulimit command that handles many process parameters, including the stack limit. Add the following to your script

ulimit -s $1

And you will see that the result is changing.

like image 167
ivg Avatar answered Oct 16 '22 10:10

ivg