GithubHelp home page GithubHelp logo

Comments (5)

timandy avatar timandy commented on August 17, 2024

@liangmanlin 请提供下测试用例,是否使用了 pprof.Do() 函数?

from routine.

liangmanlin avatar liangmanlin commented on August 17, 2024
func main(){
    t := routine.NewThreadLocal()
    for i:=0;i<100;i++{
        k := i
        go func(){
            for{
                 t.Set(K)
                 time.Sleep(10*time.Millisecond)
             }
        }()
    }
    f,_ := os.Create("cpu.prof")
    time.Sleep(time.Second)
    pprof.StartCPUProfile(f)
   time.Sleep(10*time.Second)
   pprof.StopCPUProfile()
   time.Sleep(time.Second)
}

不一定会panic,但是大概率,当groutine越多,越容易发生

from routine.

breezewish avatar breezewish commented on August 17, 2024

pprof 会按照 map[string]string 设置和处理 g.labels:

赋值:

type labelMap map[string]string

// SetGoroutineLabels sets the current goroutine's labels to match ctx.
// A new goroutine inherits the labels of the goroutine that created it.
// This is a lower-level API than Do, which should be used instead when possible.
func SetGoroutineLabels(ctx context.Context) {
	ctxLabels, _ := ctx.Value(labelContextKey{}).(*labelMap)
	runtime_setProfLabel(unsafe.Pointer(ctxLabels))
}

// ...

//go:linkname runtime_setProfLabel runtime/pprof.runtime_setProfLabel
func runtime_setProfLabel(labels unsafe.Pointer) {
	// ...
	getg().labels = labels
}

获取 profiling 结果:

type runtimeProfile struct {
	stk    []runtime.StackRecord
	labels []unsafe.Pointer
}

func (p *runtimeProfile) Label(i int) *labelMap { return (*labelMap)(p.labels[i]) }

from routine.

timandy avatar timandy commented on August 17, 2024

@liangmanlin 感谢提供测试用例!@breeswish 感谢提供源码分析!

最新版本已经修复这个 bug,可以再次尝试一下。

由于 go1.18-amd64 可能对 map 对象进行缓存,所以测试用例 TestPProf 存在一定失败的几率。
只要同一协程内 Set 后不进行额外的 labels 写入就不会出现问题,并在超出业务范围后及时 Remove

from routine.

liangmanlin avatar liangmanlin commented on August 17, 2024

经测试,已经不会panic,也正常输出了pprof

from routine.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.