优化性能分析器

This commit is contained in:
duanhf2012
2020-04-02 17:32:21 +08:00
parent aba29eab01
commit b333d67c47
2 changed files with 62 additions and 45 deletions

View File

@@ -32,10 +32,17 @@ type Record struct {
RecordName string RecordName string
} }
type Analyzer struct {
elem *list.Element
profiler *Profiler
}
type Profiler struct { type Profiler struct {
stack *list.List //Element stack *list.List //Element
stackLocker sync.RWMutex stackLocker sync.RWMutex
mapAnalyzer map[*list.Element]Analyzer
record *list.List //Record record *list.List //Record
callNum int //调用次数 callNum int //调用次数
@@ -62,22 +69,15 @@ func RegProfiler(profilerName string) *Profiler {
return pProfiler return pProfiler
} }
func (slf *Profiler) Push(tag string) { func (slf *Profiler) Push(tag string) *Analyzer{
slf.stackLocker.Lock() slf.stackLocker.Lock()
slf.stack.PushBack(&Element{tagName:tag,pushTime:time.Now()}) defer slf.stackLocker.Unlock()
slf.stackLocker.Unlock()
pElem := slf.stack.PushBack(&Element{tagName:tag,pushTime:time.Now()})
return &Analyzer{elem:pElem,profiler:slf}
} }
func (slf *Profiler) pushRecordLog(record *Record){
if slf.record.Len()>=Default_MaxRecordNum{
front := slf.stack.Front()
if front!=nil {
slf.stack.Remove(front)
}
}
slf.record.PushBack(record)
}
func (slf *Profiler) check(pElem *Element) (*Record,time.Duration) { func (slf *Profiler) check(pElem *Element) (*Record,time.Duration) {
if pElem == nil { if pElem == nil {
@@ -102,24 +102,33 @@ func (slf *Profiler) check(pElem *Element) (*Record,time.Duration) {
return &record,subTm return &record,subTm
} }
func (slf *Profiler) Pop() { func (slf *Analyzer) Pop(){
slf.stackLocker.Lock() slf.profiler.stackLocker.Lock()
defer slf.profiler.stackLocker.Unlock()
back := slf.stack.Back() pElement := slf.elem.Value.(*Element)
if back!=nil && back.Value!=nil { pElem,subTm := slf.profiler.check(pElement)
pElement := back.Value.(*Element) slf.profiler.callNum+=1
pElem,subTm := slf.check(pElement) slf.profiler.totalCostTime += subTm
slf.callNum+=1 if pElem != nil {
slf.totalCostTime += subTm slf.profiler.pushRecordLog(pElem)
if pElem != nil { }
slf.pushRecordLog(pElem) slf.profiler.stack.Remove(slf.elem)
}
func (slf *Profiler) pushRecordLog(record *Record){
if slf.record.Len()>=Default_MaxRecordNum{
front := slf.stack.Front()
if front!=nil {
slf.stack.Remove(front)
} }
slf.stack.Remove(back)
} }
slf.stackLocker.Unlock() slf.record.PushBack(record)
} }
type ReportFunType func(name string,callNum int,costTime time.Duration,record *list.List) type ReportFunType func(name string,callNum int,costTime time.Duration,record *list.List)
var reportFunc ReportFunType =DefaultReportFunction var reportFunc ReportFunType =DefaultReportFunction
@@ -165,11 +174,13 @@ func Report() {
//取栈顶是否存在异常MaxOverTime数据 //取栈顶是否存在异常MaxOverTime数据
pElem := prof.stack.Back() pElem := prof.stack.Back()
if pElem!=nil && pElem.Value!=nil{ for pElem!=nil {
pRecord,_ := prof.check(pElem.Value.(*Element)) pElement := pElem.Value.(*Element)
if pRecord!=nil { pExceptionElem,_ := prof.check(pElement)
prof.pushRecordLog(pRecord) if pExceptionElem!=nil {
prof.pushRecordLog(pExceptionElem)
} }
pElem = pElem.Prev()
} }
if prof.record.Len() == 0 { if prof.record.Len() == 0 {

View File

@@ -53,7 +53,7 @@ func (slf *Service) OnSetup(iservice IService){
} }
} }
func (slf *Service) OpenProfiler(){ func (slf *Service) OpenProfiler() {
slf.profiler = profiler.RegProfiler(slf.GetName()) slf.profiler = profiler.RegProfiler(slf.GetName())
if slf.profiler==nil { if slf.profiler==nil {
log.Fatal("rofiler.RegProfiler %s fail.",slf.GetName()) log.Fatal("rofiler.RegProfiler %s fail.",slf.GetName())
@@ -75,8 +75,9 @@ func (slf *Service) Init(iservice IService,getClientFun rpc.FuncRpcClient,getSer
} }
func (slf *Service) SetGoRouterNum(gorouterNum int32) bool { func (slf *Service) SetGoRouterNum(gorouterNum int32) bool {
//已经开始状态不允许修改协程数量 //已经开始状态不允许修改协程数量,打开性能分析器不允许开多线程
if slf.startStatus == true { if slf.startStatus == true || slf.profiler!=nil {
log.Error("open profiler mode is not allowed to set Multi-coroutine.")
return false return false
} }
@@ -102,41 +103,46 @@ func (slf *Service) Run() {
rpcRequestChan := slf.GetRpcRequestChan() rpcRequestChan := slf.GetRpcRequestChan()
rpcResponeCallBack := slf.GetRpcResponeChan() rpcResponeCallBack := slf.GetRpcResponeChan()
eventChan := slf.GetEventChan() eventChan := slf.GetEventChan()
var analyzer *profiler.Analyzer
select { select {
case <- closeSig: case <- closeSig:
bStop = true bStop = true
case rpcRequest :=<- rpcRequestChan: case rpcRequest :=<- rpcRequestChan:
if slf.profiler!=nil { if slf.profiler!=nil {
slf.profiler.Push("Req_"+rpcRequest.ServiceMethod) analyzer = slf.profiler.Push("Req_"+rpcRequest.ServiceMethod)
} }
slf.GetRpcHandler().HandlerRpcRequest(rpcRequest) slf.GetRpcHandler().HandlerRpcRequest(rpcRequest)
if slf.profiler!=nil { if analyzer!=nil {
slf.profiler.Pop() analyzer.Pop()
analyzer = nil
} }
case rpcResponeCB := <- rpcResponeCallBack: case rpcResponeCB := <- rpcResponeCallBack:
if slf.profiler!=nil { if slf.profiler!=nil {
slf.profiler.Push("Res_" + rpcResponeCB.ServiceMethod) analyzer = slf.profiler.Push("Res_" + rpcResponeCB.ServiceMethod)
} }
slf.GetRpcHandler().HandlerRpcResponeCB(rpcResponeCB) slf.GetRpcHandler().HandlerRpcResponeCB(rpcResponeCB)
if slf.profiler!=nil { if analyzer!=nil {
slf.profiler.Pop() analyzer.Pop()
analyzer = nil
} }
case ev := <- eventChan: case ev := <- eventChan:
if slf.profiler!=nil { if slf.profiler!=nil {
slf.profiler.Push(fmt.Sprintf("Event_%d", int(ev.Type))) analyzer = slf.profiler.Push(fmt.Sprintf("Event_%d", int(ev.Type)))
} }
slf.EventHandler(slf.this.(event.IEventProcessor),ev) slf.EventHandler(slf.this.(event.IEventProcessor),ev)
if slf.profiler!=nil { if analyzer!=nil {
slf.profiler.Pop() analyzer.Pop()
analyzer = nil
} }
case t := <- slf.dispatcher.ChanTimer: case t := <- slf.dispatcher.ChanTimer:
if slf.profiler!=nil { if slf.profiler!=nil {
slf.profiler.Push(fmt.Sprintf("Timer_%s", t.GetFunctionName())) analyzer = slf.profiler.Push(fmt.Sprintf("Timer_%s", t.GetFunctionName()))
} }
t.Cb() t.Cb()
if slf.profiler!=nil { if analyzer!=nil {
slf.profiler.Pop() analyzer.Pop()
analyzer = nil
} }
} }