Re: [PATCH 1/4] perf tools: Make more global variables static

From: Arnaldo Carvalho de Melo

Date: Tue Apr 07 2026 - 08:51:34 EST


On Wed, Apr 01, 2026 at 09:17:37PM -0300, Arnaldo Carvalho de Melo wrote:
> From: Ian Rogers <irogers@xxxxxxxxxx>
>
> `make check` will run sparse on the perf code base. A frequent warning
> is "warning: symbol '...' was not declared. Should it be static?" Go
> through and make global definitions without declarations static.
>
> In some cases it is deliberate due to dlsym accessing the symbol, this
> change doesn't clean up the missing declarations for perf test suites.
>
> Sometimes things can opportunistically be made const.
>
> Making somethings static exposed unused functions warnings, so
> restructuring of ifdefs was necessary for that.
>
> These changes reduce the size of the perf binary by 568 bytes.
>
> Committer notes:
>
> Refreshed the patch, the original one fell thru the cracks, updated the
> size reduction.
>
> Remove the trace-event-scripting.c changes, break the build, noticed
> with container builds and with sashiko:
>
> https://sashiko.dev/#/patchset/20260401215306.2152898-1-acme%40kernel.org

Addressing a Sashiko review comment:

---
Does moving this array into the function without the static keyword force
unnecessary stack allocation and runtime initialization?
Since top_callchain_help[] is no longer static, the compiler might allocate
space on the stack and copy the contents from .rodata every time the function
is called. Using a pointer (const char *top_callchain_help) or keeping it
static const would prevent this overhead.
---

⬢ [acme@toolbx perf-tools-next]$ cat before
text data bss dec hex filename
12249047 362081 40040 12651168 c10aa0 /home/acme/bin/perf
⬢ [acme@toolbx perf-tools-next]$ size ~/bin/perf
text data bss dec hex filename
12248983 362081 40040 12651104 c10a60 /home/acme/bin/perf
⬢ [acme@toolbx perf-tools-next]$ git diff
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 35ff2495e1ee5897..f6eb543de537a3d3 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1451,7 +1451,7 @@ parse_percent_limit(const struct option *opt, const char *arg,

int cmd_top(int argc, const char **argv)
{
- const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
+ static const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
"\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
char errbuf[BUFSIZ];
struct perf_top top = {
⬢ [acme@toolbx perf-tools-next]$

The second suggestion didn't make a difference, but lets keep it static
as well to ensure that is the case.

---
Is this also creating a stack-allocated array that requires initialization on
every call? Making distro_dwarf_types[] static const would ensure it stays
entirely in .rodata with zero runtime overhead.
---

⬢ [acme@toolbx perf-tools-next]$ cat before
text data bss dec hex filename
12248983 362081 40040 12651104 c10a60 /home/acme/bin/perf
⬢ [acme@toolbx perf-tools-next]$ size ~/bin/perf
text data bss dec hex filename
12248983 362081 40040 12651104 c10a60 /home/acme/bin/perf
⬢ [acme@toolbx perf-tools-next]$ git diff
diff --git a/tools/perf/util/debuginfo.c b/tools/perf/util/debuginfo.c
index 8b819dea36ac1ef0..0e35c13abd041c46 100644
--- a/tools/perf/util/debuginfo.c
+++ b/tools/perf/util/debuginfo.c
@@ -90,7 +90,7 @@ static struct debuginfo *__debuginfo__new(const char *path)

struct debuginfo *debuginfo__new(const char *path)
{
- const enum dso_binary_type distro_dwarf_types[] = {
+ static const enum dso_binary_type distro_dwarf_types[] = {
DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
⬢ [acme@toolbx perf-tools-next]$

I'm adding these two changes and will resubmit, holler if you disagree.
:-)

- Arnaldo