Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: Anastasios Papagiannis <[email protected]>
  • Loading branch information
tpapagian committed Mar 13, 2024
1 parent 67bdc67 commit f6f6beb
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 52 deletions.
2 changes: 1 addition & 1 deletion bpf/include/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static __u64 BPF_FUNC(get_attach_cookie, void *ctx);

static long BPF_FUNC(loop, __u32 nr_loops, void *callback_fn, void *callback_ctx, __u64 flags);

static long BPF_FUNC(ringbuf_output, void *data, uint64_t size, uint64_t flags);
static long BPF_FUNC(ringbuf_output, void *ringbuf, void *data, uint64_t size, uint64_t flags);
static void BPF_FUNC(ringbuf_reserve, void *ringbuf, uint64_t size, uint64_t flags);
static void BPF_FUNC(ringbuf_submit, void *data, uint64_t flags);
static void BPF_FUNC(ringbuf_discard, void *data, uint64_t flags);
Expand Down
5 changes: 2 additions & 3 deletions bpf/lib/bpf_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ struct event {
};

struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__type(key, int);
__type(value, struct event);
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 8LU * 1024LU * 1024LU); // 8MB
} tcpmon_map SEC(".maps");

#endif // __BPF_EVENT_H
2 changes: 1 addition & 1 deletion bpf/lib/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ static inline __attribute__((always_inline)) void perf_event_output_metric(void
__u32 zero = 0;
long err;

err = perf_event_output(ctx, map, flags, data, size);
err = ringbuf_output(map, data, size, 0);
if (err < 0) {
valp = map_lookup_elem(&tg_stats_map, &zero);
if (valp)
Expand Down
36 changes: 2 additions & 34 deletions pkg/bpf/perf_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,15 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"

"golang.org/x/sys/unix"
)

const (
PossibleCPUSysfsPath = "/sys/devices/system/cpu/possible"
)

type PerfEventConfig struct {
NumCpus int
NumPages int
MapName string
Type int
Config int
SampleType int
WakeupEvents int
MapName string
}

// GetNumPossibleCPUs returns a total number of possible CPUS, i.e. CPUs that
Expand Down Expand Up @@ -74,30 +65,7 @@ func getNumPossibleCPUsFromReader(r io.Reader) int {
// DefaultPerfEventConfig returns the default perf event configuration. It
// relies on the map root to be set.
func DefaultPerfEventConfig() *PerfEventConfig {
numCpus := GetNumPossibleCPUs()
if numCpus == 0 {
numCpus = runtime.NumCPU()
}
return &PerfEventConfig{
MapName: filepath.Join(MapPrefixPath(), eventsMapName),
Type: PERF_TYPE_SOFTWARE,
Config: PERF_COUNT_SW_BPF_OUTPUT,
SampleType: PERF_SAMPLE_RAW,
WakeupEvents: 1,
NumCpus: numCpus,
NumPages: 128,
}
}

func UpdateElementFromPointers(fd int, structPtr, sizeOfStruct uintptr) error {
ret, _, err := unix.Syscall(
unix.SYS_BPF,
BPF_MAP_UPDATE_ELEM,
structPtr,
sizeOfStruct,
)
if ret != 0 || err != 0 {
return fmt.Errorf("Unable to update element for map with file descriptor %d: %s", fd, err)
MapName: filepath.Join(MapPrefixPath(), eventsMapName),
}
return nil
}
19 changes: 6 additions & 13 deletions pkg/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"time"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/perf"
"github.com/cilium/ebpf/ringbuf"
"github.com/cilium/tetragon/pkg/api/ops"
"github.com/cilium/tetragon/pkg/api/readyapi"
"github.com/cilium/tetragon/pkg/bpf"
Expand Down Expand Up @@ -211,11 +211,9 @@ func (k *Observer) RunEvents(stopCtx context.Context, ready func()) error {
}
defer perfMap.Close()

rbSize := k.getRBSize(int(perfMap.MaxEntries()))
perfReader, err := perf.NewReader(perfMap, rbSize)

rd, err := ringbuf.NewReader(perfMap)
if err != nil {
return fmt.Errorf("creating perf array reader failed: %w", err)
return fmt.Errorf("opening ringbuf reader: %w", err)
}

// Inform caller that we're about to start processing events.
Expand All @@ -224,7 +222,7 @@ func (k *Observer) RunEvents(stopCtx context.Context, ready func()) error {

// We spawn go routine to read and process perf events,
// connected with main app through eventsQueue channel.
eventsQueue := make(chan *perf.Record, k.getRBQueueSize())
eventsQueue := make(chan *ringbuf.Record, k.getRBQueueSize())

// Listeners are ready and about to start reading from perf reader, tell
// user everything is ready.
Expand All @@ -237,7 +235,7 @@ func (k *Observer) RunEvents(stopCtx context.Context, ready func()) error {
go func() {
defer wg.Done()
for stopCtx.Err() == nil {
record, err := perfReader.Read()
record, err := rd.Read()
if err != nil {
// NOTE(JM and Djalal): count and log errors while excluding the stopping context
if stopCtx.Err() == nil {
Expand All @@ -256,11 +254,6 @@ func (k *Observer) RunEvents(stopCtx context.Context, ready func()) error {
k.recvCntr++
ringbufmetrics.PerfEventReceived.Inc()
}

if record.LostSamples > 0 {
atomic.AddUint64(&k.lostCntr, uint64(record.LostSamples))
ringbufmetrics.PerfEventLost.Add(float64(record.LostSamples))
}
}
}
}()
Expand Down Expand Up @@ -290,7 +283,7 @@ func (k *Observer) RunEvents(stopCtx context.Context, ready func()) error {

// Wait for context to be cancelled and then stop.
<-stopCtx.Done()
return perfReader.Close()
return rd.Close()
}

// Observer represents the link between the BPF perf ring and the listeners. It
Expand Down
6 changes: 6 additions & 0 deletions vendor/github.com/cilium/ebpf/ringbuf/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f6f6beb

Please sign in to comment.