PhotonLibOS icon indicating copy to clipboard operation
PhotonLibOS copied to clipboard

Reducing default stack size from 8 MB to 8 KB - is it possible?

Open medvednikov opened this issue 1 year ago • 3 comments

Hi,

I reduced the default stack size from 8 MB to 8 KB. Surprisingly RAM usage remained the same in our test program which launches thousands of coroutines. Also they take a lot more time.

Is there a certain minimum it can't be lower than?

Go uses 2-4 KB, I thought it'd be possible with photon as well.

Thanks.

medvednikov avatar May 21 '24 04:05 medvednikov

This is the change I did:

-g.writeln('photon_thread_create((void*)${wrapper_fn_name}, &${arg_tmp_var});')
+g.writeln('photon_thread_create((void*)${wrapper_fn_name}, &${arg_tmp_var}, 8 * 1024);')

medvednikov avatar May 21 '24 04:05 medvednikov

All modern OS kernels will delay the real allocation of the physical memory pages until they are actually accessed. So the memory will not add 8MB immediately after every malloc.

Furthermore, we recently add mprotect to prevent out-of-bound memory access on small stack. https://github.com/alibaba/PhotonLibOS/pull/464/files#diff-f0db3c6fae5cc5fe519ada39d68af66b8c177096a3ee36c0d92893be964cf34c

beef9999 avatar May 21 '24 06:05 beef9999

The stack size is able to be set, parameters when creating threads. like photon::threada_create(entry, arg, STACK_SIZE), that feature has already provided. Still, it can hardly be to smaller.

In 0.8 version, thread handle struct and mprotect page are located inside allocated stack space, those stuff asked at least two PAGE_SIZE as kernel provides (Yeah we have meet kernels provides 16KB pages in production environment). That means you might set at least three pages for stack.

Coldwings avatar Aug 29 '24 02:08 Coldwings