Go Runtime Implementations: Map

Code Path: src/runtime/map.go makemap Each hmap has a hash0 as seed hash. When the compiler generates the typing information for a map, the hasher function will be calculated with genhash function (code path: src/cmd/compile/internal/reflectdata/alg.go). If the map is not escaped, the hmap will be allocated by the compiler on the stack. Then makemap will allocate an array of bucket, the type of bucket is generated at compiling stage by MapBucketType (code path: src/cmd/compile/internal/reflectdata/reflect....

November 23, 2022

Go Runtime Implementations: Timer Scheduling

Code Path: src/runtime/time.go time.startTimer (implemented as addtimer) Each p has a timers list. When a user is adding a new timer, it will first try to clean up the timers list, then adds the new timer. Each timers list is a heap sorted by timer.when, and will be updated during add/delete timers. cleantimers cleantimers only checks the head of the timers list. Check if it’s timerDeleted, delete it from the timers list, and update p’s timer0When....

November 7, 2022

Go Runtime Implementations: Network Polling

Code Path: src/runtime/netpoll.go Network polling is platform-dependent, the following is of BSD family operating systems (based on kqueue). netpollinit Initialize a kqueue descriptor, and set CLOSEXEC flag on it. Initialize a non-blocking pipe, and use the reader/writer for netpollBreak. netpollBreak will try to write to the pipe and interrupt the kevent. netpoll netpoll is used to check the connections and return a list of g ready to run. It can block for delay ns at most....

November 5, 2022

Go Runtime Implementations: Select

Code Path: src/runtime/select.go IR stage IR will walk a select statement and generate the code accordingly: If there’s no case, replace the select statement with runtime.block, the current goroutine will be blocked forever. If there’s only one case, extract the send or receive operation from the case statement. Otherwise, convert case values to addresses. If there’s only one case with one default, replace it with non-block calls selectnbsend or selectnbrecv....

November 4, 2022

Go Runtime Implementations: Slices

Code Path: src/runtime/slice.go makeslicecopy makeslicecopy is used for IR patterns like m = OMAKESLICE([]T, x); OCOPY(m, s), IR will rewrite this specific order of code path and replace it with OMAKESLICECOPY. If the elements has no pointer, SSA will generate code to do a mallocgc and memmove. Otherwise, the code will be expanded to makeslicecopy: Check the length of the slice to copy to. Do mallocgc Do memmove makeslice and `makeslice641 makeslice is used for make(slice, len, cap) statements, if cap is missing, by default it will be the same as len....

November 4, 2022

Go Runtime Implementations: Interfaces

Static Definition and Initialization Code Path: src/runtime/iface.go In proc.go, itabsinit will init the itabTable with the current activeModules information. The itabsinit function will read the itablinks from each module and add them to the global hash table. During the runtime, the getitab function will also build more items dynamically and fill the hash table accordingly. itablinks was produced for each module during the linking stage, and contains an array of itab....

October 27, 2022

Go Runtime Implementations: Typing System

Compiling Code Path: src/cmd/compile/internal/ir/expr.go During the IR stage, each expression will get type info, it’s defined as miniExpr and can be assigned a types.Type value. Static Typing: types2 Code Path: src/cmd/compile/internal/types2 types and `types2 Conversion Code Path: src/cmd/compile/internal/noder/types.go Currently, both packages are in use, but much logic is being migrated to types2 package. types2 package was introduced as part of go generic features and has a better code structure than the old types package....

October 25, 2022

Go Runtime Implementations: Garbage Collection

Ref Garbage Collector Code Path: src/runtime/mgc.go gcinit gcinit runs after almost everything set up in schedinit. Set sweepDrainedMask Initialize gcController with GOGC (for GC percentage control) and GOMEMLIMIT (for memory limits). Initialize work semaphores. gcenable gcenable happens in the main goroutine, right after runtime_inittask. Start bgsweep Start bgscavenge GC GC runs a full garbage collection. Each run will finish the current GC cycle: sweep termination, mark, mark termination, and sweep....

October 18, 2022

Go Runtime Implementations: Stack and Memory Management

Stack Management Code Path: src/runtime/stack.go Stack pools are initialized before mheap_, because we don’t allocate any memory during this stage. Right after the stack pools initialized, the mheap_ will be initialized so later stack allocation is possible. stackpool stackpool is managed by the “order” (based on the ratio of stack size and _FixedStack) of the stack, each order of stack has its own stack pool. In stackinit, the stack pool will be initialized right after moduledataverify....

October 16, 2022

Go Runtime Implementations: Scheduling

Code Path: src/runtime/proc.go Start-Up Process of a Go program. Take src/runtime/asm_arm.s as an example: Take the address of g0 and m0. Set up m.g0 and g.m. Create istack. Do runtime check. Save argc and argv. Call runtime.osinit. Call runtime.schedinit. Call runtime.newproc for the main function. call runtime.mstart to start the M. runtime.osinit Take src/runtime/os_linux.go as an example: Get the number of processors. This is done by making a syscall SYS_sched_getaffinity....

October 14, 2022