go/src/runtime/trace/batch.go

89 lines
2.2 KiB
Go
Raw Normal View History

runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
// Copyright 2025 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.
package trace
import (
"fmt"
"internal/trace/tracev2"
)
// timestamp is an unprocessed timestamp.
type timestamp uint64
type batch struct {
time timestamp
gen uint64
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
data []byte
}
// readBatch copies b and parses the trace batch header inside.
// Returns the batch, bytes read, and an error.
func readBatch(b []byte) (batch, uint64, error) {
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
if len(b) == 0 {
return batch{}, 0, fmt.Errorf("batch is empty")
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
}
data := make([]byte, len(b))
copy(data, b)
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
// Read batch header byte.
if typ := tracev2.EventType(b[0]); typ == tracev2.EvEndOfGeneration {
if len(b) != 1 {
return batch{}, 1, fmt.Errorf("unexpected end of generation in batch of size >1")
}
return batch{data: data}, 1, nil
}
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
if typ := tracev2.EventType(b[0]); typ != tracev2.EvEventBatch && typ != tracev2.EvExperimentalBatch {
return batch{}, 1, fmt.Errorf("expected batch event, got event %d", typ)
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
}
total := 1
b = b[1:]
// Read the generation
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
gen, n, err := readUvarint(b)
if err != nil {
return batch{}, uint64(total + n), fmt.Errorf("error reading batch gen: %w", err)
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
}
total += n
b = b[n:]
// Read the M (discard it).
_, n, err = readUvarint(b)
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
if err != nil {
return batch{}, uint64(total + n), fmt.Errorf("error reading batch M ID: %w", err)
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
}
total += n
b = b[n:]
// Read the timestamp.
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
ts, n, err := readUvarint(b)
if err != nil {
return batch{}, uint64(total + n), fmt.Errorf("error reading batch timestamp: %w", err)
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
}
total += n
b = b[n:]
// Read the size of the batch to follow.
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
size, n, err := readUvarint(b)
if err != nil {
return batch{}, uint64(total + n), fmt.Errorf("error reading batch size: %w", err)
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
}
if size > tracev2.MaxBatchSize {
return batch{}, uint64(total + n), fmt.Errorf("invalid batch size %d, maximum is %d", size, tracev2.MaxBatchSize)
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
}
total += n
total += int(size)
if total != len(data) {
return batch{}, uint64(total), fmt.Errorf("expected complete batch")
}
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
data = data[:total]
// Return the batch.
return batch{
gen: gen,
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
time: timestamp(ts),
data: data,
}, uint64(total), nil
runtime/trace: add the flight recorder This change adds the flight recorder to the trace package. Flight recording is a technique in which trace data is kept in a circular buffer and can be flushed upon request. The implementation will be added in follow-up CLs. The flight recorder has already been implemented inside of the golang.org/x/exp/trace package. This copies the current implementation and modifies it to work within the runtime/trace package. The changes include: This adds the ability for multiple consumers (both the execution tracer and the flight recorder) to subscribe to tracing events. This change allows us to add multiple consumers without making major modifications to the runtime. Future optimizations are planned for this functionality. This removes the use of byte readers from the process that parses and processes the trace batches. This modifies the flight recorder to not parse out the trace clock frequency, since that requires knowledge of the format that's unfortunate to encode in yet another place. Right now, the trace clock frequency is considered stable for the lifetime of the program, so just grab it directly from the runtime. This change adds an in-band end-of-generation signal to the internal implementation of runtime.ReadTrace. The internal implementation is exported via linkname to runtime/trace, so the flight recorder can identify exactly when a generation has ended. This signal is also useful for ensuring that subscribers to runtime trace data always see complete generations, by starting or stopping data streaming only at generation boundaries. For #63185 Change-Id: I5c15345981a6bbe9764a3d623448237e983c64ec Reviewed-on: https://go-review.googlesource.com/c/go/+/673116 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-14 16:13:24 -04:00
}