github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/trace/v2/viewer.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package trace
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/go-asm/go/trace"
    12  	"github.com/go-asm/go/trace/traceviewer"
    13  	tracev2 "github.com/go-asm/go/trace/v2"
    14  )
    15  
    16  // viewerFrames returns the frames of the stack of ev. The given frame slice is
    17  // used to store the frames to reduce allocations.
    18  func viewerFrames(stk tracev2.Stack) []*trace.Frame {
    19  	var frames []*trace.Frame
    20  	stk.Frames(func(f tracev2.StackFrame) bool {
    21  		frames = append(frames, &trace.Frame{
    22  			PC:   f.PC,
    23  			Fn:   f.Func,
    24  			File: f.File,
    25  			Line: int(f.Line),
    26  		})
    27  		return true
    28  	})
    29  	return frames
    30  }
    31  
    32  func viewerGState(state tracev2.GoState, inMarkAssist bool) traceviewer.GState {
    33  	switch state {
    34  	case tracev2.GoUndetermined:
    35  		return traceviewer.GDead
    36  	case tracev2.GoNotExist:
    37  		return traceviewer.GDead
    38  	case tracev2.GoRunnable:
    39  		return traceviewer.GRunnable
    40  	case tracev2.GoRunning:
    41  		return traceviewer.GRunning
    42  	case tracev2.GoWaiting:
    43  		if inMarkAssist {
    44  			return traceviewer.GWaitingGC
    45  		}
    46  		return traceviewer.GWaiting
    47  	case tracev2.GoSyscall:
    48  		// N.B. A goroutine in a syscall is considered "executing" (state.Executing() == true).
    49  		return traceviewer.GRunning
    50  	default:
    51  		panic(fmt.Sprintf("unknown GoState: %s", state.String()))
    52  	}
    53  }
    54  
    55  func viewerTime(t time.Duration) float64 {
    56  	return float64(t) / float64(time.Microsecond)
    57  }