github.com/castai/kvisor@v1.7.1-0.20240516114728-b3572a2607b5/pkg/ebpftracer/c/headers/common/logging.h (about) 1 #ifndef __COMMON_LOGGING_H__ 2 #define __COMMON_LOGGING_H__ 3 4 #include <vmlinux.h> 5 6 #include <common/common.h> 7 8 statfunc void do_tracee_log( 9 void *ctx, enum bpf_log_level level, enum bpf_log_id id, s64 ret, u32 line, void *file) 10 { 11 if (!ctx || !file) 12 return; 13 14 u32 zero = 0; 15 bpf_log_output_t *log_output = bpf_map_lookup_elem(&scratch_map, &zero); 16 if (unlikely(log_output == NULL)) 17 return; 18 19 log_output->level = level; 20 log_output->id = id; 21 22 log_output->log.ret = ret; 23 log_output->log.cpu = bpf_get_smp_processor_id(); 24 log_output->log.line = line; 25 26 u64 fsize = __builtin_strlen(file); 27 if (unlikely(fsize >= BPF_MAX_LOG_FILE_LEN)) 28 fsize = BPF_MAX_LOG_FILE_LEN - 1; 29 __builtin_memcpy(log_output->log.file, file, fsize); 30 log_output->log.file[fsize] = '\0'; 31 32 bpf_log_count_t counter_buf = {}; 33 counter_buf.count = 1; 34 counter_buf.ts = bpf_ktime_get_ns(); // store the current ts 35 u64 ts_prev = 0; 36 37 bpf_log_count_t *counter = bpf_map_lookup_elem(&logs_count, &log_output->log); 38 if (likely(counter != NULL)) { 39 ts_prev = counter->ts; // store previous ts 40 41 counter->count += 1; 42 counter->ts = counter_buf.ts; // set to current ts 43 } else { 44 counter = &counter_buf; 45 bpf_map_update_elem(&logs_count, &log_output->log, counter, BPF_ANY); 46 } 47 48 // submit log when its cpu occurrence time diff is greater than 2s 49 if ((counter->ts - ts_prev) > (u64) 2000000000) { 50 log_output->count = counter->count; 51 bpf_perf_event_output(ctx, &logs, BPF_F_CURRENT_CPU, log_output, sizeof(*log_output)); 52 counter->count = 0; // reset, assuming that the consumer is incrementing 53 } 54 } 55 56 #define tracee_log(ctx, level, id, ret) do_tracee_log(ctx, level, id, ret, __LINE__, __FILE__); 57 58 #endif