github.com/alphadose/itogami@v0.4.1-0.20221016160904-c25d0a36bfe7/lib_runtime_linkage.go (about)

     1  package itogami
     2  
     3  import (
     4  	"runtime"
     5  	"unsafe"
     6  	_ "unsafe"
     7  
     8  	"github.com/alphadose/itogami/constants"
     9  )
    10  
    11  const (
    12  	cacheLinePadSize          = constants.CacheLinePadSize
    13  	uint64SubtractionConstant = ^uint64(0)
    14  )
    15  
    16  type cacheLinePadding struct{ _ [cacheLinePadSize]byte }
    17  
    18  // Linking ZenQ with golang internal runtime library to allow usage of scheduling primitives
    19  // like goready(), mcall() etc to allow low-level scheduling of goroutines
    20  
    21  type mutex struct {
    22  	// Futex-based impl treats it as uint32 key,
    23  	// while sema-based impl as M* waitm.
    24  	// Used to be a union, but unions break precise GC.
    25  	key uintptr
    26  }
    27  
    28  // The functions below are used for scheduling goroutines with exclusive control
    29  // Shifting to the below flow will remove the spinning and mutex lock implementations
    30  
    31  //go:linkname lock runtime.lock
    32  func lock(l *mutex)
    33  
    34  //go:linkname nanotime runtime.nanotime
    35  func nanotime() int64
    36  
    37  //go:linkname unlock runtime.unlock
    38  func unlock(l *mutex)
    39  
    40  //go:linkname goparkunlock runtime.goparkunlock
    41  func goparkunlock(lock *mutex, reason waitReason, traceEv byte, traceskip int)
    42  
    43  // GetG returns the pointer to the current goroutine
    44  // defined in the asm files
    45  func GetG() unsafe.Pointer
    46  
    47  //go:linkname Fastrand runtime.fastrand
    48  func Fastrand() uint32
    49  
    50  //go:linkname fastlog2 runtime.fastlog2
    51  func fastlog2(x float64) float64
    52  
    53  //go:linkname goready runtime.goready
    54  func goready(goroutinePtr unsafe.Pointer, traceskip int)
    55  
    56  //go:linkname gopark runtime.gopark
    57  func gopark(unlockf func(unsafe.Pointer, unsafe.Pointer) bool, lock unsafe.Pointer, reason waitReason, traceEv byte, traceskip int)
    58  
    59  // Active spinning runtime support.
    60  // runtime_canSpin reports whether spinning makes sense at the moment.
    61  //go:linkname runtime_canSpin sync.runtime_canSpin
    62  func runtime_canSpin(i int) bool
    63  
    64  // runtime_doSpin does active spinning.
    65  // //go:linkname runtime_doSpin sync.runtime_doSpin
    66  // func runtime_doSpin()
    67  
    68  func runtime_doSpin() {
    69  	spin(30)
    70  }
    71  
    72  //go:linkname osyield runtime.osyield
    73  func osyield()
    74  
    75  //go:linkname runtime_nanotime sync.runtime_nanotime
    76  func runtime_nanotime() int64
    77  
    78  // Semacquire waits until *s > 0 and then atomically decrements it.
    79  // It is intended as a simple sleep primitive for use by the synchronization
    80  // library and should not be used directly.
    81  //go:linkname runtime_Semacquire sync.runtime_Semacquire
    82  func runtime_Semacquire(s *uint32)
    83  
    84  // SemacquireMutex is like Semacquire, but for profiling contended Mutexes.
    85  // If lifo is true, queue waiter at the head of wait queue.
    86  // skipframes is the number of frames to omit during tracing, counting from
    87  // runtime_SemacquireMutex's caller.
    88  //go:linkname runtime_SemacquireMutex sync.runtime_SemacquireMutex
    89  func runtime_SemacquireMutex(s *uint32, lifo bool, skipframes int)
    90  
    91  // Semrelease atomically increments *s and notifies a waiting goroutine
    92  // if one is blocked in Semacquire.
    93  // It is intended as a simple wakeup primitive for use by the synchronization
    94  // library and should not be used directly.
    95  // If handoff is true, pass count directly to the first waiter.
    96  // skipframes is the number of frames to omit during tracing, counting from
    97  // runtime_Semrelease's caller.
    98  //go:linkname runtime_Semrelease sync.runtime_Semrelease
    99  func runtime_Semrelease(s *uint32, handoff bool, skipframes int)
   100  
   101  //go:linkname goyield runtime.goyield
   102  func goyield()
   103  
   104  //go:linkname mcall runtime.mcall
   105  func mcall(fn func(unsafe.Pointer))
   106  
   107  //go:linkname park_m runtime.park_m
   108  func park_m(gp unsafe.Pointer)
   109  
   110  //go:linkname fastrandn runtime.fastrandn
   111  func fastrandn(n uint32) uint32
   112  
   113  //go:linkname throw runtime.throw
   114  func throw(s string)
   115  
   116  //go:linkname Readgstatus runtime.readgstatus
   117  func Readgstatus(gp unsafe.Pointer) uint32
   118  
   119  //go:linkname casgstatus runtime.casgstatus
   120  func casgstatus(gp unsafe.Pointer, oldval, newval uint32)
   121  
   122  //go:linkname dropg runtime.dropg
   123  func dropg()
   124  
   125  //go:linkname schedule runtime.schedule
   126  func schedule()
   127  
   128  //go:linkname mallocgc runtime.mallocgc
   129  func mallocgc(size uintptr, typ unsafe.Pointer, needzero bool) unsafe.Pointer
   130  
   131  //go:linkname sysFree runtime.sysFree
   132  func sysFree(v unsafe.Pointer, n uintptr, sysStat unsafe.Pointer)
   133  
   134  //go:linkname sysFreeOS runtime.sysFreeOS
   135  func sysFreeOS(v unsafe.Pointer, n uintptr)
   136  
   137  //go:linkname gosched_m runtime.gosched_m
   138  func gosched_m(gp unsafe.Pointer)
   139  
   140  //go:linkname spin runtime.procyield
   141  func spin(cycles uint32)
   142  
   143  //go:linkname ProcPin runtime.procPin
   144  func ProcPin() int
   145  
   146  //go:linkname ProcUnpin runtime.procUnpin
   147  func ProcUnpin()
   148  
   149  // custom parking function
   150  func fast_park(gp unsafe.Pointer) {
   151  	dropg()
   152  	casgstatus(gp, _Grunning, _Gwaiting)
   153  	schedule()
   154  }
   155  
   156  // whether the system has multiple cores or a single core
   157  var multicore = runtime.NumCPU() > 1
   158  
   159  // call ready after ensuring the goroutine is parked
   160  func safe_ready(gp unsafe.Pointer) {
   161  	for Readgstatus(gp)&^_Gscan != _Gwaiting {
   162  		mcall(gosched_m)
   163  	}
   164  	goready(gp, 1)
   165  }
   166  
   167  type waitReason uint8
   168  
   169  const (
   170  	waitReasonZero                  waitReason = iota // ""
   171  	waitReasonGCAssistMarking                         // "GC assist marking"
   172  	waitReasonIOWait                                  // "IO wait"
   173  	waitReasonChanReceiveNilChan                      // "chan receive (nil chan)"
   174  	waitReasonChanSendNilChan                         // "chan send (nil chan)"
   175  	waitReasonDumpingHeap                             // "dumping heap"
   176  	waitReasonGarbageCollection                       // "garbage collection"
   177  	waitReasonGarbageCollectionScan                   // "garbage collection scan"
   178  	waitReasonPanicWait                               // "panicwait"
   179  	waitReasonSelect                                  // "select"
   180  	waitReasonSelectNoCases                           // "select (no cases)"
   181  	waitReasonGCAssistWait                            // "GC assist wait"
   182  	waitReasonGCSweepWait                             // "GC sweep wait"
   183  	waitReasonGCScavengeWait                          // "GC scavenge wait"
   184  	waitReasonChanReceive                             // "chan receive"
   185  	waitReasonChanSend                                // "chan send"
   186  	waitReasonFinalizerWait                           // "finalizer wait"
   187  	waitReasonForceGCIdle                             // "force gc (idle)"
   188  	waitReasonSemacquire                              // "semacquire"
   189  	waitReasonSleep                                   // "sleep"
   190  	waitReasonSyncCondWait                            // "sync.Cond.Wait"
   191  	waitReasonTimerGoroutineIdle                      // "timer goroutine (idle)"
   192  	waitReasonTraceReaderBlocked                      // "trace reader (blocked)"
   193  	waitReasonWaitForGCCycle                          // "wait for GC cycle"
   194  	waitReasonGCWorkerIdle                            // "GC worker (idle)"
   195  	waitReasonPreempted                               // "preempted"
   196  	waitReasonDebugCall                               // "debug call"
   197  )
   198  
   199  // Event types in the trace, args are given in square brackets.
   200  const (
   201  	traceEvNone              = 0  // unused
   202  	traceEvBatch             = 1  // start of per-P batch of events [pid, timestamp]
   203  	traceEvFrequency         = 2  // contains tracer timer frequency [frequency (ticks per second)]
   204  	traceEvStack             = 3  // stack [stack id, number of PCs, array of {PC, func string ID, file string ID, line}]
   205  	traceEvGomaxprocs        = 4  // current value of GOMAXPROCS [timestamp, GOMAXPROCS, stack id]
   206  	traceEvProcStart         = 5  // start of P [timestamp, thread id]
   207  	traceEvProcStop          = 6  // stop of P [timestamp]
   208  	traceEvGCStart           = 7  // GC start [timestamp, seq, stack id]
   209  	traceEvGCDone            = 8  // GC done [timestamp]
   210  	traceEvGCSTWStart        = 9  // GC STW start [timestamp, kind]
   211  	traceEvGCSTWDone         = 10 // GC STW done [timestamp]
   212  	traceEvGCSweepStart      = 11 // GC sweep start [timestamp, stack id]
   213  	traceEvGCSweepDone       = 12 // GC sweep done [timestamp, swept, reclaimed]
   214  	traceEvGoCreate          = 13 // goroutine creation [timestamp, new goroutine id, new stack id, stack id]
   215  	traceEvGoStart           = 14 // goroutine starts running [timestamp, goroutine id, seq]
   216  	traceEvGoEnd             = 15 // goroutine ends [timestamp]
   217  	traceEvGoStop            = 16 // goroutine stops (like in select{}) [timestamp, stack]
   218  	traceEvGoSched           = 17 // goroutine calls Gosched [timestamp, stack]
   219  	traceEvGoPreempt         = 18 // goroutine is preempted [timestamp, stack]
   220  	traceEvGoSleep           = 19 // goroutine calls Sleep [timestamp, stack]
   221  	traceEvGoBlock           = 20 // goroutine blocks [timestamp, stack]
   222  	traceEvGoUnblock         = 21 // goroutine is unblocked [timestamp, goroutine id, seq, stack]
   223  	traceEvGoBlockSend       = 22 // goroutine blocks on chan send [timestamp, stack]
   224  	traceEvGoBlockRecv       = 23 // goroutine blocks on chan recv [timestamp, stack]
   225  	traceEvGoBlockSelect     = 24 // goroutine blocks on select [timestamp, stack]
   226  	traceEvGoBlockSync       = 25 // goroutine blocks on Mutex/RWMutex [timestamp, stack]
   227  	traceEvGoBlockCond       = 26 // goroutine blocks on Cond [timestamp, stack]
   228  	traceEvGoBlockNet        = 27 // goroutine blocks on network [timestamp, stack]
   229  	traceEvGoSysCall         = 28 // syscall enter [timestamp, stack]
   230  	traceEvGoSysExit         = 29 // syscall exit [timestamp, goroutine id, seq, real timestamp]
   231  	traceEvGoSysBlock        = 30 // syscall blocks [timestamp]
   232  	traceEvGoWaiting         = 31 // denotes that goroutine is blocked when tracing starts [timestamp, goroutine id]
   233  	traceEvGoInSyscall       = 32 // denotes that goroutine is in syscall when tracing starts [timestamp, goroutine id]
   234  	traceEvHeapAlloc         = 33 // gcController.heapLive change [timestamp, heap_alloc]
   235  	traceEvHeapGoal          = 34 // gcController.heapGoal (formerly next_gc) change [timestamp, heap goal in bytes]
   236  	traceEvTimerGoroutine    = 35 // not currently used; previously denoted timer goroutine [timer goroutine id]
   237  	traceEvFutileWakeup      = 36 // denotes that the previous wakeup of this goroutine was futile [timestamp]
   238  	traceEvString            = 37 // string dictionary entry [ID, length, string]
   239  	traceEvGoStartLocal      = 38 // goroutine starts running on the same P as the last event [timestamp, goroutine id]
   240  	traceEvGoUnblockLocal    = 39 // goroutine is unblocked on the same P as the last event [timestamp, goroutine id, stack]
   241  	traceEvGoSysExitLocal    = 40 // syscall exit on the same P as the last event [timestamp, goroutine id, real timestamp]
   242  	traceEvGoStartLabel      = 41 // goroutine starts running with label [timestamp, goroutine id, seq, label string id]
   243  	traceEvGoBlockGC         = 42 // goroutine blocks on GC assist [timestamp, stack]
   244  	traceEvGCMarkAssistStart = 43 // GC mark assist start [timestamp, stack]
   245  	traceEvGCMarkAssistDone  = 44 // GC mark assist done [timestamp]
   246  	traceEvUserTaskCreate    = 45 // trace.NewContext [timestamp, internal task id, internal parent task id, stack, name string]
   247  	traceEvUserTaskEnd       = 46 // end of a task [timestamp, internal task id, stack]
   248  	traceEvUserRegion        = 47 // trace.WithRegion [timestamp, internal task id, mode(0:start, 1:end), stack, name string]
   249  	traceEvUserLog           = 48 // trace.Log [timestamp, internal task id, key string id, stack, value string]
   250  	traceEvCount             = 49
   251  	// Byte is used but only 6 bits are available for event type.
   252  	// The remaining 2 bits are used to specify the number of arguments.
   253  	// That means, the max event type value is 63.
   254  )
   255  
   256  // defined constants
   257  const (
   258  	// G status
   259  	//
   260  	// Beyond indicating the general state of a G, the G status
   261  	// acts like a lock on the goroutine's stack (and hence its
   262  	// ability to execute user code).
   263  	//
   264  	// If you add to this list, add to the list
   265  	// of "okay during garbage collection" status
   266  	// in mgcmark.go too.
   267  	//
   268  	// TODO(austin): The _Gscan bit could be much lighter-weight.
   269  	// For example, we could choose not to run _Gscanrunnable
   270  	// goroutines found in the run queue, rather than CAS-looping
   271  	// until they become _Grunnable. And transitions like
   272  	// _Gscanwaiting -> _Gscanrunnable are actually okay because
   273  	// they don't affect stack ownership.
   274  
   275  	// _Gidle means this goroutine was just allocated and has not
   276  	// yet been initialized.
   277  	_Gidle = iota // 0
   278  
   279  	// _Grunnable means this goroutine is on a run queue. It is
   280  	// not currently executing user code. The stack is not owned.
   281  	_Grunnable // 1
   282  
   283  	// _Grunning means this goroutine may execute user code. The
   284  	// stack is owned by this goroutine. It is not on a run queue.
   285  	// It is assigned an M and a P (g.m and g.m.p are valid).
   286  	_Grunning // 2
   287  
   288  	// _Gsyscall means this goroutine is executing a system call.
   289  	// It is not executing user code. The stack is owned by this
   290  	// goroutine. It is not on a run queue. It is assigned an M.
   291  	_Gsyscall // 3
   292  
   293  	// _Gwaiting means this goroutine is blocked in the runtime.
   294  	// It is not executing user code. It is not on a run queue,
   295  	// but should be recorded somewhere (e.g., a channel wait
   296  	// queue) so it can be ready()d when necessary. The stack is
   297  	// not owned *except* that a channel operation may read or
   298  	// write parts of the stack under the appropriate channel
   299  	// lock. Otherwise, it is not safe to access the stack after a
   300  	// goroutine enters _Gwaiting (e.g., it may get moved).
   301  	_Gwaiting // 4
   302  
   303  	// _Gmoribund_unused is currently unused, but hardcoded in gdb
   304  	// scripts.
   305  	_Gmoribund_unused // 5
   306  
   307  	// _Gdead means this goroutine is currently unused. It may be
   308  	// just exited, on a free list, or just being initialized. It
   309  	// is not executing user code. It may or may not have a stack
   310  	// allocated. The G and its stack (if any) are owned by the M
   311  	// that is exiting the G or that obtained the G from the free
   312  	// list.
   313  	_Gdead // 6
   314  
   315  	// _Genqueue_unused is currently unused.
   316  	_Genqueue_unused // 7
   317  
   318  	// _Gcopystack means this goroutine's stack is being moved. It
   319  	// is not executing user code and is not on a run queue. The
   320  	// stack is owned by the goroutine that put it in _Gcopystack.
   321  	_Gcopystack // 8
   322  
   323  	// _Gpreempted means this goroutine stopped itself for a
   324  	// suspendG preemption. It is like _Gwaiting, but nothing is
   325  	// yet responsible for ready()ing it. Some suspendG must CAS
   326  	// the status to _Gwaiting to take responsibility for
   327  	// ready()ing this G.
   328  	_Gpreempted // 9
   329  
   330  	// _Gscan combined with one of the above states other than
   331  	// _Grunning indicates that GC is scanning the stack. The
   332  	// goroutine is not executing user code and the stack is owned
   333  	// by the goroutine that set the _Gscan bit.
   334  	//
   335  	// _Gscanrunning is different: it is used to briefly block
   336  	// state transitions while GC signals the G to scan its own
   337  	// stack. This is otherwise like _Grunning.
   338  	//
   339  	// atomicstatus&~Gscan gives the state the goroutine will
   340  	// return to when the scan completes.
   341  	_Gscan          = 0x1000
   342  	_Gscanrunnable  = _Gscan + _Grunnable  // 0x1001
   343  	_Gscanrunning   = _Gscan + _Grunning   // 0x1002
   344  	_Gscansyscall   = _Gscan + _Gsyscall   // 0x1003
   345  	_Gscanwaiting   = _Gscan + _Gwaiting   // 0x1004
   346  	_Gscanpreempted = _Gscan + _Gpreempted // 0x1009
   347  )