What is a safe Maximum Stack Size or How to measure use of stack?

后端 未结 6 1784
栀梦
栀梦 2020-12-15 11:12

I have an app with a number of worker threads, one for each core. On a modern 8 core machine, I have 8 of these threads. My app loads a lot of plugins, which also have their

6条回答
  •  旧巷少年郎
    2020-12-15 11:51

    For the sake of completeness, I am adding a version of the CommittedStackSize function provided in opc0de's answer for determining the amount of used stack that will work both for x86 32- and 64-bit versions of Windows (opc0de's function is for Win32 only).

    opc0de's function queries the address of the base of the stack and the lowest committed stack base from Window's Thread Information Block (TIB). There are two differences among x86 and x64:

    • TIB is pointed to by the FS segment register on Win32, but by the GS on Win64 (see here)
    • The absolute offsets of items in the structure differ (mostly because some items are pointers, i.e. 4 bytes and 8 bytes on Win32/64, respectively)

    Additionally note that there is a small difference in the BASM code, because on x64, abs is required to make the assembler use an absolute offset from the the segment register.

    Therefore, a version that will work on both Win32 and Win64 version looks like this:

    {$IFDEF MSWINDOWS}
    function CommittedStackSize: NativeUInt;
    //NB: Win32 uses FS, Win64 uses GS as base for Thread Information Block.
    asm
     {$IFDEF WIN32}
      mov eax, [fs:04h] // TIB: base of the stack
      mov edx, [fs:08h] // TIB: lowest committed stack page
      sub eax, edx      // compute difference in EAX (=Result)
     {$ENDIF}
     {$IFDEF WIN64}
      mov rax, abs [gs:08h] // TIB: base of the stack
      mov rdx, abs [gs:10h] // TIB: lowest committed stack page
      sub rax, rdx          // compute difference in RAX (=Result)
     {$ENDIF}
    {$ENDIF}
    end;
    

提交回复
热议问题