Ingen beskrivning

flamegraph.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import (
  16. "encoding/json"
  17. "html/template"
  18. "net/http"
  19. "strings"
  20. "github.com/google/pprof/internal/graph"
  21. "github.com/google/pprof/internal/measurement"
  22. "github.com/google/pprof/internal/report"
  23. )
  24. type treeNode struct {
  25. Name string `json:"n"`
  26. FullName string `json:"f"`
  27. Cum int64 `json:"v"`
  28. CumFormat string `json:"l"`
  29. Percent string `json:"p"`
  30. Children []*treeNode `json:"c"`
  31. }
  32. // flamegraph generates a web page containing a flamegraph.
  33. func (ui *webInterface) flamegraph(w http.ResponseWriter, req *http.Request) {
  34. // Force the call tree so that the graph is a tree.
  35. // Also do not trim the tree so that the flame graph contains all functions.
  36. rpt, errList := ui.makeReport(w, req, []string{"svg"}, func(cfg *config) {
  37. cfg.CallTree = true
  38. cfg.Trim = false
  39. })
  40. if rpt == nil {
  41. return // error already reported
  42. }
  43. // Generate dot graph.
  44. g, config := report.GetDOT(rpt)
  45. var nodes []*treeNode
  46. nroots := 0
  47. rootValue := int64(0)
  48. nodeArr := []string{}
  49. nodeMap := map[*graph.Node]*treeNode{}
  50. // Make all nodes and the map, collect the roots.
  51. for _, n := range g.Nodes {
  52. v := n.CumValue()
  53. fullName := n.Info.PrintableName()
  54. node := &treeNode{
  55. Name: graph.ShortenFunctionName(fullName),
  56. FullName: fullName,
  57. Cum: v,
  58. CumFormat: config.FormatValue(v),
  59. Percent: strings.TrimSpace(measurement.Percentage(v, config.Total)),
  60. }
  61. nodes = append(nodes, node)
  62. if len(n.In) == 0 {
  63. nodes[nroots], nodes[len(nodes)-1] = nodes[len(nodes)-1], nodes[nroots]
  64. nroots++
  65. rootValue += v
  66. }
  67. nodeMap[n] = node
  68. // Get all node names into an array.
  69. nodeArr = append(nodeArr, n.Info.Name)
  70. }
  71. // Populate the child links.
  72. for _, n := range g.Nodes {
  73. node := nodeMap[n]
  74. for child := range n.Out {
  75. node.Children = append(node.Children, nodeMap[child])
  76. }
  77. }
  78. rootNode := &treeNode{
  79. Name: "root",
  80. FullName: "root",
  81. Cum: rootValue,
  82. CumFormat: config.FormatValue(rootValue),
  83. Percent: strings.TrimSpace(measurement.Percentage(rootValue, config.Total)),
  84. Children: nodes[0:nroots],
  85. }
  86. // JSON marshalling flame graph
  87. b, err := json.Marshal(rootNode)
  88. if err != nil {
  89. http.Error(w, "error serializing flame graph", http.StatusInternalServerError)
  90. ui.options.UI.PrintErr(err)
  91. return
  92. }
  93. ui.render(w, req, "flamegraph", rpt, errList, config.Labels, webArgs{
  94. FlameGraph: template.JS(b),
  95. Nodes: nodeArr,
  96. })
  97. }