golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.71k stars 17.5k forks source link

cmd/trace: generate trace graph for custom region selection #30895

Open AlexRouSg opened 5 years ago

AlexRouSg commented 5 years ago

I propose that cmd/trace should have an option in the web UI to generate a trace graph for an arbitrary list of user defined regions.

I want/need to see the timing between certain user defined regions and having such an option in the web ui would be very useful. I could just dump them all into the same task but this means recompiling the program and changing code just to change what regions get shown together. Also if the program failed to call task.End() for any reason, then the trace graph doesn't get generated at all.

ianlancetaylor commented 5 years ago

CC @hyangah @pjweinbgo

bcmills commented 5 years ago

CC @ianthehat

rsc commented 5 years ago

The actual viewer UI is part of Chrome DevTools (now Catapult) and is taken from upstream. The bare-bones web pages around it are generated by cmd/trace, and we could do filtering in those before invoking the DevTools. But how would the regions get defined? That is, how would you express to the tool which regions to keep in the filtered trace?

AlexRouSg commented 5 years ago

Was thinking of a multi list selector in the web UI but if that's too much, I guess a command line arg would be good enough.

Maybe go tool trace -with-regions <Task>/<Region> <No task region> <Region from any task> trace.out Then it generates a trace graph with those regions and maybe put it under localhost/traceregions

ianlancetaylor commented 5 years ago

How is a region defined? Can you give a specific example of the -with-regions argument?

AlexRouSg commented 5 years ago

When I say "regions" I meant trace regions users defined by using runtime/Trace

e.g.

package main

import (
    "context"
    "fmt"
    "runtime/trace"
)

func Example() {
    trace.WithRegion(context.Background(), "foo", func() {
        fmt.Println("foo")
    })

    ctx, task := trace.NewTask(ctx, "awesomeTask")
    trace.WithRegion(ctx, "bar", func() {
        fmt.Println("awesomeTask/bar")
    })

    task.End()
}

And then you execute go test -trace=out.trace && go tool trace -with-regions="awesometask/bar foo" out.trace if a task isn't specified then it pulls in all regions matching the name from any task.

rsc commented 5 years ago

@hyangah and @pjweinbgo, does the command line option in the comment above this one seem reasonable? Also is it implementable? Thanks.

hyangah commented 5 years ago

@AlexRouSg can you elaborate what information you want to see from the output visualization?

AlexRouSg commented 5 years ago

Do you want to see other activities such as goroutines, P assignment, and syscalls associated with the specified task/region?

Or do you just want to see only the task/region timing info (nothing other than that)?

What specific visualization options(trace? goroutine? ...) do you want to be altered?

Like what you get when you go to http://127.0.0.1:49854/trace?taskid=1#0:1284.290663 but instead of seeing events for the selected task, you see events for a selected list of regions that may or may not be in the same task or have a task.

What do you mean by "localhost/traceregions" here?

The URL for accessing the graph. I don't really care about this part.

rsc commented 5 years ago

@hyangah and @pjweinbgo, does this seem like something that can be implemented? I'm not asking you to implement it, just to weigh in on whether it makes sense to accept as a path forward. Thanks.

hyangah commented 5 years ago

@rsc I don't think I fully understand the proposed feature even though I agree that cmd/trace needs more love. Sketch of the desired visualization, or concrete examples may be helpful.

AlexRouSg commented 5 years ago

Does this help?

new

hyangah commented 5 years ago

@AlexRouSg why not just implementing an handler in the server-side, take the region name or regexp as a text input field, and pass it to the handler? (instead of the command line flag).

The visualization needs to be careful when handling the case where there are so many matching regions and tasks.

AlexRouSg commented 5 years ago

I originally wanted a multiselect thing on the webpage, something like: image

But thought that was too much so the command line was an alternative way to get the list to the handler.

hyangah commented 5 years ago

command line requires reparsing of the trace. I think a simple text input box would be good enough.

AlexRouSg commented 5 years ago

I'm fine with a textbox

rsc commented 5 years ago

It sounds like there is an agreement here about a text input box for selecting the trace subsection. I will mark this as accepted but I don't think we plan to do it any time soon. Help welcome.