@@ -45,14 +45,22 @@ def clean(args):
4545
4646
4747def  merge (args ):
48-     if  len (args ) <  3 :
49-         print (
50-             "Usage: %s merge <llvm-profdata> <output> <paths>\n "  %  __file__ 
51-             +  "\t Merges all profraw files from path into output." 
52-         )
53-         return  1 
54-     cmd  =  [args [0 ], "merge" , "-o" , args [1 ]]
55-     for  path  in  args [2 :]:
48+     parser  =  argparse .ArgumentParser (
49+         prog = "perf-helper merge" ,
50+         description = "Merges all profraw files from path(s) into output" ,
51+     )
52+     parser .add_argument ("profdata" , help = "Path to llvm-profdata tool" )
53+     parser .add_argument ("output" , help = "Output filename" )
54+     parser .add_argument (
55+         "paths" , nargs = "+" , help = "Folder(s) containing input profraw files" 
56+     )
57+     parser .add_argument ("--sample" , action = "store_true" , help = "Sample profile" )
58+     opts  =  parser .parse_args (args )
59+ 
60+     cmd  =  [opts .profdata , "merge" , "-o" , opts .output ]
61+     if  opts .sample :
62+         cmd  +=  ["--sample" ]
63+     for  path  in  opts .paths :
5664        cmd .extend (findFilesWithExtension (path , "profraw" ))
5765    subprocess .check_call (cmd )
5866    return  0 
@@ -73,25 +81,30 @@ def merge_fdata(args):
7381
7482def  perf (args ):
7583    parser  =  argparse .ArgumentParser (
76-         prog = "perf-helper perf" , description = "perf wrapper for BOLT profile collection" 
84+         prog = "perf-helper perf" ,
85+         description = "perf wrapper for BOLT/CSSPGO profile collection" ,
7786    )
7887    parser .add_argument (
7988        "--lbr" , action = "store_true" , help = "Use perf with branch stacks" 
8089    )
90+     parser .add_argument ("--csspgo" , action = "store_true" , help = "Enable CSSPGO flags" )
8191    parser .add_argument ("cmd" , nargs = argparse .REMAINDER , help = "" )
8292
8393    opts  =  parser .parse_args (args )
8494    cmd  =  opts .cmd [1 :]
8595
96+     event  =  "br_inst_retired.near_taken:uppp"  if  opts .csspgo  else  "cycles:u" 
8697    perf_args  =  [
8798        "perf" ,
8899        "record" ,
89-         "--event=cycles:u " ,
100+         f "--event={ event }  " ,
90101        "--freq=max" ,
91102        "--output=%d.perf.data"  %  os .getpid (),
92103    ]
93-     if  opts .lbr :
104+     if  opts .lbr   or   opts . csspgo :
94105        perf_args  +=  ["--branch-filter=any,u" ]
106+     if  opts .csspgo :
107+         perf_args  +=  ["-g" , "--call-graph=fp" ]
95108    perf_args .extend (cmd )
96109
97110    start_time  =  time .time ()
@@ -127,6 +140,26 @@ def perf2bolt(args):
127140    return  0 
128141
129142
143+ def  perf2prof (args ):
144+     parser  =  argparse .ArgumentParser (
145+         prog = "perf-helper perf2prof" ,
146+         description = "perf to CSSPGO prof conversion wrapper" ,
147+     )
148+     parser .add_argument ("profgen" , help = "Path to llvm-profgen binary" )
149+     parser .add_argument ("binary" , help = "Input binary" )
150+     parser .add_argument ("paths" , nargs = "+" , help = "Path containing perf.data files" )
151+     opts  =  parser .parse_args (args )
152+ 
153+     profgen_args  =  [opts .profgen , f"--binary={ opts .binary }  " ]
154+     for  path  in  opts .paths :
155+         for  filename  in  findFilesWithExtension (path , "perf.data" ):
156+             subprocess .check_call (
157+                 profgen_args 
158+                 +  [f"--perfdata={ filename }  " , f"--output={ filename }  .profraw" ]
159+             )
160+     return  0 
161+ 
162+ 
130163def  dtrace (args ):
131164    parser  =  argparse .ArgumentParser (
132165        prog = "perf-helper dtrace" ,
@@ -707,6 +740,7 @@ def bolt_optimize(args):
707740    "merge-fdata" : merge_fdata ,
708741    "perf" : perf ,
709742    "perf2bolt" : perf2bolt ,
743+     "perf2prof" : perf2prof ,
710744}
711745
712746
0 commit comments