Cannot get CPU profile via xctrace

Hi, I'm a new owner of Apple laptop since I always using PC. I'm an experienced perf user but now I'm happy having my MBP2021 laptop so I need to learn new tools. I found that xctrace is the new way to capture profiles on Apple devices so I've installed one and ran on my app. Unfortunately, I didn't manage to make it work. Here is my script:

❯  xctrace record --template 'CPU Counters' --launch -- ./target/release/deps/flower-b0a05df673ad26db tests::test_run
Starting recording with the CPU Counters template. Launching process: flower-b0a05df673ad26db. 
Ctrl-C to stop the recording
Target app exited, ending recording...
Recording completed. Saving output file...
Output file saved as: Launch_flower-b0a05df673ad26db_2022-05-09_21.02.47_4E6DC3F7.trace.


❯ xctrace import --template 'CPU Counters' --input Launch_flower-b0a05df673ad26db_2022-05-09_21.02.47_4E6DC3F7.trace 
Import failed with error: Cannot find matching importer for url: Launch_flower-b0a05df673ad26db_2022-05-09_21.02.47_4E6DC3F7.trace/ -- file:///Users/pzixel/Documents/Repos/flower/

I tried to find any information about to how to make it work but failed. I hope I can find some support here.

Replies

Hello there,

'CPU Counters' needs to be preconfigured before use with xctrace. It doesn't record any events by default. To change configuration:

  1. Open Instruments.app by right-clicking on Xcode icon -> Open Developer Tool -> Instruments or find it with Spotlight.
  2. Open 'CPU Counters' template.
  3. Open File -> Recording options....
  4. Select events to capture, e.g. Instructions, Cycles, etc.
  5. Save the template File -> Save as Template..., e.g. with name Preconfigured CPU Counters

New saved template can be used with xctrace now: xctrace record --template 'Preconfigured CPU Counters' --launch -- ./target/release/deps/flower-b0a05df673ad26db tests::test_run

Output is a .trace file. It can be opened with Instruments.app or used with xctrace export command.

Following command shows table of content for trace: xctrace export --input input.trace --toc.

To get content itself: xctrace export --input input.trace --xpath '/trace-toc/run[@number="1"]/data/table[@schema="my-table-schema"]'

Import subcommand transforms supported file formats (.logarchive, .ktrace, .sample, etc) to .trace format which can be opened in Instruments.app or used with xctrace export.

Thank you for an answer. i did what you told but for some reason tracing process just hangs out.

❯ xctrace record --template 'Preconfigured CPU Counters' --launch -- ./target/release/deps/flower-8140bd4c6b5d1486
Starting recording with the Preconfigured CPU Counters template. Launching process: flower-8140bd4c6b5d1486. 
... nothing happened here for more than 20 minutes since I ran it

OTOH running it normally finishes almost immediately:

❯ time ./target/release/deps/flower-8140bd4c6b5d1486
./target/release/deps/flower-8140bd4c6b5d1486  0.02s user 0.01s system 130% cpu 0.023 total

Can I do anything to make trace end with results here?

Hello @Pzixel,

Thank you for reporting the issue.

Please file a feedback with steps to reproduce, samples output taken during xctrace hangs (sample xctrace) and sysdiagnose by using the feedback app or by going to https://feedbackassistant.apple.com. This is necessary to figure out why xctrace hangs and find a solution.

Hi @Developer Tools Engineer

Mac M2 reproduced the same issue: xctrace CPU Counters Sample By Events hangs.

xctrace record --template 'MyTemplate' --launch -- /Users/zhaixiang/jdk-21.jdk/Contents/Home/bin/java -version

Starting recording with the MyTemplate template. Launching process: java. ... nothing happened here for more than 20 minutes since I ran it.

MyTemplate Sample By Events including Cycles (FIXED_CYCLES), INST_ALL and L1I_CACHE_MISS_DEMAND.

I have sent feedback: https://feedbackassistant.apple.com/feedback/12912584

Thanks, Leslie Zhai