History

bpf: Add bpf_read_branch_records() helper

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.

Daniel Xu committed 6 years ago

Commit fff7b641 parent 2f14b2d2 files · +65 −1Browse files

Showing the changes to include/uapi/linux (1 of 2 files). Show all

Unified · Split

include/uapi/linux/bpf.h+24−1
@@ -2892,6 +2892,25 @@ union bpf_attr { · 2891 unmodified lines
28922892 * Obtain the 64bit jiffies
28932893 * Return
28942894 * 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.
28952914 */
28962915 #define __BPF_FUNC_MAPPER(FN) \
28972916 FN(unspec), \
@@ -3012,7 +3031,8 @@ union bpf_attr { · 114 unmodified lines
30123031 FN(probe_read_kernel_str), \
30133032 FN(tcp_send_ack), \
30143033 FN(send_signal_thread), \
3015− FN(jiffies64),
3034+ FN(jiffies64), \
3035+ FN(read_branch_records),
30163036
30173037 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
30183038 * function eBPF program intends to call
@@ -3091,6 +3111,9 @@ enum bpf_func_id { · 72 unmodified lines
30913111 /* BPF_FUNC_sk_storage_get flags */
30923112 #define BPF_SK_STORAGE_GET_F_CREATE (1ULL << 0)
30933113
3114+/* BPF_FUNC_read_branch_records flags. */
3115+#define BPF_F_GET_BRANCH_RECORDS_SIZE (1ULL << 0)
3116+
30943117 /* Mode for BPF_FUNC_skb_adjust_room helper. */
30953118 enum bpf_adj_room_mode {
30963119 BPF_ADJ_ROOM_NET,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t