1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
|
//go:build linux
package perf
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"os"
"syscall"
"testing"
"time"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/testutils/fdtrace"
"github.com/go-quicktest/qt"
)
var (
readTimeout = 250 * time.Millisecond
)
func TestMain(m *testing.M) {
fdtrace.TestMain(m)
}
func TestPerfReader(t *testing.T) {
events := perfEventArray(t)
rd, err := NewReader(events, 4096)
if err != nil {
t.Fatal(err)
}
defer rd.Close()
qt.Assert(t, qt.Equals(rd.BufferSize(), 4096))
outputSamples(t, events, 5, 5)
_, rem := checkRecord(t, rd)
qt.Assert(t, qt.IsTrue(rem >= 5), qt.Commentf("expected at least 5 Remaining"))
_, rem = checkRecord(t, rd)
qt.Assert(t, qt.Equals(rem, 0), qt.Commentf("expected zero Remaining"))
rd.SetDeadline(time.Now().Add(4 * time.Millisecond))
_, err = rd.Read()
qt.Assert(t, qt.ErrorIs(err, os.ErrDeadlineExceeded), qt.Commentf("expected os.ErrDeadlineExceeded"))
}
func TestReaderSetDeadline(t *testing.T) {
events := perfEventArray(t)
rd, err := NewReader(events, 4096)
if err != nil {
t.Fatal(err)
}
defer rd.Close()
rd.SetDeadline(time.Now().Add(-time.Second))
if _, err := rd.Read(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Error("Expected os.ErrDeadlineExceeded from first Read, got:", err)
}
if _, err := rd.Read(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Error("Expected os.ErrDeadlineExceeded from second Read, got:", err)
}
rd.SetDeadline(time.Now().Add(10 * time.Millisecond))
if _, err := rd.Read(); !errors.Is(err, os.ErrDeadlineExceeded) {
t.Error("Expected os.ErrDeadlineExceeded from third Read, got:", err)
}
}
func TestReaderSetDeadlinePendingEvents(t *testing.T) {
events := perfEventArray(t)
rd, err := NewReaderWithOptions(events, 4096, ReaderOptions{WakeupEvents: 2})
if err != nil {
t.Fatal(err)
}
defer rd.Close()
outputSamples(t, events, 5)
rd.SetDeadline(time.Now().Add(-time.Second))
_, rem := checkRecord(t, rd)
qt.Assert(t, qt.Equals(rem, 0), qt.Commentf("expected zero Remaining"))
outputSamples(t, events, 5)
// another sample should not be returned before we get ErrFlushed to indicate initial set of samples read
_, err = rd.Read()
if !errors.Is(err, os.ErrDeadlineExceeded) {
t.Error("Expected os.ErrDeadlineExceeded from second Read, got:", err)
}
// the second sample should now be read
_, _ = checkRecord(t, rd)
}
func TestReaderFlushPendingEvents(t *testing.T) {
testutils.LockOSThreadToSingleCPU(t)
events := perfEventArray(t)
rd, err := NewReaderWithOptions(events, 4096, ReaderOptions{WakeupEvents: 2})
if err != nil {
t.Fatal(err)
}
defer rd.Close()
outputSamples(t, events, 5)
wait := make(chan int)
go func() {
wait <- 0
_, rem := checkRecord(t, rd)
wait <- rem
}()
<-wait
time.Sleep(10 * time.Millisecond)
err = rd.Flush()
qt.Assert(t, qt.IsNil(err))
rem := <-wait
qt.Assert(t, qt.Equals(rem, 0), qt.Commentf("expected zero Remaining"))
outputSamples(t, events, 5)
// another sample should not be returned before we get ErrFlushed to indicate initial set of samples read
_, err = rd.Read()
if !errors.Is(err, ErrFlushed) {
t.Error("Expected ErrFlushed from second Read, got:", err)
}
// the second sample should now be read
_, _ = checkRecord(t, rd)
}
func outputSamples(tb testing.TB, events *ebpf.Map, sampleSizes ...byte) {
prog := outputSamplesProg(tb, events, sampleSizes...)
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(tb, err)
if err != nil {
tb.Fatal(err)
}
if errno := syscall.Errno(-int32(ret)); errno != 0 {
tb.Fatal("Expected 0 as return value, got", errno)
}
}
// outputSamplesProg creates a program which submits a series of samples to a PerfEventArray.
//
// The format of each sample is:
//
// index: 0 1 2 3 ... size - 1
// content: size id 0xff 0xff ... 0xff [padding]
//
// padding is an implementation detail of the perf buffer and 1-7 bytes long. The
// contents are undefined.
func outputSamplesProg(tb testing.TB, events *ebpf.Map, sampleSizes ...byte) *ebpf.Program {
tb.Helper()
// Requires at least 4.9 (0515e5999a46 "bpf: introduce BPF_PROG_TYPE_PERF_EVENT program type")
testutils.SkipOnOldKernel(tb, "4.9", "perf events support")
const bpfFCurrentCPU = 0xffffffff
var maxSampleSize byte
for _, sampleSize := range sampleSizes {
if sampleSize < 2 {
tb.Fatalf("Sample size %d is too small to contain size and counter", sampleSize)
}
if sampleSize > maxSampleSize {
maxSampleSize = sampleSize
}
}
// Fill a buffer on the stack, and stash context somewhere
insns := asm.Instructions{
asm.LoadImm(asm.R0, ^int64(0), asm.DWord),
asm.Mov.Reg(asm.R9, asm.R1),
}
bufDwords := int(maxSampleSize/8) + 1
for i := 0; i < bufDwords; i++ {
insns = append(insns,
asm.StoreMem(asm.RFP, int16(i+1)*-8, asm.R0, asm.DWord),
)
}
for i, sampleSize := range sampleSizes {
insns = append(insns,
// Restore stashed context.
asm.Mov.Reg(asm.R1, asm.R9),
// map
asm.LoadMapPtr(asm.R2, events.FD()),
// flags
asm.LoadImm(asm.R3, bpfFCurrentCPU, asm.DWord),
// buffer
asm.Mov.Reg(asm.R4, asm.RFP),
asm.Add.Imm(asm.R4, int32(bufDwords*-8)),
// buffer[0] = size
asm.StoreImm(asm.R4, 0, int64(sampleSize), asm.Byte),
// buffer[1] = i
asm.StoreImm(asm.R4, 1, int64(i&math.MaxUint8), asm.Byte),
// size
asm.Mov.Imm(asm.R5, int32(sampleSize)),
asm.FnPerfEventOutput.Call(),
)
}
insns = append(insns, asm.Return())
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
License: "GPL",
Type: ebpf.XDP,
Instructions: insns,
})
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() { prog.Close() })
return prog
}
func checkRecord(tb testing.TB, rd *Reader) (id int, remaining int) {
tb.Helper()
rec, err := rd.Read()
qt.Assert(tb, qt.IsNil(err))
qt.Assert(tb, qt.IsTrue(rec.CPU >= 0), qt.Commentf("Record has invalid CPU number"))
size := int(rec.RawSample[0])
qt.Assert(tb, qt.IsTrue(len(rec.RawSample) >= size), qt.Commentf("RawSample is at least size bytes"))
for i, v := range rec.RawSample[2:size] {
qt.Assert(tb, qt.Equals(v, 0xff), qt.Commentf("filler at position %d should match", i+2))
}
// padding is ignored since it's value is undefined.
return int(rec.RawSample[1]), rec.Remaining
}
func TestPerfReaderLostSample(t *testing.T) {
// To generate a lost sample perf record:
//
// 1. Fill the perf ring buffer almost completely, with the output_large program.
// The buffer is sized in number of pages, which are architecture dependant.
//
// 2. Write an extra event that doesn't fit in the space remaining.
//
// 3. Write a smaller event that does fit, with output_single program.
// Lost sample records are generated opportunistically, when the kernel
// is writing an event and realizes that there were events lost previously.
//
// The event size is hardcoded in the test BPF programs, there's no way
// to parametrize it without rebuilding the programs.
//
// The event size needs to be selected so that, for any page size, there are at least
// 48 bytes left in the perf ring page after filling it with a whole number of events:
//
// - PERF_RECORD_LOST: 8 (perf_event_header) + 16 (PERF_RECORD_LOST)
//
// - output_single: 8 (perf_event_header) + 4 (size) + 5 (payload) + 7 (padding to 64bits)
//
// By selecting an event size of the form 2^n + 2^(n+1), for any page size 2^(n+m), m >= 0,
// the number of bytes left, x, after filling a page with a whole number of events is:
//
// 2^(n+m) 2^n * 2^m
// x = 2^n * frac(---------------) <=> x = 2^n * frac(---------------)
// 2^n + 2^(n+1) 2^n + 2^n * 2
//
// 2^n * 2^m
// <=> x = 2^n * frac(---------------)
// 2^n * (1 + 2)
//
// 2^m
// <=> x = 2^n * frac(-----)
// 3
//
// 1 2
// <=> x = 2^n * - or x = 2^n * -
// 3 3
//
// Selecting n = 6, we have:
//
// x = 64 or x = 128, no matter the page size 2^(6+m)
//
// event size = 2^6 + 2^7 = 192
//
// Accounting for perf headers, output_large uses a 180 byte payload:
//
// 8 (perf_event_header) + 4 (size) + 180 (payload)
const (
eventSize = 192
)
var (
pageSize = os.Getpagesize()
maxEvents = (pageSize / eventSize)
)
if remainder := pageSize % eventSize; remainder != 64 && remainder != 128 {
// Page size isn't 2^(6+m), m >= 0
t.Fatal("unsupported page size:", pageSize)
}
var sampleSizes []byte
// Fill the ring with the maximum number of output_large events that will fit,
// and generate a lost event by writing an additional event.
for i := 0; i < maxEvents+1; i++ {
sampleSizes = append(sampleSizes, 180)
}
// Generate a small event to trigger the lost record
sampleSizes = append(sampleSizes, 5)
events := perfEventArray(t)
rd, err := NewReader(events, pageSize)
if err != nil {
t.Fatal(err)
}
defer rd.Close()
outputSamples(t, events, sampleSizes...)
for range sampleSizes {
record, err := rd.Read()
if err != nil {
t.Fatal(err)
}
if record.RawSample == nil && record.LostSamples != 1 {
t.Fatal("Expected a record with LostSamples 1, got", record.LostSamples)
}
}
}
func TestPerfReaderOverwritable(t *testing.T) {
// Smallest buffer size.
pageSize := os.Getpagesize()
const sampleSize = math.MaxUint8
// Account for perf header (8) and size (4), align to 8 bytes as perf does.
realSampleSize := internal.Align(sampleSize+8+4, 8)
maxEvents := pageSize / realSampleSize
var sampleSizes []byte
for i := 0; i < maxEvents; i++ {
sampleSizes = append(sampleSizes, sampleSize)
}
// Append an extra sample that will overwrite the first sample.
sampleSizes = append(sampleSizes, sampleSize)
events := perfEventArray(t)
rd, err := NewReaderWithOptions(events, pageSize, ReaderOptions{Overwritable: true})
if err != nil {
t.Fatal(err)
}
defer rd.Close()
_, err = rd.Read()
qt.Assert(t, qt.ErrorIs(err, errMustBePaused))
outputSamples(t, events, sampleSizes...)
qt.Assert(t, qt.IsNil(rd.Pause()))
rd.SetDeadline(time.Now())
nextID := maxEvents
for i := 0; i < maxEvents; i++ {
id, rem := checkRecord(t, rd)
qt.Assert(t, qt.Equals(id, nextID))
qt.Assert(t, qt.Equals(rem, -1))
nextID--
}
}
func TestPerfReaderOverwritableEmpty(t *testing.T) {
events := perfEventArray(t)
rd, err := NewReaderWithOptions(events, os.Getpagesize(), ReaderOptions{Overwritable: true})
if err != nil {
t.Fatal(err)
}
defer rd.Close()
err = rd.Pause()
if err != nil {
t.Fatal(err)
}
rd.SetDeadline(time.Now().Add(4 * time.Millisecond))
_, err = rd.Read()
qt.Assert(t, qt.ErrorIs(err, os.ErrDeadlineExceeded), qt.Commentf("expected os.ErrDeadlineExceeded"))
err = rd.Resume()
if err != nil {
t.Fatal(err)
}
}
func TestPerfReaderClose(t *testing.T) {
events := perfEventArray(t)
rd, err := NewReader(events, 4096)
if err != nil {
t.Fatal(err)
}
defer rd.Close()
errs := make(chan error, 1)
waiting := make(chan struct{})
go func() {
close(waiting)
_, err := rd.Read()
errs <- err
}()
<-waiting
// Close should interrupt Read
if err := rd.Close(); err != nil {
t.Fatal(err)
}
select {
case <-errs:
case <-time.After(time.Second):
t.Fatal("Close doesn't interrupt Read")
}
// And we should be able to call it multiple times
if err := rd.Close(); err != nil {
t.Fatal(err)
}
if _, err := rd.Read(); err == nil {
t.Fatal("Read on a closed PerfReader doesn't return an error")
}
}
func TestCreatePerfEvent(t *testing.T) {
fd, err := createPerfEvent(0, ReaderOptions{Watermark: 1, Overwritable: false})
if err != nil {
t.Fatal("Can't create perf event:", err)
}
fd.Close()
}
func TestReadRecord(t *testing.T) {
var buf bytes.Buffer
err := binary.Write(&buf, internal.NativeEndian, &perfEventHeader{})
if err != nil {
t.Fatal(err)
}
var rec Record
err = readRecord(&buf, &rec, make([]byte, perfEventHeaderSize), false)
if !IsUnknownEvent(err) {
t.Error("readRecord should return unknown event error, got", err)
}
}
func TestPause(t *testing.T) {
t.Parallel()
events := perfEventArray(t)
rd, err := NewReader(events, 4096)
if err != nil {
t.Fatal(err)
}
defer rd.Close()
// Reader is already unpaused by default. It should be idempotent.
if err = rd.Resume(); err != nil {
t.Fatal(err)
}
// Write a sample. The reader should read it.
prog := outputSamplesProg(t, events, 5)
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil || ret != 0 {
t.Fatal("Can't write sample")
}
if _, err := rd.Read(); err != nil {
t.Fatal(err)
}
// Pause. No notification should trigger.
if err = rd.Pause(); err != nil {
t.Fatal(err)
}
errChan := make(chan error, 1)
go func() {
// Read one notification then send any errors and exit.
_, err := rd.Read()
errChan <- err
}()
ret, _, err = prog.Test(internal.EmptyBPFContext)
if err == nil && ret == 0 {
t.Fatal("Unexpectedly wrote sample while paused")
} // else Success
select {
case err := <-errChan:
// Failure: Pause was unsuccessful.
t.Fatalf("received notification on paused reader: %s", err)
case <-time.After(readTimeout):
// Success
}
// Pause should be idempotent.
if err = rd.Pause(); err != nil {
t.Fatal(err)
}
// Resume. Now notifications should continue.
if err = rd.Resume(); err != nil {
t.Fatal(err)
}
ret, _, err = prog.Test(internal.EmptyBPFContext)
if err != nil || ret != 0 {
t.Fatal("Can't write sample")
}
select {
case err := <-errChan:
if err != nil {
t.Fatal(err)
} // else Success
case <-time.After(readTimeout):
t.Fatal("timed out waiting for notification after resume")
}
if err = rd.Close(); err != nil {
t.Fatal(err)
}
// Pause/Resume after close should be no-op.
err = rd.Pause()
qt.Assert(t, qt.Not(qt.Equals(err, ErrClosed)), qt.Commentf("returns unwrapped ErrClosed"))
qt.Assert(t, qt.ErrorIs(err, ErrClosed), qt.Commentf("doesn't wrap ErrClosed"))
err = rd.Resume()
qt.Assert(t, qt.Not(qt.Equals(err, ErrClosed)), qt.Commentf("returns unwrapped ErrClosed"))
qt.Assert(t, qt.ErrorIs(err, ErrClosed), qt.Commentf("doesn't wrap ErrClosed"))
}
func TestPerfReaderWakeupEvents(t *testing.T) {
testutils.LockOSThreadToSingleCPU(t)
events := perfEventArray(t)
numEvents := 2
rd, err := NewReaderWithOptions(events, 4096, ReaderOptions{WakeupEvents: numEvents})
if err != nil {
t.Fatal(err)
}
defer rd.Close()
prog := outputSamplesProg(t, events, 5)
// Send enough events to trigger WakeupEvents.
for i := 0; i < numEvents; i++ {
_, _, err = prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
qt.Assert(t, qt.IsNil(err))
}
time.AfterFunc(5*time.Second, func() {
// Interrupt Read() in case the implementation is buggy.
rd.Close()
})
for i := 0; i < numEvents; i++ {
checkRecord(t, rd)
}
}
func TestReadWithoutWakeup(t *testing.T) {
t.Parallel()
events := perfEventArray(t)
rd, err := NewReaderWithOptions(events, 1, ReaderOptions{WakeupEvents: 2})
if err != nil {
t.Fatal(err)
}
defer rd.Close()
prog := outputSamplesProg(t, events, 5)
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(ret, 0))
rd.SetDeadline(time.Now())
checkRecord(t, rd)
}
func BenchmarkReader(b *testing.B) {
events := perfEventArray(b)
prog := outputSamplesProg(b, events, 80)
rd, err := NewReader(events, 4096)
if err != nil {
b.Fatal(err)
}
defer rd.Close()
buf := internal.EmptyBPFContext
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ret, _, err := prog.Test(buf)
if err != nil {
b.Fatal(err)
} else if errno := syscall.Errno(-int32(ret)); errno != 0 {
b.Fatal("Expected 0 as return value, got", errno)
}
if _, err = rd.Read(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkReadInto(b *testing.B) {
events := perfEventArray(b)
prog := outputSamplesProg(b, events, 80)
rd, err := NewReader(events, 4096)
if err != nil {
b.Fatal(err)
}
defer rd.Close()
buf := internal.EmptyBPFContext
b.ResetTimer()
b.ReportAllocs()
var rec Record
for i := 0; i < b.N; i++ {
// NB: Submitting samples into the perf event ring dominates
// the benchmark time unfortunately.
ret, _, err := prog.Test(buf)
if err != nil {
b.Fatal(err)
} else if errno := syscall.Errno(-int32(ret)); errno != 0 {
b.Fatal("Expected 0 as return value, got", errno)
}
if err := rd.ReadInto(&rec); err != nil {
b.Fatal(err)
}
}
}
// This exists just to make the example below nicer.
func bpfPerfEventOutputProgram() (*ebpf.Program, *ebpf.Map) {
return nil, nil
}
// ExampleReader submits a perf event using BPF,
// and then reads it in user space.
//
// The BPF will look something like this:
//
// struct map events __section("maps") = {
// .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
// };
//
// __section("xdp") int output_single(void *ctx) {
// unsigned char buf[] = {
// 1, 2, 3, 4, 5
// };
//
// return perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &buf[0], 5);
// }
//
// Also see BPF_F_CTXLEN_MASK if you want to sample packet data
// from SKB or XDP programs.
func ExampleReader() {
prog, events := bpfPerfEventOutputProgram()
defer prog.Close()
defer events.Close()
rd, err := NewReader(events, 4096)
if err != nil {
panic(err)
}
defer rd.Close()
// Writes out a sample with content 1,2,3,4,4
ret, _, err := prog.Test(internal.EmptyBPFContext)
if err != nil || ret != 0 {
panic("Can't write sample")
}
record, err := rd.Read()
if err != nil {
panic(err)
}
// Data is padded with 0 for alignment
fmt.Println("Sample:", record.RawSample)
}
// ReadRecord allows reducing memory allocations.
func ExampleReader_ReadInto() {
prog, events := bpfPerfEventOutputProgram()
defer prog.Close()
defer events.Close()
rd, err := NewReader(events, 4096)
if err != nil {
panic(err)
}
defer rd.Close()
for i := 0; i < 2; i++ {
// Write out two samples
ret, _, err := prog.Test(internal.EmptyBPFContext)
if err != nil || ret != 0 {
panic("Can't write sample")
}
}
var rec Record
for i := 0; i < 2; i++ {
if err := rd.ReadInto(&rec); err != nil {
panic(err)
}
fmt.Println("Sample:", rec.RawSample[:5])
}
}
func perfEventArray(tb testing.TB) *ebpf.Map {
events, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.PerfEventArray,
})
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() { events.Close() })
return events
}
|