Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp530716ybt; Fri, 10 Jul 2020 06:15:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxDtHy8oCSpA5g6DSVHoKerso3Wfs0fzs09N05LDO6P5UpAoiBpL9smxzaPbQfJPBC7lGxx X-Received: by 2002:a50:cf43:: with SMTP id d3mr80963353edk.40.1594386942916; Fri, 10 Jul 2020 06:15:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594386942; cv=none; d=google.com; s=arc-20160816; b=cW0Dbkch691s04Yn/sKEwjfEwz2YzbwLhDsmBoGQ1tpgjZg7nby1pTHgNSVqmXbLvB 37dUwRFxPUi2kbAKtxFIkWqHuw0icsKZ7VG52bZS1PKkt67SMg041hpIO3BNxgzXibH7 aR+dBybkhqsCa9CgSPf1cgA3Z+DYTTlauFHGEUJpnIytge8mTKPyJawq1hPI8B2bWBVy hLXISiO7Y37u4VgpvZk/1tyCIwLDbrtBhbM61lWgqdz5+uZfIsCD2TeEsnsM5gARR0fH pioBuBcg3m1kTPSj0bhcgE16peJGo39AqT7AgoaVfaZyAqnThzm3v/himHzQCv0Xgj2V KVBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=g8xtd7zhQVNGIjEK9Fl8Rtctg5K/7TddoLm/+Jva81I=; b=z18Iqdo988YtU1u4IljtVvFu4/wn/PijACS4YitJpVNvsG5pviQipXv8qjczywFB2I HKrmRxWfZlc08QsNcn2vBjmckA0zyyuu/+BaR+JzTEzdbPV9JWkAf48TfkXn73kCgCl1 NKxjrcz3Awhf0CJ8d1WG75fqkHeGQUSteoamAN8BmGB4ESt+oOZ5fH5zfkul3ur1v8Rs G8ARsNMwfJDbCjlgDjKQuyYYVe6dHgvfIQl04j2zo3Xi6XdVKRdCJ6D7KPlwZB0GkhsP AomP2X42E4JmH+Z73kN7OkKTkhjgjdn25iH3tOlfZaiQoyQ8YI3qGjXxPoU9INhXiXn6 fIng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=plqeZHsn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u23si3962925eds.479.2020.07.10.06.15.18; Fri, 10 Jul 2020 06:15:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=plqeZHsn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727871AbgGJNPD (ORCPT + 99 others); Fri, 10 Jul 2020 09:15:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726840AbgGJNPC (ORCPT ); Fri, 10 Jul 2020 09:15:02 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 915B3C08C5CE for ; Fri, 10 Jul 2020 06:15:02 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id q17so2230516pls.9 for ; Fri, 10 Jul 2020 06:15:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=g8xtd7zhQVNGIjEK9Fl8Rtctg5K/7TddoLm/+Jva81I=; b=plqeZHsnbyfq16f826KOUC1+ujd6YdGAePKrMa8/41ZaA5spKLuBB9HW4bFp9G/pxH Q+YRCKfC6lzov7L4JJBqyrREq9ae5ZIddaRstbWzFbbYh1410hxE28brqjSz0zu8Tuhr 6OtbN8QMt/tcXcRemKmfrWZzEjuoHdmYXT0C5vSqb1cNeHSCwWNJwivqCopN5l3nROQd ZJ6tt1sJYj86Sy/YVMBBbAudvUYtdvx7XyJSi5YBFT0xzT/VcJw05Ky/gIhRrA6jDagm mCp7pwBPikkYZosbzBeufm/ni1r+BDLdRy/pXYKXVl4EJQE/LKe70yFtzAzzOaQ2tRiO w54w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=g8xtd7zhQVNGIjEK9Fl8Rtctg5K/7TddoLm/+Jva81I=; b=SP58L2M2DmVQQLqyu4lx6iJLAWHUeJyiu2zWl1Buq9eL+YYVubB24kIhzViEq8dTK3 v/UD0qkN73CnLUT8naBtfBLu4QnYGsplG790gd9E+CC5wxe9Y/b/61HIpdI1U2dpYe/p PRSr4iqFaaev+nX2ZIJ5NibNoxb1oVfF6lLfH1ZzprJh5MyMq5UlW+reZfiqixCL6sc7 xCr/6w3B90cs4DbH/nG+n28xgEJqyzB6rBhc0B4SP+jFNy2epSFBOldXSBJxkKeO0FOb df+1hBVY2vNVLPrv6ejLojdDTKtWSJ/3hH/k///xcqGZKyuwhkDNEnVpcJzRthT0FTct hHJw== X-Gm-Message-State: AOAM532szs14ZpMNa7X9A7mrhc3BsVviJUOpXdw8hzDr/UE1GZOFmE7T n1Y2ecmblFpZCcZcZnVWSRM= X-Received: by 2002:a17:902:b095:: with SMTP id p21mr56173472plr.4.1594386902058; Fri, 10 Jul 2020 06:15:02 -0700 (PDT) Received: from mail.google.com ([149.248.10.52]) by smtp.gmail.com with ESMTPSA id 207sm6177859pfa.100.2020.07.10.06.14.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jul 2020 06:15:01 -0700 (PDT) Date: Fri, 10 Jul 2020 21:14:53 +0800 From: Changbin Du To: Arnaldo Carvalho de Melo Cc: Changbin Du , Jiri Olsa , Peter Zijlstra , Ingo Molnar , Namhyung Kim , Steven Rostedt , linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 09/17] perf: util: add general function to parse sublevel options Message-ID: <20200710131453.xnhmpeenprfqcvic@mail.google.com> References: <20200708164605.31245-1-changbin.du@gmail.com> <20200708164605.31245-10-changbin.du@gmail.com> <20200708173423.GC22437@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200708173423.GC22437@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 08, 2020 at 02:34:23PM -0300, Arnaldo Carvalho de Melo wrote: > Em Thu, Jul 09, 2020 at 12:45:57AM +0800, Changbin Du escreveu: > > This factors out a general function perf_parse_sublevel_options() to parse > > sublevel options. The 'sublevel' options is something like the '--debug' > > options which allow more sublevel options. > > Please don't add stuff to util.h/util.c, its too generic a name and > we've been trying to move things to more appropriate locations, so > follow the example of: > > [acme@quaco perf]$ ls -la tools/perf/util/parse-regs-options.* > -rw-rw-r--. 1 acme acme 1932 Feb 12 15:11 tools/perf/util/parse-regs-options.c > -rw-rw-r--. 1 acme acme 316 Dec 20 2019 tools/perf/util/parse-regs-options.h > [acme@quaco perf]$ > I crated files util/parse-sublevel-options.c and util/parse-sublevel-options.h to hold this new function. > Good job identifying this code and reusing it! > > - Arnaldo > > > Signed-off-by: Changbin Du > > --- > > tools/perf/util/debug.c | 61 ++++++++++++----------------------------- > > tools/perf/util/util.c | 56 +++++++++++++++++++++++++++++++++++++ > > tools/perf/util/util.h | 7 +++++ > > 3 files changed, 80 insertions(+), 44 deletions(-) > > > > diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c > > index adb656745ecc..79999c57a609 100644 > > --- a/tools/perf/util/debug.c > > +++ b/tools/perf/util/debug.c > > @@ -20,6 +20,7 @@ > > #include "target.h" > > #include "ui/helpline.h" > > #include "ui/ui.h" > > +#include "util/util.h" > > > > #include > > > > @@ -173,65 +174,37 @@ void trace_event(union perf_event *event) > > trace_event_printer, event); > > } > > > > -static struct debug_variable { > > - const char *name; > > - int *ptr; > > -} debug_variables[] = { > > - { .name = "verbose", .ptr = &verbose }, > > - { .name = "ordered-events", .ptr = &debug_ordered_events}, > > - { .name = "stderr", .ptr = &redirect_to_stderr}, > > - { .name = "data-convert", .ptr = &debug_data_convert }, > > - { .name = "perf-event-open", .ptr = &debug_peo_args }, > > +static struct sublevel_option debug_opts[] = { > > + { .name = "verbose", .value_ptr = &verbose }, > > + { .name = "ordered-events", .value_ptr = &debug_ordered_events}, > > + { .name = "stderr", .value_ptr = &redirect_to_stderr}, > > + { .name = "data-convert", .value_ptr = &debug_data_convert }, > > + { .name = "perf-event-open", .value_ptr = &debug_peo_args }, > > { .name = NULL, } > > }; > > > > int perf_debug_option(const char *str) > > { > > - struct debug_variable *var = &debug_variables[0]; > > - char *vstr, *s = strdup(str); > > - int v = 1; > > - > > - vstr = strchr(s, '='); > > - if (vstr) > > - *vstr++ = 0; > > - > > - while (var->name) { > > - if (!strcmp(s, var->name)) > > - break; > > - var++; > > - } > > - > > - if (!var->name) { > > - pr_err("Unknown debug variable name '%s'\n", s); > > - free(s); > > - return -1; > > - } > > + int ret; > > > > - if (vstr) { > > - v = atoi(vstr); > > - /* > > - * Allow only values in range (0, 10), > > - * otherwise set 0. > > - */ > > - v = (v < 0) || (v > 10) ? 0 : v; > > - } > > + ret = perf_parse_sublevel_options(str, debug_opts); > > + if (ret) > > + return ret; > > > > - if (quiet) > > - v = -1; > > + /* Allow only verbose value in range (0, 10), otherwise set 0. */ > > + verbose = (verbose < 0) || (verbose > 10) ? 0 : verbose; > > > > - *var->ptr = v; > > - free(s); > > return 0; > > } > > > > int perf_quiet_option(void) > > { > > - struct debug_variable *var = &debug_variables[0]; > > + struct sublevel_option *opt = &debug_opts[0]; > > > > /* disable all debug messages */ > > - while (var->name) { > > - *var->ptr = -1; > > - var++; > > + while (opt->name) { > > + *opt->value_ptr = -1; > > + opt++; > > } > > > > return 0; > > diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c > > index 37a9492edb3e..7e532a93835b 100644 > > --- a/tools/perf/util/util.c > > +++ b/tools/perf/util/util.c > > @@ -416,3 +416,59 @@ char *perf_exe(char *buf, int len) > > } > > return strcpy(buf, "perf"); > > } > > + > > +static int parse_one_sublevel_option(const char *str, > > + struct sublevel_option *opts) > > +{ > > + struct sublevel_option *opt = &opts[0]; > > + char *vstr, *s = strdup(str); > > + int v = 1; > > + > > + vstr = strchr(s, '='); > > + if (vstr) > > + *vstr++ = 0; > > + > > + while (opt->name) { > > + if (!strcmp(s, opt->name)) > > + break; > > + opt++; > > + } > > + > > + if (!opt->name) { > > + pr_err("Unknown option name '%s'\n", s); > > + free(s); > > + return -1; > > + } > > + > > + if (vstr) > > + v = atoi(vstr); > > + > > + *opt->value_ptr = v; > > + free(s); > > + return 0; > > +} > > + > > +/* parse options like --foo a=,b,c... */ > > +int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts) > > +{ > > + char *s = strdup(str); > > + char *p = NULL; > > + int ret; > > + > > + if (!s) > > + return -1; > > + > > + p = strtok(s, ","); > > + while (p) { > > + ret = parse_one_sublevel_option(p, opts); > > + if (ret) { > > + free(s); > > + return ret; > > + } > > + > > + p = strtok(NULL, ","); > > + } > > + > > + free(s); > > + return 0; > > +} > > \ No newline at end of file > > diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h > > index f486fdd3a538..8cb1f980935c 100644 > > --- a/tools/perf/util/util.h > > +++ b/tools/perf/util/util.h > > @@ -52,6 +52,13 @@ void perf_set_multithreaded(void); > > > > char *perf_exe(char *buf, int len); > > > > +struct sublevel_option { > > + const char *name; > > + int *value_ptr; > > +}; > > + > > +int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts); > > + > > #ifndef O_CLOEXEC > > #ifdef __sparc__ > > #define O_CLOEXEC 0x400000 > > -- > > 2.25.1 > > > > -- > > - Arnaldo -- Cheers, Changbin Du