File: test_trace1.b

package info (click to toggle)
bpfcc 0.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,368 kB
  • sloc: ansic: 132,727; python: 36,226; cpp: 26,973; sh: 710; yacc: 525; makefile: 141; lex: 94
file content (43 lines) | stat: -rw-r--r-- 963 bytes parent folder | download | duplicates (2)
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
// Copyright (c) PLUMgrid, Inc.
// Licensed under the Apache License, Version 2.0 (the "License")
struct Ptr {
  u64 ptr:64;
};
struct Counters {
  u64 stat1:64;
  u64 stat2:64;
};
Table<Ptr, Counters, FIXED_MATCH, AUTO> stats(1024);

// example with on_valid syntax
u32 sys_wr (struct proto::pt_regs *ctx) {
  struct Ptr key = {.ptr=ctx->di};
  struct Counters *leaf;
  leaf = stats[key];
  if leaf {
    atomic_add(leaf->stat2, 1);
  }
  log("sys_wr: %p\n", ctx->di);
  return 0;
}

// example with smallest available syntax
// note: if stats[key] fails, program returns early
u32 sys_rd (struct proto::pt_regs *ctx) {
  struct Ptr key = {.ptr=ctx->di};
  atomic_add(stats[key].stat1, 1);
}

// example with if/else case
u32 sys_bpf (struct proto::pt_regs *ctx) {
  struct Ptr key = {.ptr=ctx->di};
  struct Counters *leaf;
  leaf = stats[key];
  if leaf {
    atomic_add(leaf->stat1, 1);
  } else {
    log("update %llx failed\n", ctx->di);
  }
  return 0;
}