1# Introduction 2 3Internally in VM we have different tasks for two components `GC` and `JIT`. Both of this components can use more than one thread for solving one task or another. To optimize usage of hardware(cores)/OS(threads) resources we introduce TaskManager. The main goal of TaskManager is to use limited number of resources(OS threads) and schedule different types of tasks and achieve optimal metric values for `GC` and `JIT`. I.e. for example if we expect STW(stop-the-world pause) soon - we should increase priority for `GC` tasks. And vice-versa if we do not expect `GC` tasks anytime soon - we should increase priority for `JIT` tasks. 4 5For Hybrid mode (i.e. we can execute both static and dynamic code) TaskManager also should take into account the language context. I.e. if we are currently executing static code - the `GC` tasks for static instance have higher priority than `GC` tasks for dynamic instance, and vice versa. 6 7# Implementation details 8 9##Task 10 11It is some limited by time task, which has these properties: 12- priority of operation: `background` or `foreground`; 13- for which VM instance (`static` or `dynamic`) this task is executed; 14- which component is the owner of this task (`GC`, `JIT`, etc.) 15 16The time is required to finish the task should be limited by some reasonable value, to make the whole system with Task management optimal. 17 18 19##WorkerThread 20 21WorkerThread is the thread which executing tasks. Each WorkerThread has two queues - one for the foreground tasks and one for the background tasks. WorkerThread always try to execute task from the foreground queue first, if it is empty, then it try to execute task from the background queue. If no tasks in both queues - WorkerThread request `TaskManager` for more tasks. 22 23##TaskManager 24 25Is responsible for tasks "scheduling". I.e. each `WorkerThread` request `TaskManager` for new tasks. TaskManager creates array of tasks in proportion based on the current context(i.e. which language currently executed - static or dynamic, do we expect GC soon or not, etc). 26 27TaskManager make decision about scheduling based on some state of the system. The state of the system is provided by changing priority for TaskQueue(s). For example if we are currently expect a lot of GC because we have a lot of allocations, we will increase priority for TaskQueue for GC tasks. TaskManager choose more tasks from TaskQueues with higher priority. First tasks with `foreground` priority are selected in proportion based on TaskQueues priority, then if there are tasks with `foreground` priority in some TaskQueues they are selected without priority/proportion rules, if not - `background` tasks will be selected in proportion based on TasqQueues priority. 28 29##TaskQueue 30 31It is a queue for tasks of the same "kind" or "components" (for example "JIT") which is fullfilled by corresponding components of VM. 32 33Each component 34 35For managing worker threads we should provide machinery for: 361. Managing thread pool - `ThreadPool`, `WorkerThread` 372. Managing tasks - `TaskQueue`, `TaskExecutor` etc. 383. Change priority of worker threads `ThreadManager` 394. Distribute worker threads from `ThreadPool` between static and dynamic VM instances to optimize performance and power efficiency 40 41NB: the minimal entity in this machinery is WorkerThread, i.e. we shouldn't have one `TaskQueue` for tasks from different task producers (such as GC, JIT etc.) 42 43# ThreadPool 44 45This entity is provide functionality for: 461. Creation of thread pool with `N` worker threads 471. Getting available `WorkerThread` from the pool of available worker threads 481. Returning `WorkerThread` back to the pool of available worker threads 49 50# Thread Manager 51 52Thread Manager is responsible for: 531. Keeping thread pool 541. Distribute threads from pool between consumers in accordance with different policies 551. Provide machinery for changing priority of the threads managed by current Thread Manager 56 57## API 58 59TBD 60 61# WorkerThread 62 63The thread which is responsible for Task execution. 64 65Has these states: 661. Idle - waiting for task 672. Running - currently executing some task 683. ShuttingDown - currently termination is in progress 69