Created
April 28, 2016 07:25
-
-
Save sitano/e50f146c6bb486909a517c167ab7d04d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
_ "unsafe" | |
"fmt" | |
"runtime/pprof" | |
"os" | |
"time" | |
) | |
// Event types in the trace, args are given in square brackets. | |
const ( | |
traceEvNone = 0 // unused | |
traceEvBatch = 1 // start of per-P batch of events [pid, timestamp] | |
traceEvFrequency = 2 // contains tracer timer frequency [frequency (ticks per second)] | |
traceEvStack = 3 // stack [stack id, number of PCs, array of PCs] | |
traceEvGomaxprocs = 4 // current value of GOMAXPROCS [timestamp, GOMAXPROCS, stack id] | |
traceEvProcStart = 5 // start of P [timestamp, thread id] | |
traceEvProcStop = 6 // stop of P [timestamp] | |
traceEvGCStart = 7 // GC start [timestamp, stack id] | |
traceEvGCDone = 8 // GC done [timestamp] | |
traceEvGCScanStart = 9 // GC scan start [timestamp] | |
traceEvGCScanDone = 10 // GC scan done [timestamp] | |
traceEvGCSweepStart = 11 // GC sweep start [timestamp, stack id] | |
traceEvGCSweepDone = 12 // GC sweep done [timestamp] | |
traceEvGoCreate = 13 // goroutine creation [timestamp, new goroutine id, start PC, stack id] | |
traceEvGoStart = 14 // goroutine starts running [timestamp, goroutine id] | |
traceEvGoEnd = 15 // goroutine ends [timestamp] | |
traceEvGoStop = 16 // goroutine stops (like in select{}) [timestamp, stack] | |
traceEvGoSched = 17 // goroutine calls Gosched [timestamp, stack] | |
traceEvGoPreempt = 18 // goroutine is preempted [timestamp, stack] | |
traceEvGoSleep = 19 // goroutine calls Sleep [timestamp, stack] | |
traceEvGoBlock = 20 // goroutine blocks [timestamp, stack] | |
traceEvGoUnblock = 21 // goroutine is unblocked [timestamp, goroutine id, stack] | |
traceEvGoBlockSend = 22 // goroutine blocks on chan send [timestamp, stack] | |
traceEvGoBlockRecv = 23 // goroutine blocks on chan recv [timestamp, stack] | |
traceEvGoBlockSelect = 24 // goroutine blocks on select [timestamp, stack] | |
traceEvGoBlockSync = 25 // goroutine blocks on Mutex/RWMutex [timestamp, stack] | |
traceEvGoBlockCond = 26 // goroutine blocks on Cond [timestamp, stack] | |
traceEvGoBlockNet = 27 // goroutine blocks on network [timestamp, stack] | |
traceEvGoSysCall = 28 // syscall enter [timestamp, stack] | |
traceEvGoSysExit = 29 // syscall exit [timestamp, goroutine id, real timestamp] | |
traceEvGoSysBlock = 30 // syscall blocks [timestamp] | |
traceEvGoWaiting = 31 // denotes that goroutine is blocked when tracing starts [goroutine id] | |
traceEvGoInSyscall = 32 // denotes that goroutine is in syscall when tracing starts [goroutine id] | |
traceEvHeapAlloc = 33 // memstats.heap_live change [timestamp, heap_alloc] | |
traceEvNextGC = 34 // memstats.next_gc change [timestamp, next_gc] | |
traceEvTimerGoroutine = 35 // denotes timer goroutine [timer goroutine id] | |
traceEvFutileWakeup = 36 // denotes that the previous wakeup of this goroutine was futile [timestamp] | |
traceEvCount = 37 | |
) | |
type mutex struct { | |
// Futex-based impl treats it as uint32 key, | |
// while sema-based impl as M* waitm. | |
// Used to be a union, but unions break precise GC. | |
key uintptr | |
} | |
type g struct {} | |
//go:linkname lock runtime.lock | |
func lock(l *mutex) | |
//go:linkname unlock runtime.unlock | |
func unlock(l *mutex) | |
//go:linkname goparkunlock runtime.goparkunlock | |
func goparkunlock(lock *mutex, reason string, traceEv byte, traceskip int) | |
//go:linkname goready runtime.goready | |
func goready(gp *g, traceskip int) | |
// func goparkunlock(lock *mutex, reason string, traceEv byte, traceskip int) { | |
// gopark(parkunlock_c, unsafe.Pointer(lock), reason, traceEv, traceskip) | |
// } | |
// func goready(gp *g, traceskip int) { | |
// systemstack(func() { | |
// ready(gp, traceskip) | |
// }) | |
// } | |
// 1go:linkname list_doSpin runtime.sync_runtime_doSpin | |
// 1go:linkname list_doSpin sync.runtime_doSpin | |
// 1func list_doSpin() | |
// go:linkname byteIndex strings.IndexByte | |
// 1func byteIndex(s string, c byte) int | |
func main() { | |
l := &mutex{} | |
go func() { | |
lock(l) | |
goparkunlock(l, "xxx", traceEvGoBlock, 1) | |
}() | |
fmt.Println("Hello") | |
for { | |
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) | |
time.Sleep(time.Second * 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment