11from __future__ import print_function
22import re
3+ import os
34import struct
45import subprocess
56import sys
67from six .moves import xrange
8+ import io
9+ import gzip
710
8- PY3 = sys . version_info [ 0 ] >= 3
11+ from vmprof . binary import read_word , read_string , read_words
912
13+ PY3 = sys .version_info [0 ] >= 3
1014WORD_SIZE = struct .calcsize ('L' )
1115
12- from vmprof .binary import read_word , read_string , read_words
1316
1417
1518def read_trace (fileobj , depth , version , profile_lines = False ):
@@ -30,6 +33,7 @@ def read_trace(fileobj, depth, version, profile_lines=False):
3033 trace [i ] = - trace [i ]
3134 return trace
3235
36+
3337MARKER_STACKTRACE = b'\x01 '
3438MARKER_VIRTUAL_IP = b'\x02 '
3539MARKER_TRAILER = b'\x03 '
@@ -53,12 +57,14 @@ def read_trace(fileobj, depth, version, profile_lines=False):
5357VMPROF_GC_TAG = 5
5458VMPROF_ASSEMBLER_TAG = 6
5559
60+
5661class AssemblerCode (int ):
5762 pass
5863
5964class JittedCode (int ):
6065 pass
6166
67+
6268def wrap_kind (kind , pc ):
6369 if kind == VMPROF_ASSEMBLER_TAG :
6470 return AssemblerCode (pc )
@@ -67,6 +73,15 @@ def wrap_kind(kind, pc):
6773 assert kind == VMPROF_CODE_TAG
6874 return pc
6975
76+
77+ def gunzip (fileobj ):
78+ is_gzipped = fileobj .read (2 ) == b'\037 \213 '
79+ fileobj .seek (- 2 , os .SEEK_CUR )
80+ if is_gzipped :
81+ fileobj = io .BufferedReader (gzip .GzipFile (fileobj = fileobj ))
82+ return fileobj
83+
84+
7085class BufferTooSmallError (Exception ):
7186 def get_buf (self ):
7287 return b"" .join (self .args [0 ])
@@ -174,6 +189,7 @@ def read_one_marker(fileobj, status, buffer_so_far=None):
174189 return False
175190
176191def read_prof_bit_by_bit (fileobj ):
192+ fileobj = gunzip (fileobj )
177193 # note that we don't want to use all of this on normal files, since it'll
178194 # cost us quite a bit in memory and performance and parsing 200M files in
179195 # CPython is slow (pypy does better, use pypy)
@@ -193,7 +209,9 @@ def read_prof_bit_by_bit(fileobj):
193209 buf = e .get_buf ()
194210 return status .period , status .profiles , status .virtual_ips , status .interp_name
195211
196- def read_prof (fileobj , virtual_ips_only = False ): #
212+ def read_prof (fileobj , virtual_ips_only = False ):
213+ fileobj = gunzip (fileobj )
214+
197215 assert read_word (fileobj ) == 0 # header count
198216 assert read_word (fileobj ) == 3 # header size
199217 assert read_word (fileobj ) == 0
0 commit comments