Skip to content

Commit

Permalink
Merge branch 'main' into relative-symbolz
Browse files Browse the repository at this point in the history
  • Loading branch information
aalexand authored Sep 27, 2024
2 parents 2c70c19 + cba816a commit f30d439
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 85 deletions.
2 changes: 1 addition & 1 deletion internal/driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func defaultConfig() config {
Trim: true,
DivideBy: 1.0,
Sort: "flat",
Granularity: "functions",
Granularity: "", // Default depends on the display format
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ func aggregate(prof *profile.Profile, cfg config) error {
var function, filename, linenumber, address bool
inlines := !cfg.NoInlines
switch cfg.Granularity {
case "":
function = true // Default granularity is "functions"
case "addresses":
if inlines {
return nil
Expand Down
22 changes: 18 additions & 4 deletions internal/driver/html/stacks.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,26 @@ body {
width: 100%;
position: relative; /* Allows absolute positioning of child boxes */
}
/* Shows details of frame that is under the mouse */
/* Holder for current frame details. */
#current-details {
position: absolute;
top: 5px;
right: 5px;
position: relative;
background: #eee; /* Light grey gives better contrast with boxes */
font-size: 12pt;
padding: 0 4px;
width: 100%;
}
/* Shows details of frame that is under the mouse */
#current-details-left {
float: left;
max-width: 60%;
white-space: nowrap;
overflow-x: hidden;
}
#current-details-right {
float: right;
max-width: 40%;
white-space: nowrap;
overflow-x: hidden;
}
/* Background of a single flame-graph frame */
.boxbg {
Expand Down
5 changes: 4 additions & 1 deletion internal/driver/html/stacks.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
</head>
<body>
{{template "header" .}}
<div id="current-details">
<div id="current-details-left"></div>
<div id="current-details-right"> </div>
</div>
<div id="stack-holder">
<div id="stack-chart"></div>
<div id="current-details"></div>
</div>
<div id="action-menu" class="submenu">
<span id="action-title"></span>
Expand Down
72 changes: 45 additions & 27 deletions internal/driver/html/stacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function stackViewer(stacks, nodes) {
let actionMenuOn = false; // Is action menu visible?
let actionTarget = null; // Box on which action menu is operating.
let diff = false; // Are we displaying a diff?
let shown = 0; // How many profile values are being displayed?

for (const stack of stacks.Stacks) {
if (stack.Value < 0) {
Expand All @@ -39,7 +40,8 @@ function stackViewer(stacks, nodes) {
const search = find('search');
const actions = find('action-menu');
const actionTitle = find('action-title');
const detailBox = find('current-details');
const leftDetailBox = find('current-details-left');
const rightDetailBox = find('current-details-right');

window.addEventListener('resize', render);
window.addEventListener('popstate', render);
Expand Down Expand Up @@ -69,6 +71,7 @@ function stackViewer(stacks, nodes) {
}});

render();
clearDetails();

// Helper functions follow:

Expand Down Expand Up @@ -176,17 +179,27 @@ function stackViewer(stacks, nodes) {
if (actionMenuOn) return;
const src = stacks.Sources[box.src];
div.title = details(box) + ' │ ' + src.FullName + (src.Inlined ? "\n(inlined)" : "");
detailBox.innerText = summary(box.sumpos, box.sumneg);
leftDetailBox.innerText = src.FullName + (src.Inlined ? " (inlined)" : "");
let timing = summary(box.sumpos, box.sumneg);
if (box.self != 0) {
timing = "self " + unitText(box.self) + " │ " + timing;
}
rightDetailBox.innerText = timing;
// Highlight all boxes that have the same source as box.
toggleClass(box.src, 'hilite2', true);
}

function handleLeave(box) {
if (actionMenuOn) return;
detailBox.innerText = '';
clearDetails();
toggleClass(box.src, 'hilite2', false);
}

function clearDetails() {
leftDetailBox.innerText = '';
rightDetailBox.innerText = percentText(shown);
}

// Return list of sources that match the regexp given by the 'p' URL parameter.
function urlPivots() {
const pivots = [];
Expand Down Expand Up @@ -231,10 +244,14 @@ function stackViewer(stacks, nodes) {
const x = PADDING;
const y = 0;

// Show summary for pivots if we are actually pivoting.
const showPivotSummary = !(pivots.length == 1 && pivots[0] == 0);

shown = pos + neg;
displayList.length = 0;
renderStacks(0, xscale, x, y, places, +1); // Callees
renderStacks(0, xscale, x, y-ROW, places, -1); // Callers (ROW left for separator)
display(xscale, pos, neg, displayList);
display(xscale, pos, neg, displayList, showPivotSummary);
}

// renderStacks creates boxes with top-left at x,y with children drawn as
Expand Down Expand Up @@ -262,22 +279,22 @@ function stackViewer(stacks, nodes) {
// // Group represents a displayed (sub)tree.
// interface Group {
// name: string; // Full name of source
// src: number; // Index in stacks.Sources
// src: number; // Index in stacks.Sources
// self: number; // Contribution as leaf (may be < 0 for diffs)
// sumpos: number; // Sum of |self| of positive nodes in tree (>= 0)
// sumneg: number; // Sum of |self| of negative nodes in tree (>= 0)
// sumpos: number; // Sum of |self| of positive nodes in tree (>= 0)
// sumneg: number; // Sum of |self| of negative nodes in tree (>= 0)
// places: Place[]; // Stack slots that contributed to this group
// }
//
// // Box is a rendered item.
// interface Box {
// x: number; // X coordinate of top-left
// y: number; // Y coordinate of top-left
// width: number; // Width of box to display
// src: number; // Index in stacks.Sources
// sumpos: number; // From corresponding Group
// sumneg: number; // From corresponding Group
// self: number; // From corresponding Group
// x: number; // X coordinate of top-left
// y: number; // Y coordinate of top-left
// width: number; // Width of box to display
// src: number; // Index in stacks.Sources
// sumpos: number; // From corresponding Group
// sumneg: number; // From corresponding Group
// self: number; // From corresponding Group
// };

function groupWidth(xscale, g) {
Expand All @@ -297,14 +314,14 @@ function stackViewer(stacks, nodes) {
y: y,
width: width,
src: g.src,
sumpos: g.sumpos,
sumneg: g.sumneg,
sumpos: g.sumpos,
sumneg: g.sumneg,
self: g.self,
};
displayList.push(box);
if (direction > 0) {
// Leave gap on left hand side to indicate self contribution.
x += xscale*Math.abs(g.self);
// Leave gap on left hand side to indicate self contribution.
x += xscale*Math.abs(g.self);
}
}
y += direction * ROW;
Expand Down Expand Up @@ -354,9 +371,9 @@ function stackViewer(stacks, nodes) {
groups.push(group);
}
if (stack.Value < 0) {
group.sumneg += -stack.Value;
group.sumneg += -stack.Value;
} else {
group.sumpos += stack.Value;
group.sumpos += stack.Value;
}
group.self += (place.Pos == stack.Sources.length-1) ? stack.Value : 0;
group.places.push(place);
Expand All @@ -372,7 +389,7 @@ function stackViewer(stacks, nodes) {
return groups;
}

function display(xscale, posTotal, negTotal, list) {
function display(xscale, posTotal, negTotal, list, showPivotSummary) {
// Sort boxes so that text selection follows a predictable order.
list.sort(function(a, b) {
if (a.y != b.y) return a.y - b.y;
Expand All @@ -381,14 +398,15 @@ function stackViewer(stacks, nodes) {

// Adjust Y coordinates so that zero is at top.
let adjust = (list.length > 0) ? list[0].y : 0;
adjust -= ROW + 2*PADDING; // Room for details

const divs = [];
for (const box of list) {
box.y -= adjust;
divs.push(drawBox(xscale, box));
}
divs.push(drawSep(-adjust, posTotal, negTotal));
if (showPivotSummary) {
divs.push(drawSep(-adjust, posTotal, negTotal));
}

const h = (list.length > 0 ? list[list.length-1].y : 0) + 4*ROW;
chart.style.height = h+'px';
Expand Down Expand Up @@ -423,8 +441,8 @@ function stackViewer(stacks, nodes) {
const delta = box.sumpos - box.sumneg;
const partWidth = xscale * Math.abs(delta);
if (partWidth >= MIN_WIDTH) {
r.appendChild(makeRect((delta < 0 ? 'negative' : 'positive'),
0, 0, partWidth, ROW-1));
r.appendChild(makeRect((delta < 0 ? 'negative' : 'positive'),
0, 0, partWidth, ROW-1));
}
}

Expand Down Expand Up @@ -522,9 +540,9 @@ function stackViewer(stacks, nodes) {
seen.add(place.Stack);
const stack = stacks.Stacks[place.Stack];
if (stack.Value < 0) {
neg += -stack.Value;
neg += -stack.Value;
} else {
pos += stack.Value;
pos += stack.Value;
}
}
return [pos, neg];
Expand Down
3 changes: 3 additions & 0 deletions internal/driver/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ func printCurrentOptions(p *profile.Profile, ui plugin.UI) {
// Add quotes for empty values.
v = `""`
}
if n == "granularity" && v == "" {
v = "(default)"
}
if comment != "" {
comment = commentStart + " " + comment
}
Expand Down
34 changes: 15 additions & 19 deletions internal/driver/interactive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,40 +186,36 @@ func TestInteractiveCommands(t *testing.T) {
{
"top 10 --cum focus1 -ignore focus2",
map[string]string{
"granularity": "functions",
"nodecount": "10",
"sort": "cum",
"focus": "focus1|focus2",
"ignore": "ignore",
"nodecount": "10",
"sort": "cum",
"focus": "focus1|focus2",
"ignore": "ignore",
},
},
{
"top10 --cum focus1 -ignore focus2",
map[string]string{
"granularity": "functions",
"nodecount": "10",
"sort": "cum",
"focus": "focus1|focus2",
"ignore": "ignore",
"nodecount": "10",
"sort": "cum",
"focus": "focus1|focus2",
"ignore": "ignore",
},
},
{
"dot",
map[string]string{
"granularity": "functions",
"nodecount": "80",
"sort": "flat",
"nodecount": "80",
"sort": "flat",
},
},
{
"tags -ignore1 -ignore2 focus1 >out",
map[string]string{
"granularity": "functions",
"nodecount": "80",
"sort": "flat",
"output": "out",
"tagfocus": "focus1",
"tagignore": "ignore1|ignore2",
"nodecount": "80",
"sort": "flat",
"output": "out",
"tagfocus": "focus1",
"tagignore": "ignore1|ignore2",
},
},
{
Expand Down
4 changes: 3 additions & 1 deletion internal/driver/stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func (ui *webInterface) stackView(w http.ResponseWriter, req *http.Request) {
rpt, errList := ui.makeReport(w, req, []string{"svg"}, func(cfg *config) {
cfg.CallTree = true
cfg.Trim = false
cfg.Granularity = "filefunctions"
if cfg.Granularity == "" {
cfg.Granularity = "filefunctions"
}
})
if rpt == nil {
return // error already reported
Expand Down
24 changes: 22 additions & 2 deletions internal/report/shortnames.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,38 @@
package report

import (
"path/filepath"
"regexp"

"github.com/google/pprof/internal/graph"
)

var sepRE = regexp.MustCompile(`::|\.`)
var (
sepRE = regexp.MustCompile(`::|\.`)
fileSepRE = regexp.MustCompile(`/`)
)

// fileNameSuffixes returns a non-empty sequence of shortened file names
// (in decreasing preference) that can be used to represent name.
func fileNameSuffixes(name string) []string {
if name == "" {
// Avoid returning "." when symbol info is missing
return []string{""}
}
return allSuffixes(filepath.ToSlash(filepath.Clean(name)), fileSepRE)
}

// shortNameList returns a non-empty sequence of shortened names
// (in decreasing preference) that can be used to represent name.
func shortNameList(name string) []string {
name = graph.ShortenFunctionName(name)
seps := sepRE.FindAllStringIndex(name, -1)
return allSuffixes(name, sepRE)
}

// allSuffixes returns a list of suffixes (in order of decreasing length)
// found by splitting at re.
func allSuffixes(name string, re *regexp.Regexp) []string {
seps := re.FindAllStringIndex(name, -1)
result := make([]string, 0, len(seps)+1)
result = append(result, name)
for _, sep := range seps {
Expand Down
26 changes: 26 additions & 0 deletions internal/report/shortnames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,29 @@ func TestShortNames(t *testing.T) {
})
}
}

func TestFileNameSuffixes(t *testing.T) {
type testCase struct {
name string
in string
out []string
}
test := func(name, in string, out ...string) testCase {
return testCase{name, in, out}
}

for _, c := range []testCase{
test("empty", "", ""),
test("simple", "foo", "foo"),
test("manypaths", "a/b/c", "a/b/c", "b/c", "c"),
test("leading", "/a/b", "/a/b", "a/b", "b"),
test("trailing", "a/b", "a/b", "b"),
} {
t.Run(c.name, func(t *testing.T) {
got := fileNameSuffixes(c.in)
if !reflect.DeepEqual(c.out, got) {
t.Errorf("fileNameSuffixes(%q) = %#v, expecting %#v", c.in, got, c.out)
}
})
}
}
Loading

0 comments on commit f30d439

Please sign in to comment.