summary refs log tree commit diff
path: root/pkgs/development/tools/profiling/pyflame/default.nix
blob: 2467769ad3de7553ad5e261e062043945a9d7d4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
{ stdenv, autoreconfHook, coreutils, fetchFromGitHub, fetchpatch, pkgconfig, procps
# pyflame needs one python version per ABI
# are currently supported
# * 2.6 or 2.7 for 2.x ABI
# * 3.4 or 3.5 for 3.{4,5} ABI
# * 3.6        for 3.6 ABI
# * 3.7        for 3.7+ ABI
# to disable support for an ABI, make the corresponding argument null
, python2, python35, python36, python37, python3
}:
stdenv.mkDerivation rec {
  pname = "pyflame";
  version = "1.6.7";
  src = fetchFromGitHub {
    owner = "uber";
    repo = "pyflame";
    rev = "v${version}";
    sha256 = "0hz1ryimh0w8zyxx4y8chcn54d6b02spflj5k9rcg26an2chkg2w";
  };

  # Uber's abandoned this since Jun 2018, so we have to patch a lot.
  # Yay.
  patches = let
    # "Add support for Python3.7 (#151)":
    py37-support = [ # https://github.com/uber/pyflame/pull/153
      (fetchpatch { # "Add support for python3.7"
        url = "https://github.com/uber/pyflame/commit/5ee674c4b09a29b82a0e2d7a4ce064fea3df1f4c.patch";
        sha256 = "19v0yl8frbsq1dkvcmr1zsxf9v75bs8hvlkiv2x8cwylndvz2g5n";
      })
      (fetchpatch { # "Add python3.7 to travis test matrix"
        url = "https://github.com/uber/pyflame/commit/610b5281502ff6d57471e84071f17a33d30f3bcf.patch";
        sha256 = "13kwzrz0zwmdiirg061wvz7zvdl2w9dnrc81xbkxpm1hh8h0mi9z";
      })
      (fetchpatch { # "Update ppa and Ubuntu version"
        url = "https://github.com/uber/pyflame/commit/ec82a43c90da64815a87d4e3fe2a12ec3c93dc38.patch";
        sha256 = "1rrcsj5095ns5iyk6ij9kylv8hsrflxjld7b4s5dbpk8jqkf3ndi";
      })
      (fetchpatch { # "Clang-Format"
        url = "https://github.com/uber/pyflame/commit/fb81e40398d6209c38d49d0b6758d9581b3c2bba.patch";
        sha256 = "024namalrsai8ppl87lqsalfgd2fbqsnbkhpg8q93bvsdxldwc6r";
      })
    ];

    # "Fix pyflame for code compiled with ld -z separate-code":
    separate-code-support = [ # https://github.com/uber/pyflame/pull/170
      (fetchpatch { # "Fix for code compiled with ld -z separate-code"
        url = "https://github.com/uber/pyflame/commit/739a77d9b9abf9599f633d49c9ec98a201bfe058.patch";
        sha256 = "03xhdysr5s73bw3a7nj2h45dylj9a4c1f1i3xqm1nngpd6arq4y6";
      })
    ];

    # "Improve PtraceSeize error output"
    full-ptrace-seize-errors = [ # https://github.com/uber/pyflame/pull/152
      (fetchpatch { # "Print whole error output from PtraceSeize"
        url = "https://github.com/uber/pyflame/commit/4b0e2c1b442b0f0c6ac5f56471359cea9886aa0f.patch";
        sha256 = "0nkqs5zszf78cna0bavcdg18g7rdmn72li3091ygpkgxn77cnvis";
      })
      (fetchpatch { # "Print whole error for PtraceSeize"
        url = "https://github.com/uber/pyflame/commit/1abb23abe4912c4a27553f0b3b5c934753f41f6d.patch";
        sha256 = "07razp9rlq3s92j8a3iak3qk2h4x4xwz4y915h52ivvnxayscj89";
      })
    ];
  in stdenv.lib.concatLists [
    py37-support
    # Without this, tests will leak memory and run forever.
    separate-code-support
    full-ptrace-seize-errors
  ];

  nativeBuildInputs = [ autoreconfHook pkgconfig procps ];
  buildInputs = [ python37 python36 python2 python35 ];

  postPatch = ''
    patchShebangs .

    # some tests will fail in the sandbox
    substituteInPlace tests/test_end_to_end.py \
      --replace 'skipif(IS_DOCKER' 'skipif(True'

    # don't use patchShebangs here to be explicit about the python version
    substituteInPlace utils/flame-chart-json \
      --replace '#!usr/bin/env python' '#!${python3.interpreter}'

    # Many tests require the build machine to have kernel.yama.ptrace_scope = 0,
    # but hardened machines have it set to 1. On build machines that cannot run
    # these tests, skip them to avoid breaking the build.
    if [[ $(sysctl -n kernel.yama.ptrace_scope || echo 0) != "0" ]]; then
      for test in \
        test_monitor \
        test_non_gil \
        test_threaded \
        test_unthreaded \
        test_legacy_pid_handling \
        test_exclude_idle \
        test_exit_early \
        test_sample_not_python \
        test_include_ts \
        test_include_ts_exclude_idle \
        test_thread_dump \
        test_no_line_numbers \
        test_utf8_output; do

        substituteInPlace tests/test_end_to_end.py \
          --replace "def $test(" "\
@pytest.mark.skip('build machine had kernel.yama.ptrace_scope != 0')
def $test("
      done
    fi
  '';

  postInstall = ''
    install -D utils/flame-chart-json $out/bin/flame-chart-json
  '';

  doCheck = true;
  # reproduces the logic of their test script, but without downloading pytest
  # from the internet with pip
  checkPhase = let inherit (stdenv) lib; in
    lib.concatMapStringsSep "\n" (python: ''
      set -x
      PYMAJORVERSION=${lib.substring 0 1 python.version} \
        PATH=${lib.makeBinPath [ coreutils ]}\
        PYTHONPATH= \
        ${python.pkgs.pytest}/bin/pytest -v tests/
      set +x
    '') (lib.filter (x: x != null) buildInputs);

  meta = with stdenv.lib; {
    description = "A ptracing profiler for Python ";
    longDescription = ''
      Pyflame is a high performance profiling tool that generates flame graphs
      for Python. Pyflame uses the Linux ptrace(2) system call to collect
      profiling information. It can take snapshots of the Python call stack
      without explicit instrumentation, meaning you can profile a program
      without modifying its source code.
    '';
    homepage = https://github.com/uber/pyflame;
    license = licenses.asl20;
    maintainers = [ maintainers.symphorien ];
    # arm: https://github.com/uber/pyflame/issues/136
    platforms = [ "i686-linux" "x86_64-linux" ];
  };
}