Merge pull request #201311 from aaronjheng/perf-cleanup

perf-linux: clean up code

authored by Sergei Trofimovich and committed by GitHub ee19d044 b394f86d

+15 -380
-352
pkgs/os-specific/linux/kernel/perf/5.19-binutils-2.39-support.patch
··· 1 - Fetched as: 2 - $ wget 'https://github.com/torvalds/linux/compare/00b32625982e0c796f0abb8effcac9c05ef55bd3...600b7b26c07a070d0153daa76b3806c1e52c9e00.patch' 3 - 4 - Adds support for binutils-2.39 API change around init_disassemble_info(). 5 - --- a/tools/build/Makefile.feature 6 - +++ b/tools/build/Makefile.feature 7 - @@ -70,6 +70,7 @@ FEATURE_TESTS_BASIC := \ 8 - libaio \ 9 - libzstd \ 10 - disassembler-four-args \ 11 - + disassembler-init-styled \ 12 - file-handle 13 - 14 - # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list 15 - --- a/tools/build/feature/Makefile 16 - +++ b/tools/build/feature/Makefile 17 - @@ -18,6 +18,7 @@ FILES= \ 18 - test-libbfd.bin \ 19 - test-libbfd-buildid.bin \ 20 - test-disassembler-four-args.bin \ 21 - + test-disassembler-init-styled.bin \ 22 - test-reallocarray.bin \ 23 - test-libbfd-liberty.bin \ 24 - test-libbfd-liberty-z.bin \ 25 - @@ -248,6 +249,9 @@ $(OUTPUT)test-libbfd-buildid.bin: 26 - $(OUTPUT)test-disassembler-four-args.bin: 27 - $(BUILD) -DPACKAGE='"perf"' -lbfd -lopcodes 28 - 29 - +$(OUTPUT)test-disassembler-init-styled.bin: 30 - + $(BUILD) -DPACKAGE='"perf"' -lbfd -lopcodes 31 - + 32 - $(OUTPUT)test-reallocarray.bin: 33 - $(BUILD) 34 - 35 - --- a/tools/build/feature/test-all.c 36 - +++ b/tools/build/feature/test-all.c 37 - @@ -166,6 +166,10 @@ 38 - # include "test-disassembler-four-args.c" 39 - #undef main 40 - 41 - +#define main main_test_disassembler_init_styled 42 - +# include "test-disassembler-init-styled.c" 43 - +#undef main 44 - + 45 - #define main main_test_libzstd 46 - # include "test-libzstd.c" 47 - #undef main 48 - --- /dev/null 49 - +++ b/tools/build/feature/test-disassembler-init-styled.c 50 - @@ -0,0 +1,13 @@ 51 - +// SPDX-License-Identifier: GPL-2.0 52 - +#include <stdio.h> 53 - +#include <dis-asm.h> 54 - + 55 - +int main(void) 56 - +{ 57 - + struct disassemble_info info; 58 - + 59 - + init_disassemble_info(&info, stdout, 60 - + NULL, NULL); 61 - + 62 - + return 0; 63 - +} 64 - 65 - --- a/tools/build/Makefile.feature 66 - +++ b/tools/build/Makefile.feature 67 - @@ -135,8 +135,7 @@ FEATURE_DISPLAY ?= \ 68 - get_cpuid \ 69 - bpf \ 70 - libaio \ 71 - - libzstd \ 72 - - disassembler-four-args 73 - + libzstd 74 - 75 - # Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features. 76 - # If in the future we need per-feature checks/flags for features not 77 - 78 - --- /dev/null 79 - +++ b/tools/include/tools/dis-asm-compat.h 80 - @@ -0,0 +1,55 @@ 81 - +/* SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause */ 82 - +#ifndef _TOOLS_DIS_ASM_COMPAT_H 83 - +#define _TOOLS_DIS_ASM_COMPAT_H 84 - + 85 - +#include <stdio.h> 86 - +#include <dis-asm.h> 87 - + 88 - +/* define types for older binutils version, to centralize ifdef'ery a bit */ 89 - +#ifndef DISASM_INIT_STYLED 90 - +enum disassembler_style {DISASSEMBLER_STYLE_NOT_EMPTY}; 91 - +typedef int (*fprintf_styled_ftype) (void *, enum disassembler_style, const char*, ...); 92 - +#endif 93 - + 94 - +/* 95 - + * Trivial fprintf wrapper to be used as the fprintf_styled_func argument to 96 - + * init_disassemble_info_compat() when normal fprintf suffices. 97 - + */ 98 - +static inline int fprintf_styled(void *out, 99 - + enum disassembler_style style, 100 - + const char *fmt, ...) 101 - +{ 102 - + va_list args; 103 - + int r; 104 - + 105 - + (void)style; 106 - + 107 - + va_start(args, fmt); 108 - + r = vfprintf(out, fmt, args); 109 - + va_end(args); 110 - + 111 - + return r; 112 - +} 113 - + 114 - +/* 115 - + * Wrapper for init_disassemble_info() that hides version 116 - + * differences. Depending on binutils version and architecture either 117 - + * fprintf_func or fprintf_styled_func will be called. 118 - + */ 119 - +static inline void init_disassemble_info_compat(struct disassemble_info *info, 120 - + void *stream, 121 - + fprintf_ftype unstyled_func, 122 - + fprintf_styled_ftype styled_func) 123 - +{ 124 - +#ifdef DISASM_INIT_STYLED 125 - + init_disassemble_info(info, stream, 126 - + unstyled_func, 127 - + styled_func); 128 - +#else 129 - + (void)styled_func; 130 - + init_disassemble_info(info, stream, 131 - + unstyled_func); 132 - +#endif 133 - +} 134 - + 135 - +#endif /* _TOOLS_DIS_ASM_COMPAT_H */ 136 - 137 - --- a/tools/perf/Makefile.config 138 - +++ b/tools/perf/Makefile.config 139 - @@ -298,6 +298,7 @@ FEATURE_CHECK_LDFLAGS-libpython := $(PYTHON_EMBED_LDOPTS) 140 - FEATURE_CHECK_LDFLAGS-libaio = -lrt 141 - 142 - FEATURE_CHECK_LDFLAGS-disassembler-four-args = -lbfd -lopcodes -ldl 143 - +FEATURE_CHECK_LDFLAGS-disassembler-init-styled = -lbfd -lopcodes -ldl 144 - 145 - CORE_CFLAGS += -fno-omit-frame-pointer 146 - CORE_CFLAGS += -ggdb3 147 - @@ -924,13 +925,16 @@ ifndef NO_LIBBFD 148 - ifeq ($(feature-libbfd-liberty), 1) 149 - EXTLIBS += -lbfd -lopcodes -liberty 150 - FEATURE_CHECK_LDFLAGS-disassembler-four-args += -liberty -ldl 151 - + FEATURE_CHECK_LDFLAGS-disassembler-init-styled += -liberty -ldl 152 - else 153 - ifeq ($(feature-libbfd-liberty-z), 1) 154 - EXTLIBS += -lbfd -lopcodes -liberty -lz 155 - FEATURE_CHECK_LDFLAGS-disassembler-four-args += -liberty -lz -ldl 156 - + FEATURE_CHECK_LDFLAGS-disassembler-init-styled += -liberty -lz -ldl 157 - endif 158 - endif 159 - $(call feature_check,disassembler-four-args) 160 - + $(call feature_check,disassembler-init-styled) 161 - endif 162 - 163 - ifeq ($(feature-libbfd-buildid), 1) 164 - @@ -1044,6 +1048,10 @@ ifeq ($(feature-disassembler-four-args), 1) 165 - CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE 166 - endif 167 - 168 - +ifeq ($(feature-disassembler-init-styled), 1) 169 - + CFLAGS += -DDISASM_INIT_STYLED 170 - +endif 171 - + 172 - ifeq (${IS_64_BIT}, 1) 173 - ifndef NO_PERF_READ_VDSO32 174 - $(call feature_check,compile-32) 175 - --- a/tools/perf/util/annotate.c 176 - +++ b/tools/perf/util/annotate.c 177 - @@ -1720,6 +1720,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil 178 - #include <bpf/btf.h> 179 - #include <bpf/libbpf.h> 180 - #include <linux/btf.h> 181 - +#include <tools/dis-asm-compat.h> 182 - 183 - static int symbol__disassemble_bpf(struct symbol *sym, 184 - struct annotate_args *args) 185 - @@ -1762,9 +1763,9 @@ static int symbol__disassemble_bpf(struct symbol *sym, 186 - ret = errno; 187 - goto out; 188 - } 189 - - init_disassemble_info(&info, s, 190 - - (fprintf_ftype) fprintf); 191 - - 192 - + init_disassemble_info_compat(&info, s, 193 - + (fprintf_ftype) fprintf, 194 - + fprintf_styled); 195 - info.arch = bfd_get_arch(bfdf); 196 - info.mach = bfd_get_mach(bfdf); 197 - 198 - 199 - --- a/tools/bpf/Makefile 200 - +++ b/tools/bpf/Makefile 201 - @@ -34,7 +34,7 @@ else 202 - endif 203 - 204 - FEATURE_USER = .bpf 205 - -FEATURE_TESTS = libbfd disassembler-four-args 206 - +FEATURE_TESTS = libbfd disassembler-four-args disassembler-init-styled 207 - FEATURE_DISPLAY = libbfd disassembler-four-args 208 - 209 - check_feat := 1 210 - @@ -56,6 +56,9 @@ endif 211 - ifeq ($(feature-disassembler-four-args), 1) 212 - CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE 213 - endif 214 - +ifeq ($(feature-disassembler-init-styled), 1) 215 - +CFLAGS += -DDISASM_INIT_STYLED 216 - +endif 217 - 218 - $(OUTPUT)%.yacc.c: $(srctree)/tools/bpf/%.y 219 - $(QUIET_BISON)$(YACC) -o $@ -d $< 220 - --- a/tools/bpf/bpf_jit_disasm.c 221 - +++ b/tools/bpf/bpf_jit_disasm.c 222 - @@ -28,6 +28,7 @@ 223 - #include <sys/types.h> 224 - #include <sys/stat.h> 225 - #include <limits.h> 226 - +#include <tools/dis-asm-compat.h> 227 - 228 - #define CMD_ACTION_SIZE_BUFFER 10 229 - #define CMD_ACTION_READ_ALL 3 230 - @@ -64,7 +65,9 @@ static void get_asm_insns(uint8_t *image, size_t len, int opcodes) 231 - assert(bfdf); 232 - assert(bfd_check_format(bfdf, bfd_object)); 233 - 234 - - init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf); 235 - + init_disassemble_info_compat(&info, stdout, 236 - + (fprintf_ftype) fprintf, 237 - + fprintf_styled); 238 - info.arch = bfd_get_arch(bfdf); 239 - info.mach = bfd_get_mach(bfdf); 240 - info.buffer = image; 241 - 242 - --- a/tools/bpf/Makefile 243 - +++ b/tools/bpf/Makefile 244 - @@ -35,7 +35,7 @@ endif 245 - 246 - FEATURE_USER = .bpf 247 - FEATURE_TESTS = libbfd disassembler-four-args disassembler-init-styled 248 - -FEATURE_DISPLAY = libbfd disassembler-four-args 249 - +FEATURE_DISPLAY = libbfd 250 - 251 - check_feat := 1 252 - NON_CHECK_FEAT_TARGETS := clean bpftool_clean runqslower_clean resolve_btfids_clean 253 - 254 - --- a/tools/bpf/bpftool/Makefile 255 - +++ b/tools/bpf/bpftool/Makefile 256 - @@ -93,7 +93,7 @@ INSTALL ?= install 257 - RM ?= rm -f 258 - 259 - FEATURE_USER = .bpftool 260 - -FEATURE_TESTS = libbfd disassembler-four-args zlib libcap \ 261 - +FEATURE_TESTS = libbfd disassembler-four-args disassembler-init-styled zlib libcap \ 262 - clang-bpf-co-re 263 - FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \ 264 - clang-bpf-co-re 265 - @@ -117,6 +117,9 @@ endif 266 - ifeq ($(feature-disassembler-four-args), 1) 267 - CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE 268 - endif 269 - +ifeq ($(feature-disassembler-init-styled), 1) 270 - + CFLAGS += -DDISASM_INIT_STYLED 271 - +endif 272 - 273 - LIBS = $(LIBBPF) -lelf -lz 274 - LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz 275 - --- a/tools/bpf/bpftool/jit_disasm.c 276 - +++ b/tools/bpf/bpftool/jit_disasm.c 277 - @@ -24,6 +24,7 @@ 278 - #include <sys/stat.h> 279 - #include <limits.h> 280 - #include <bpf/libbpf.h> 281 - +#include <tools/dis-asm-compat.h> 282 - 283 - #include "json_writer.h" 284 - #include "main.h" 285 - @@ -39,15 +40,12 @@ static void get_exec_path(char *tpath, size_t size) 286 - } 287 - 288 - static int oper_count; 289 - -static int fprintf_json(void *out, const char *fmt, ...) 290 - +static int printf_json(void *out, const char *fmt, va_list ap) 291 - { 292 - - va_list ap; 293 - char *s; 294 - int err; 295 - 296 - - va_start(ap, fmt); 297 - err = vasprintf(&s, fmt, ap); 298 - - va_end(ap); 299 - if (err < 0) 300 - return -1; 301 - 302 - @@ -73,6 +71,32 @@ static int fprintf_json(void *out, const char *fmt, ...) 303 - return 0; 304 - } 305 - 306 - +static int fprintf_json(void *out, const char *fmt, ...) 307 - +{ 308 - + va_list ap; 309 - + int r; 310 - + 311 - + va_start(ap, fmt); 312 - + r = printf_json(out, fmt, ap); 313 - + va_end(ap); 314 - + 315 - + return r; 316 - +} 317 - + 318 - +static int fprintf_json_styled(void *out, 319 - + enum disassembler_style style __maybe_unused, 320 - + const char *fmt, ...) 321 - +{ 322 - + va_list ap; 323 - + int r; 324 - + 325 - + va_start(ap, fmt); 326 - + r = printf_json(out, fmt, ap); 327 - + va_end(ap); 328 - + 329 - + return r; 330 - +} 331 - + 332 - void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, 333 - const char *arch, const char *disassembler_options, 334 - const struct btf *btf, 335 - @@ -99,11 +123,13 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, 336 - assert(bfd_check_format(bfdf, bfd_object)); 337 - 338 - if (json_output) 339 - - init_disassemble_info(&info, stdout, 340 - - (fprintf_ftype) fprintf_json); 341 - + init_disassemble_info_compat(&info, stdout, 342 - + (fprintf_ftype) fprintf_json, 343 - + fprintf_json_styled); 344 - else 345 - - init_disassemble_info(&info, stdout, 346 - - (fprintf_ftype) fprintf); 347 - + init_disassemble_info_compat(&info, stdout, 348 - + (fprintf_ftype) fprintf, 349 - + fprintf_styled); 350 - 351 - /* Update architecture info for offload. */ 352 - if (arch) {
···
+15 -28
pkgs/os-specific/linux/kernel/perf/default.nix
··· 4 , fetchurl 5 , kernel 6 , elfutils 7 - , python2 8 , python3 9 - , python3Packages 10 , perl 11 , newt 12 , slang ··· 61 62 inherit (kernel) src; 63 64 - patches = lib.optionals (lib.versionAtLeast kernel.version "5.19" && lib.versionOlder kernel.version "5.20") [ 65 - # binutils-2.39 support around init_disassemble_info() 66 - # API change. 67 - # Will be included in 5.20. 68 - ./5.19-binutils-2.39-support.patch 69 - ]; 70 - 71 postPatch = '' 72 - patchShebangs scripts tools/perf/pmu-events/jevents.py 73 - '' + lib.optionalString (lib.versionAtLeast kernel.version "5.8") '' 74 - substituteInPlace tools/perf/scripts/python/flamegraph.py \ 75 - --replace "/usr/share/d3-flame-graph/d3-flamegraph-base.html" \ 76 - "${d3-flame-graph-templates}/share/d3-flame-graph/d3-flamegraph-base.html" 77 - ''; 78 79 - preConfigure = '' 80 cd tools/perf 81 - 82 - substituteInPlace Makefile \ 83 - --replace /usr/include/elfutils $elfutils/include/elfutils 84 85 for x in util/build-id.c util/dso.c; do 86 substituteInPlace $x --replace /usr/lib/debug /run/current-system/sw/lib/debug 87 done 88 89 - if [ -f bash_completion ]; then 90 - sed -i 's,^have perf,_have perf,' bash_completion 91 - fi 92 ''; 93 94 makeFlags = [ "prefix=$(out)" "WERROR=0" ] ++ kernel.makeFlags; ··· 127 then [ libbfd libopcodes ] 128 else [ libbfd_2_38 libopcodes_2_38 ]) 129 ++ lib.optional withGtk gtk2 130 - ++ (if (lib.versionAtLeast kernel.version "4.19") then [ python3 ] else [ python2 ]) 131 ++ lib.optional withZstd zstd 132 ++ lib.optional withLibcap libcap 133 - ++ lib.optional (lib.versionAtLeast kernel.version "6.0") python3Packages.setuptools; 134 135 NIX_CFLAGS_COMPILE = toString [ 136 "-Wno-error=cpp" ··· 140 ]; 141 142 doCheck = false; # requires "sparse" 143 - doInstallCheck = false; # same 144 145 - separateDebugInfo = true; 146 installFlags = [ "install" "install-man" "ASCIIDOC8=1" "prefix=$(out)" ]; 147 148 - postInstall ='' 149 # Same as perf. Remove. 150 rm -f $out/bin/trace 151 ''; 152 153 preFixup = '' 154 # Pull in 'objdump' into PATH to make annotations work. 155 # The embeded Python interpreter will search PATH to calculate the Python path configuration(Should be fixed by upstream). 156 # Add python.interpreter to PATH for now. 157 wrapProgram $out/bin/perf \ 158 - --prefix PATH : ${lib.makeBinPath ([ binutils-unwrapped ] ++ (if (lib.versionAtLeast kernel.version "4.19") then [ python3 ] else [ python2 ]))} 159 ''; 160 161 meta = with lib; {
··· 4 , fetchurl 5 , kernel 6 , elfutils 7 , python3 8 , perl 9 , newt 10 , slang ··· 59 60 inherit (kernel) src; 61 62 postPatch = '' 63 + # Linux scripts 64 + patchShebangs scripts 65 66 cd tools/perf 67 68 for x in util/build-id.c util/dso.c; do 69 substituteInPlace $x --replace /usr/lib/debug /run/current-system/sw/lib/debug 70 done 71 72 + '' + lib.optionalString (lib.versionAtLeast kernel.version "5.8") '' 73 + substituteInPlace scripts/python/flamegraph.py \ 74 + --replace "/usr/share/d3-flame-graph/d3-flamegraph-base.html" \ 75 + "${d3-flame-graph-templates}/share/d3-flame-graph/d3-flamegraph-base.html" 76 + 77 + '' + lib.optionalString (lib.versionAtLeast kernel.version "6.0") '' 78 + patchShebangs pmu-events/jevents.py 79 ''; 80 81 makeFlags = [ "prefix=$(out)" "WERROR=0" ] ++ kernel.makeFlags; ··· 114 then [ libbfd libopcodes ] 115 else [ libbfd_2_38 libopcodes_2_38 ]) 116 ++ lib.optional withGtk gtk2 117 ++ lib.optional withZstd zstd 118 ++ lib.optional withLibcap libcap 119 + ++ lib.optional (lib.versionAtLeast kernel.version "6.0") python3.pkgs.setuptools; 120 121 NIX_CFLAGS_COMPILE = toString [ 122 "-Wno-error=cpp" ··· 126 ]; 127 128 doCheck = false; # requires "sparse" 129 130 installFlags = [ "install" "install-man" "ASCIIDOC8=1" "prefix=$(out)" ]; 131 132 + # TODO: Add completions based on perf-completion.sh 133 + postInstall = '' 134 # Same as perf. Remove. 135 rm -f $out/bin/trace 136 ''; 137 138 + separateDebugInfo = true; 139 + 140 preFixup = '' 141 # Pull in 'objdump' into PATH to make annotations work. 142 # The embeded Python interpreter will search PATH to calculate the Python path configuration(Should be fixed by upstream). 143 # Add python.interpreter to PATH for now. 144 wrapProgram $out/bin/perf \ 145 + --prefix PATH : ${lib.makeBinPath [ binutils-unwrapped python3 ]} 146 ''; 147 148 meta = with lib; {