aras-p / ClangBuildAnalyzer

Clang build analysis tool using -ftime-trace
The Unlicense
969 stars 60 forks source link

Not working with Xcode #50

Closed revolter closed 1 year ago

revolter commented 3 years ago
> ~/Downloads/ClangBuildAnalyzer-mac --start clang_analyzer
Build tracing started. Do some Clang builds with '-ftime-trace', then run 'ClangBuildAnalyzer --stop clang_analyzer <filename>' to stop tracing and save session to a file.

> ~/Downloads/ClangBuildAnalyzer-mac --stop clang_analyzer clang_analyzer_report
Stopping build tracing and saving to 'clang_analyzer_report'...
ERROR: no .json files found under 'clang_analyzer'.

macOS Catalina 10.15.5 (19F101)

image

ClangBuildAnalyzerTest.zip

aras-p commented 3 years ago

Well, are the .json files under clang_analyzer folder? I don't know where your Xcode invocation places them.

revolter commented 3 years ago

I attached the entire project. Where does your Xcode invocation place them?

revolter commented 3 years ago

I changed File > Project Settings... > Per-User Project Settings > Derived Data to Project-relative Location:

image

But it still doesn't work:

> ls -al DerivedData/ClangBuildAnalyzerTest/Build/Intermediates.noindex/ClangBuildAnalyzerTest.build/Debug-iphoneos/ClangBuildAnalyzerTest.build/Objects-normal/arm64
Permissions Size User   Group Date Modified    Name
.rw-r--r--   497 revolt staff 2020-10-04 19:04 AppDelegate.d
.rw-r--r--   220 revolt staff 2020-10-04 19:04 AppDelegate.dia
.rw-r--r--  2.7k revolt staff 2020-10-04 19:04 AppDelegate.json
.rw-r--r--   26k revolt staff 2020-10-04 19:04 AppDelegate.o
.rw-r--r--    11 revolt staff 2020-10-04 19:06 ClangBuildAnalyzerSession.txt
.rw-r--r--   870 revolt staff 2020-10-04 19:04 ClangBuildAnalyzerTest.LinkFileList
.rw-r--r--   47k revolt staff 2020-10-04 19:04 ClangBuildAnalyzerTest_dependency_info.dat
.rw-r--r--   490 revolt staff 2020-10-04 19:04 main.d
.rw-r--r--   220 revolt staff 2020-10-04 19:04 main.dia
.rw-r--r--  2.6k revolt staff 2020-10-04 19:04 main.json
.rw-r--r--  4.7k revolt staff 2020-10-04 19:04 main.o
.rw-r--r--   501 revolt staff 2020-10-04 19:04 SceneDelegate.d
.rw-r--r--   220 revolt staff 2020-10-04 19:04 SceneDelegate.dia
.rw-r--r--  2.7k revolt staff 2020-10-04 19:04 SceneDelegate.json
.rw-r--r--   19k revolt staff 2020-10-04 19:04 SceneDelegate.o
.rw-r--r--   503 revolt staff 2020-10-04 19:04 ViewController.d
.rw-r--r--   220 revolt staff 2020-10-04 19:04 ViewController.dia
.rw-r--r--  1.2k revolt staff 2020-10-04 19:04 ViewController.json
.rw-r--r--  5.8k revolt staff 2020-10-04 19:04 ViewController.o
> ~/Downloads/ClangBuildAnalyzer-mac --stop DerivedData/ClangBuildAnalyzerTest/Build/Intermediates.noindex/ClangBuildAnalyzerTest.build/Debug-iphoneos/ClangBuildAnalyzerTest.build/Objects-normal/arm64 clang_analyzer_report
Stopping build tracing and saving to 'clang_analyzer_report'...
ERROR: no .json files found under 'DerivedData/ClangBuildAnalyzerTest/Build/Intermediates.noindex/ClangBuildAnalyzerTest.build/Debug-iphoneos/ClangBuildAnalyzerTest.build/Objects-normal/arm64'.
aras-p commented 3 years ago

@revolter can you send one of the these *.json files? I don't have an Xcode version that supports -ftime-trace here, and installing one would require me to update my macOS version, which I don't quite want to do just yet :)

revolter commented 3 years ago

Sure, here it is:

{"traceEvents":[{"pid":1,"tid":0,"ph":"X","ts":3284,"dur":2722480,"name":"Module Load","args":{"detail":"UIKit"}},{"pid":1,"tid":0,"ph":"X","ts":2576,"dur":2724509,"name":"Source","args":{"detail":"/Users/revolt/Downloads/ClangBuildAnalyzerTest/ClangBuildAnalyzerTest/AppDelegate.h"}},{"pid":1,"tid":0,"ph":"X","ts":2159,"dur":2733988,"name":"Frontend"},{"pid":1,"tid":0,"ph":"X","ts":2737305,"dur":501,"name":"PerModulePasses"},{"pid":1,"tid":0,"ph":"X","ts":2738004,"dur":674,"name":"OptFunction","args":{"detail":"\u0001-[AppDelegate application:didFinishLaunchingWithOptions:]"}},{"pid":1,"tid":0,"ph":"X","ts":2738679,"dur":502,"name":"OptFunction","args":{"detail":"\u0001-[AppDelegate application:configurationForConnectingSceneSession:options:]"}},{"pid":1,"tid":0,"ph":"X","ts":2737811,"dur":47510,"name":"OptModule","args":{"detail":"/Users/revolt/Downloads/ClangBuildAnalyzerTest/ClangBuildAnalyzerTest/AppDelegate.m"}},{"pid":1,"tid":0,"ph":"X","ts":2737807,"dur":47635,"name":"CodeGenPasses"},{"pid":1,"tid":0,"ph":"X","ts":2736549,"dur":50501,"name":"Backend"},{"pid":1,"tid":0,"ph":"X","ts":127,"dur":2788096,"name":"ExecuteCompiler"},{"pid":1,"tid":1,"ph":"X","ts":0,"dur":2788095,"name":"Total ExecuteCompiler","args":{"count":1,"avg ms":2788}},{"pid":1,"tid":2,"ph":"X","ts":0,"dur":2734334,"name":"Total Frontend","args":{"count":2,"avg ms":1367}},{"pid":1,"tid":3,"ph":"X","ts":0,"dur":2724508,"name":"Total Source","args":{"count":1,"avg ms":2724}},{"pid":1,"tid":4,"ph":"X","ts":0,"dur":2722479,"name":"Total Module Load","args":{"count":1,"avg ms":2722}},{"pid":1,"tid":5,"ph":"X","ts":0,"dur":50500,"name":"Total Backend","args":{"count":1,"avg ms":50}},{"pid":1,"tid":6,"ph":"X","ts":0,"dur":48006,"name":"Total OptModule","args":{"count":2,"avg ms":24}},{"pid":1,"tid":7,"ph":"X","ts":0,"dur":47635,"name":"Total CodeGenPasses","args":{"count":1,"avg ms":47}},{"pid":1,"tid":8,"ph":"X","ts":0,"dur":1872,"name":"Total OptFunction","args":{"count":12,"avg ms":0}},{"pid":1,"tid":9,"ph":"X","ts":0,"dur":1826,"name":"Total RunPass","args":{"count":195,"avg ms":0}},{"pid":1,"tid":10,"ph":"X","ts":0,"dur":501,"name":"Total PerModulePasses","args":{"count":1,"avg ms":0}},{"pid":1,"tid":11,"ph":"X","ts":0,"dur":210,"name":"Total DebugType","args":{"count":30,"avg ms":0}},{"pid":1,"tid":12,"ph":"X","ts":0,"dur":96,"name":"Total Module LoadIndex","args":{"count":1,"avg ms":0}},{"pid":1,"tid":13,"ph":"X","ts":0,"dur":12,"name":"Total PerFunctionPasses","args":{"count":1,"avg ms":0}},{"pid":1,"tid":14,"ph":"X","ts":0,"dur":1,"name":"Total PerformPendingInstantiations","args":{"count":1,"avg ms":0}},{"cat":"","pid":1,"tid":0,"ts":0,"ph":"M","name":"process_name","args":{"name":"clang"}}]}
aras-p commented 3 years ago

Hmm that JSON file is fine. I'm wondering if there's something entirely else going on, like for some reason this executable can't read the files or whatever. No ideas right now, sorry! Once/if I update my Mac to support this Xcode version then I can debug it myself, meanwhile "try debugging" is the best advice I can give :/

revolter commented 3 years ago

I remember ~/Downloads requiring special permissions in the latest macOS version, so I moved both the executable and my test project to ~/Development, and even tried running it with sudo to no avail 🤔

i-ky commented 3 years ago

@revolter, your ClangBuildAnalyzerSession.txt seems to be newer than your .json files. Make sure you do the compilation between --start and --stop commands. Or use --all command if it's available to you.

revolter commented 3 years ago

your ClangBuildAnalyzerSession.txt seems to be newer than your .json files

Indeed 🤔 Firstly, it should have said this instead.

Secondly, there's a chicken and egg problem. The DerivedData is not created until you build the project for the first time. After this, you can start the analyzer. But for the next build, it doesn't generate the JSON files again, as it does only an incremental build of what changed. If you clean the project instead, it also deletes the analyzer session file, and you're back at the beginning. Can't I store the session in a directory, and use the JSON files from another directory?

z-aki commented 2 years ago

There are multiple ways out:

  1. Create a new schema that runs --start as the first thing in Xcode build process.
  2. modify the build output via OBJROOT and/or SYMROOT to be out of ~/Library so that build products can be in a child directory of CBA session file.
  3. clean all json files from derived data, build the project, use a post build rule to run -all.
  4. Use xcodebuild with -derivedDataPath and same as 2
hukz37 commented 2 years ago

Friends, is there a specific solution for this? I also encountered the same problem. There is no * JSON file, the build directory of Xcode does have JSON file, thank you

QQ20220517-205300@2x
hukz37 commented 2 years ago

your ClangBuildAnalyzerSession.txt seems to be newer than your .json files

Indeed 🤔 Firstly, it should have said this instead.

Secondly, there's a chicken and egg problem. The DerivedData is not created until you build the project for the first time. After this, you can start the analyzer. But for the next build, it doesn't generate the JSON files again, as it does only an incremental build of what changed. If you clean the project instead, it also deletes the analyzer session file, and you're back at the beginning. Can't I store the session in a directory, and use the JSON files from another directory?

Hello, have you solved it? An IOS project. I want to use this project to analyze the build time

alecazam commented 1 year ago

CBA works fine. You just need to add -fime-trace to the C flags settings (will get set on C++ flags too), and then point it to the folders in DevivedData where Xcode puts the data. These folders can change after a clean though, and have randomized characters in them. The Report Naviagator shows the command line.

CompileC /Users/Foo/Library/Developer/Xcode/DerivedData/foo-erbkczkopelnfhennypqjfnicqai/Build/Intermediates.noindex/foo.build/Debug/foo.build/Objects-normal/arm64/bar.o

In that folder alongside the .o file, there will be a json with the timing results from clang /Users/Foo/Library/Developer/Xcode/DerivedData/foo-erbkczkopelnfhennypqjfnicqai/Build/Intermediates.noindex/foo.build/Debug/foo.build/Objects-normal/arm64/bar.json

You can open any of the json files in that folder with perfetto.dev to see what includes are pulled in. For example, I found that any inclusion of std STL string causes 5 instantiations of basic_string to be injected into each file.

Keep in mind that Xcode workspaces reroute all output to a different folder than the project does. I just wrote a script to process all this for my app. You can reference that if it helps.

https://github.com/alecazam/kram/blob/main/scripts/cba.sh