Skip to content

Commit 066dacb

Browse files
WangNan0acmel
authored andcommitted
perf bpf: Add API to set values to map entries in a bpf object
bpf__config_obj() is introduced as a core API to config BPF object after loading. One configuration option of maps is introduced. After this patch BPF object can accept assignments like: map:my_map.value=1234 (map.my_map.value looks pretty. However, there's a small but hard to fix problem related to flex's greedy matching. Please see [1]. Choose ':' to avoid it in a simpler way.) This patch is more complex than the work it does because the consideration of extension. In designing BPF map configuration, the following things should be considered: 1. Array indices selection: perf should allow user setting different value for different slots in an array, with syntax like: map:my_map.value[0,3...6]=1234; 2. A map should be set by different config terms, each for a part of it. For example, set each slot to the pid of a thread; 3. Type of value: integer is not the only valid value type. A perf counter can also be put into a map after commit 35578d7 ("bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU counter") 4. For a hash table, it should be possible to use a string or other value as a key; 5. It is possible that map configuration is unable to be setup during parsing. A perf counter is an example. Therefore, this patch does the following: 1. Instead of updating map element during parsing, this patch stores map config options in 'struct bpf_map_priv'. Following patches will apply those configs at an appropriate time; 2. Link map operations in a list so a map can have multiple config terms attached, so different parts can be configured separately; 3. Make 'struct bpf_map_priv' extensible so that the following patches can add new types of keys and operations; 4. Use bpf_obj_config__map_funcs array to support more map config options. Since the patch changing the event parser to parse BPF object config is relative large, I've put it in another commit. Code in this patch can be tested after applying the next patch. [1] http://lkml.kernel.org/g/564ED621.4050500@huawei.com Signed-off-by: Wang Nan <wangnan0@huawei.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Brendan Gregg <brendan.d.gregg@gmail.com> Cc: Cody P Schafer <dev@codyps.com> Cc: He Kuang <hekuang@huawei.com> Cc: Jeremie Galarneau <jeremie.galarneau@efficios.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kirill Smelkov <kirr@nexedi.com> Cc: Li Zefan <lizefan@huawei.com> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1456132275-98875-4-git-send-email-wangnan0@huawei.com Signed-off-by: He Kuang <hekuang@huawei.com> [ Changes "maps:my_map.value" to "map:my_map.value", improved error messages ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent dd42baf commit 066dacb

2 files changed

Lines changed: 314 additions & 0 deletions

File tree

‎tools/perf/util/bpf-loader.c‎

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,261 @@ int bpf__foreach_tev(struct bpf_object *obj,
739739
return 0;
740740
}
741741

742+
enum bpf_map_op_type {
743+
BPF_MAP_OP_SET_VALUE,
744+
};
745+
746+
enum bpf_map_key_type {
747+
BPF_MAP_KEY_ALL,
748+
};
749+
750+
struct bpf_map_op {
751+
struct list_head list;
752+
enum bpf_map_op_type op_type;
753+
enum bpf_map_key_type key_type;
754+
union {
755+
u64 value;
756+
} v;
757+
};
758+
759+
struct bpf_map_priv {
760+
struct list_head ops_list;
761+
};
762+
763+
static void
764+
bpf_map_op__delete(struct bpf_map_op *op)
765+
{
766+
if (!list_empty(&op->list))
767+
list_del(&op->list);
768+
free(op);
769+
}
770+
771+
static void
772+
bpf_map_priv__purge(struct bpf_map_priv *priv)
773+
{
774+
struct bpf_map_op *pos, *n;
775+
776+
list_for_each_entry_safe(pos, n, &priv->ops_list, list) {
777+
list_del_init(&pos->list);
778+
bpf_map_op__delete(pos);
779+
}
780+
}
781+
782+
static void
783+
bpf_map_priv__clear(struct bpf_map *map __maybe_unused,
784+
void *_priv)
785+
{
786+
struct bpf_map_priv *priv = _priv;
787+
788+
bpf_map_priv__purge(priv);
789+
free(priv);
790+
}
791+
792+
static struct bpf_map_op *
793+
bpf_map_op__new(void)
794+
{
795+
struct bpf_map_op *op;
796+
797+
op = zalloc(sizeof(*op));
798+
if (!op) {
799+
pr_debug("Failed to alloc bpf_map_op\n");
800+
return ERR_PTR(-ENOMEM);
801+
}
802+
INIT_LIST_HEAD(&op->list);
803+
804+
op->key_type = BPF_MAP_KEY_ALL;
805+
return op;
806+
}
807+
808+
static int
809+
bpf_map__add_op(struct bpf_map *map, struct bpf_map_op *op)
810+
{
811+
struct bpf_map_priv *priv;
812+
const char *map_name;
813+
int err;
814+
815+
map_name = bpf_map__get_name(map);
816+
err = bpf_map__get_private(map, (void **)&priv);
817+
if (err) {
818+
pr_debug("Failed to get private from map %s\n", map_name);
819+
return err;
820+
}
821+
822+
if (!priv) {
823+
priv = zalloc(sizeof(*priv));
824+
if (!priv) {
825+
pr_debug("No enough memory to alloc map private\n");
826+
return -ENOMEM;
827+
}
828+
INIT_LIST_HEAD(&priv->ops_list);
829+
830+
if (bpf_map__set_private(map, priv, bpf_map_priv__clear)) {
831+
free(priv);
832+
return -BPF_LOADER_ERRNO__INTERNAL;
833+
}
834+
}
835+
836+
list_add_tail(&op->list, &priv->ops_list);
837+
return 0;
838+
}
839+
840+
static int
841+
__bpf_map__config_value(struct bpf_map *map,
842+
struct parse_events_term *term)
843+
{
844+
struct bpf_map_def def;
845+
struct bpf_map_op *op;
846+
const char *map_name;
847+
int err;
848+
849+
map_name = bpf_map__get_name(map);
850+
851+
err = bpf_map__get_def(map, &def);
852+
if (err) {
853+
pr_debug("Unable to get map definition from '%s'\n",
854+
map_name);
855+
return -BPF_LOADER_ERRNO__INTERNAL;
856+
}
857+
858+
if (def.type != BPF_MAP_TYPE_ARRAY) {
859+
pr_debug("Map %s type is not BPF_MAP_TYPE_ARRAY\n",
860+
map_name);
861+
return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
862+
}
863+
if (def.key_size < sizeof(unsigned int)) {
864+
pr_debug("Map %s has incorrect key size\n", map_name);
865+
return -BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE;
866+
}
867+
switch (def.value_size) {
868+
case 1:
869+
case 2:
870+
case 4:
871+
case 8:
872+
break;
873+
default:
874+
pr_debug("Map %s has incorrect value size\n", map_name);
875+
return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
876+
}
877+
878+
op = bpf_map_op__new();
879+
if (IS_ERR(op))
880+
return PTR_ERR(op);
881+
op->op_type = BPF_MAP_OP_SET_VALUE;
882+
op->v.value = term->val.num;
883+
884+
err = bpf_map__add_op(map, op);
885+
if (err)
886+
bpf_map_op__delete(op);
887+
return err;
888+
}
889+
890+
static int
891+
bpf_map__config_value(struct bpf_map *map,
892+
struct parse_events_term *term,
893+
struct perf_evlist *evlist __maybe_unused)
894+
{
895+
if (!term->err_val) {
896+
pr_debug("Config value not set\n");
897+
return -BPF_LOADER_ERRNO__OBJCONF_CONF;
898+
}
899+
900+
if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) {
901+
pr_debug("ERROR: wrong value type\n");
902+
return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE;
903+
}
904+
905+
return __bpf_map__config_value(map, term);
906+
}
907+
908+
struct bpf_obj_config__map_func {
909+
const char *config_opt;
910+
int (*config_func)(struct bpf_map *, struct parse_events_term *,
911+
struct perf_evlist *);
912+
};
913+
914+
struct bpf_obj_config__map_func bpf_obj_config__map_funcs[] = {
915+
{"value", bpf_map__config_value},
916+
};
917+
918+
static int
919+
bpf__obj_config_map(struct bpf_object *obj,
920+
struct parse_events_term *term,
921+
struct perf_evlist *evlist,
922+
int *key_scan_pos)
923+
{
924+
/* key is "map:<mapname>.<config opt>" */
925+
char *map_name = strdup(term->config + sizeof("map:") - 1);
926+
struct bpf_map *map;
927+
int err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
928+
char *map_opt;
929+
size_t i;
930+
931+
if (!map_name)
932+
return -ENOMEM;
933+
934+
map_opt = strchr(map_name, '.');
935+
if (!map_opt) {
936+
pr_debug("ERROR: Invalid map config: %s\n", map_name);
937+
goto out;
938+
}
939+
940+
*map_opt++ = '\0';
941+
if (*map_opt == '\0') {
942+
pr_debug("ERROR: Invalid map option: %s\n", term->config);
943+
goto out;
944+
}
945+
946+
map = bpf_object__get_map_by_name(obj, map_name);
947+
if (!map) {
948+
pr_debug("ERROR: Map %s doesn't exist\n", map_name);
949+
err = -BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST;
950+
goto out;
951+
}
952+
953+
*key_scan_pos += map_opt - map_name;
954+
for (i = 0; i < ARRAY_SIZE(bpf_obj_config__map_funcs); i++) {
955+
struct bpf_obj_config__map_func *func =
956+
&bpf_obj_config__map_funcs[i];
957+
958+
if (strcmp(map_opt, func->config_opt) == 0) {
959+
err = func->config_func(map, term, evlist);
960+
goto out;
961+
}
962+
}
963+
964+
pr_debug("ERROR: Invalid map config option '%s'\n", map_opt);
965+
err = -BPF_LOADER_ERRNO__OBJCONF_MAP_OPT;
966+
out:
967+
free(map_name);
968+
if (!err)
969+
key_scan_pos += strlen(map_opt);
970+
return err;
971+
}
972+
973+
int bpf__config_obj(struct bpf_object *obj,
974+
struct parse_events_term *term,
975+
struct perf_evlist *evlist,
976+
int *error_pos)
977+
{
978+
int key_scan_pos = 0;
979+
int err;
980+
981+
if (!obj || !term || !term->config)
982+
return -EINVAL;
983+
984+
if (!prefixcmp(term->config, "map:")) {
985+
key_scan_pos = sizeof("map:") - 1;
986+
err = bpf__obj_config_map(obj, term, evlist, &key_scan_pos);
987+
goto out;
988+
}
989+
err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
990+
out:
991+
if (error_pos)
992+
*error_pos = key_scan_pos;
993+
return err;
994+
995+
}
996+
742997
#define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START)
743998
#define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
744999
#define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
@@ -753,6 +1008,14 @@ static const char *bpf_loader_strerror_table[NR_ERRNO] = {
7531008
[ERRCODE_OFFSET(PROLOGUE)] = "Failed to generate prologue",
7541009
[ERRCODE_OFFSET(PROLOGUE2BIG)] = "Prologue too big for program",
7551010
[ERRCODE_OFFSET(PROLOGUEOOB)] = "Offset out of bound for prologue",
1011+
[ERRCODE_OFFSET(OBJCONF_OPT)] = "Invalid object config option",
1012+
[ERRCODE_OFFSET(OBJCONF_CONF)] = "Config value not set (missing '=')",
1013+
[ERRCODE_OFFSET(OBJCONF_MAP_OPT)] = "Invalid object map config option",
1014+
[ERRCODE_OFFSET(OBJCONF_MAP_NOTEXIST)] = "Target map doesn't exist",
1015+
[ERRCODE_OFFSET(OBJCONF_MAP_VALUE)] = "Incorrect value type for map",
1016+
[ERRCODE_OFFSET(OBJCONF_MAP_TYPE)] = "Incorrect map type",
1017+
[ERRCODE_OFFSET(OBJCONF_MAP_KEYSIZE)] = "Incorrect map key size",
1018+
[ERRCODE_OFFSET(OBJCONF_MAP_VALUESIZE)] = "Incorrect map value size",
7561019
};
7571020

7581021
static int
@@ -872,3 +1135,16 @@ int bpf__strerror_load(struct bpf_object *obj,
8721135
bpf__strerror_end(buf, size);
8731136
return 0;
8741137
}
1138+
1139+
int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
1140+
struct parse_events_term *term __maybe_unused,
1141+
struct perf_evlist *evlist __maybe_unused,
1142+
int *error_pos __maybe_unused, int err,
1143+
char *buf, size_t size)
1144+
{
1145+
bpf__strerror_head(err, buf, size);
1146+
bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE,
1147+
"Can't use this config term with this map type");
1148+
bpf__strerror_end(buf, size);
1149+
return 0;
1150+
}

‎tools/perf/util/bpf-loader.h‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string.h>
1111
#include <bpf/libbpf.h>
1212
#include "probe-event.h"
13+
#include "evlist.h"
1314
#include "debug.h"
1415

1516
enum bpf_loader_errno {
@@ -24,10 +25,19 @@ enum bpf_loader_errno {
2425
BPF_LOADER_ERRNO__PROLOGUE, /* Failed to generate prologue */
2526
BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */
2627
BPF_LOADER_ERRNO__PROLOGUEOOB, /* Offset out of bound for prologue */
28+
BPF_LOADER_ERRNO__OBJCONF_OPT, /* Invalid object config option */
29+
BPF_LOADER_ERRNO__OBJCONF_CONF, /* Config value not set (lost '=')) */
30+
BPF_LOADER_ERRNO__OBJCONF_MAP_OPT, /* Invalid object map config option */
31+
BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST, /* Target map not exist */
32+
BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE, /* Incorrect value type for map */
33+
BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE, /* Incorrect map type */
34+
BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE, /* Incorrect map key size */
35+
BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,/* Incorrect map value size */
2736
__BPF_LOADER_ERRNO__END,
2837
};
2938

3039
struct bpf_object;
40+
struct parse_events_term;
3141
#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
3242

3343
typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
@@ -53,6 +63,14 @@ int bpf__strerror_load(struct bpf_object *obj, int err,
5363
char *buf, size_t size);
5464
int bpf__foreach_tev(struct bpf_object *obj,
5565
bpf_prog_iter_callback_t func, void *arg);
66+
67+
int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
68+
struct perf_evlist *evlist, int *error_pos);
69+
int bpf__strerror_config_obj(struct bpf_object *obj,
70+
struct parse_events_term *term,
71+
struct perf_evlist *evlist,
72+
int *error_pos, int err, char *buf,
73+
size_t size);
5674
#else
5775
static inline struct bpf_object *
5876
bpf__prepare_load(const char *filename __maybe_unused,
@@ -83,6 +101,15 @@ bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
83101
return 0;
84102
}
85103

104+
static inline int
105+
bpf__config_obj(struct bpf_object *obj __maybe_unused,
106+
struct parse_events_term *term __maybe_unused,
107+
struct perf_evlist *evlist __maybe_unused,
108+
int *error_pos __maybe_unused)
109+
{
110+
return 0;
111+
}
112+
86113
static inline int
87114
__bpf_strerror(char *buf, size_t size)
88115
{
@@ -118,5 +145,16 @@ static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
118145
{
119146
return __bpf_strerror(buf, size);
120147
}
148+
149+
static inline int
150+
bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
151+
struct parse_events_term *term __maybe_unused,
152+
struct perf_evlist *evlist __maybe_unused,
153+
int *error_pos __maybe_unused,
154+
int err __maybe_unused,
155+
char *buf, size_t size)
156+
{
157+
return __bpf_strerror(buf, size);
158+
}
121159
#endif
122160
#endif

0 commit comments

Comments
 (0)