Blame view

samples/bpf/tracex6_user.c 1.4 KB
47efb3027   Kaixu Xia   samples/bpf: exam...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <stdio.h>
  #include <unistd.h>
  #include <stdlib.h>
  #include <stdbool.h>
  #include <string.h>
  #include <fcntl.h>
  #include <poll.h>
  #include <sys/ioctl.h>
  #include <linux/perf_event.h>
  #include <linux/bpf.h>
  #include "libbpf.h"
  #include "bpf_load.h"
  
  #define SAMPLE_PERIOD  0x7fffffffffffffffULL
  
  static void test_bpf_perf_event(void)
  {
  	int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
  	int *pmu_fd = malloc(nr_cpus * sizeof(int));
5ed3ccbd5   Kaixu Xia   bpf: fix build wa...
20
  	int status, i;
47efb3027   Kaixu Xia   samples/bpf: exam...
21
22
23
24
25
26
27
28
29
30
31
32
33
  
  	struct perf_event_attr attr_insn_pmu = {
  		.freq = 0,
  		.sample_period = SAMPLE_PERIOD,
  		.inherit = 0,
  		.type = PERF_TYPE_HARDWARE,
  		.read_format = 0,
  		.sample_type = 0,
  		.config = 0,/* PMU: cycles */
  	};
  
  	for (i = 0; i < nr_cpus; i++) {
  		pmu_fd[i] = perf_event_open(&attr_insn_pmu, -1/*pid*/, i/*cpu*/, -1/*group_fd*/, 0);
5ed3ccbd5   Kaixu Xia   bpf: fix build wa...
34
  		if (pmu_fd[i] < 0) {
47efb3027   Kaixu Xia   samples/bpf: exam...
35
36
  			printf("event syscall failed
  ");
5ed3ccbd5   Kaixu Xia   bpf: fix build wa...
37
38
  			goto exit;
  		}
47efb3027   Kaixu Xia   samples/bpf: exam...
39
40
41
42
  
  		bpf_update_elem(map_fd[0], &i, &pmu_fd[i], BPF_ANY);
  		ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0);
  	}
5ed3ccbd5   Kaixu Xia   bpf: fix build wa...
43
44
45
46
47
48
  	status = system("ls > /dev/null");
  	if (status)
  		goto exit;
  	status = system("sleep 2");
  	if (status)
  		goto exit;
47efb3027   Kaixu Xia   samples/bpf: exam...
49

5ed3ccbd5   Kaixu Xia   bpf: fix build wa...
50
  exit:
47efb3027   Kaixu Xia   samples/bpf: exam...
51
52
  	for (i = 0; i < nr_cpus; i++)
  		close(pmu_fd[i]);
5ed3ccbd5   Kaixu Xia   bpf: fix build wa...
53
  	close(map_fd[0]);
47efb3027   Kaixu Xia   samples/bpf: exam...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  	free(pmu_fd);
  }
  
  int main(int argc, char **argv)
  {
  	char filename[256];
  
  	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  
  	if (load_bpf_file(filename)) {
  		printf("%s", bpf_log_buf);
  		return 1;
  	}
  
  	test_bpf_perf_event();
5ed3ccbd5   Kaixu Xia   bpf: fix build wa...
69
  	read_trace_pipe();
47efb3027   Kaixu Xia   samples/bpf: exam...
70
71
72
  
  	return 0;
  }