Behind the Scenes of Go Scheduler
Tags: go, scheduler, linux, programming
Published: 2022-03-19 07:05:15

## what is the Go scheduler? Why do we even care?
Well, it is the _behind-the-scenes orchestrator of any GO program_. In order to explain this, Let's see a demo program and understand what are the jobs of the go scheduler and why we should care about it.

```Go
func main(){
   someTasks := getTask()
   
   for _,task := range someTasks{
       // processing each task in seperate goroutine
       go processTask(task)                         // creates goroutines.
   }
   
   // waits for the result Channel
   for res := range resultCh{                       // waits      
      log.Info(res)
   }
}

func processTask(t task){
   // fetching some meta-data about the task.
   go fetchMetaData(t)                              // creates goroutine
   
   UsefulInfo := complexComputation(t)

   // writing to a file.
   file,err := os.OpenFile()                        // blocking call
   ....
}
```

we have the main goroutine. It creates some processing goroutines. And then it waits on a channel. Each processTask goroutine creates another goroutine for fetching some meta-data and then it does some long-running complicated processing, and then it writes to the file system.<br>
Now the scheduler is what makes it so that 
- The goroutines created actually run, (and that they run concurrently, independently).
- It pauses and resumes them when they block on a channel or mutex operation.
- It's also the thing that coordinates network IO and blocking system calls like that file system call.
- It also runs runtime tasks like garbage collection.
                   

> _It can do it all for 10s and even hundreds of thousands of goroutine and its design and scheduling decisions about what to run when have a huge impact on the performance of any program._

<p align="center">
<img src="https://media.giphy.com/media/1hM5ljbq5Y9Udki5sU/giphy.gif" alt="why does go need a scheduler" width="30%" height="30%" />
</p>

### Why does Go need a scheduler?
So, Go uses goroutines, or what we call user-space threads. Now they're conceptually similar to kernel threads. But the big difference is that they're managed entirely by the GO runtime. Now, why would go choose to use these user-space threads? And it's because they can be implemented to be more lighter weight and faster. Goroutine, for example, has a smaller memory footprint than kernel threads. They're also a lot faster to create and destroy and context switch between. So that's nice. We want these lightweight user-space threads. There's only one problem. The operating system only knows how to schedule to put kernel threads on the hardware on your CPU cores. So how would these goroutines run? And this is where the go scheduler comes in. It puts goroutines on kernel threads that run on the CPU.

### What should be the basic properties of Go scheduler?
- _use a small number of kernel threads_
so our first point here would be these operating system threads (kernel threads), they're expensive, so we want to use a small number of them. 
- _should support high **concurrency**_
Our second point comes from the fact that GO is intended for highly performant systems. So we want to be able to support high concurrency. The applications should be able to create lots of goroutines and our schedulers should be able to keep up.
- _leverage **parallelism** i.e scale to N cores_
And a third point has to do with the fact that we live in an age of multicore. We want to leverage hardware parallelism. We want to scale to N cores. Basically, if your machine has N cores, it can run up to N things in parallel because it can run up to N threads in parallel.

![how scheduling works](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uls260fm4xgssu4x3tbb.png)

<p>
<b><em>This is how the scheduling works</em></b>, where when the goroutine makes a call that switches into the scheduler, under the hood the runtime switches your program into the scheduler. So it's at those events that create events and the channel receives. Now what the scheduler does of this time, it might actually continue running the same goroutine on the thread. This is what happens when you create a goroutine that goroutines can keep running. So in that picture, we see G<sub>main</sub> keeps running on the main thread. In the case of a channel receive, however, we want the goroutine to block so G<sub>main</sub> is switched out for G<sub>1</sub>.
</p>
So how do we go about multiplexing these goroutines onto kernel threads? you'll see we can tease it apart into two simpler questions. The question of when to create these kernel threads and a question of how to distribute goroutines across threads.

<p align="center">
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v150izasxgeqau8zn47r.png" alt="fifo run queue" />
</p>

### Run Queue
So our program creates these goroutines we need somewhere to know that these goroutines are ready to run, and they need to be scheduled on the kernel threads. So to do this we're going to use a heap-allocated struct called a `runqueue` which is a _FIFO Queue_. So when there's a create event, the New goroutine is added to the tail of the queue and when it's time to run a goroutine, you run it from the head of the queue. 

### Re-use threads
Well, let's see, we want threads, but we don't want too many thread creations and destructions. So what do we do? Well, we could just reuse threads. The way the scheme works is will create a thread when needed, so when there is a goroutine to run and all our threads are busy. But then when the thread frees up, say, the goroutine exits, rather than destroying it, would keep it around for reuse. And the way we do this is we will _park the thread_, which means we'll put it to sleep so it frees up the CPU and we'll track it so we can find it later when we need to wake it up. Now for the question of how to distribute goroutines across the threads we create, will have that runqueue and all threads will get their work from that runQueue.

![Thread reusing-Explaination](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dvjpfe8hiovppvzp7w0.gif)

So we have the main goroutine running on the main thread. Now Gmain creates G<sub>1</sub>, so we put G<sub>1</sub> on the runQueue. Now we do our checks. There's work to do. All threads are busy, so we're going to start T<sub>1</sub>. Now T<sub>1</sub> comes along and it pops off G<sub>1</sub> from the run Queue and it runs it. Now G<sub>1</sub> exits. Rather than destroying T<sub>1</sub>, we're going to park it and put it on that idle threads list. G<sub>2</sub> is created by G<sub>main</sub> and this time we're going to do the same thing added to the run queue. But. There is an idle threat, so we're just going to reuse that thread and we're going to have T<sub>1</sub> run G<sub>2</sub> this time.

<p align="center">
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rgr8ixidqz0pri3m4ng.png" alt="contention" />
</p>

### Unbounded number of threads - contention
So at this point, we have a scheme. There's concurrency, it provides parallelism, and it nicely reduces thread creations. Does anybody see any problems with this scheme? Well, for one, we have many threads mutating a single data structure, so we need a lock, and locks are inherently bad for scalability. What they do is they effectively serialize goroutines scheduling. We can only schedule 1 goroutine at a time. But there's a bigger problem with this scheme. This is what happens if G<sub>main</sub> creates 10,000 long-running goroutines in quick succession. We're going to create 10,000 threads, so in this scheme, we can still have an unbounded number of threads accessing the runQueue and an unbounded number of threads accessing a shared data structure with the lock results in _contention_ which means it is not scalable.
### Limiting the number of threads that can access the runQueue
The problem is these unbounded threads access the run queue. So hey, why don't we just? Limits the number of threads that can access the runQueue. This scheme is very similar to the scheme we just discussed, except there's one extra check when we create a thread, before we do that, we check to see if the number of goroutines running threads is already equal to or greater than the limit.<br>
Now, an important thing to note here is that this _limit applies to threads accessing the runQueue only_ because we're trying to solve the contention problems. So threads running goroutines and it does not apply to threads blocked in system calls. Now as before, we're going to keep threads around for reuse and we're going to use the runQueue to give them work.

![contention solution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72bk4zlhr5tdlcn54gdf.gif)

Setting the limit to the number of CPU cores and that way we get all the parallelism we want. So we're going to limit the number of threads that can be running goroutines at any time to 2. Now we have 2 goroutine running threads already and now G<sub>main</sub> is going to create another goroutine. Now we're going to add G<sub>2</sub> to the runQueue. We're going to do our checks, but this time there's an extra check, and wait a minute. We already have 2 goroutine running threads, so this time we're not going to create another thread. Well, what happens to G<sub>2</sub> then just sitting in the runQueue? Well, don't worry. At a future scheduling point, like when G<sub>1</sub> blocks on the channel, G<sub>2</sub> will be scheduled.
### Avoid shared runQueue
What happens when the number of cores increases? Say you upgrade your hardware and you suddenly have 124 cores. Well, as the number of cores increases. That limit goes up, which means we can have more threads running goroutines and more threads accessing the runQueue and before you know it, contention again.<br>
This idea of setting the number of goroutine running threads to the number of CPU cores is still a good idea, right? It means we're maximally leveraging parallelism. We literally cannot do better because we're limited by the hardware. So we'll hold onto it. The problem, in this case, was this shared runQueue.<br>Let's just use distributed runQueues. The way this scheme works is on an N-core machine we will have N runQueue and we're going to use one runQueue per thread. So a thread claims a runQueue to run goroutines, which means it inserts and removes goroutines from that runQueue only for the time it's associated with it. Now, as before, we're going to reuse threads.

![distributed RunQueue with work stealing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qavpsm4oyac9to4ghdz8.gif)

### Use distributed RunQueue
<p>
In this case we have two runQueue because we're running on 2 core, we have runQueue<sub>a</sub> and runQueue<sub>b</sub>. The main thread is currently associated with runQueue<sub>a</sub>. Now the main thread creates a goroutine, G<sub>1</sub> it's added to its runQueue. As before, we do our checks. We can have two goroutines running threads and we have only one, so we're going to start a new thread. We're going to start T<sub>1</sub>. The T<sub>1</sub> is going to claim the  runQueue<sub>b</sub> and wait a minute. Its runQueue is empty. There is work in the system, but it's in the other runQueue. So the idea here, it's called <em>work stealing</em>, if the local runQueues is empty, the thread is going to pick another random runQueue and steal half its workload. Now the nice thing about work-stealing is it organically <em>balances work across threads</em>, right? Because the free threads will steal work from the overloaded threads.
</p>
OK, Let's apply work-stealing to solve our problem. So we're in this situation where T<sub>1</sub> has an empty local runQueue, so it's just going to steal G<sub>1</sub> and then it can run it. So far it scales nicely with the number of CPU cores because we have effectively per Core runQueues and threads don't contend because these runQueues are independent. The work across threads is balanced too, because of work-stealing.

![blocking syscall](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hirtccxsusfx9p7qfg6c.gif)

### Goroutine starvation
So once we can continue and finally get to run G<sub>1</sub>. Now, this is the part of the program we care about. G<sub>1</sub> is going to create G<sub>3</sub> and then it's going to do that blocking system call. OK. So T<sub>1</sub> runs G<sub>1</sub> which creates G<sub>3</sub> which gets put into its runQueue. Now we're not going to start a thread this time because we already have two goroutines running threads on our 2 core.
Now G<sub>1</sub> does that blocking system call. Well, when G<sub>1</sub> does that syscall it is going to block, but T<sub>1</sub> is also going to block.<br> 
The problem, <em>we have a local run queue with work, but its thread is blocked.</em> Well, all we need really is a mechanism to transfer the blocked threads runQueue to another thread. This mechanism can simply be a background thread, <b>a monitor</b>. That takes the runQueue and gives it away.
### Now, why do we need a background thread?
Why can't the thread itself before entering the system call just give its runQueue away? That would be simpler and the reason is that we don't know ahead of time. For how long a system call is going to block, right we don't want the thread to give away its runQueue and finish up the system called really quickly and then be out of work. And so we use this background thread.<br>
Who do we give away the runQueue to? If we have parked threads, we can wake them up and give them the runQueue and if we don't have parked threads, we can start a thread.<br>Now, wait a minute. What about our limits to the number of goroutine running threads? Well, remember that's exactly what it is. It is a limit on the number of goroutines running threads. The original thread is now blocked in a system call, so it's effectively freed up a slot for another goroutine running thread. This mechanism is called <em>handoff</em> and it's nice because it prevents goroutine starvation. It means G<sub>3</sub> will get to run.

![handoff](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xm81d8tpu6b4g7j2m3aj.gif)

### Handoff
OK, T<sub>1</sub> is blocked and G<sub>3</sub> is sitting in the runQueue. We're going to hand it off. So that background thread does its checks, it finds the runQueue with work, it sees the thread is blocked and blocked for a little bit of time, so it starts the thread T<sub>2</sub> and then the monitor is going to Hand, the runQueue to T<sub>2</sub> and then it can run G<sub>3</sub>.<br> We have finally reached the end of our program and even better, we have a scheme that seems to work. It scales nicely, were solved goroutines starvation. We've solved unbalanced workloads.
### Non-cooperative CPU-bound computation
Now everything we've talked about so far, all the scheduling works as long as the program makes those calls to call into the scheduler right? It happens under the hood, but the program does that goroutine creation, does that channel blocking its cooperative? What happens if we have a good team that does not cooperate, that does not make any of those calls like say, a complicated algorithm is a CPU-bound computation that runs for a long long time and never yields the CPU? Well, if we never call into the scheduler, nothing else is going to get scheduled. So such a CPU hog can starve the runQueue.
### Cooperative preemption
So what we need is a mechanism to preempt these goroutines, so they're running for too long. We need a mechanism to force them to yield to the scheduler. So the Go scheduler implements preemption. This is a form of preemption called <em>cooperative preemption</em> and the way it does this is, there is a background monitor thread called the <b>systmon</b> which stands for system monitor, and the systmon and basically detects long-running goroutines, so that has been running for 10 milliseconds and it un-schedules them when possible. Now, where would it put these preempted goroutines? We don't want to put them back on the runQueue. It would be unfair to the other goroutines that it effectively starved. So where do we put them? The Go scheduler puts them on a global runQueue that's right the Go scheduler has a global runQueue in addition to those distributed per core runQueues. It uses this as a lower priority queue of sorts, so threads check it less frequently than their local runQueue, so contention is not a problem in this case.
### Closure
OK, no more surprises. I promised with that, we now have a full understanding of the main ideas, both big and sneaky, behind the Go scheduler. We started out with a list of goals. How did we do with our goals? Use a small number of kernel threads. We can support high concurrency and we can leverage parallelism. We scale to N-cores and this falls out of those three ideas that we discussed. <br>
Let's move on to the harder questions. <b>What are the limitations of the scheduler?</b><br>Well, for one, there is no notion of goroutine's priority. It uses a first in, first out runQueue vs Linux scheduler which uses a priority queue. Now the cost-benefit tradeoff is doing this might not actually make sense for 
go programs. <br>The second limitation is there's no strong preemption, so there is no strong fairness in latency guarantees. It's entirely possible for a goroutine in certain cases to bring the inspire system to slow down in a fault. <br>And finally, the third limitation that I want to touch upon today is the scheduler is not aware of the actual hardware topology, so there's no real guaranteed locality between the data and the Goroutine computation, and with that we have come to an end and thank you for reading.
<br>
_Gopher Artwork credit_
[Maria Letta](https://github.com/MariaLetta/free-gophers-pack)
[Ashley Mcnamara](https://github.com/ashleymcnamara/gophers)

_Reference:_
[Go's Work Stealing Scheduler (sourcegraph.com)](https://about.sourcegraph.com/go/gos-work-stealing-scheduler/)
[Go scheduler talk by Kavya Joshi](https://youtu.be/YHRO5WQGh0k)
[Scalable Go scheduler talk by Dmitry Vyukov](https://www.youtube.com/watch?v=-K11rY57K7k)