Comments (5)
@liangmanlin 请提供下测试用例,是否使用了 pprof.Do() 函数?
from routine.
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.
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.
@liangmanlin 感谢提供测试用例!@breeswish 感谢提供源码分析!
最新版本已经修复这个 bug,可以再次尝试一下。
由于 go1.18-amd64
可能对 map
对象进行缓存,所以测试用例 TestPProf
存在一定失败的几率。
只要同一协程内 Set
后不进行额外的 labels
写入就不会出现问题,并在超出业务范围后及时 Remove
。
from routine.
经测试,已经不会panic,也正常输出了pprof
from routine.
Related Issues (19)
- 想问一下可靠性如何?作者有把此工具用在线上项目吗? HOT 3
- 如何使用泛型版本(How to use the generic version) HOT 1
- 引用的包里面怎么获取不到值呢? HOT 5
- [Question] can I directly modify the golang runtime? HOT 1
- [Question] When will the Golang 1.19 compatible version be released HOT 1
- threadlocalmap用切片表示不能复用 HOT 4
- [build err] on go1.20 HOT 3
- How to use the api ? HOT 2
- [Question] How does routine use g.labels to store data? HOT 2
- 为什么只有创建,而没有Get方法? HOT 12
- API 是否应该命名为 GoLocal 或者 RoutineLocal 而非 ThreadLocal ? HOT 1
- [Question] 也许用 cgoCtxt 这个字段(而不是 lables) 会更安全? HOT 2
- [Bug] Library is not working in go 1.21 HOT 4
- go 1.2demo结果运行不一样[Question] <title>
- [Question] invalid operation: cannot index routine.NewThreadLocal (value of type func() routine.ThreadLocal) HOT 3
- 如何验证thread是否被GC回收?占用的内存是否被释放? HOT 5
- 代码段求解 HOT 8
- TestPProf会报错 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from routine.