History

bpf: add a test case for helper bpf_perf_prog_read_value

The bpf sample program trace_event is enhanced to use the new helper to print out enabled/running time.

Yonghong Song committed 9 years ago

Commit 81b9cf81 parent 4bebdc74 files · +23 −6Browse files

Unified · Split

4 files
  1. samples/bpf/trace_event_kern.c +10 −0
  2. samples/bpf/trace_event_user.c +8 −5
  3. tools/include/uapi/linux/bpf.h +2 −1
  4. tools/testing/selftests/bpf/bpf_helpers.h +3 −0
samples/bpf/trace_event_kern.c+10−0
@@ -37,10 +37,14 @@ struct bpf_map_def SEC("maps") stackmap = { · 36 unmodified lines
3737 SEC("perf_event")
3838 int bpf_prog1(struct bpf_perf_event_data *ctx)
3939 {
40+ char time_fmt1[] = "Time Enabled: %llu, Time Running: %llu";
41+ char time_fmt2[] = "Get Time Failed, ErrCode: %d";
4042 char fmt[] = "CPU-%d period %lld ip %llx";
4143 u32 cpu = bpf_get_smp_processor_id();
44+ struct bpf_perf_event_value value_buf;
4245 struct key_t key;
4346 u64 *val, one = 1;
47+ int ret;
4448
4549 if (ctx->sample_period < 10000)
4650 /* ignore warmup */
@@ -54,6 +58,12 @@ int bpf_prog1(struct bpf_perf_event_data *ctx) · 7 unmodified lines
5458 return 0;
5559 }
5660
61+ ret = bpf_perf_prog_read_value(ctx, (void *)&value_buf, sizeof(struct bpf_perf_event_value));
62+ if (!ret)
63+ bpf_trace_printk(time_fmt1, sizeof(time_fmt1), value_buf.enabled, value_buf.running);
64+ else
65+ bpf_trace_printk(time_fmt2, sizeof(time_fmt2), ret);
66+
5767 val = bpf_map_lookup_elem(&counts, &key);
5868 if (val)
5969 (*val)++;
samples/bpf/trace_event_user.c+8−5
@@ -127,6 +127,9 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr) · 126 unmodified lines
127127 int *pmu_fd = malloc(nr_cpus * sizeof(int));
128128 int i, error = 0;
129129
130+ /* system wide perf event, no need to inherit */
131+ attr->inherit = 0;
132+
130133 /* open perf_event on all cpus */
131134 for (i = 0; i < nr_cpus; i++) {
132135 pmu_fd[i] = sys_perf_event_open(attr, -1, i, -1, 0);
@@ -154,6 +157,11 @@ static void test_perf_event_task(struct perf_event_attr *attr) · 21 unmodified lines
154157 {
155158 int pmu_fd;
156159
160+ /* per task perf event, enable inherit so the "dd ..." command can be traced properly.
161+ * Enabling inherit will cause bpf_perf_prog_read_time helper failure.
162+ */
163+ attr->inherit = 1;
164+
157165 /* open task bound event */
158166 pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
159167 if (pmu_fd < 0) {
@@ -175,14 +183,12 @@ static void test_bpf_perf_event(void) · 15 unmodified lines
175183 .freq = 1,
176184 .type = PERF_TYPE_HARDWARE,
177185 .config = PERF_COUNT_HW_CPU_CYCLES,
178− .inherit = 1,
179186 };
180187 struct perf_event_attr attr_type_sw = {
181188 .sample_freq = SAMPLE_FREQ,
182189 .freq = 1,
183190 .type = PERF_TYPE_SOFTWARE,
184191 .config = PERF_COUNT_SW_CPU_CLOCK,
185− .inherit = 1,
186192 };
187193 struct perf_event_attr attr_hw_cache_l1d = {
188194 .sample_freq = SAMPLE_FREQ,
@@ -192,7 +198,6 @@ static void test_bpf_perf_event(void) · 3 unmodified lines
192198 PERF_COUNT_HW_CACHE_L1D |
193199 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
194200 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
195− .inherit = 1,
196201 };
197202 struct perf_event_attr attr_hw_cache_branch_miss = {
198203 .sample_freq = SAMPLE_FREQ,
@@ -202,7 +207,6 @@ static void test_bpf_perf_event(void) · 3 unmodified lines
202207 PERF_COUNT_HW_CACHE_BPU |
203208 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
204209 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
205− .inherit = 1,
206210 };
207211 struct perf_event_attr attr_type_raw = {
208212 .sample_freq = SAMPLE_FREQ,
@@ -210,7 +214,6 @@ static void test_bpf_perf_event(void) · 1 unmodified line
210214 .type = PERF_TYPE_RAW,
211215 /* Intel Instruction Retired */
212216 .config = 0xc0,
213− .inherit = 1,
214217 };
215218
216219 printf("Test HW_CPU_CYCLES\n");
tools/include/uapi/linux/bpf.h+2−1
@@ -698,7 +698,8 @@ union bpf_attr { · 697 unmodified lines
698698 FN(sk_redirect_map), \
699699 FN(sock_map_update), \
700700 FN(xdp_adjust_meta), \
701− FN(perf_event_read_value),
701+ FN(perf_event_read_value), \
702+ FN(perf_prog_read_value),
702703
703704 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
704705 * function eBPF program intends to call
tools/testing/selftests/bpf/bpf_helpers.h+3−0
@@ -75,6 +75,9 @@ static int (*bpf_sock_map_update)(void *map, void *key, void *value, · 74 unmodified lines
7575 static int (*bpf_perf_event_read_value)(void *map, unsigned long long flags,
7676 void *buf, unsigned int buf_size) =
7777 (void *) BPF_FUNC_perf_event_read_value;
78+static int (*bpf_perf_prog_read_value)(void *ctx, void *buf,
79+ unsigned int buf_size) =
80+ (void *) BPF_FUNC_perf_prog_read_value;
7881
7982
8083 /* llvm builtin functions that eBPF C program may use to