1{ lib
2, stdenv
3, fetchpatch
4, fetchurl
5, kernel
6, elfutils
7, python3
8, perl
9, newt
10, slang
11, asciidoc
12, xmlto
13, makeWrapper
14, docbook_xsl
15, docbook_xml_dtd_45
16, libxslt
17, flex
18, bison
19, pkg-config
20, libunwind
21, binutils-unwrapped
22, libiberty
23, audit
24, libbfd
25, libbfd_2_38
26, libopcodes
27, libopcodes_2_38
28, openssl
29, systemtap
30, numactl
31, zlib
32, babeltrace
33, withGtk ? false
34, gtk2
35, withZstd ? true
36, zstd
37, withLibcap ? true
38, libcap
39}:
40let
41 d3-flame-graph-templates = stdenv.mkDerivation rec {
42 pname = "d3-flame-graph-templates";
43 version = "4.1.3";
44
45 src = fetchurl {
46 url = "https://registry.npmjs.org/d3-flame-graph/-/d3-flame-graph-${version}.tgz";
47 sha256 = "sha256-W5/Vh5jarXUV224aIiTB2TnBFYT3naEIcG2945QjY8Q=";
48 };
49
50 installPhase = ''
51 install -D -m 0755 -t $out/share/d3-flame-graph/ ./dist/templates/*
52 '';
53 };
54in
55
56stdenv.mkDerivation {
57 pname = "perf-linux";
58 version = kernel.version;
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;
82
83 hardeningDisable = [ "format" ];
84
85 # perf refers both to newt and slang
86 nativeBuildInputs = [
87 asciidoc
88 xmlto
89 docbook_xsl
90 docbook_xml_dtd_45
91 libxslt
92 flex
93 bison
94 libiberty
95 audit
96 makeWrapper
97 pkg-config
98 python3
99 ];
100
101 buildInputs = [
102 elfutils
103 newt
104 slang
105 libunwind
106 zlib
107 openssl
108 systemtap.stapBuild
109 numactl
110 python3
111 perl
112 babeltrace
113 ] ++ (if (lib.versionAtLeast kernel.version "5.19")
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"
123 "-Wno-error=bool-compare"
124 "-Wno-error=deprecated-declarations"
125 "-Wno-error=stringop-truncation"
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; {
149 homepage = "https://perf.wiki.kernel.org/";
150 description = "Linux tools to profile with performance counters";
151 maintainers = with maintainers; [ viric ];
152 platforms = platforms.linux;
153 broken = kernel.kernelOlder "5";
154 };
155}