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

View as plain text