8000 capture: order live timestamps with a shared monotonic clock by tomrford · Pull Request #82 · tomrford/gocan · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions asc/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ var errWriterClosed = errors.New("ASC writer is closed")
// measurement start time.
//
// The header date is written in local time. ASC has no time zone field, and
// Vector tools read the header as local time. A record whose timestamp
// regresses is written at the previous record's time, so offsets never
// decrease.
// Vector tools read the header as local time. Timestamps must not decrease;
// a regressing record is rejected without being written. Equal times are allowed.
type Writer struct {
output *bufio.Writer

Expand Down Expand Up @@ -143,7 +142,7 @@ func (writer *Writer) writeRecord(timestamp time.Time, body string) error {
}
}
if timestamp.Before(writer.last) {
timestamp = writer.last
return fmt.Errorf("ASC timestamp %s precedes previous record %s", timestamp, writer.last)
}

offset := timestamp.Sub(writer.start).Microseconds()
Expand Down
14 changes: 14 additions & 0 deletions asc/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ func TestWriterStreamsCapture(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err := writer.WriteFrame(gocan.FrameEvent{
Bus: 1, Timestamp: start.Add(-time.Nanosecond), Direction: gocan.DirectionReceive, Frame: classic,
}); err == nil {
t.Fatal("accepted frame before measurement start")
}
regressed := state
regressed.Timestamp = state.Timestamp.Add(-time.Nanosecond)
if err := writer.WriteEvent(regressed); err == nil {
t.Fatal("accepted event before previous record")
}
if err := writer.WriteEvent(state); err != nil {
t.Fatalf("equal timestamp: %v", err)
}

remote, err := gocan.NewRemoteFrame(0x321, 8, false)
if err != nil {
Expand Down Expand Up @@ -102,6 +115,7 @@ func TestWriterStreamsCapture(t *testing.T) {
"0.000000 Start of measurement",
"0.000000 1 123 Rx d 2 AA BB",
"0.001000 CAN 1 Status:chip status error passive - TxErr: 132 RxErr: 0",
"0.001000 CAN 1 Status:chip status error passive - TxErr: 132 RxErr: 0",
"0.002000 1 321 Tx r 8",
"0.003000 CANFD 2 Tx 1ABCDEx - 1 1 9 12 00 01 02 03 04 05 06 07 08 09 0A 0B 0 0 7000 0 0 0 0 0",
"0.004000 2 ErrorFrame",
Expand Down
6 changes: 4 additions & 2 deletions bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ var (
// result and records an accepted transmission before returning nil.
//
// Within a bus, each native read or write and its capture append are serialised:
// an accepted transmission is recorded before the next native read. Timestamps
// describe host observation order, not the order of buffered traffic on the wire.
// an accepted transmission is recorded before the next native read. Drivers use
// Capture.RecordFrame and RecordEvent, whose shared clock follows capture append
// order across buses. Timestamps describe host observation order, not the order
// of buffered traffic on the wire.
// Readiness waits do not hold up transmission.
//
// Capture returns the non-nil capture that records this bus's traffic. It
Expand Down
43 changes: 40 additions & 3 deletions capture.go
< 8000 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const (
// Prune discards it, so a layer that captures indefinitely must prune, and
// decides for itself what history is worth keeping.
type Capture struct {
mu sync.RWMutex
mu sync.RWMutex
origin time.Time

generation uint64
chunks []*captureChunk
Expand Down Expand Up @@ -66,6 +67,7 @@ func NewCapture() *Capture {
initialCaptureChunkPayloadCapacity,
)
return &Capture{
origin: time.Now(),
generation: captureEpochs.Add(1),
chunks: []*captureChunk{active},
active: active,
Expand Down Expand Up @@ -448,6 +450,22 @@ func (capture *Capture) prepareNextLocked(recordCapacity, payloadCapacity int) {
// carry UTC timestamps with no monotonic reading, so a round-tripped event
// may not compare equal to the original even though the instants match.
func (capture *Capture) Append(event FrameEvent) error {
return capture.appendFrame(event, false)
}

// RecordFrame records a live frame, replacing event.Timestamp with the capture
// clock. RecordFrame and RecordEvent timestamps follow append order across all
// buses sharing this capture, using monotonic elapsed time from NewCapture's
// wall-clock reading. Later wall-clock adjustments do not affect this clock;
// system sleep may pause it on some platforms. Clear does not reset it.
// Mixing these methods with supplied timestamps in Append or AppendEvent does
// not guarantee timestamp order.
func (capture *Capture) RecordFrame(event FrameEvent) error {
event.Timestamp = capture.origin
return capture.appendFrame(event, true)
}

func (capture *Capture) appendFrame(event FrameEvent, live bool) error {
if err := event.Validate(); err != nil {
return err
}
Expand All @@ -458,6 +476,10 @@ func (capture *Capture) Append(event FrameEvent) error {
capture.mu.Lock()
defer capture.mu.Unlock()

timestamp := event.Timestamp.UnixNano()
if live {
timestamp = capture.origin.UnixNano() + time.Since(capture.origin).Nanoseconds()
}
capture.rotate(payloadLength)
chunk := capture.active

Expand All @@ -472,7 +494,7 @@ func (capture *Capture) Append(event FrameEvent) error {
payloadOffset := chunk.appendPayload(event.Frame.Data[:payloadLength])
recordIndex := uint32(len(chunk.records))
chunk.records = append(chunk.records, makeFrameRecord(
event.Timestamp.UnixNano(),
timestamp,
frameRecordData{
payloadOffset: payloadOffset,
keyIndex: keyState.index,
Expand Down Expand Up @@ -504,13 +526,28 @@ func (capture *Capture) Append(event FrameEvent) error {
// Only the wall-clock reading of event.Timestamp is stored. Retrieved events
// carry UTC timestamps with no monotonic reading.
func (capture *Capture) AppendEvent(event Event) error {
return capture.appendEvent(event, false)
}

// RecordEvent records a live non-frame event, replacing event.Timestamp with
// the same capture clock as RecordFrame.
func (capture *Capture) RecordEvent(event Event) error {
event.Timestamp = capture.origin
return capture.appendEvent(event, true)
}

func (capture *Capture) appendEvent(event Event, live bool) error {
if err := event.Validate(); err != nil {
return err
}

capture.mu.Lock()
defer capture.mu.Unlock()

timestamp := event.Timestamp.UnixNano()
if live {
timestamp = capture.origin.UnixNano() + time.Since(capture.origin).Nanoseconds()
}
capture.rotate(0)
chunk := capture.active
eventState, ok := chunk.eventStates[event.Bus]
Expand All @@ -520,7 +557,7 @@ func (capture *Capture) AppendEvent(event Event) error {

recordIndex := uint32(len(chunk.records))
chunk.records = append(chunk.records, makeEventRecord(
event.Timestamp.UnixNano(),
timestamp,
eventRecordData{
bus: event.Bus,
previous: eventState.lastRecord,
Expand Down
62 changes: 57 additions & 5 deletions capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,20 @@ func requireBusEvents(t *testing.T, label string, got, want []Event) {
}

type captureWriterProbe struct {
failAt int
failure error
calls int
frames []uint32
events []EventKind
failAt int
failure error
calls int
frames []uint32
events []EventKind
timestamps []time.Time
}

func (writer *captureWriterProbe) WriteFrame(event FrameEvent) error {
if err := writer.next(); err != nil {
return err
}
writer.frames = append(writer.frames, event.Frame.ID)
writer.timestamps = append(writer.timestamps, event.Timestamp)
return nil
}

Expand All @@ -88,6 +90,7 @@ func (writer *captureWriterProbe) WriteEvent(event Event) error {
return err
}
writer.events = append(writer.events, event.Kind)
writer.timestamps = append(writer.timestamps, event.Timestamp)
return nil
}

Expand All @@ -100,6 +103,55 @@ func (writer *captureWriterProbe) next() error {
return nil
}

func TestCaptureLiveClock(t *testing.T) {
start := time.Now()
capture := NewCapture()
if err := capture.RecordFrame(FrameEvent{Bus: 1, Direction: DirectionReceive, Frame: Frame{ID: MaxExtendedID + 1}}); err == nil {
t.Fatal("accepted invalid live frame")
}
if err := capture.RecordEvent(Event{Bus: 1, Kind: EventControllerState}); err == nil {
t.Fatal("accepted invalid live controller state")
}
if capture.Len() != 0 {
t.Fatal("invalid records changed capture")
}

for range 2 {
var writers sync.WaitGroup
for bus := BusID(1); bus <= 4; bus++ {
writers.Go(func() {
for range 1024 {
// Supplied old times are ignored by live recording.
if err := capture.RecordFrame(FrameEvent{Bus: bus, Timestamp: captureTestBase, Direction: DirectionReceive}); err != nil {
t.Error(err)
return
}
if err := capture.RecordEvent(Event{Bus: bus, Kind: EventErrorFrame}); err != nil {
t.Error(err)
return
}
}
})
}
writers.Wait()
end := time.Now()
writer := &captureWriterProbe{}
if _, err := capture.WriteRecordsSince(Cursor{}, writer); err != nil {
t.Fatal(err)
}
if len(writer.timestamps) != 8192 {
t.Fatalf("record count = %d, want 8192", len(writer.timestamps))
}
for i, timestamp := range writer.timestamps {
if timestamp.Before(start) || timestamp.After(end) {
t.Fatalf("record %d timestamp %s outside ordered interval [%s, %s]", i, timestamp, start, end)
}
start = timestamp
}
capture.Clear()
}
}

// newTestCapture builds a capture whose first chunk is small enough for tests
// to seal, without the prepared successor so rotation exercises the
// synchronous fallback path.
Expand Down
58 changes: 20 additions & 38 deletions drivers/internal/pcan/pcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/binary"
"errors"
"fmt"
"time"

"github.com/tomrford/gocan"
)
Expand Down Expand Up @@ -228,16 +227,15 @@ func decodePCANReceive(
fdAPI bool,
status pcanStatus,
bus gocan.BusID,
timestamp time.Time,
) (pcanReceiveObservation, error) {
if frameStatus, ok := decodeStatusFrame(messageType, data); ok {
return decodePCANStatus(frameStatus, bus, timestamp)
return decodePCANStatus(frameStatus, bus)
}
if messageType&pcanMessageError != 0 {
return decodePCANErrorFrame(data, status, bus, timestamp)
return decodePCANErrorFrame(data, status, bus)
}
if status != pcanStatusOK {
return decodePCANStatus(status, bus, timestamp)
return decodePCANStatus(status, bus)
}
frame, err := decodeMessage(id, messageType, dlc, data, fdAPI)
if err != nil {
Expand All @@ -250,29 +248,19 @@ func decodePCANErrorFrame(
data []byte,
status pcanStatus,
bus gocan.BusID,
timestamp time.Time,
) (pcanReceiveObservation, error) {
if len(data) < 4 {
return pcanReceiveObservation{}, fmt.Errorf("PCAN error frame has %d data bytes", len(data))
}
errorEvent, err := gocan.NewErrorFrameEvent(bus, timestamp)
if err != nil {
return pcanReceiveObservation{}, err
}
errorEvent := gocan.Event{Bus: bus, Kind: gocan.EventErrorFrame}
var observation pcanReceiveObservation
observation.addEvent(errorEvent)

if status&pcanStatusBusOff != 0 {
state, err := gocan.NewControllerStateEvent(
bus,
timestamp,
gocan.ControllerBusOff,
0,
0,
false,
)
if err != nil {
return pcanReceiveObservation{}, err
state := gocan.Event{
Bus: bus,
Kind: gocan.EventControllerState,
ControllerState: gocan.ControllerBusOff,
}
observation.addEvent(state)
observation.terminal = fmt.Errorf("%w: PCAN controller entered bus-off", gocan.ErrBusOff)
Expand All @@ -287,16 +275,13 @@ func decodePCANErrorFrame(
} else if txErrorCount >= 96 || rxErrorCount >= 96 {
state = gocan.ControllerWarning
}
stateEvent, err := gocan.NewControllerStateEvent(
bus,
timestamp,
state,
txErrorCount,
rxErrorCount,
true,
)
if err != nil {
return pcanReceiveObservation{}, err
stateEvent := gocan.Event{
Bus: bus,
Kind: gocan.EventControllerState,
ControllerState: state,
TXErrorCount: txErrorCount,
RXErrorCount: rxErrorCount,
ErrorCountsKnown: true,
}
observation.addEvent(stateEvent)
return observation, nil
Expand All @@ -305,13 +290,9 @@ func decodePCANErrorFrame(
func decodePCANStatus(
status pcanStatus,
bus gocan.BusID,
timestamp time.Time,
) (pcanReceiveObservation, error) {
if status&(pcanStatusOverrun|pcanStatusQueueOverrun) != 0 {
event, err := gocan.NewReceiveOverrunEvent(bus, timestamp)
if err != nil {
return pcanReceiveObservation{}, err
}
event := gocan.Event{Bus: bus, Kind: gocan.EventReceiveOverrun}
observation := pcanReceiveObservation{
terminal: fmt.Errorf("%w: PCAN status %#08x", gocan.ErrReceiveOverrun, uint32(status)),
}
Expand All @@ -332,9 +313,10 @@ func decodePCANStatus(
default:
return pcanReceiveObservation{}, fmt.Errorf("unsupported PCAN receive status %#08x", uint32(status))
}
event, err := gocan.NewControllerStateEvent(bus, timestamp, state, 0, 0, false)
if err != nil {
return pcanReceiveObservation{}, err
event := gocan.Event{
Bus: bus,
Kind: gocan.EventControllerState,
ControllerState: state,
}
var observation pcanReceiveObservation
observation.addEvent(event)
Expand Down
Loading
0