Source file src/cmd/vendor/github.com/google/pprof/internal/driver/stacks.go

     1  // Copyright 2022 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  
    15  package driver
    16  
    17  import (
    18  	"encoding/json"
    19  	"html/template"
    20  	"net/http"
    21  
    22  	"github.com/google/pprof/internal/report"
    23  )
    24  
    25  // stackView generates the new flamegraph view.
    26  func (ui *webInterface) stackView(w http.ResponseWriter, req *http.Request) {
    27  	// Get all data in a report.
    28  	rpt, errList := ui.makeReport(w, req, []string{"svg"}, func(cfg *config) {
    29  		cfg.CallTree = true
    30  		cfg.Trim = false
    31  		cfg.Granularity = "filefunctions"
    32  	})
    33  	if rpt == nil {
    34  		return // error already reported
    35  	}
    36  
    37  	// Make stack data and generate corresponding JSON.
    38  	stacks := rpt.Stacks()
    39  	b, err := json.Marshal(stacks)
    40  	if err != nil {
    41  		http.Error(w, "error serializing stacks for flame graph",
    42  			http.StatusInternalServerError)
    43  		ui.options.UI.PrintErr(err)
    44  		return
    45  	}
    46  
    47  	nodes := make([]string, len(stacks.Sources))
    48  	for i, src := range stacks.Sources {
    49  		nodes[i] = src.FullName
    50  	}
    51  	nodes[0] = "" // root is not a real node
    52  
    53  	_, legend := report.TextItems(rpt)
    54  	ui.render(w, req, "stacks", rpt, errList, legend, webArgs{
    55  		Stacks: template.JS(b),
    56  		Nodes:  nodes,
    57  	})
    58  }
    59  

View as plain text