Skip to content

Commit

Permalink
Change C format styling to LLVM
Browse files Browse the repository at this point in the history
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
kakkoyun committed Mar 24, 2022
1 parent 3ac7d42 commit d4d4c10
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -244,7 +244,7 @@ format: go/fmt

.PHONY: c/fmt
c/fmt:
clang-format -i --style=GNU $(BPF_SRC)
clang-format -i --style=LLVM $(BPF_SRC)

.PHONY: go/fmt
go/fmt:
Expand Down
64 changes: 30 additions & 34 deletions parca-agent.bpf.c
Expand Up @@ -34,90 +34,86 @@
// Max depth of each stack trace to track
#define MAX_STACK_DEPTH 127

#define BPF_MAP(_name, _type, _key_type, _value_type, _max_entries) \
struct bpf_map_def SEC ("maps") _name = { \
.type = _type, \
.key_size = sizeof (_key_type), \
.value_size = sizeof (_value_type), \
.max_entries = _max_entries, \
#define BPF_MAP(_name, _type, _key_type, _value_type, _max_entries) \
struct bpf_map_def SEC("maps") _name = { \
.type = _type, \
.key_size = sizeof(_key_type), \
.value_size = sizeof(_value_type), \
.max_entries = _max_entries, \
};

// Stack Traces are slightly different
// in that the value is 1 big byte array
// of the stack addresses
#define BPF_STACK_TRACE(_name, _max_entries) \
struct bpf_map_def SEC ("maps") _name = { \
.type = BPF_MAP_TYPE_STACK_TRACE, \
.key_size = sizeof (u32), \
.value_size = sizeof (size_t) * MAX_STACK_DEPTH, \
.max_entries = _max_entries, \
#define BPF_STACK_TRACE(_name, _max_entries) \
struct bpf_map_def SEC("maps") _name = { \
.type = BPF_MAP_TYPE_STACK_TRACE, \
.key_size = sizeof(u32), \
.value_size = sizeof(size_t) * MAX_STACK_DEPTH, \
.max_entries = _max_entries, \
};

#define BPF_HASH(_name, _key_type, _value_type) \
BPF_MAP (_name, BPF_MAP_TYPE_HASH, _key_type, _value_type, 10240);
#define BPF_HASH(_name, _key_type, _value_type) \
BPF_MAP(_name, BPF_MAP_TYPE_HASH, _key_type, _value_type, 10240);

/*============================= INTERNAL STRUCTS ============================*/

typedef struct stack_count_key
{
typedef struct stack_count_key {
u32 pid;
int user_stack_id;
int kernel_stack_id;
} stack_count_key_t;

/*================================ MAPS =====================================*/

BPF_HASH (counts, stack_count_key_t, u64);
BPF_STACK_TRACE (stack_traces, MAX_STACK_ADDRESSES);
BPF_HASH(counts, stack_count_key_t, u64);
BPF_STACK_TRACE(stack_traces, MAX_STACK_ADDRESSES);

/*=========================== HELPER FUNCTIONS ==============================*/

static __always_inline void *
bpf_map_lookup_or_try_init (void *map, const void *key, const void *init)
{
bpf_map_lookup_or_try_init(void *map, const void *key, const void *init) {
void *val;
long err;

val = bpf_map_lookup_elem (map, key);
val = bpf_map_lookup_elem(map, key);
if (val)
return val;

err = bpf_map_update_elem (map, key, init, BPF_NOEXIST);
err = bpf_map_update_elem(map, key, init, BPF_NOEXIST);
// 17 == EEXIST
if (err && err != -17)
return 0;

return bpf_map_lookup_elem (map, key);
return bpf_map_lookup_elem(map, key);
}

// This code gets a bit complex. Probably not suitable for casual hacking.
SEC ("perf_event")
int
do_sample (struct bpf_perf_event_data *ctx)
{
u64 id = bpf_get_current_pid_tgid ();
SEC("perf_event")
int do_sample(struct bpf_perf_event_data *ctx) {
u64 id = bpf_get_current_pid_tgid();
u32 tgid = id >> 32;
u32 pid = id;

if (pid == 0)
return 0;

// create map key
stack_count_key_t key = { .pid = tgid };
stack_count_key_t key = {.pid = tgid};

// get stacks
key.user_stack_id = bpf_get_stackid (ctx, &stack_traces, BPF_F_USER_STACK);
key.kernel_stack_id = bpf_get_stackid (ctx, &stack_traces, 0);
key.user_stack_id = bpf_get_stackid(ctx, &stack_traces, BPF_F_USER_STACK);
key.kernel_stack_id = bpf_get_stackid(ctx, &stack_traces, 0);

u64 zero = 0;
u64 *count;
count = bpf_map_lookup_or_try_init (&counts, &key, &zero);
count = bpf_map_lookup_or_try_init(&counts, &key, &zero);
if (!count)
return 0;

__sync_fetch_and_add (count, 1);
__sync_fetch_and_add(count, 1);

return 0;
}

char LICENSE[] SEC ("license") = "GPL";
char LICENSE[] SEC("license") = "GPL";

0 comments on commit d4d4c10

Please sign in to comment.