From d0439b5597d6df8503c085563535fb8a8f253136 Mon Sep 17 00:00:00 2001 From: zhuofeng Date: Tue, 10 Sep 2024 19:39:48 +0800 Subject: [PATCH] feat: filter profiler --- server/querier/profile/model/model.go | 1 + server/querier/profile/service/profile.go | 55 ++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/server/querier/profile/model/model.go b/server/querier/profile/model/model.go index 8871b3cbc78..4edc2ff310d 100644 --- a/server/querier/profile/model/model.go +++ b/server/querier/profile/model/model.go @@ -43,6 +43,7 @@ type ProfileTreeNode struct { ParentNodeID int SelfValue int TotalValue int + Depth int } type Debug struct { diff --git a/server/querier/profile/service/profile.go b/server/querier/profile/service/profile.go index f54b9e83092..b024ecb3f89 100644 --- a/server/querier/profile/service/profile.go +++ b/server/querier/profile/service/profile.go @@ -213,6 +213,7 @@ func GenerateProfile(args model.Profile, cfg *config.QuerierConfig, where string if preNodeID >= 0 { nodes[preNodeID].ParentNodeID = nodeID + nodes[preNodeID].Depth = nodes[nodeID].Depth + 1 } else { // remember the entier stack stackToNodeID[profileLocationCompress] = nodeID // compressed stack @@ -236,14 +237,43 @@ func GenerateProfile(args model.Profile, cfg *config.QuerierConfig, where string for i := range result.FunctionValues.Values { result.FunctionValues.Values[i] = []int{0, 0} } + result.NodeValues.Values = make([][]int, 0, len(nodes)) + maxDepth := 0 + // 0.5% + threshold := nodes[0].TotalValue / 200 + for _, node := range nodes { locationID := node.LocationID result.FunctionValues.Values[locationID][0] += node.SelfValue result.FunctionValues.Values[locationID][1] += node.TotalValue - result.NodeValues.Values = append(result.NodeValues.Values, []int{locationID, node.ParentNodeID, node.SelfValue, node.TotalValue}) } + deleteNodeIDs := []int{} + for i, node := range nodes { + if node.TotalValue >= threshold || node.Depth < 6 { + continue + } + if node.ParentNodeID > 0 { + nodes[node.ParentNodeID].SelfValue = nodes[node.ParentNodeID].SelfValue + node.TotalValue + } + deleteNodeIDs = append(deleteNodeIDs, i) + } + + mapping := getMapping(len(nodes), deleteNodeIDs) + + for _, node := range nodes { + if node.TotalValue >= threshold || node.Depth < 6 { + result.NodeValues.Values = append(result.NodeValues.Values, []int{node.LocationID, mapping[node.ParentNodeID], node.SelfValue, node.TotalValue}) + } + + if node.Depth > maxDepth { + maxDepth = node.Depth + } + } + + log.Infof("total nodes len=%d,total value is %d, threshold is %d, valid node len=%d, maxDepth=%d", len(nodes), nodes[0].TotalValue, threshold, len(result.NodeValues.Values), maxDepth) + result.Functions = locations locationTypes := GetLocationType(locations, result.FunctionValues.Values, args.ProfileEventType) result.FunctionTypes = locationTypes @@ -270,12 +300,14 @@ func updateAllParentNodes(nodes []model.ProfileTreeNode, thisNodeID, selfValue, thisNode.SelfValue += selfValue thisNode.TotalValue += totalValue + depth := 0 // parent nodes for thisNode.ParentNodeID >= 0 { + depth++ thisNode = &nodes[thisNode.ParentNodeID] thisNode.TotalValue += totalValue } - + nodes[thisNodeID].Depth = depth } func CutKernelFunction(profileLocationByteSlice []byte, maxKernelStackDepth int, sep string) ([]byte, bool) { @@ -343,3 +375,22 @@ func GetLocationType(locations []string, locationValues [][]int, profileEventTyp } return locationTypes } + +func getMapping(arrLen int, indicesToRemove []int) map[int]int { + removed := make(map[int]struct{}) + for _, index := range indicesToRemove { + removed[index] = struct{}{} + } + + mapping := make(map[int]int) + newIndex := 0 + + for i := 0; i < arrLen; i++ { + if _, found := removed[i]; !found { + mapping[i] = newIndex + newIndex++ + } + } + + return mapping +}