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 11, 2024
1 parent fe2fc65 commit 3d52646
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
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
62 changes: 60 additions & 2 deletions server/querier/profile/service/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,51 @@ 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))
// 0.5%
threshold := nodes[0].TotalValue / 200

// 先计算下 FunctionValues, 裁剪会修改node的数据
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})
}

// 获取需要删除的 node 数据的 id,即 下标
deleteNodeIDs := []int{}
for i, node := range nodes {
if node.TotalValue >= threshold {
continue
}
if node.ParentNodeID >= 0 {
nodes[node.ParentNodeID].SelfValue = nodes[node.ParentNodeID].SelfValue + node.TotalValue
}
deleteNodeIDs = append(deleteNodeIDs, i)
}

// 获取删除node后的下表映射,用于更新ParentNodeID
mapping := getMapping(len(nodes), deleteNodeIDs)

// 只返回符合条件的 node 数据
maxDepth := 0
for _, node := range nodes {
if node.TotalValue >= threshold {
if node.ParentNodeID == -1 {
result.NodeValues.Values = append(result.NodeValues.Values, []int{node.LocationID, node.ParentNodeID, node.SelfValue, node.TotalValue})
} else {
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
Expand All @@ -270,12 +307,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) {
Expand Down Expand Up @@ -343,3 +382,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 3d52646

Please sign in to comment.