Disclosed is a method for automatic stack depth adjustment in a computer operating system. Computer operating systems often need to prepare a special stack area for a thread of execution. If the prepared stack area is too small the thread may exhaust it and suffer a failure or lengthy recovery. If the prepared stack area is too large the thread consumes stack resource unnecessarily. This disclosure describes a method for use in a computer operating system to determine the amount of stack area to prepare for threads.
Automatic computer operating system stack depth adjustment
Computer operating systems often need to prepare a special stack area for a thread of execution. This may occur when a thread executes a system call to enter the kernel, when a thread disables interrupts, or when the context changes due to the arrival of an interrupt.
Often a balance must be reached in the size of the stack area that's prepared. When a thread disables interrupts, for example, the operating system may need to prepare a stack area that is composed of memory pages that are "pinned" (that is, the pages are known to be resident in the computer system and are not eligible to be paged out or re-assigned to another thread). On one hand, pinned pages are a limited resource, so assigning too many pinned pages for a thread's stack is wasteful. On the other hand, if the thread exhausts the stack area that has been prepared it may fail or may suffer a lengthy recovery process to prepare additional stack area, so assigning too few pinned pages for a thread's stack is risky.
Some operating systems prepare thread stack areas using sizes that are based on empirical experience from typical product installations or on tunable values set by a system administrator.
Components
To implement this method, an Operating System will maintain:
1) A well-defined subset of Operating System executable code that implements the Operating System's Stack Management function. Threads of execution will enter the Stack Management code when they require a stack to be prepared (perhaps, for example, when disabling interrupts or switching contexts). The address of the last instruction that the thread executed before entering the Stack Management code will be called the Entry Point Address.
2) A Stack Requirements table. This table consists of a set of entries with each entry containing an Entry Point Address and a corresponding Stack Size. The intent is that the table contains a list of the appropriate stack sizes to be prepared for threads entering the Stack Management code from various entry points.
3) A Current Default Stack Size.
4) A Stack Size Increment.
Method
1
To implement this method, an Operating System will perform several actions with specific triggers:
1) When the Operating System is initialized the Stack Requirements Table will be emptied and the Current Default Stack Size and Stack Size Increment will be set to their initial starting values.
2) When a thread of execution enters the Stack Management code, the Operating System will search for the thread's Entry Point Address in the Stack Requirements Table. If the Entry Point Address is found in the table then the Operating System will prepare a stack with the corresponding Stack Size from the table. If the Entry Point Address is not found in the table, then the Operating System will prepare a stack with the Current Default Stack Size.
3) When a thread exhausts the stack that had b...