// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Trace event writing API for trace2runtime.go.

package runtime

import (
	
	
	
)

// traceArg is a simple wrapper type to help ensure that arguments passed
// to traces are well-formed.
type traceArg uint64

// traceEventWriter is the high-level API for writing trace events.
//
// See the comment on traceWriter about style for more details as to why
// this type and its methods are structured the way they are.
type traceEventWriter struct {
	tl traceLocker
}

// eventWriter creates a new traceEventWriter. It is the main entrypoint for writing trace events.
//
// Before creating the event writer, this method will emit a status for the current goroutine
// or proc if it exists, and if it hasn't had its status emitted yet. goStatus and procStatus indicate
// what the status of goroutine or P should be immediately *before* the events that are about to
// be written using the eventWriter (if they exist). No status will be written if there's no active
// goroutine or P.
//
// Callers can elect to pass a constant value here if the status is clear (e.g. a goroutine must have
// been Runnable before a GoStart). Otherwise, callers can query the status of either the goroutine
// or P and pass the appropriate status.
//
// In this case, the default status should be tracev2.GoBad or tracev2.ProcBad to help identify bugs sooner.
func ( traceLocker) ( tracev2.GoStatus,  tracev2.ProcStatus) traceEventWriter {
	if  := .mp.p.ptr();  != nil && !.trace.statusWasTraced(.gen) && .trace.acquireStatus(.gen) {
		.writer().writeProcStatus(uint64(.id), , .trace.inSweep).end()
	}
	if  := .mp.curg;  != nil && !.trace.statusWasTraced(.gen) && .trace.acquireStatus(.gen) {
		.writer().writeGoStatus(uint64(.goid), int64(.mp.procid), , .inMarkAssist, 0 /* no stack */).end()
	}
	return traceEventWriter{}
}

// event writes out a trace event.
func ( traceEventWriter) ( tracev2.EventType,  ...traceArg) {
	.tl.writer().event(, ...).end()
}

// stack takes a stack trace skipping the provided number of frames.
// It then returns a traceArg representing that stack which may be
// passed to write.
func ( traceLocker) ( int) traceArg {
	return traceArg(traceStack(, nil, .gen))
}

// startPC takes a start PC for a goroutine and produces a unique
// stack ID for it.
//
// It then returns a traceArg representing that stack which may be
// passed to write.
func ( traceLocker) ( uintptr) traceArg {
	// +PCQuantum because makeTraceFrame expects return PCs and subtracts PCQuantum.
	return traceArg(trace.stackTab[.gen%2].put([]uintptr{
		logicalStackSentinel,
		startPCForTrace() + sys.PCQuantum,
	}))
}

// string returns a traceArg representing s which may be passed to write.
// The string is assumed to be relatively short and popular, so it may be
// stored for a while in the string dictionary.
func ( traceLocker) ( string) traceArg {
	return traceArg(trace.stringTab[.gen%2].put(.gen, ))
}

// uniqueString returns a traceArg representing s which may be passed to write.
// The string is assumed to be unique or long, so it will be written out to
// the trace eagerly.
func ( traceLocker) ( string) traceArg {
	return traceArg(trace.stringTab[.gen%2].emit(.gen, ))
}

// rtype returns a traceArg representing typ which may be passed to write.
func ( traceLocker) ( *abi.Type) traceArg {
	return traceArg(trace.typeTab[.gen%2].put())
}