Skip to content

Commit

Permalink
feat: filter profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
lzf575 committed Sep 12, 2024
1 parent e0218b7 commit 8321168
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions server/querier/profile/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ProfileTreeNode struct {
ParentNodeID int
SelfValue int
TotalValue int
Depth int
}

type Debug struct {
Expand Down
80 changes: 79 additions & 1 deletion server/querier/profile/service/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ 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))
for _, node := range nodes {
locationID := node.LocationID
Expand All @@ -244,6 +245,11 @@ func GenerateProfile(args model.Profile, cfg *config.QuerierConfig, where string
result.NodeValues.Values = append(result.NodeValues.Values, []int{locationID, node.ParentNodeID, node.SelfValue, node.TotalValue})
}

// 0.1%
valueThreshold := nodes[0].TotalValue / 1000
depthThreshold := 12
filterResults(nodes, &result, valueThreshold, depthThreshold)

result.Functions = locations
locationTypes := GetLocationType(locations, result.FunctionValues.Values, args.ProfileEventType)
result.FunctionTypes = locationTypes
Expand All @@ -256,6 +262,60 @@ func GenerateProfile(args model.Profile, cfg *config.QuerierConfig, where string
return
}

func isFilterDiscard(node *model.ProfileTreeNode, minTotalValueThreshold, minDepthThreshold int) bool {
return node.TotalValue < minTotalValueThreshold && node.Depth > minDepthThreshold
}

func filterResults(nodes []model.ProfileTreeNode, result *model.ProfileTree, minTotalValueThreshold, minDepthThreshold int) {
if len(nodes) < 10000 {
return
}
// get the nodeid that needs to be deleted,
deleteNodeIDs := []int{}
for i, node := range nodes {
calculateDepth(nodes, i)
if !isFilterDiscard(&node, minTotalValueThreshold, minDepthThreshold) {
continue
}
if node.ParentNodeID >= 0 {
nodes[node.ParentNodeID].SelfValue = nodes[node.ParentNodeID].SelfValue + node.TotalValue
}
deleteNodeIDs = append(deleteNodeIDs, i)
}

// 获取删除 nodeId 后的id和删除前的id的映射表,用于更新ParentNodeID
// obtain the mapping table between the id after nodeId is deleted and the id before deletion, used to update ParentNodeID
mapping := getMapping(len(nodes), deleteNodeIDs)

maxDepth := 0
result.NodeValues.Values = result.NodeValues.Values[:0]
for _, node := range nodes {
if isFilterDiscard(&node, minTotalValueThreshold, minDepthThreshold) {
continue
}
parentNodeID := node.ParentNodeID
if node.ParentNodeID != -1 {
parentNodeID = mapping[node.ParentNodeID]
}
result.NodeValues.Values = append(result.NodeValues.Values, []int{node.LocationID, parentNodeID, node.SelfValue, node.TotalValue})

if node.Depth > maxDepth {
maxDepth = node.Depth
}
}
log.Infof("profile total nodes count %d, total value is %d, valueThreshold is %d, valid node count %d, maxDepth=%d", len(nodes), nodes[0].TotalValue, minTotalValueThreshold, len(result.NodeValues.Values), maxDepth)
}

func calculateDepth(nodes []model.ProfileTreeNode, thisNodeID int) {
thisNode := &nodes[thisNodeID]
depth := 1
for thisNode.ParentNodeID >= 0 {
depth++
thisNode = &nodes[thisNode.ParentNodeID]
}
nodes[thisNodeID].Depth = depth
}

func newProfileTreeNode(locationID, selfValue, totalValue int) model.ProfileTreeNode {
node := model.ProfileTreeNode{}
node.LocationID = locationID
Expand All @@ -275,7 +335,6 @@ func updateAllParentNodes(nodes []model.ProfileTreeNode, thisNodeID, selfValue,
thisNode = &nodes[thisNode.ParentNodeID]
thisNode.TotalValue += totalValue
}

}

func CutKernelFunction(profileLocationByteSlice []byte, maxKernelStackDepth int, sep string) ([]byte, bool) {
Expand Down Expand Up @@ -343,3 +402,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
}

0 comments on commit 8321168

Please sign in to comment.