File Index
Files and directories
Sign in
FILE

bpf_map.c

File preview with syntax highlighting.

linux-6.19.14/tools/perf/util/bpf_map.c C
 1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
 2
 3#include "util/bpf_map.h"
 4#include <bpf/bpf.h>
 5#include <bpf/libbpf.h>
 6#include <linux/err.h>
 7#include <linux/kernel.h>
 8#include <errno.h>
 9#include <stdbool.h>
10#include <stdlib.h>
11#include <unistd.h>
12
13static bool bpf_map__is_per_cpu(enum bpf_map_type type)
14{
15	return type == BPF_MAP_TYPE_PERCPU_HASH ||
16	       type == BPF_MAP_TYPE_PERCPU_ARRAY ||
17	       type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
18	       type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE;
19}
20
21static void *bpf_map__alloc_value(const struct bpf_map *map)
22{
23	if (bpf_map__is_per_cpu(bpf_map__type(map)))
24		return malloc(round_up(bpf_map__value_size(map), 8) *
25			      sysconf(_SC_NPROCESSORS_CONF));
26
27	return malloc(bpf_map__value_size(map));
28}
29
30int bpf_map__fprintf(struct bpf_map *map, FILE *fp)
31{
32	void *prev_key = NULL, *key, *value;
33	int fd = bpf_map__fd(map), err;
34	int printed = 0;
35
36	if (fd < 0)
37		return fd;
38
39	err = -ENOMEM;
40	key = malloc(bpf_map__key_size(map));
41	if (key == NULL)
42		goto out;
43
44	value = bpf_map__alloc_value(map);
45	if (value == NULL)
46		goto out_free_key;
47
48	while ((err = bpf_map_get_next_key(fd, prev_key, key) == 0)) {
49		int intkey = *(int *)key;
50
51		if (!bpf_map_lookup_elem(fd, key, value)) {
52			bool boolval = *(bool *)value;
53			if (boolval)
54				printed += fprintf(fp, "[%d] = %d,\n", intkey, boolval);
55		} else {
56			printed += fprintf(fp, "[%d] = ERROR,\n", intkey);
57		}
58
59		prev_key = key;
60	}
61
62	if (err == ENOENT)
63		err = printed;
64
65	free(value);
66out_free_key:
67	free(key);
68out:
69	return err;
70}
~/ Read-only file preview.