| .. | .. | 
|---|
|  | 1 | +// SPDX-License-Identifier: GPL-2.0-only | 
|---|
| 1 | 2 | /* | 
|---|
| 2 | 3 | * sampleip: sample instruction pointer and frequency count in a BPF map. | 
|---|
| 3 | 4 | * | 
|---|
| 4 | 5 | * Copyright 2016 Netflix, Inc. | 
|---|
| 5 |  | - * | 
|---|
| 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. | 
|---|
| 9 | 6 | */ | 
|---|
| 10 | 7 | #include <stdio.h> | 
|---|
| 11 | 8 | #include <stdlib.h> | 
|---|
| 12 |  | -#include <stdio.h> | 
|---|
| 13 | 9 | #include <unistd.h> | 
|---|
| 14 | 10 | #include <errno.h> | 
|---|
| 15 | 11 | #include <signal.h> | 
|---|
| 16 | 12 | #include <string.h> | 
|---|
| 17 |  | -#include <assert.h> | 
|---|
| 18 | 13 | #include <linux/perf_event.h> | 
|---|
| 19 | 14 | #include <linux/ptrace.h> | 
|---|
| 20 | 15 | #include <linux/bpf.h> | 
|---|
| 21 |  | -#include <sys/ioctl.h> | 
|---|
| 22 |  | -#include "libbpf.h" | 
|---|
| 23 |  | -#include "bpf_load.h" | 
|---|
|  | 16 | +#include <bpf/bpf.h> | 
|---|
|  | 17 | +#include <bpf/libbpf.h> | 
|---|
| 24 | 18 | #include "perf-sys.h" | 
|---|
| 25 | 19 | #include "trace_helpers.h" | 
|---|
| 26 | 20 |  | 
|---|
| .. | .. | 
|---|
| 29 | 23 | #define MAX_IPS		8192 | 
|---|
| 30 | 24 | #define PAGE_OFFSET	0xffff880000000000 | 
|---|
| 31 | 25 |  | 
|---|
|  | 26 | +static int map_fd; | 
|---|
| 32 | 27 | static int nr_cpus; | 
|---|
| 33 | 28 |  | 
|---|
| 34 | 29 | static void usage(void) | 
|---|
| .. | .. | 
|---|
| 38 | 33 | printf("       duration   # sampling duration (seconds), default 5\n"); | 
|---|
| 39 | 34 | } | 
|---|
| 40 | 35 |  | 
|---|
| 41 |  | -static int sampling_start(int *pmu_fd, int freq) | 
|---|
|  | 36 | +static int sampling_start(int freq, struct bpf_program *prog, | 
|---|
|  | 37 | +			  struct bpf_link *links[]) | 
|---|
| 42 | 38 | { | 
|---|
| 43 |  | -	int i; | 
|---|
|  | 39 | +	int i, pmu_fd; | 
|---|
| 44 | 40 |  | 
|---|
| 45 | 41 | struct perf_event_attr pe_sample_attr = { | 
|---|
| 46 | 42 | .type = PERF_TYPE_SOFTWARE, | 
|---|
| .. | .. | 
|---|
| 51 | 47 | }; | 
|---|
| 52 | 48 |  | 
|---|
| 53 | 49 | for (i = 0; i < nr_cpus; i++) { | 
|---|
| 54 |  | -		pmu_fd[i] = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i, | 
|---|
|  | 50 | +		pmu_fd = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i, | 
|---|
| 55 | 51 | -1 /* group_fd */, 0 /* flags */); | 
|---|
| 56 |  | -		if (pmu_fd[i] < 0) { | 
|---|
|  | 52 | +		if (pmu_fd < 0) { | 
|---|
| 57 | 53 | fprintf(stderr, "ERROR: Initializing perf sampling\n"); | 
|---|
| 58 | 54 | return 1; | 
|---|
| 59 | 55 | } | 
|---|
| 60 |  | -		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, | 
|---|
| 61 |  | -			     prog_fd[0]) == 0); | 
|---|
| 62 |  | -		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0); | 
|---|
|  | 56 | +		links[i] = bpf_program__attach_perf_event(prog, pmu_fd); | 
|---|
|  | 57 | +		if (libbpf_get_error(links[i])) { | 
|---|
|  | 58 | +			fprintf(stderr, "ERROR: Attach perf event\n"); | 
|---|
|  | 59 | +			links[i] = NULL; | 
|---|
|  | 60 | +			close(pmu_fd); | 
|---|
|  | 61 | +			return 1; | 
|---|
|  | 62 | +		} | 
|---|
| 63 | 63 | } | 
|---|
| 64 | 64 |  | 
|---|
| 65 | 65 | return 0; | 
|---|
| 66 | 66 | } | 
|---|
| 67 | 67 |  | 
|---|
| 68 |  | -static void sampling_end(int *pmu_fd) | 
|---|
|  | 68 | +static void sampling_end(struct bpf_link *links[]) | 
|---|
| 69 | 69 | { | 
|---|
| 70 | 70 | int i; | 
|---|
| 71 | 71 |  | 
|---|
| 72 | 72 | for (i = 0; i < nr_cpus; i++) | 
|---|
| 73 |  | -		close(pmu_fd[i]); | 
|---|
|  | 73 | +		bpf_link__destroy(links[i]); | 
|---|
| 74 | 74 | } | 
|---|
| 75 | 75 |  | 
|---|
| 76 | 76 | struct ipcount { | 
|---|
| .. | .. | 
|---|
| 110 | 110 | for (i = 0; i < max; i++) { | 
|---|
| 111 | 111 | if (counts[i].ip > PAGE_OFFSET) { | 
|---|
| 112 | 112 | sym = ksym_search(counts[i].ip); | 
|---|
|  | 113 | +			if (!sym) { | 
|---|
|  | 114 | +				printf("ksym not found. Is kallsyms loaded?\n"); | 
|---|
|  | 115 | +				continue; | 
|---|
|  | 116 | +			} | 
|---|
|  | 117 | + | 
|---|
| 113 | 118 | printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name, | 
|---|
| 114 | 119 | counts[i].count); | 
|---|
| 115 | 120 | } else { | 
|---|
| .. | .. | 
|---|
| 127 | 132 | static void int_exit(int sig) | 
|---|
| 128 | 133 | { | 
|---|
| 129 | 134 | printf("\n"); | 
|---|
| 130 |  | -	print_ip_map(map_fd[0]); | 
|---|
|  | 135 | +	print_ip_map(map_fd); | 
|---|
| 131 | 136 | exit(0); | 
|---|
| 132 | 137 | } | 
|---|
| 133 | 138 |  | 
|---|
| 134 | 139 | int main(int argc, char **argv) | 
|---|
| 135 | 140 | { | 
|---|
|  | 141 | +	int opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS, error = 1; | 
|---|
|  | 142 | +	struct bpf_object *obj = NULL; | 
|---|
|  | 143 | +	struct bpf_program *prog; | 
|---|
|  | 144 | +	struct bpf_link **links; | 
|---|
| 136 | 145 | char filename[256]; | 
|---|
| 137 |  | -	int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS; | 
|---|
| 138 | 146 |  | 
|---|
| 139 | 147 | /* process arguments */ | 
|---|
| 140 | 148 | while ((opt = getopt(argc, argv, "F:h")) != -1) { | 
|---|
| .. | .. | 
|---|
| 162 | 170 | } | 
|---|
| 163 | 171 |  | 
|---|
| 164 | 172 | /* create perf FDs for each CPU */ | 
|---|
| 165 |  | -	nr_cpus = sysconf(_SC_NPROCESSORS_CONF); | 
|---|
| 166 |  | -	pmu_fd = malloc(nr_cpus * sizeof(int)); | 
|---|
| 167 |  | -	if (pmu_fd == NULL) { | 
|---|
| 168 |  | -		fprintf(stderr, "ERROR: malloc of pmu_fd\n"); | 
|---|
| 169 |  | -		return 1; | 
|---|
|  | 173 | +	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); | 
|---|
|  | 174 | +	links = calloc(nr_cpus, sizeof(struct bpf_link *)); | 
|---|
|  | 175 | +	if (!links) { | 
|---|
|  | 176 | +		fprintf(stderr, "ERROR: malloc of links\n"); | 
|---|
|  | 177 | +		goto cleanup; | 
|---|
|  | 178 | +	} | 
|---|
|  | 179 | + | 
|---|
|  | 180 | +	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | 
|---|
|  | 181 | +	obj = bpf_object__open_file(filename, NULL); | 
|---|
|  | 182 | +	if (libbpf_get_error(obj)) { | 
|---|
|  | 183 | +		fprintf(stderr, "ERROR: opening BPF object file failed\n"); | 
|---|
|  | 184 | +		obj = NULL; | 
|---|
|  | 185 | +		goto cleanup; | 
|---|
|  | 186 | +	} | 
|---|
|  | 187 | + | 
|---|
|  | 188 | +	prog = bpf_object__find_program_by_name(obj, "do_sample"); | 
|---|
|  | 189 | +	if (!prog) { | 
|---|
|  | 190 | +		fprintf(stderr, "ERROR: finding a prog in obj file failed\n"); | 
|---|
|  | 191 | +		goto cleanup; | 
|---|
| 170 | 192 | } | 
|---|
| 171 | 193 |  | 
|---|
| 172 | 194 | /* load BPF program */ | 
|---|
| 173 |  | -	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | 
|---|
| 174 |  | -	if (load_bpf_file(filename)) { | 
|---|
| 175 |  | -		fprintf(stderr, "ERROR: loading BPF program (errno %d):\n", | 
|---|
| 176 |  | -			errno); | 
|---|
| 177 |  | -		if (strcmp(bpf_log_buf, "") == 0) | 
|---|
| 178 |  | -			fprintf(stderr, "Try: ulimit -l unlimited\n"); | 
|---|
| 179 |  | -		else | 
|---|
| 180 |  | -			fprintf(stderr, "%s", bpf_log_buf); | 
|---|
| 181 |  | -		return 1; | 
|---|
|  | 195 | +	if (bpf_object__load(obj)) { | 
|---|
|  | 196 | +		fprintf(stderr, "ERROR: loading BPF object file failed\n"); | 
|---|
|  | 197 | +		goto cleanup; | 
|---|
| 182 | 198 | } | 
|---|
|  | 199 | + | 
|---|
|  | 200 | +	map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map"); | 
|---|
|  | 201 | +	if (map_fd < 0) { | 
|---|
|  | 202 | +		fprintf(stderr, "ERROR: finding a map in obj file failed\n"); | 
|---|
|  | 203 | +		goto cleanup; | 
|---|
|  | 204 | +	} | 
|---|
|  | 205 | + | 
|---|
| 183 | 206 | signal(SIGINT, int_exit); | 
|---|
| 184 | 207 | signal(SIGTERM, int_exit); | 
|---|
| 185 | 208 |  | 
|---|
| 186 | 209 | /* do sampling */ | 
|---|
| 187 | 210 | printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n", | 
|---|
| 188 | 211 | freq, secs); | 
|---|
| 189 |  | -	if (sampling_start(pmu_fd, freq) != 0) | 
|---|
| 190 |  | -		return 1; | 
|---|
|  | 212 | +	if (sampling_start(freq, prog, links) != 0) | 
|---|
|  | 213 | +		goto cleanup; | 
|---|
|  | 214 | + | 
|---|
| 191 | 215 | sleep(secs); | 
|---|
| 192 |  | -	sampling_end(pmu_fd); | 
|---|
| 193 |  | -	free(pmu_fd); | 
|---|
|  | 216 | +	error = 0; | 
|---|
| 194 | 217 |  | 
|---|
|  | 218 | +cleanup: | 
|---|
|  | 219 | +	sampling_end(links); | 
|---|
| 195 | 220 | /* output sample counts */ | 
|---|
| 196 |  | -	print_ip_map(map_fd[0]); | 
|---|
|  | 221 | +	if (!error) | 
|---|
|  | 222 | +		print_ip_map(map_fd); | 
|---|
| 197 | 223 |  | 
|---|
| 198 |  | -	return 0; | 
|---|
|  | 224 | +	free(links); | 
|---|
|  | 225 | +	bpf_object__close(obj); | 
|---|
|  | 226 | +	return error; | 
|---|
| 199 | 227 | } | 
|---|