Açıklama Yok

flamegraph.go 2.8KB

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