5 Essentials for Go Development

  1. The project structure [https://github.com/golang-standards/project-layout]
  2. Coding standard [gofmt and golint]
  3. Coding IDE [vim + vim-go]. My favorite!
  4. Go Packages Fundamentals [https://medium.com/rungo/everything-you-need-to-know-about-packages-in-go-b8bac62b74cc]
Advertisements

Go Runtime Scheduler Design Internals

Concurrency is one of the most exciting features of Go language. A single threaded program runs serially, but if you have tasks that can run concurrently, you create threads for such tasks. Threads execute independently and progress concurrently. Go supports creation of thousands of threads in an application!! How is it possible? It’s Go’s runtime. Go programs are compiled and the executable is self-contained (runtime is linked with the binary).

Let’s understand the design motivation of the Go runtime. The runtime must follow the resource constraints. The system must run multiple threads. CPU core can run only one thread a time and if there are more threads than available cores, threads are paused/resumes (context switched). During a context switch, thread execution state is preserved and another thread is loaded. Creation of a thread requires resources and hence there is a limit.

Under the constraints, Go runtime maximise CPU utilisation, minimises latencies and memory footprint.

Go provides concurrency with language primitives of Goroutines and channels. Using Goroutines applications can grow dynamically (forking new Goroutines). Channels are internal to Go runtime and system has no knowledge of channels.

Let’s understand Goroutines in detail. It is essentially a light weight thread, exists on the user space. Goroutines are frugal with resources. Unlike system threads, a Goroutine has a small stack and it grows as needed. It is one of the reasons that Go can support creation of thousands of Goroutines.

So how are thousands of Goroutines managed by the runtime? Instead of delegating the responsibility to the system scheduler, Go uses its own scheduler. It is coded in Go itself.

How does Go scheduler work? Let’s understand thread model used in applications. An application can use system thread, managed by the OS. These threads can make system call and access system resources (e.g. CPU). However, these threads are expensive as they consume system resources such as signal masks, PID, cgroup etc. Context switch is expensive as they trap in to kernel. In contrast, user threads are created and managed by the application, consume less resources and context switch is fast because it does not go through the kernel. A user thread needs a system thread to execute the code on CPU or accessing any other system resources.  

Now the next decision is to maintain a ratio of user threads and system threads. The first model is using N user threads and one system threads. It gives fast context switching but can’t use multiple cores (if available). Also if a user thread is blocked hence there is no available system thread, other user threads will wait. Other scheme is to have 1-on-1 mapping of user threads to system threads. It provides good CPU utilisation but context switching is slow. Then third option is to create many-to-many mapping.

Go takes the third option. Goroutines are distributed on a set of system/OS threads. There are three major entities in the Go scheduler: A set of machines (M), Goroutines (G) and processors (P). There are minor entities such as global and local run queue and thread cache.

Let’s understand “MGP”: Machines are a representation for OS threads. An OS thread is part of a pool of worker threads. On Linux, it is a standard POSIX thread. A M runs a set of Gs. A G Goroutine represents a user space Goroutine; it has its own IP, stack and blocking info. The P represents a logical entity for available processors or a scheduling context. Every worker (M) needs a P to run G. The number of P is pre-decided (GOMAXPROCS) and fixed during the run. 

A M runs a set of Gs. A G Goroutine represents a user space Goroutine; it has its own IP, stack and blocking info. The P represents a logical entity for available processors or a scheduling context. Every worker (M) needs a P to run G. The number of P is pre-decided (GOMAXPROCS) and fixed during the run. 

In this diagram, we have a setup with 2 Processors, defined by GOMAXPROCS. There are two worker machine (M). Each M has a local run queue. As new Goroutines are created and is runnable, they are added to the local run queue. If local run queue is full, new G’s are added to the global run queue. Idle G are kept in an idle queue.

What happens when a G makes a system call? The scheduler would know that G is blocked and hence its M is also blocked. P is not utilised, and can be used by some other M.

The scheduler takes back the P, and creates a new M, and assign to the new worker machine (M). The runnable queue is also moved to the new worker. When M’ comes back, it would try to find an idle P. If not possible, it would move the G’ to the global queue and park itself in Thread cache. The scheduler makes sure there are enough threads to run all contexts. There can be more than M even for a P=1, because a worker might get stuck in a syscall.

A scenario can occur in which a P is idle as its run queue is exhausted. In such was it would try to pick G’s from global queue. If global queue is also empty, what to do?  There are two major scheduling paradigms for distributing work. The first is work-sharing in which G would get distributed to other P’s. In contrast, work-stealing scheduler an idle processor would steal G’s from other P’s run queue. Till a P is busy, there is no G movement. So, an idle P would steal about half of Gs from another P. Work Stealing has better resource utilisation and lower migration of Gs.

In this diagram, we have an idle P, local and global run queue are empty, so it will randomly pick a P and steal half of Gs.

Go has a sophisticated scheduler, it offers massive concurrency and intelligent scheduling and always tries to achieve maximum utilisation, minimum latencies.

Golang Essentials for C & C++ Developers: Part III

The Empty Interface

  • An empty interface is used when type of a variable is unknown.
  • There are scenarios such as printf function, raw buffers where you would not know the type.

Use case

// Definition of a map is --> var map[key]value
// If we do not know the type of the value and want
// to use the map for generic types, then an empty
// interface helps

// scenario 1: map value type is unknown
	var mymap map[int] interface{}
	mymap = make(map[int]interface{})
	mymap[10] = "python"
	mymap[21] = 22
	
	fmt.Println(mymap[10])
	fmt.Println(mymap[21])
// scenario 2: map key and value types are unknown
	var mymap1 map[interface{}] interface{}
	mymap1 = make(map[interface{}]interface{})
	mymap1[10] = "python"
	mymap1[21] = 22
	mymap1["test"] = 10
	fmt.Println(mymap1[10])
	fmt.Println(mymap1["test"])

An array with interface

var []arr interface{}{}

Written with StackEdit.

Golang Essentials for C & C++ Developers: Part II

Function with return value name

func f() (r int) {
   r = 1
   return
}

Return Multiple Values

func f() (int, string) {
    return 10, "ten"
}

Variadic Functions

package main

import "fmt"

func varFunc(a int, b ...string) (int) {
        for v:= 0; v <= 3; v++ {
        fmt.Println(b[v])
    }
    return a
}

func main() {
        strs := []string {"abc", "def", "ghi", "jkl"}
        a := varFunc(10, strs...)
        fmt.Println(a)
}

Closure

  • Function defined inside a function

defer statement

 func main() {
        defer func(a string) {
                fmt.Println(a)
        } ("bye bye")
}

Written with StackEdit.

Golang Essentials for C & C++ Developers: Part I

I have been a C/C++ developer for more than a decade and now getting a chance to work in Go. There are many past references of C, C++, and Python to learn the language quickly. but there are some new syntax that appear unfamiliar. We share such language specific nuances, used widely in production quality code, to help us browsing Golang code.

Methods

  • A method is similar to a member function of a class (user defined type).
package main
import "fmt"

type Node struct {
    val int64
}

// Returns a pointer of Node type
//
func (n *Node) addNode() *Node {
    newNode := new(Node)
    newNode.val = 10

    return newNode
}

func main() {
        n := &Node {10}
        m := n.addNode()

        fmt.Print(m)
}

A sample program of a linked list in Go with comments explaining the code.

package main

import "fmt"

type Node struct {
        prev *Node
        next *Node

        // key can assume any standard type
        key  interface{}
}

type List struct {
        head *Node
        tail *Node
}
// A method defined for type List, returns nothing
// and accepts a key
//
func (L *List) Insert(key interface{}) {
        // Create a new node with 'next' pointing
        // to Head. The 'prev' is by default zero.
        //
        list := &Node{
                next: L.head,
                prev: nil,
                key:  key,
        }

        // Attaching the new node at the head by
        // updating prev of current head to the
        // newly created node.
        //
        if L.head != nil {
                L.head.prev = list
        }

        // List is the first node so prev is nil
        list.prev = nil

        // Update the head
        L.head = list

        l := L.head

        // Now find the last node and update its tail link
        // to the newly added node.
        //
        for l.next != nil {
                l = l.next
        }

        L.tail = l

        fmt.Printf("head->%d\n", L.head.key)
        fmt.Printf("tail->%d\n", L.tail.key)
}
// Another method defined for type List
//
func (l *List) Display() {
        list := l.head

        for list != nil {
                fmt.Printf("%+v->", list.key)
                list = list.next
        }
        fmt.Println()
}

// A function defined for type List
//
func Display(l *List) {
        list := l.head

        for list != nil {
                fmt.Printf("%+v->", list.key)
                list = list.next
        }
        fmt.Println()
}
func main() {
        link := List{}
        link.Insert(1)
        link.Insert(2)
        link.Insert(3)
        link.Insert(4)
        link.Insert(5)

        // Calling a method
        link.Display()

        // Calling a function
        Display(&link)
}

References

Written with StackEdit.