Skip to content

Commit

Permalink
Add CPU id to trace and trace metadata (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnurizen authored Nov 21, 2024
1 parent 2c6d1ac commit 03ee436
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ type Trace struct {
TID libpf.PID
APMTraceID libpf.APMTraceID
APMTransactionID libpf.APMTransactionID
CPU int
}
1 change: 1 addition & 0 deletions reporter/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type TraceEventMeta struct {
Comm string
APMServiceName string
PID, TID libpf.PID
CPU int
}

type TraceReporter interface {
Expand Down
1 change: 1 addition & 0 deletions tracehandler/tracehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func (m *traceHandler) HandleTrace(bpfTrace *host.Trace) {
PID: bpfTrace.PID,
TID: bpfTrace.TID,
APMServiceName: "", // filled in below
CPU: bpfTrace.CPU,
}

if !m.reporter.SupportsReportTraceEvent() {
Expand Down
4 changes: 2 additions & 2 deletions tracer/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func startPerfEventMonitor(ctx context.Context, perfEventMap *ebpf.Map,
// calls. Returns a function that can be called to retrieve perf event array
// error counts.
func startPollingPerfEventMonitor(ctx context.Context, perfEventMap *ebpf.Map,
pollFrequency time.Duration, perCPUBufferSize int, triggerFunc func([]byte),
pollFrequency time.Duration, perCPUBufferSize int, triggerFunc func([]byte, int),
) func() (lost, noData, readError uint64) {
eventReader, err := perf.NewReader(perfEventMap, perCPUBufferSize)
if err != nil {
Expand Down Expand Up @@ -178,7 +178,7 @@ func startPollingPerfEventMonitor(ctx context.Context, perfEventMap *ebpf.Map,
noDataCount.Add(1)
continue
}
triggerFunc(data.RawSample)
triggerFunc(data.RawSample, data.CPU)
}
}
}()
Expand Down
7 changes: 4 additions & 3 deletions tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ func (t *Tracer) eBPFMetricsCollector(
//
// If the raw trace contains a kernel stack ID, the kernel stack is also
// retrieved and inserted at the appropriate position.
func (t *Tracer) loadBpfTrace(raw []byte) *host.Trace {
func (t *Tracer) loadBpfTrace(raw []byte, cpu int) *host.Trace {
frameListOffs := int(unsafe.Offsetof(C.Trace{}.frames))

if len(raw) < frameListOffs {
Expand All @@ -863,6 +863,7 @@ func (t *Tracer) loadBpfTrace(raw []byte) *host.Trace {
PID: libpf.PID(ptr.pid),
TID: libpf.PID(ptr.tid),
KTime: times.KTime(ptr.ktime),
CPU: cpu,
}

// Trace fields included in the hash:
Expand Down Expand Up @@ -912,8 +913,8 @@ func (t *Tracer) StartMapMonitors(ctx context.Context, traceOutChan chan *host.T
eventMetricCollector := t.startEventMonitor(ctx)

startPollingPerfEventMonitor(ctx, t.ebpfMaps["trace_events"], t.intervals.TracePollInterval(),
t.samplesPerSecond*int(unsafe.Sizeof(C.Trace{})), func(rawTrace []byte) {
traceOutChan <- t.loadBpfTrace(rawTrace)
t.samplesPerSecond*int(unsafe.Sizeof(C.Trace{})), func(rawTrace []byte, cpu int) {
traceOutChan <- t.loadBpfTrace(rawTrace, cpu)
})

pidEvents := make([]uint32, 0)
Expand Down

0 comments on commit 03ee436

Please sign in to comment.