|
| 1 | +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
| 2 | +// Copyright (c) 2020 Wenbo Zhang |
| 3 | +// |
| 4 | +// Based on llcstat(8) from BCC by Teng Qin. |
| 5 | +// 29-Sep-2020 Wenbo Zhang Created this. |
| 6 | +#include <argp.h> |
| 7 | +#include <signal.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <unistd.h> |
| 11 | +#include <linux/perf_event.h> |
| 12 | +#include <asm/unistd.h> |
| 13 | +#include <bpf/libbpf.h> |
| 14 | +#include <bpf/bpf.h> |
| 15 | +#include "llcstat.h" |
| 16 | +#include "llcstat.skel.h" |
| 17 | +#include "trace_helpers.h" |
| 18 | + |
| 19 | +struct env { |
| 20 | + int sample_period; |
| 21 | + time_t duration; |
| 22 | + bool verbose; |
| 23 | +} env = { |
| 24 | + .sample_period = 100, |
| 25 | + .duration = 10, |
| 26 | +}; |
| 27 | + |
| 28 | +static volatile bool exiting; |
| 29 | + |
| 30 | +const char *argp_program_version = "llcstat 0.1"; |
| 31 | +const char *argp_program_bug_address = "<[email protected]>"; |
| 32 | +const char argp_program_doc[] = |
| 33 | +"Summarize cache references and misses by PID.\n" |
| 34 | +"\n" |
| 35 | +"USAGE: llcstat [--help] [-c SAMPLE_PERIOD] [duration]\n"; |
| 36 | + |
| 37 | +static const struct argp_option opts[] = { |
| 38 | + { "sample_period", 'c', "SAMPLE_PERIOD", 0, "Sample one in this many " |
| 39 | + "number of cache reference / miss events" }, |
| 40 | + { "verbose", 'v', NULL, 0, "Verbose debug output" }, |
| 41 | + {}, |
| 42 | +}; |
| 43 | + |
| 44 | +static error_t parse_arg(int key, char *arg, struct argp_state *state) |
| 45 | +{ |
| 46 | + static int pos_args; |
| 47 | + |
| 48 | + switch (key) { |
| 49 | + case 'v': |
| 50 | + env.verbose = true; |
| 51 | + break; |
| 52 | + case 'c': |
| 53 | + errno = 0; |
| 54 | + env.sample_period = strtol(arg, NULL, 10); |
| 55 | + if (errno) { |
| 56 | + fprintf(stderr, "invalid sample period\n"); |
| 57 | + argp_usage(state); |
| 58 | + } |
| 59 | + break; |
| 60 | + case ARGP_KEY_ARG: |
| 61 | + if (pos_args++) { |
| 62 | + fprintf(stderr, |
| 63 | + "unrecognized positional argument: %s\n", arg); |
| 64 | + argp_usage(state); |
| 65 | + } |
| 66 | + errno = 0; |
| 67 | + env.duration = strtol(arg, NULL, 10); |
| 68 | + if (errno) { |
| 69 | + fprintf(stderr, "invalid duration\n"); |
| 70 | + argp_usage(state); |
| 71 | + } |
| 72 | + break; |
| 73 | + default: |
| 74 | + return ARGP_ERR_UNKNOWN; |
| 75 | + } |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +static int nr_cpus; |
| 80 | + |
| 81 | +static int open_and_attach_perf_event(__u64 config, int period, |
| 82 | + struct bpf_program *prog, |
| 83 | + struct bpf_link *links[]) |
| 84 | +{ |
| 85 | + struct perf_event_attr attr = { |
| 86 | + .type = PERF_TYPE_HARDWARE, |
| 87 | + .freq = 0, |
| 88 | + .sample_period = period, |
| 89 | + .config = config, |
| 90 | + }; |
| 91 | + int i, fd; |
| 92 | + |
| 93 | + for (i = 0; i < nr_cpus; i++) { |
| 94 | + fd = syscall(__NR_perf_event_open, &attr, -1, i, -1, 0); |
| 95 | + if (fd < 0) { |
| 96 | + fprintf(stderr, "failed to init perf sampling: %s\n", |
| 97 | + strerror(errno)); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + links[i] = bpf_program__attach_perf_event(prog, fd); |
| 101 | + if (libbpf_get_error(links[i])) { |
| 102 | + fprintf(stderr, "failed to attach perf event on cpu: " |
| 103 | + "%d\n", i); |
| 104 | + links[i] = NULL; |
| 105 | + close(fd); |
| 106 | + return -1; |
| 107 | + } |
| 108 | + } |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +int libbpf_print_fn(enum libbpf_print_level level, |
| 113 | + const char *format, va_list args) |
| 114 | +{ |
| 115 | + if (level == LIBBPF_DEBUG && !env.verbose) |
| 116 | + return 0; |
| 117 | + return vfprintf(stderr, format, args); |
| 118 | +} |
| 119 | + |
| 120 | +static void sig_handler(int sig) |
| 121 | +{ |
| 122 | + exiting = true; |
| 123 | +} |
| 124 | + |
| 125 | +static void print_map(struct bpf_map *map) |
| 126 | +{ |
| 127 | + __u64 total_ref = 0, total_miss = 0, total_hit, hit; |
| 128 | + __u64 lookup_key = -1, next_key; |
| 129 | + int err, fd = bpf_map__fd(map); |
| 130 | + struct info info; |
| 131 | + __u32 pid, cpu; |
| 132 | + |
| 133 | + while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) { |
| 134 | + err = bpf_map_lookup_elem(fd, &next_key, &info); |
| 135 | + if (err < 0) { |
| 136 | + fprintf(stderr, "failed to lookup infos: %d\n", err); |
| 137 | + return; |
| 138 | + } |
| 139 | + hit = info.ref > info.miss ? info.ref - info.miss : 0; |
| 140 | + pid = next_key >> 32; |
| 141 | + cpu = next_key; |
| 142 | + printf("%-8u %-16s %-4u %12llu %12llu %6.2f%%\n", pid, info.comm, |
| 143 | + cpu, info.ref, info.miss, info.ref > 0 ? |
| 144 | + hit * 1.0 / info.ref * 100 : 0); |
| 145 | + total_miss += info.miss; |
| 146 | + total_ref += info.ref; |
| 147 | + lookup_key = next_key; |
| 148 | + } |
| 149 | + total_hit = total_ref > total_miss ? total_ref - total_miss : 0; |
| 150 | + printf("Total References: %llu Total Misses: %llu Hit Rate: %.2f%%\n", |
| 151 | + total_ref, total_miss, total_ref > 0 ? |
| 152 | + total_hit * 1.0 / total_ref * 100 : 0); |
| 153 | + |
| 154 | + lookup_key = -1; |
| 155 | + while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) { |
| 156 | + err = bpf_map_delete_elem(fd, &next_key); |
| 157 | + if (err < 0) { |
| 158 | + fprintf(stderr, "failed to cleanup infos: %d\n", err); |
| 159 | + return; |
| 160 | + } |
| 161 | + lookup_key = next_key; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +int main(int argc, char **argv) |
| 166 | +{ |
| 167 | + struct bpf_link **rlinks = NULL, **mlinks = NULL; |
| 168 | + static const struct argp argp = { |
| 169 | + .options = opts, |
| 170 | + .parser = parse_arg, |
| 171 | + .doc = argp_program_doc, |
| 172 | + }; |
| 173 | + struct llcstat_bpf *obj; |
| 174 | + int err, i; |
| 175 | + |
| 176 | + err = argp_parse(&argp, argc, argv, 0, NULL, NULL); |
| 177 | + if (err) |
| 178 | + return err; |
| 179 | + |
| 180 | + libbpf_set_print(libbpf_print_fn); |
| 181 | + |
| 182 | + err = bump_memlock_rlimit(); |
| 183 | + if (err) { |
| 184 | + fprintf(stderr, "failed to increase rlimit: %d\n", err); |
| 185 | + return 1; |
| 186 | + } |
| 187 | + |
| 188 | + obj = llcstat_bpf__open(); |
| 189 | + if (!obj) { |
| 190 | + fprintf(stderr, "failed to open and/or load BPF object\n"); |
| 191 | + return 1; |
| 192 | + } |
| 193 | + |
| 194 | + nr_cpus = libbpf_num_possible_cpus(); |
| 195 | + mlinks = calloc(nr_cpus, sizeof(*mlinks)); |
| 196 | + rlinks = calloc(nr_cpus, sizeof(*rlinks)); |
| 197 | + if (!mlinks || !rlinks) { |
| 198 | + fprintf(stderr, "failed to alloc mlinks or rlinks\n"); |
| 199 | + goto cleanup; |
| 200 | + } |
| 201 | + |
| 202 | + err = llcstat_bpf__load(obj); |
| 203 | + if (err) { |
| 204 | + fprintf(stderr, "failed to load BPF object: %d\n", err); |
| 205 | + goto cleanup; |
| 206 | + } |
| 207 | + |
| 208 | + if (open_and_attach_perf_event(PERF_COUNT_HW_CACHE_MISSES, |
| 209 | + env.sample_period, |
| 210 | + obj->progs.on_cache_miss, mlinks)) |
| 211 | + goto cleanup; |
| 212 | + if (open_and_attach_perf_event(PERF_COUNT_HW_CACHE_REFERENCES, |
| 213 | + env.sample_period, |
| 214 | + obj->progs.on_cache_ref, rlinks)) |
| 215 | + goto cleanup; |
| 216 | + |
| 217 | + printf("Running for %ld seconds or Hit Ctrl-C to end.\n", env.duration); |
| 218 | + |
| 219 | + signal(SIGINT, sig_handler); |
| 220 | + |
| 221 | + sleep(env.duration); |
| 222 | + |
| 223 | + printf("%-8s %-16s %-4s %12s %12s %7s\n", |
| 224 | + "PID", "NAME", "CPU", "REFERENCE", "MISS", "HIT%"); |
| 225 | + |
| 226 | + print_map(obj->maps.infos); |
| 227 | + |
| 228 | +cleanup: |
| 229 | + for (i = 0; i < nr_cpus; i++) { |
| 230 | + bpf_link__destroy(mlinks[i]); |
| 231 | + bpf_link__destroy(rlinks[i]); |
| 232 | + } |
| 233 | + free(mlinks); |
| 234 | + free(rlinks); |
| 235 | + llcstat_bpf__destroy(obj); |
| 236 | + |
| 237 | + return err != 0; |
| 238 | +} |
0 commit comments