Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Support go1.7.2 and go1.7.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
huandu committed Oct 25, 2016
1 parent 5bd4a71 commit 068e661
Show file tree
Hide file tree
Showing 392 changed files with 22,154 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ go:
- 1.6.3
- 1.7
- 1.7.1
- 1.7.2
- 1.7.3
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ println(id)

See [godoc](https://godoc.org/github.com/huandu/goroutine) for more details.

## Caveats ##
## Supported builds ##

Package goroutine is not well tested due to lack of test machines.
Ideally, it should work on all go >= go1.5.
Expand All @@ -32,6 +32,7 @@ Tested platforms.
* go1.6.3
* go1.7
* go1.7.1
* go1.7.3
* Travis CI (See https://travis-ci.org/huandu/goroutine)
* go1.5
* go1.5.1
Expand All @@ -44,6 +45,30 @@ Tested platforms.
* go1.6.3
* go1.7
* go1.7.1
* go1.7.2
* go1.7.3

## How it works ##

Go program is a statically built binary. The runtime inside a binary is statically linked. It means, if I know Go version
and runtime source code for this version, I can copy struct declaration from runtime package source to my package and
cast runtime internal pointers to its underlying struct safely. As Go is an open source project, I can always find the right
struct for an interesting runtime pointer and then manipulate it.

In this package, I just get current goroutine pointer (copy the `getg()` implementation from compiler) and cast it to a right
struct. It sounds simple. However, the struct `g` refers many other internal types defined in `runtime` package. I cannot
simply copy some necessary types to my package to make it work. I have to scan all types and constants in `runtime` and its
internal packages to make the struct `g` well defined. Another challenge is that Go authors update `runtime` structs in nearly
every major version (or even in a minor version). I have to maintain hacked code for every Go release. I develop a
semi-automatical tool to make things easier. I guess I may need to think of other better way to avoid to generate hacked source
for every Go release.

NOTE: Starting from go1.7.2, Go compiler generates some constants definition for `runtime` package according to build flags and
environment when building. It makes current hack impossible to handle all posibile flag and environment combinations. I make a
hack to detour it and no impact to the major task of this package - get goroutine id. However, it's not a perfect solution.
If I want to do more tricks in runtime, such constants may bother me.

I'm think of a perfect solution. If you have any suggestion, please open issue and let me know. Many thanks.

## License ##

Expand Down
44 changes: 44 additions & 0 deletions hack/go1_7_2/runtime/alg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

import (
"github.com/huandu/goroutine/hack/go1_7_2/runtime/internal/sys"
"unsafe"
)

const (
c0 = uintptr((8-sys.PtrSize)/4*2860486313 + (sys.PtrSize-4)/4*33054211828000289)
c1 = uintptr((8-sys.PtrSize)/4*3267000013 + (sys.PtrSize-4)/4*23344194077549503)
)

// type algorithms - known to compiler
const (
alg_NOEQ = iota
alg_MEM0
alg_MEM8
alg_MEM16
alg_MEM32
alg_MEM64
alg_MEM128
alg_STRING
alg_INTER
alg_NILINTER
alg_FLOAT32
alg_FLOAT64
alg_CPLX64
alg_CPLX128
alg_max
)

// typeAlg is also copied/used in reflect/type.go.
// keep them in sync.
type typeAlg struct {
hash func(unsafe.Pointer, uintptr) uintptr

equal func(unsafe.Pointer, unsafe.Pointer) bool
}

const hashRandomBytes = sys.PtrSize / 4 * 64
87 changes: 87 additions & 0 deletions hack/go1_7_2/runtime/cgocall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Cgo call and callback support.
//
// To call into the C function f from Go, the cgo-generated code calls
// runtime.cgocall(_cgo_Cfunc_f, frame), where _cgo_Cfunc_f is a
// gcc-compiled function written by cgo.
//
// runtime.cgocall (below) locks g to m, calls entersyscall
// so as not to block other goroutines or the garbage collector,
// and then calls runtime.asmcgocall(_cgo_Cfunc_f, frame).
//
// runtime.asmcgocall (in asm_$GOARCH.s) switches to the m->g0 stack
// (assumed to be an operating system-allocated stack, so safe to run
// gcc-compiled code on) and calls _cgo_Cfunc_f(frame).
//
// _cgo_Cfunc_f invokes the actual C function f with arguments
// taken from the frame structure, records the results in the frame,
// and returns to runtime.asmcgocall.
//
// After it regains control, runtime.asmcgocall switches back to the
// original g (m->curg)'s stack and returns to runtime.cgocall.
//
// After it regains control, runtime.cgocall calls exitsyscall, which blocks
// until this m can run Go code without violating the $GOMAXPROCS limit,
// and then unlocks g from m.
//
// The above description skipped over the possibility of the gcc-compiled
// function f calling back into Go. If that happens, we continue down
// the rabbit hole during the execution of f.
//
// To make it possible for gcc-compiled C code to call a Go function p.GoF,
// cgo writes a gcc-compiled function named GoF (not p.GoF, since gcc doesn't
// know about packages). The gcc-compiled C function f calls GoF.
//
// GoF calls crosscall2(_cgoexp_GoF, frame, framesize). Crosscall2
// (in cgo/gcc_$GOARCH.S, a gcc-compiled assembly file) is a two-argument
// adapter from the gcc function call ABI to the 6c function call ABI.
// It is called from gcc to call 6c functions. In this case it calls
// _cgoexp_GoF(frame, framesize), still running on m->g0's stack
// and outside the $GOMAXPROCS limit. Thus, this code cannot yet
// call arbitrary Go code directly and must be careful not to allocate
// memory or use up m->g0's stack.
//
// _cgoexp_GoF calls runtime.cgocallback(p.GoF, frame, framesize, ctxt).
// (The reason for having _cgoexp_GoF instead of writing a crosscall3
// to make this call directly is that _cgoexp_GoF, because it is compiled
// with 6c instead of gcc, can refer to dotted names like
// runtime.cgocallback and p.GoF.)
//
// runtime.cgocallback (in asm_$GOARCH.s) switches from m->g0's
// stack to the original g (m->curg)'s stack, on which it calls
// runtime.cgocallbackg(p.GoF, frame, framesize).
// As part of the stack switch, runtime.cgocallback saves the current
// SP as m->g0->sched.sp, so that any use of m->g0's stack during the
// execution of the callback will be done below the existing stack frames.
// Before overwriting m->g0->sched.sp, it pushes the old value on the
// m->g0 stack, so that it can be restored later.
//
// runtime.cgocallbackg (below) is now running on a real goroutine
// stack (not an m->g0 stack). First it calls runtime.exitsyscall, which will
// block until the $GOMAXPROCS limit allows running this goroutine.
// Once exitsyscall has returned, it is safe to do things like call the memory
// allocator or invoke the Go callback function p.GoF. runtime.cgocallbackg
// first defers a function to unwind m->g0.sched.sp, so that if p.GoF
// panics, m->g0.sched.sp will be restored to its old value: the m->g0 stack
// and the m->curg stack will be unwound in lock step.
// Then it calls p.GoF. Finally it pops but does not execute the deferred
// function, calls runtime.entersyscall, and returns to runtime.cgocallback.
//
// After it regains control, runtime.cgocallback switches back to
// m->g0's stack (the pointer is still in m->g0.sched.sp), restores the old
// m->g0.sched.sp value from the stack, and returns to _cgoexp_GoF.
//
// _cgoexp_GoF immediately returns to crosscall2, which restores the
// callee-save registers for gcc and returns to GoF, which returns to f.

package runtime

// Addresses collected in a cgo backtrace when crashing.
// Length must match arg.Max in x_cgo_callers in runtime/cgo/gcc_traceback.c.
type cgoCallers [32]uintptr

const cgoCheckPointerFail = "cgo argument has Go pointer to Go pointer"
const cgoResultFail = "cgo result has Go pointer"
10 changes: 10 additions & 0 deletions hack/go1_7_2/runtime/cgocheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Code to check that pointer writes follow the cgo rules.
// These functions are invoked via the write barrier when debug.cgocheck > 1.

package runtime

const cgoWriteBarrierFail = "Go pointer stored into non-Go memory"
35 changes: 35 additions & 0 deletions hack/go1_7_2/runtime/chan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

import (
"unsafe"
)

const (
maxAlign = 8
hchanSize = unsafe.Sizeof(hchan{}) + uintptr(-int(unsafe.Sizeof(hchan{}))&(maxAlign-1))
debugChan = false
)

type hchan struct {
qcount uint
dataqsiz uint
buf unsafe.Pointer
elemsize uint16
closed uint32
elemtype *_type
sendx uint
recvx uint
recvq waitq
sendq waitq

lock mutex
}

type waitq struct {
first *sudog
last *sudog
}
13 changes: 13 additions & 0 deletions hack/go1_7_2/runtime/compiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

// Compiler is the name of the compiler toolchain that built the
// running binary. Known toolchains are:
//
// gc Also known as cmd/compile.
// gccgo The gccgo front end, part of the GCC compiler suite.
//
const Compiler = "gc"
86 changes: 86 additions & 0 deletions hack/go1_7_2/runtime/cpuprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// CPU profiling.
// Based on algorithms and data structures used in
// http://code.google.com/p/google-perftools/.
//
// The main difference between this code and the google-perftools
// code is that this code is written to allow copying the profile data
// to an arbitrary io.Writer, while the google-perftools code always
// writes to an operating system file.
//
// The signal handler for the profiling clock tick adds a new stack trace
// to a hash table tracking counts for recent traces. Most clock ticks
// hit in the cache. In the event of a cache miss, an entry must be
// evicted from the hash table, copied to a log that will eventually be
// written as profile data. The google-perftools code flushed the
// log itself during the signal handler. This code cannot do that, because
// the io.Writer might block or need system calls or locks that are not
// safe to use from within the signal handler. Instead, we split the log
// into two halves and let the signal handler fill one half while a goroutine
// is writing out the other half. When the signal handler fills its half, it
// offers to swap with the goroutine. If the writer is not done with its half,
// we lose the stack trace for this clock tick (and record that loss).
// The goroutine interacts with the signal handler by calling getprofile() to
// get the next log piece to write, implicitly handing back the last log
// piece it obtained.
//
// The state of this dance between the signal handler and the goroutine
// is encoded in the Profile.handoff field. If handoff == 0, then the goroutine
// is not using either log half and is waiting (or will soon be waiting) for
// a new piece by calling notesleep(&p.wait). If the signal handler
// changes handoff from 0 to non-zero, it must call notewakeup(&p.wait)
// to wake the goroutine. The value indicates the number of entries in the
// log half being handed off. The goroutine leaves the non-zero value in
// place until it has finished processing the log half and then flips the number
// back to zero. Setting the high bit in handoff means that the profiling is over,
// and the goroutine is now in charge of flushing the data left in the hash table
// to the log and returning that data.
//
// The handoff field is manipulated using atomic operations.
// For the most part, the manipulation of handoff is orderly: if handoff == 0
// then the signal handler owns it and can change it to non-zero.
// If handoff != 0 then the goroutine owns it and can change it to zero.
// If that were the end of the story then we would not need to manipulate
// handoff using atomic operations. The operations are needed, however,
// in order to let the log closer set the high bit to indicate "EOF" safely
// in the situation when normally the goroutine "owns" handoff.

package runtime

const (
numBuckets = 1 << 10
logSize = 1 << 17
assoc = 4
maxCPUProfStack = 64
)

type cpuprofEntry struct {
count uintptr
depth int
stack [maxCPUProfStack]uintptr
}

type cpuProfile struct {
on bool
wait note
count uintptr
evicts uintptr
lost uintptr

hash [numBuckets]struct {
entry [assoc]cpuprofEntry
}

log [2][logSize / 2]uintptr
nlog int
toggle int32
handoff uint32

wtoggle uint32
wholding bool
flushing bool
eodSent bool
}
Loading

0 comments on commit 068e661

Please sign in to comment.