Branch records are a CPU feature that can be configured to record certain branches that are taken during code execution. This data is particularly interesting for profile guided optimizations. perf has had branch record support for a while but the data collection can be a bit coarse grained. We (Facebook) have seen in experiments that associating metadata with branch records can improve results (after postprocessing). We generally use bpf_probe_read_*() to get metadata out of userspace. That's why bpf support for branch records is useful. Aside from this particular use case, having branch data available to bpf progs can be useful to get stack traces out of userspace applications that omit frame pointers.
Showing the changes to include/uapi/linux (1 of 2 files). Show all
Unified · Split
| @@ -2892,6 +2892,25 @@ union bpf_attr { · 2891 unmodified lines | ||
| 2892 | 2892 | * Obtain the 64bit jiffies |
| 2893 | 2893 | * Return |
| 2894 | 2894 | * The 64 bit jiffies |
| 2895 | + * | |
| 2896 | + * int bpf_read_branch_records(struct bpf_perf_event_data *ctx, void *buf, u32 size, u64 flags) | |
| 2897 | + * Description | |
| 2898 | + * For an eBPF program attached to a perf event, retrieve the | |
| 2899 | + * branch records (struct perf_branch_entry) associated to *ctx* | |
| 2900 | + * and store it in the buffer pointed by *buf* up to size | |
| 2901 | + * *size* bytes. | |
| 2902 | + * Return | |
| 2903 | + * On success, number of bytes written to *buf*. On error, a | |
| 2904 | + * negative value. | |
| 2905 | + * | |
| 2906 | + * The *flags* can be set to **BPF_F_GET_BRANCH_RECORDS_SIZE** to | |
| 2907 | + * instead return the number of bytes required to store all the | |
| 2908 | + * branch entries. If this flag is set, *buf* may be NULL. | |
| 2909 | + * | |
| 2910 | + * **-EINVAL** if arguments invalid or **size** not a multiple | |
| 2911 | + * of sizeof(struct perf_branch_entry). | |
| 2912 | + * | |
| 2913 | + * **-ENOENT** if architecture does not support branch records. | |
| 2895 | 2914 | */ |
| 2896 | 2915 | #define __BPF_FUNC_MAPPER(FN) \ |
| 2897 | 2916 | FN(unspec), \ |
| @@ -3012,7 +3031,8 @@ union bpf_attr { · 114 unmodified lines | ||
| 3012 | 3031 | FN(probe_read_kernel_str), \ |
| 3013 | 3032 | FN(tcp_send_ack), \ |
| 3014 | 3033 | FN(send_signal_thread), \ |
| 3015 | − FN(jiffies64), | |
| 3034 | + FN(jiffies64), \ | |
| 3035 | + FN(read_branch_records), | |
| 3016 | 3036 | |
| 3017 | 3037 | /* integer value in 'imm' field of BPF_CALL instruction selects which helper |
| 3018 | 3038 | * function eBPF program intends to call |
| @@ -3091,6 +3111,9 @@ enum bpf_func_id { · 72 unmodified lines | ||
| 3091 | 3111 | /* BPF_FUNC_sk_storage_get flags */ |
| 3092 | 3112 | #define BPF_SK_STORAGE_GET_F_CREATE (1ULL << 0) |
| 3093 | 3113 | |
| 3114 | +/* BPF_FUNC_read_branch_records flags. */ | |
| 3115 | +#define BPF_F_GET_BRANCH_RECORDS_SIZE (1ULL << 0) | |
| 3116 | + | |
| 3094 | 3117 | /* Mode for BPF_FUNC_skb_adjust_room helper. */ |
| 3095 | 3118 | enum bpf_adj_room_mode { |
| 3096 | 3119 | BPF_ADJ_ROOM_NET, |