public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	Aditya Gupta <adityag@linux.ibm.com>,
	"Steinar H. Gunderson" <sesse@google.com>,
	Charlie Jenkins <charlie@rivosinc.com>,
	Changbin Du <changbin.du@huawei.com>,
	"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
	James Clark <james.clark@linaro.org>,
	Kajol Jain <kjain@linux.ibm.com>,
	Athira Rajeev <atrajeev@linux.vnet.ibm.com>,
	Li Huafei <lihuafei1@huawei.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	Andi Kleen <ak@linux.intel.com>,
	Chaitanya S Prakash <chaitanyas.prakash@arm.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	llvm@lists.linux.dev, Song Liu <song@kernel.org>,
	bpf@vger.kernel.org
Subject: Re: [PATCH v3 03/18] perf capstone: Move capstone functionality into its own file
Date: Tue, 28 Jan 2025 12:56:22 -0800	[thread overview]
Message-ID: <Z5lEdtLVHRZwxuY8@google.com> (raw)
In-Reply-To: <CAP-5=fV0w9tLFr7xYHFUH=UUq+tr+o5EYUik0d74rMWa9=Qi+A@mail.gmail.com>

On Fri, Jan 24, 2025 at 04:59:21PM -0800, Ian Rogers wrote:
> On Fri, Jan 24, 2025 at 1:58 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > On Wed, Jan 22, 2025 at 09:42:53AM -0800, Ian Rogers wrote:
> > > Capstone disassembly support was split between disasm.c and
> > > print_insn.c. Move support out of these files into capstone.[ch] and
> > > remove include capstone/capstone.h from those files. As disassembly
> > > routines can fail, make failure the only option without
> > > HAVE_LIBCAPSTONE_SUPPORT. For simplicity's sake, duplicate the
> > > read_symbol utility function.
> > >
> > > The intent with moving capstone support into a single file is that
> > > dynamic support, using dlopen for libcapstone, can be added in later
> > > patches. This can potentially always succeed or fail, so relying on
> > > ifdefs isn't sufficient. Using dlopen is a useful option to minimize
> > > the perf tools dependencies and potentially size.
> > >
> > > Signed-off-by: Ian Rogers <irogers@google.com>
> > > ---
> > >  tools/perf/builtin-script.c  |   2 -
> > >  tools/perf/util/Build        |   1 +
> > >  tools/perf/util/capstone.c   | 536 +++++++++++++++++++++++++++++++++++
> > >  tools/perf/util/capstone.h   |  24 ++
> > >  tools/perf/util/disasm.c     | 358 +----------------------
> > >  tools/perf/util/print_insn.c | 117 +-------
> > >  6 files changed, 569 insertions(+), 469 deletions(-)
> > >  create mode 100644 tools/perf/util/capstone.c
> > >  create mode 100644 tools/perf/util/capstone.h
> > >
> > > diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> > > index 33667b534634..f05b2b70d5a7 100644
> > > --- a/tools/perf/builtin-script.c
> > > +++ b/tools/perf/builtin-script.c
> > > @@ -1200,7 +1200,6 @@ static int any_dump_insn(struct evsel *evsel __maybe_unused,
> > >                        u8 *inbuf, int inlen, int *lenp,
> > >                        FILE *fp)
> > >  {
> > > -#ifdef HAVE_LIBCAPSTONE_SUPPORT
> > >       if (PRINT_FIELD(BRSTACKDISASM)) {
> > >               int printed = fprintf_insn_asm(x->machine, x->thread, x->cpumode, x->is64bit,
> > >                                              (uint8_t *)inbuf, inlen, ip, lenp,
> > > @@ -1209,7 +1208,6 @@ static int any_dump_insn(struct evsel *evsel __maybe_unused,
> > >               if (printed > 0)
> > >                       return printed;
> > >       }
> > > -#endif
> > >       return fprintf(fp, "%s", dump_insn(x, ip, inbuf, inlen, lenp));
> > >  }
> > >
> > > diff --git a/tools/perf/util/Build b/tools/perf/util/Build
> > > index 5ec97e8d6b6d..9542decf9625 100644
> > > --- a/tools/perf/util/Build
> > > +++ b/tools/perf/util/Build
> > > @@ -8,6 +8,7 @@ perf-util-y += block-info.o
> > >  perf-util-y += block-range.o
> > >  perf-util-y += build-id.o
> > >  perf-util-y += cacheline.o
> > > +perf-util-y += capstone.o
> > >  perf-util-y += config.o
> > >  perf-util-y += copyfile.o
> > >  perf-util-y += ctype.o
> > > diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c
> > > new file mode 100644
> > > index 000000000000..c0a6d94ebc18
> > > --- /dev/null
> > > +++ b/tools/perf/util/capstone.c
> > > @@ -0,0 +1,536 @@
> > > +// SPDX-License-Identifier: GPL-2.0
> > > +#include "capstone.h"
> > > +#include "annotate.h"
> > > +#include "addr_location.h"
> > > +#include "debug.h"
> > > +#include "disasm.h"
> > > +#include "dso.h"
> > > +#include "machine.h"
> > > +#include "map.h"
> > > +#include "namespaces.h"
> > > +#include "print_insn.h"
> > > +#include "symbol.h"
> > > +#include "thread.h"
> > > +#include <fcntl.h>
> > > +#include <string.h>
> > > +
> > > +#ifdef HAVE_LIBCAPSTONE_SUPPORT
> > > +#include <capstone/capstone.h>
> > > +#endif
> >
> > I think you can use a big #ifdef throughout the file to minimize the
> > #ifdef dances.  Usually it goes to the header to provide dummy static
> > inlines and make the .c file depends on config.  But I know you will
> > add dlopen code for the #else case later.
> 
> So I think big ifdefs like:
> 
> #if HAVE_xyz
> // 100s of lines
> #else
> // 100s of lines
> #endif
> 
> are best avoided. It is also the point of the shim-ing that we do
> 
> ... perf_foobar(...)
> {
> #if NO_SHIM
>   ... foobar(...);
> #else
>   //dlsym code
> #endif
> }
> 
> Having the shimming and not shimming as two separate functions buried
> in a 100 #ifdef loses that the code is common except for the shimming.

Right, can we split the common part and move it out of #ifdef?

> 
> For example, in the current code we have find_file_offset:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/disasm.c?h=perf-tools-next&id=91b7747dc70d64b5ec56ffe493310f207e7ffc99#n1371
> 
> It is only possible to understand the use of this seemingly common
> code by trying to interpret what's going on with the #ifdefs.
> 
> I think it stylistically it is okay to have multiple stubbed out
> functions inside a #if or #else, such as:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/include/linux/perf_event.h?h=perf-tools-next&id=91b7747dc70d64b5ec56ffe493310f207e7ffc99#n1797

I think the convention in the kernel community is that it's better to
remove #ifdef's in .c files and add a dummy functions under !condition
in .h files.  If that's not possible, I think we should make the code
less conditional by minimizing the #ifdef's.

> 
> But when the logic is shared and all in one file it becomes next to
> impossible to determine what's in use and what's not. Other than by
> tweaking things and trying to get build errors.
> 
> So for the shims I've placed the #if inside the function to make it
> clear the function is a shim. For the other functions that are over
> 100s of lines, for clarity the individual functions have #if
> HAVE_LIBLLVM_SUPPORT around them to make it clear that the function
> only has a meaning in that context - ie the source code doesn't make
> you go on a #ifdef finding expedition to try to understand when the
> code is in use.

I think the both approaches have their own pros and cons.  Some people
prefer one and others may have different opinions.  I think the big
conditional block is better and easy to follow.  Maybe we cannot agree
on this.  Then I believe it'd be better to follow the convention, no?

Thanks,
Namhyung


  reply	other threads:[~2025-01-28 20:56 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-22 17:42 [PATCH v3 00/18] Support dynamic opening of capstone/llvm remove BUILD_NONDISTRO Ian Rogers
2025-01-22 17:42 ` [PATCH v3 01/18] perf build: Remove libtracefs configuration Ian Rogers
2025-01-22 17:42 ` [PATCH v3 02/18] perf map: Constify objdump offset/address conversion APIs Ian Rogers
2025-01-22 17:42 ` [PATCH v3 03/18] perf capstone: Move capstone functionality into its own file Ian Rogers
2025-01-24 21:58   ` Namhyung Kim
2025-01-25  0:59     ` Ian Rogers
2025-01-28 20:56       ` Namhyung Kim [this message]
2025-01-28 22:16         ` Ian Rogers
2025-01-22 17:42 ` [PATCH v3 04/18] perf llvm: Move llvm " Ian Rogers
2025-01-24 22:04   ` Namhyung Kim
2025-01-22 17:42 ` [PATCH v3 05/18] perf capstone: Remove open_capstone_handle Ian Rogers
2025-01-22 17:42 ` [PATCH v3 06/18] perf capstone: Support for dlopen-ing libcapstone.so Ian Rogers
2025-01-24 22:22   ` Namhyung Kim
2025-01-25  5:20     ` Ian Rogers
2025-01-28 20:58       ` Namhyung Kim
2025-01-28 22:13         ` Ian Rogers
2025-01-22 17:42 ` [PATCH v3 07/18] perf llvm: Support for dlopen-ing libLLVM.so Ian Rogers
2025-01-24 22:54   ` Namhyung Kim
2025-01-25  1:21     ` Ian Rogers
2025-01-22 17:42 ` [PATCH v3 08/18] perf llvm: Mangle libperf-llvm.so function names Ian Rogers
2025-01-22 17:42 ` [PATCH v3 09/18] perf dso: Move read_symbol from llvm/capstone to dso Ian Rogers
2025-01-22 17:43 ` [PATCH v3 10/18] perf dso: Support BPF programs in dso__read_symbol Ian Rogers
2025-03-25 17:44   ` Ian Rogers
2025-01-22 17:43 ` [PATCH v3 11/18] perf llvm: Disassemble cleanup Ian Rogers
2025-01-22 17:43 ` [PATCH v3 12/18] perf dso: Clean up read_symbol error handling Ian Rogers
2025-01-22 17:43 ` [PATCH v3 13/18] perf build: Remove libbfd support Ian Rogers
2025-01-29  0:31   ` Daniel Xu
2025-01-29  1:40     ` Ian Rogers
2025-01-29 16:46       ` Daniel Xu
2025-01-29 19:36         ` Ian Rogers
2025-01-22 17:43 ` [PATCH v3 14/18] perf build: Remove libiberty support Ian Rogers
2025-01-22 17:43 ` [PATCH v3 15/18] perf build: Remove unused defines Ian Rogers
2025-01-22 17:43 ` [PATCH v3 16/18] perf disasm: Remove disasm_bpf Ian Rogers
2025-01-22 17:43 ` [PATCH v3 17/18] perf disasm: Make ins__scnprintf and ins__is_nop static Ian Rogers
2025-01-22 17:43 ` [PATCH v3 18/18] perf srcline: Fallback between addr2line implementations Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z5lEdtLVHRZwxuY8@google.com \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adityag@linux.ibm.com \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=atrajeev@linux.vnet.ibm.com \
    --cc=bpf@vger.kernel.org \
    --cc=chaitanyas.prakash@arm.com \
    --cc=changbin.du@huawei.com \
    --cc=charlie@rivosinc.com \
    --cc=dvyukov@google.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=justinstitt@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=kjain@linux.ibm.com \
    --cc=lihuafei1@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=sesse@google.com \
    --cc=song@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox