Skip to content

Commit 66ee9a7

Browse files
tracee.bpf.c: optimize with unlikely (#2131)
use the likely and unlikely macros which help branch predictions, in hot path if statements which are unlikely/practically impossible to fail.
1 parent fef3871 commit 66ee9a7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

pkg/ebpf/c/tracee.bpf.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@
8989
#define UPROBE_MAGIC_NUMBER 20220829
9090
// clang-format on
9191

92+
// helper macros for branch prediction
93+
#define likely(x) __builtin_expect((x), 1)
94+
#define unlikely(x) __builtin_expect((x), 0)
95+
9296
enum buf_idx_e
9397
{
9498
SUBMIT_BUF_IDX,
@@ -1608,21 +1612,21 @@ static __always_inline task_info_t *init_task_info(u32 key, bool *initialized)
16081612
if (initialized != NULL) {
16091613
*initialized = task_info == NULL;
16101614
}
1611-
if (task_info == NULL) {
1615+
if (unlikely(task_info == NULL)) {
16121616
// unlikely code path - possibly optimize with unlikely macro later
16131617

16141618
// get the submit buffer to fill the task_info_t in the map
16151619
// this is done because allocating the stack space for task_info_t usually
16161620
// crosses the verifier limit.
16171621
int buf_idx = SUBMIT_BUF_IDX;
16181622
void *submit_buffer = bpf_map_lookup_elem(&bufs, &buf_idx);
1619-
if (submit_buffer == NULL)
1623+
if (unlikely(submit_buffer == NULL))
16201624
return NULL;
16211625

16221626
bpf_map_update_elem(&task_info_map, &key, submit_buffer, BPF_NOEXIST);
16231627
task_info = bpf_map_lookup_elem(&task_info_map, &key);
16241628
// appease the verifier
1625-
if (task_info == NULL) {
1629+
if (unlikely(task_info == NULL)) {
16261630
return NULL;
16271631
}
16281632
task_info->syscall_traced = false;
@@ -1638,7 +1642,7 @@ static __always_inline int init_event_data(event_data_t *data, void *ctx)
16381642
{
16391643
int zero = 0;
16401644
data->config = bpf_map_lookup_elem(&config_map, &zero);
1641-
if (data->config == NULL)
1645+
if (unlikely(data->config == NULL))
16421646
return 0;
16431647

16441648
data->task = (struct task_struct *) bpf_get_current_task();
@@ -1647,13 +1651,13 @@ static __always_inline int init_event_data(event_data_t *data, void *ctx)
16471651
data->buf_off = sizeof(event_context_t);
16481652
int buf_idx = SUBMIT_BUF_IDX;
16491653
data->submit_p = bpf_map_lookup_elem(&bufs, &buf_idx);
1650-
if (data->submit_p == NULL)
1654+
if (unlikely(data->submit_p == NULL))
16511655
return 0;
16521656

16531657
// check if task_info was initialized in this call
16541658
bool task_info_initalized = false;
16551659
data->task_info = init_task_info(data->context.task.host_tid, &task_info_initalized);
1656-
if (data->task_info == NULL) {
1660+
if (unlikely(data->task_info == NULL)) {
16571661
return 0;
16581662
}
16591663
bool container_lookup_required = true;
@@ -2805,7 +2809,7 @@ int tracepoint__raw_syscalls__sys_enter(struct bpf_raw_tracepoint_args *ctx)
28052809
u32 task_id = bpf_get_current_pid_tgid(); // get the tid only
28062810
// use config as garbage value here
28072811
task_info_t *task_info = init_task_info(task_id, NULL);
2808-
if (task_info == NULL) {
2812+
if (unlikely(task_info == NULL)) {
28092813
return 0;
28102814
}
28112815

@@ -2931,7 +2935,7 @@ int tracepoint__raw_syscalls__sys_exit(struct bpf_raw_tracepoint_args *ctx)
29312935

29322936
u32 task_id = bpf_get_current_pid_tgid(); // get tid
29332937
task_info_t *task_info = init_task_info(task_id, NULL);
2934-
if (task_info == NULL) {
2938+
if (unlikely(task_info == NULL)) {
29352939
return 0;
29362940
}
29372941

0 commit comments

Comments
 (0)