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. Get the huge page size. Run osArchInit. <- Seems not used. runtime.schedinit Initialize all the locks. Set up the g.racectx. Stop the world. moduledataverify. Defined in src/runtime/symtab.go, it will check moduledata’s binary info. stackinit. Defined in src/runtime/stack.go, it will setup the global stack pool, all stacks will be allocated by it. mallocinit. Defined in src/runtime/malloc.go, it will initialize the memory allocator used by Go. cpuinit. Set up the internal/cpu information. alginit. Defined in src/runtime/alg.go, set up the CPU instructions that will be used by some internal algorithms, depending on cpuinit. fastrandinit. Initialize the g0.m with mcommoninit. modulesinit. Defined in src/runtime/symtab.go, initialize the dynamic loaded modules. typelinksinit. Defined in src/runtime/type.go, initialized the dynamic-loaded module type informations. itabsinit. Defined in src/runtime/iface.go, update itabTable with the dynamic-loaded modules. stkobjinit. Defined in src/runtime/stkframe.go, this will set up methodValueCallFrameObjs, used by later GC module. Save the signal mask as initSigMask. Parse argv. Defined in src/runtime/runtime1.go. Parse environment variables. parsedebugvars. Defined in src/runtime/runtime1.go. gcinit. Set up the number of processors, and trim it accordingly with procresize: Grow allp as necessary. Initialize all the p’s of allp. Release old p’s. Track the idle and runnable p, and return the runnable p’s. There should be zero runnable p, since this is only a bootstrap process. Start the world. runtime.mstart mstart0 set up the stack info, then calls runtime.mstart1 to allocate the m:
...