<s> import json <EOL> import sys <EOL> from collections import deque <EOL> from optparse import OptionParser <EOL> class LineReader : <EOL> def __init__ ( self , fp ) : <EOL> self . fp = fp <EOL> self . line = None <EOL> def peek ( self ) : <EOL> if self . line == None : <EOL> self . line = self . fp . readline ( ) <EOL> return self . line <EOL> def next ( self ) : <EOL> self . line = None <EOL> class StackFrameNode : <EOL> def __init__ ( self , stack_id , name , dso ) : <EOL> self . stack_id = stack_id <EOL> self . parent_id = <NUM_LIT:0> <EOL> self . name = name <EOL> self . dso = dso <EOL> self . children = { } <EOL> def ToDict ( self , out_dict ) : <EOL> if self . stack_id : <EOL> node_dict = { } <EOL> node_dict [ '<STR_LIT:name>' ] = self . name <EOL> node_dict [ '<STR_LIT>' ] = self . dso <EOL> if self . parent_id : <EOL> node_dict [ '<STR_LIT>' ] = self . parent_id <EOL> out_dict [ self . stack_id ] = node_dict <EOL> for child in self . children . values ( ) : <EOL> child . ToDict ( out_dict ) <EOL> return out_dict <EOL> class PerfSample : <EOL> def __init__ ( self , stack_id , ts , cpu , tid , weight , type , comm ) : <EOL> self . stack_id = stack_id <EOL> self . ts = ts <EOL> self . cpu = cpu <EOL> self . tid = tid <EOL> self . weight = weight <EOL> self . type = type <EOL> self . comm = comm <EOL> def ToDict ( self ) : <EOL> ret = { } <EOL> ret [ '<STR_LIT>' ] = self . ts * <NUM_LIT> <EOL> ret [ '<STR_LIT>' ] = self . tid <EOL> ret [ '<STR_LIT>' ] = self . cpu <EOL> ret [ '<STR_LIT>' ] = self . weight <EOL> ret [ '<STR_LIT:name>' ] = self . type <EOL> ret [ '<STR_LIT>' ] = self . comm <EOL> assert self . stack_id != <NUM_LIT:0> <EOL> if self . stack_id : <EOL> ret [ '<STR_LIT>' ] = self . stack_id <EOL> return ret <EOL> def Main ( args ) : <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = <NUM_LIT:0> , <EOL> type = "<STR_LIT:int>" , help = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> fp = open ( args [ <NUM_LIT:0> ] ) <EOL> reader = LineReader ( fp ) <EOL> samples = [ ] <EOL> root_chain = StackFrameNode ( <NUM_LIT:0> , '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> next_stack_id = <NUM_LIT:1> <EOL> tot_period = <NUM_LIT:0> <EOL> saved_period = <NUM_LIT:0> <EOL> while True : <EOL> l = reader . peek ( ) <EOL> reader . next ( ) <EOL> if not l : <EOL> break <EOL> if len ( l ) >= <NUM_LIT:1> and l [ <NUM_LIT:0> ] == '<STR_LIT:#>' : <EOL> continue <EOL> l = l . strip ( ) <EOL> toks = l . split ( '<STR_LIT:\t>' ) <EOL> samp_command = toks [ <NUM_LIT:0> ] <EOL> samp_tid = int ( toks [ <NUM_LIT:1> ] ) <EOL> samp_cpu = int ( toks [ <NUM_LIT:2> ] [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> samp_ts = float ( toks [ <NUM_LIT:3> ] [ <NUM_LIT:0> : - <NUM_LIT:1> ] ) <EOL> samp_period = int ( toks [ <NUM_LIT:5> ] ) <EOL> samp_type = toks [ <NUM_LIT:4> ] [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> tot_period += samp_period <EOL> chain = deque ( ) <EOL> while True : <EOL> l = reader . peek ( ) <EOL> reader . next ( ) <EOL> if l and l . strip ( ) != '<STR_LIT>' : <EOL> toks1 = l . strip ( ) . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> toks2 = toks1 [ <NUM_LIT:1> ] . rsplit ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> cs_name = toks2 [ <NUM_LIT:0> ] <EOL> cs_dso = toks2 [ <NUM_LIT:1> ] [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> chain . appendleft ( ( cs_name , cs_dso ) ) <EOL> else : <EOL> seen_syms = set ( ) <EOL> stack_frame = root_chain <EOL> for call in chain : <EOL> if call not in seen_syms : <EOL> seen_syms . add ( call ) <EOL> if call in stack_frame . children : <EOL> stack_frame = stack_frame . children [ call ] <EOL> else : <EOL> new_node = StackFrameNode ( next_stack_id , call [ <NUM_LIT:0> ] , call [ <NUM_LIT:1> ] ) <EOL> next_stack_id += <NUM_LIT:1> <EOL> new_node . parent_id = stack_frame . stack_id <EOL> stack_frame . children [ call ] = new_node <EOL> stack_frame = new_node <EOL> sample = PerfSample ( stack_frame . stack_id , <EOL> samp_ts , <EOL> samp_cpu , <EOL> samp_tid , <EOL> samp_period , <EOL> samp_type , <EOL> samp_command ) <EOL> samples . append ( sample ) <EOL> saved_period += samp_period <EOL> break <EOL> if options . limit_samples and len ( samples ) >= options . limit_samples : <EOL> break <EOL> trace_dict = { } <EOL> trace_dict [ '<STR_LIT>' ] = [ s . ToDict ( ) for s in samples ] <EOL> trace_dict [ '<STR_LIT>' ] = root_chain . ToDict ( { } ) <EOL> trace_dict [ '<STR_LIT>' ] = [ ] <EOL> json . dump ( trace_dict , sys . stdout , indent = <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( Main ( sys . argv [ <NUM_LIT:1> : ] ) ) </s>
