From patchwork Sat Oct 17 10:48:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 55158 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lf0-f69.google.com (mail-lf0-f69.google.com [209.85.215.69]) by patches.linaro.org (Postfix) with ESMTPS id DB96A22FFA for ; Sat, 17 Oct 2015 10:50:52 +0000 (UTC) Received: by lfaz124 with SMTP id z124sf25795012lfa.0 for ; Sat, 17 Oct 2015 03:50:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type:sender:precedence :list-id:x-original-sender:x-original-authentication-results :mailing-list:list-post:list-help:list-archive:list-unsubscribe; bh=xM8HHgQN6uwxXpv04MxDMuXN6+7T/gduVQazVp+n12A=; b=FjdPUIg20C909RXNox8C623PVAfxYy1rdcskIKjjZjUdh5Q4U3e74COjWJnavoQUeP AePcF6Dyq3EYprcimsa4DUxJosrG/R3nud/L+/8T4iiq9f9vfPbbrKvcG1rkMSi3vf6s FFIm2d2LRoepA2QrtzMW4ZoobrGZWoPvkaV3Rfabs0ttOaF2j1GUuedTupIwNzSa5lxP tTpKj6uUaVWyhKmJQZbGuAqfOx/0q38XmfuFM3uDBtwDybwNxBUZ9FK1P0dV74irCvu6 lSSXHSyYZWtCEiVKLpbdJRAkMYh2qdLxW80RYnvZnyf2fLWa6adaPs1gOtE4yImc21wk VGjA== X-Gm-Message-State: ALoCoQn0CYiIp0IaO4egIdllBkeUSDyWZMcjee3xD3q57SzCklVjA3+FHtTTiiUTb8Oe2FkaflIW X-Received: by 10.194.118.65 with SMTP id kk1mr4785672wjb.5.1445079051840; Sat, 17 Oct 2015 03:50:51 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.27.206 with SMTP id b197ls364755lfb.26.gmail; Sat, 17 Oct 2015 03:50:51 -0700 (PDT) X-Received: by 10.112.151.106 with SMTP id up10mr10614778lbb.106.1445079051495; Sat, 17 Oct 2015 03:50:51 -0700 (PDT) Received: from mail-lb0-f177.google.com (mail-lb0-f177.google.com. [209.85.217.177]) by mx.google.com with ESMTPS id pq6si15795257lbb.61.2015.10.17.03.50.51 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Oct 2015 03:50:51 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.177 as permitted sender) client-ip=209.85.217.177; Received: by lbbpp2 with SMTP id pp2so86910722lbb.0 for ; Sat, 17 Oct 2015 03:50:51 -0700 (PDT) X-Received: by 10.112.202.35 with SMTP id kf3mr10403249lbc.19.1445079051323; Sat, 17 Oct 2015 03:50:51 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp436757lbq; Sat, 17 Oct 2015 03:50:50 -0700 (PDT) X-Received: by 10.66.100.164 with SMTP id ez4mr22333374pab.141.1445079050155; Sat, 17 Oct 2015 03:50:50 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ew2si36085563pbb.249.2015.10.17.03.50.48; Sat, 17 Oct 2015 03:50:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752816AbbJQKuA (ORCPT + 30 others); Sat, 17 Oct 2015 06:50:00 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:7864 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752301AbbJQKtW (ORCPT ); Sat, 17 Oct 2015 06:49:22 -0400 Received: from 172.24.1.50 (EHLO szxeml428-hub.china.huawei.com) ([172.24.1.50]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CWX99306; Sat, 17 Oct 2015 18:48:54 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml428-hub.china.huawei.com (10.82.67.183) with Microsoft SMTP Server id 14.3.235.1; Sat, 17 Oct 2015 18:48:41 +0800 From: Wang Nan To: , , CC: , , , , , , , , , , , , Wang Nan , Arnaldo Carvalho de Melo Subject: [RFC PATCH 1/7] perf tools: Add API to config maps in bpf object Date: Sat, 17 Oct 2015 10:48:24 +0000 Message-ID: <1445078910-73699-2-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1445078910-73699-1-git-send-email-wangnan0@huawei.com> References: <1445078910-73699-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: wangnan0@huawei.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.177 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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 configuration like: maps.my_map.value=1234 This patch is more complex than the work it really does because the consideration of extension. In designing of BPF map configuration, following things should be considered: 1. Array indics selection: perf should allow user setting different value to different slots in an array, with syntax like: maps.my_map.value[0,3-6]=1234; 2. Type of value: integer is not the only valid value type. Perf event can also be put into a map after commit 35578d7984003097af2b1e3 (bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter); 3. For hash table, it is possible to use string or other as key; 4. It is possible that map configuration is unable to be setup during parsing. Perf event is an example. Therefore, this patch does tie following thing for extension: 1. Instead of update map element during parsing, this patch stores map config options in 'struct bpf_map_priv'. Following patches would apply those configs at proper time; 2. Make 'struct bpf_map_priv' extensible so following patches can add new key and value operations; 3. Use bpf_config_map_funcs array to support more maps configuration. Signed-off-by: Wang Nan Signed-off-by: He Kuang Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/n/ebpf-36xcrahy9n0ayc05mu7aajpk@git.kernel.org --- tools/perf/util/bpf-loader.c | 180 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 27 +++++++ 2 files changed, 207 insertions(+) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index 73ff9a9..a5a1c36 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -10,11 +10,13 @@ #include #include "perf.h" #include "debug.h" +#include "util.h" #include "bpf-loader.h" #include "bpf-prologue.h" #include "llvm-utils.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES +#include "parse-events.h" #include "llvm-utils.h" #define DEFINE_PRINT_FN(name, level) \ @@ -633,6 +635,170 @@ int bpf__foreach_tev(struct bpf_object *obj, return 0; } +enum bpf_map_priv_key_type { + BPF_MAP_PRIV_KEY_ALL, +}; + +enum bpf_map_priv_value_type { + BPF_MAP_PRIV_VAL_VALUE, +}; + +struct bpf_map_priv { + struct { + enum bpf_map_priv_key_type type; + } key; + + struct { + enum bpf_map_priv_value_type type; + union { + u64 val; + }; + } value; +}; + +static void +bpf_map_priv__clear(struct bpf_map *map __maybe_unused, + void *_priv) +{ + struct bpf_map_priv *priv = _priv; + + free(priv); +} + +static int +bpf__config_obj_map_array_value(struct bpf_map *map, + struct parse_events_term *term) +{ + struct bpf_map_priv *priv; + struct bpf_map_def def; + const char *map_name; + int err; + + map_name = bpf_map__get_name(map); + + err = bpf_map__get_def(map, &def); + if (err) { + pr_debug("Unable to get map definition from '%s'\n", + map_name); + return -EINVAL; + } + + if (def.type != BPF_MAP_TYPE_ARRAY) { + pr_debug("Map %s type is not BPF_MAP_TYPE_ARRAY\n", + map_name); + return -EBADF; + } + if (def.key_size < sizeof(unsigned int)) { + pr_debug("Map %s has incorrect key size\n", map_name); + return -EINVAL; + } + switch (def.value_size) { + case 1: + case 2: + case 4: + case 8: + break; + default: + pr_debug("Map %s has incorrect value size\n", map_name); + return -EINVAL; + } + + priv = zalloc(sizeof(*priv)); + if (!priv) { + pr_debug("No enough memory to alloc map private\n"); + return -ENOMEM; + } + + priv->key.type = BPF_MAP_PRIV_KEY_ALL; + priv->value.type = BPF_MAP_PRIV_VAL_VALUE; + priv->value.val = term->val.num; + return bpf_map__set_private(map, priv, bpf_map_priv__clear); +} + +static int +bpf__config_obj_map_value(struct bpf_map *map, + struct parse_events_term *term, + struct perf_evlist *evlist __maybe_unused) +{ + if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) + return bpf__config_obj_map_array_value(map, term); + + pr_debug("ERROR: wrong value type\n"); + return -EINVAL; +} + +struct bpf_config_map_func { + const char *config_opt; + int (*config_func)(struct bpf_map *, struct parse_events_term *, + struct perf_evlist *); +}; + +struct bpf_config_map_func bpf_config_map_funcs[] = { + {"value", bpf__config_obj_map_value}, +}; + +static int +bpf__config_obj_map(struct bpf_object *obj, + struct parse_events_term *term, + struct perf_evlist *evlist) +{ + /* key is "maps.." */ + char *map_name = strdup(term->config + sizeof("maps.") - 1); + struct bpf_map *map; + int err = -ENOENT; + char *map_opt; + size_t i; + + if (!map_name) + return -ENOMEM; + + map_opt = strchr(map_name, '.'); + if (!map_opt) { + pr_debug("ERROR: Invalid map config: %s\n", map_name); + goto out; + } + + *map_opt++ = '\0'; + if (*map_opt == '\0') { + pr_debug("ERROR: Invalid map option: %s\n", term->config); + goto out; + } + + map = bpf_object__get_map_by_name(obj, map_name); + if (!map) { + pr_debug("ERROR: Map %s doesn't exist\n", map_name); + goto out; + } + + for (i = 0; i < ARRAY_SIZE(bpf_config_map_funcs); i++) { + struct bpf_config_map_func *func = &bpf_config_map_funcs[i]; + + if (strcmp(map_opt, func->config_opt) == 0) { + err = func->config_func(map, term, evlist); + goto out; + } + } + + pr_debug("ERROR: invalid config option '%s' for maps\n", + map_opt); + err = -ENOENT; +out: + free(map_name); + return err; +} + +int bpf__config_obj(struct bpf_object *obj, + struct parse_events_term *term, + struct perf_evlist *evlist) +{ + if (!obj || !term || !term->config) + return -ENODEV; + + if (!prefixcmp(term->config, "maps.")) + return bpf__config_obj_map(obj, term, evlist); + return -ENODEV; +} + #define bpf__strerror_head(err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\ @@ -675,3 +841,17 @@ int bpf__strerror_load(struct bpf_object *obj __maybe_unused, bpf__strerror_end(buf, size); return 0; } + +int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, + struct parse_events_term *term, + struct perf_evlist *evlist __maybe_unused, + int err, char *buf, size_t size) +{ + bpf__strerror_head(err, buf, size); + bpf__strerror_entry(ENODEV, "Invalid config option: '%s'", term->config) + bpf__strerror_entry(ENOENT, "Config target in '%s' is invalid", term->config) + bpf__strerror_entry(EBADF, "Map type mismatch in '%s'", term->config) + bpf__strerror_entry(EINVAL, "Invalid config value") + bpf__strerror_end(buf, size); + return 0; +} diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h index d8f1945..dfec9b8 100644 --- a/tools/perf/util/bpf-loader.h +++ b/tools/perf/util/bpf-loader.h @@ -9,9 +9,11 @@ #include #include #include "probe-event.h" +#include "evlist.h" #include "debug.h" struct bpf_object; +struct parse_events_term; #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, @@ -34,6 +36,13 @@ int bpf__strerror_load(struct bpf_object *obj, int err, char *buf, size_t size); int bpf__foreach_tev(struct bpf_object *obj, bpf_prog_iter_callback_t func, void *arg); + +int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term, + struct perf_evlist *evlist); +int bpf__strerror_config_obj(struct bpf_object *obj, + struct parse_events_term *term, + struct perf_evlist *evlist, + int err, char *buf, size_t size); #else static inline struct bpf_object * bpf__prepare_load(const char *filename __maybe_unused, @@ -65,6 +74,14 @@ bpf__foreach_tev(struct bpf_object *obj __maybe_unused, } static inline int +bpf__config_obj(struct bpf_object *obj __maybe_unused, + struct parse_events_term *term __maybe_unused, + struct perf_evlist *evlist __maybe_unused) +{ + return 0; +} + +static inline int __bpf_strerror(char *buf, size_t size) { if (!size) @@ -90,5 +107,15 @@ static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused, { return __bpf_strerror(buf, size); } + +static inline int +bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, + struct parse_events_term *term __maybe_unused, + struct perf_evlist *evlist __maybe_unused, + int err __maybe_unused, + char *buf, size_t size) +{ + return __bpf_strerror(buf, size); +} #endif #endif