/
analyze-callgrind.go
64 lines (52 loc) · 1.75 KB
/
analyze-callgrind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cmd
import (
"fmt"
"strings"
"github.com/tideways/toolkit/xhprof"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(analyzeCallgrindCmd)
analyzeCallgrindCmd.Flags().StringVarP(&field, "dimension", "d", "excl_wt", "Dimension to view/sort (wt, excl_wt)")
analyzeCallgrindCmd.Flags().Float32VarP(&minPercent, "min", "m", 1, "Display items having minimum percentage (default 1%) of --dimension, with respect to max value")
}
var analyzeCallgrindCmd = &cobra.Command{
Use: "analyze-callgrind filepaths...",
Short: "Parse the output of callgrind outputs into a sorted tabular output.",
Long: `Parse the output of callgrind outputs into a sorted tabular output.`,
Args: cobra.MinimumNArgs(1),
RunE: analyzeCallgrind,
}
func analyzeCallgrind(cmd *cobra.Command, args []string) error {
maps := make([]*xhprof.PairCallMap, 0, len(args))
for _, arg := range args {
f := xhprof.NewFile(arg, "callgrind")
m, err := f.GetPairCallMap()
if err != nil {
return err
}
maps = append(maps, m)
}
avgMap := xhprof.AvgPairCallMaps(maps)
profile := avgMap.Flatten()
fieldInfo, ok := fieldsMap[field]
if !ok {
fmt.Printf("Provided field (%s) is not valid, defaulting to excl_wt\n", field)
field = "excl_wt"
fieldInfo = fieldsMap[field]
}
profile.SortBy(fieldInfo.Name)
// Change default to 10 for exclusive fields, only when user
// hasn't manually provided 1%
if strings.HasPrefix(field, "excl_") && !cmd.Flags().Changed("min") {
minPercent = float32(10)
}
minPercent = minPercent / 100.0
minValue := minPercent * profile.Calls[0].GetFloat32Field(fieldInfo.Name)
profile = profile.SelectGreater(fieldInfo.Name, minValue)
err := renderProfile(profile, field, fieldInfo, minValue)
if err != nil {
return err
}
return nil
}