foldtrace

package
v0.1.75 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2025 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FoldTraceCmd = &cobra.Command{
	Use:   "fold-trace",
	Short: "Trace an execution trace and fold it for visualization",
	Long:  usage,
	RunE: func(cmd *cobra.Command, args []string) error {
		data, err := getInputData(cmd, args)
		if err != nil {
			return fmt.Errorf("unable to read input data: %w", err)
		}
		td := &TraceData{}
		err = json.Unmarshal(data, td)
		if err != nil {
			return fmt.Errorf("unable to unmarshal input json trace: %w", err)
		}

		folded := make(map[string]uint64)

		contexts := []string{*inputRootContextName}
		currentDepth := uint64(1)
		lastLabel := ""

		metricType := *inputMetric
		for idx, op := range td.StructLogs {
			if op.Depth > currentDepth {
				contexts = append(contexts, lastLabel)
			} else if op.Depth < currentDepth {
				contexts = contexts[:len(contexts)-1]
			}
			currentDepth = op.Depth

			currentLabel := op.OP

			if isCall(op.OP) {
				if len(op.Stack) < 6 {
					log.Warn().Int("stackLength", len(op.Stack)).Msg("detected a call with a stack that's too short")
				} else {
					currentLabel = op.OP + " to " + op.Stack[len(op.Stack)-2]
				}
			}
			lastLabel = currentLabel

			var metricValue uint64
			if metricType == "gas" {
				metricValue = op.GasCost
			} else if metricType == "actualgas" {
				metricValue = getActualUsedGas(idx, td)
			} else {
				metricValue = 1
			}
			currentMetricPath := strings.Join(contexts, ";") + ";" + currentLabel

			if !isCall(op.OP) {
				folded[currentMetricPath] += metricValue
			}
			if isCall(op.OP) && len(op.Stack[len(op.Stack)-2]) < 6 {
				folded[currentMetricPath] += metricValue
			}

			log.Trace().Strs("context", contexts).Uint64("depth", currentDepth).Str("currentLabel", currentLabel).Uint64("pc", op.PC).Msg("trace operation")

		}
		for context, metric := range folded {
			fmt.Printf("%s %d\n", context, metric)
		}

		return nil
	},
	Args: func(cmd *cobra.Command, args []string) error {
		err := validateInputMetric()
		if err != nil {
			return err
		}
		return nil
	},
}

Functions

This section is empty.

Types

type TraceData

type TraceData struct {
	StructLogs []TraceOperation `json:"structLogs"`
}

type TraceOperation

type TraceOperation struct {
	PC      uint64   `json:"pc"`
	OP      string   `json:"op"`
	Gas     uint64   `json:"gas"`
	GasCost uint64   `json:"gasCost"`
	Depth   uint64   `json:"depth"`
	Stack   []string `json:"stack"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL