On 07/28/2015 01:17 PM, Kaixu Xia wrote:
This is a simple example and shows how to use the new ability
to get the selected Hardware PMU counter value.

Signed-off-by: Kaixu Xia <xiaka...@huawei.com>
...
diff --git a/samples/bpf/tracex6_user.c b/samples/bpf/tracex6_user.c
new file mode 100644
index 0000000..e607eac
--- /dev/null
+++ b/samples/bpf/tracex6_user.c
@@ -0,0 +1,67 @@
+#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"
+
+static void test_bpf_perf_event(void)
+{
+       int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
+       int *pmu_fd = malloc(nr_cpus * sizeof(int));
+       unsigned long value;
+       int i;
+
+       struct perf_event_attr attr_insn_pmu = {
+               .freq = 0,
+               .sample_period = 0x7fffffffffffffffULL,

Nit: #define ...

+               .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);
+               if (pmu_fd[i] < 0)
+                       printf("event syscall failed\n");
+
+               bpf_update_elem(map_fd[0], &i, (pmu_fd + i), BPF_ANY);

Nit: you use pmu_fd[i], but here (pmu_fd + i)? Maybe better &pmu_fd[i]?

+               ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0);
+       }
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to