2 * sampleip: sample instruction pointer and frequency count in a BPF map.
4 * Copyright 2016 Netflix, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
18 #include <linux/perf_event.h>
19 #include <linux/ptrace.h>
20 #include <linux/bpf.h>
21 #include <sys/ioctl.h>
25 #define DEFAULT_FREQ 99
26 #define DEFAULT_SECS 5
28 #define PAGE_OFFSET 0xffff880000000000
32 static void usage(void)
34 printf("USAGE: sampleip [-F freq] [duration]\n");
35 printf(" -F freq # sample frequency (Hertz), default 99\n");
36 printf(" duration # sampling duration (seconds), default 5\n");
39 static int sampling_start(int *pmu_fd
, int freq
)
43 struct perf_event_attr pe_sample_attr
= {
44 .type
= PERF_TYPE_SOFTWARE
,
46 .sample_period
= freq
,
47 .config
= PERF_COUNT_SW_CPU_CLOCK
,
51 for (i
= 0; i
< nr_cpus
; i
++) {
52 pmu_fd
[i
] = perf_event_open(&pe_sample_attr
, -1 /* pid */, i
,
53 -1 /* group_fd */, 0 /* flags */);
55 fprintf(stderr
, "ERROR: Initializing perf sampling\n");
58 assert(ioctl(pmu_fd
[i
], PERF_EVENT_IOC_SET_BPF
,
60 assert(ioctl(pmu_fd
[i
], PERF_EVENT_IOC_ENABLE
, 0) == 0);
66 static void sampling_end(int *pmu_fd
)
70 for (i
= 0; i
< nr_cpus
; i
++)
79 /* used for sorting */
80 struct ipcount counts
[MAX_IPS
];
82 static int count_cmp(const void *p1
, const void *p2
)
84 return ((struct ipcount
*)p1
)->count
- ((struct ipcount
*)p2
)->count
;
87 static void print_ip_map(int fd
)
94 printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
96 /* fetch IPs and counts */
98 while (bpf_get_next_key(fd
, &key
, &next_key
) == 0) {
99 bpf_lookup_elem(fd
, &next_key
, &value
);
100 counts
[i
].ip
= next_key
;
101 counts
[i
++].count
= value
;
107 qsort(counts
, max
, sizeof(struct ipcount
), count_cmp
);
108 for (i
= 0; i
< max
; i
++) {
109 if (counts
[i
].ip
> PAGE_OFFSET
) {
110 sym
= ksym_search(counts
[i
].ip
);
111 printf("0x%-17llx %-32s %u\n", counts
[i
].ip
, sym
->name
,
114 printf("0x%-17llx %-32s %u\n", counts
[i
].ip
, "(user)",
119 if (max
== MAX_IPS
) {
120 printf("WARNING: IP hash was full (max %d entries); ", max
);
121 printf("may have dropped samples\n");
125 static void int_exit(int sig
)
128 print_ip_map(map_fd
[0]);
132 int main(int argc
, char **argv
)
135 int *pmu_fd
, opt
, freq
= DEFAULT_FREQ
, secs
= DEFAULT_SECS
;
137 /* process arguments */
138 while ((opt
= getopt(argc
, argv
, "F:h")) != -1) {
149 if (argc
- optind
== 1)
150 secs
= atoi(argv
[optind
]);
151 if (freq
== 0 || secs
== 0) {
156 /* initialize kernel symbol translation */
157 if (load_kallsyms()) {
158 fprintf(stderr
, "ERROR: loading /proc/kallsyms\n");
162 /* create perf FDs for each CPU */
163 nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
164 pmu_fd
= malloc(nr_cpus
* sizeof(int));
165 if (pmu_fd
== NULL
) {
166 fprintf(stderr
, "ERROR: malloc of pmu_fd\n");
170 /* load BPF program */
171 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
172 if (load_bpf_file(filename
)) {
173 fprintf(stderr
, "ERROR: loading BPF program (errno %d):\n",
175 if (strcmp(bpf_log_buf
, "") == 0)
176 fprintf(stderr
, "Try: ulimit -l unlimited\n");
178 fprintf(stderr
, "%s", bpf_log_buf
);
181 signal(SIGINT
, int_exit
);
184 printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
186 if (sampling_start(pmu_fd
, freq
) != 0)
189 sampling_end(pmu_fd
);
192 /* output sample counts */
193 print_ip_map(map_fd
[0]);