Context Switching
Synchronous system call
Problem
Synchronous system call blocks the thread until I/O is to be completed.
When synchronous system call occurs, OS thread is to be waiting state, and enqueue into waiting queue. This is not occupied by CPU.
This means synchronous system call reduces parallelism.
multiple goroutines can't be executed parallely since OS thread is blocked and waiting which has multiple logical processor including goroutine,
How to handle this problem in go scheduler?
Solution
Go scheduler gets another OS thread from thread pool or creates new one.
Process is shown below
- Synchronous system call occurs. (e.g. File Read)
- OS thread is blocked and transforming state from running to waiting.
Which is located into waiting queue. - Go scheduler gets another OS thread from thread pool cache or creates new one.
- And move the logical processor to new one except goroutine which makes I/O task.
- With new OS thread, the other goroutines are activated by go scheduler.
- In one side, IO completion, the I/O goroutine is returned to new OS thread.
- Old OS thread is returned to thread pool cache.
Asynchronous system call
Problem
Asynchronous system call occurs when file descriptor (FD as acronym) is used on network IO with non-blocking mode.
Async syscall doesn't block the OS thread but occurs error when the socket buffer is empty trying Read or buffer full trying Write.
In async mode, it should provide retrying mechanism network IO after error occurs.
How does go handle this problem?
Solution
Go uses NetPoller for handling asynchronous system call.
The NetPoller uses interface provided by OS (epoll kqueue, IOPC) to do polling status of FD.
- Asynchronous system call occurs in goroutine
- Go scheduler move the goroutine to NetPoller thread out of OS thread which is created.
- NetPoller do polling the status of FD
- If IO operation is ready, NetPoller could get notification from OS
- NetPoller will notify to the goroutine to retry I/O
- The goroutine would return to origin logical run queue when I/O operation completes.