No Description

flamegraph.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. Cum int64 `json:"v"`
  27. CumFormat string `json:"l"`
  28. Percent string `json:"p"`
  29. Children []*treeNode `json:"c"`
  30. }
  31. // flamegraph generates a web page containing a flamegraph.
  32. func (ui *webInterface) flamegraph(w http.ResponseWriter, req *http.Request) {
  33. // Force the call tree so that the graph is a tree.
  34. // Also do not trim the tree so that the flame graph contains all functions.
  35. rpt, errList := ui.makeReport(w, req, []string{"svg"}, "call_tree", "true", "trim", "false")
  36. if rpt == nil {
  37. return // error already reported
  38. }
  39. // Generate dot graph.
  40. g, config := report.GetDOT(rpt)
  41. var nodes []*treeNode
  42. nroots := 0
  43. rootValue := int64(0)
  44. nodeMap := map[*graph.Node]*treeNode{}
  45. // Make all nodes and the map, collect the roots.
  46. for _, n := range g.Nodes {
  47. v := n.CumValue()
  48. node := &treeNode{
  49. Name: n.Info.PrintableName(),
  50. Cum: v,
  51. CumFormat: config.FormatValue(v),
  52. Percent: strings.TrimSpace(measurement.Percentage(v, config.Total)),
  53. }
  54. nodes = append(nodes, node)
  55. if len(n.In) == 0 {
  56. nodes[nroots], nodes[len(nodes)-1] = nodes[len(nodes)-1], nodes[nroots]
  57. nroots++
  58. rootValue += v
  59. }
  60. nodeMap[n] = node
  61. }
  62. // Populate the child links.
  63. for _, n := range g.Nodes {
  64. node := nodeMap[n]
  65. for child := range n.Out {
  66. node.Children = append(node.Children, nodeMap[child])
  67. }
  68. }
  69. rootNode := &treeNode{
  70. Name: "root",
  71. Cum: rootValue,
  72. CumFormat: config.FormatValue(rootValue),
  73. Percent: strings.TrimSpace(measurement.Percentage(rootValue, config.Total)),
  74. Children: nodes[0:nroots],
  75. }
  76. // JSON marshalling flame graph
  77. b, err := json.Marshal(rootNode)
  78. if err != nil {
  79. http.Error(w, "error serializing flame graph", http.StatusInternalServerError)
  80. ui.options.UI.PrintErr(err)
  81. return
  82. }
  83. ui.render(w, "/flamegraph", "flamegraph", rpt, errList, config.Labels, webArgs{
  84. FlameGraph: template.JS(b),
  85. })
  86. }