You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

104 lines
3.0 KiB

  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"}, "call_tree", "true", "trim", "false")
  37. if rpt == nil {
  38. return // error already reported
  39. }
  40. // Generate dot graph.
  41. g, config := report.GetDOT(rpt)
  42. var nodes []*treeNode
  43. nroots := 0
  44. rootValue := int64(0)
  45. nodeArr := []string{}
  46. nodeMap := map[*graph.Node]*treeNode{}
  47. // Make all nodes and the map, collect the roots.
  48. for _, n := range g.Nodes {
  49. v := n.CumValue()
  50. fullName := n.Info.PrintableName()
  51. node := &treeNode{
  52. Name: graph.ShortenFunctionName(fullName),
  53. FullName: fullName,
  54. Cum: v,
  55. CumFormat: config.FormatValue(v),
  56. Percent: strings.TrimSpace(measurement.Percentage(v, config.Total)),
  57. }
  58. nodes = append(nodes, node)
  59. if len(n.In) == 0 {
  60. nodes[nroots], nodes[len(nodes)-1] = nodes[len(nodes)-1], nodes[nroots]
  61. nroots++
  62. rootValue += v
  63. }
  64. nodeMap[n] = node
  65. // Get all node names into an array.
  66. nodeArr = append(nodeArr, n.Info.Name)
  67. }
  68. // Populate the child links.
  69. for _, n := range g.Nodes {
  70. node := nodeMap[n]
  71. for child := range n.Out {
  72. node.Children = append(node.Children, nodeMap[child])
  73. }
  74. }
  75. rootNode := &treeNode{
  76. Name: "root",
  77. FullName: "root",
  78. Cum: rootValue,
  79. CumFormat: config.FormatValue(rootValue),
  80. Percent: strings.TrimSpace(measurement.Percentage(rootValue, config.Total)),
  81. Children: nodes[0:nroots],
  82. }
  83. // JSON marshalling flame graph
  84. b, err := json.Marshal(rootNode)
  85. if err != nil {
  86. http.Error(w, "error serializing flame graph", http.StatusInternalServerError)
  87. ui.options.UI.PrintErr(err)
  88. return
  89. }
  90. ui.render(w, "flamegraph", rpt, errList, config.Labels, webArgs{
  91. FlameGraph: template.JS(b),
  92. Nodes: nodeArr,
  93. })
  94. }