vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

cpptools takes hours of CPU time per day for a project which compiles in 5 seconds

Open MSoegtropIMC opened this issue 6 years ago • 64 comments

Type: LanguageService

Describe the bug

  • OS and Version: OSX 10.15.4
  • VS Code Version: 1.45.1
  • C/C++ Extension Version: 0.28.1

For every C++ project, even tiny ones which compile in a few seconds, a cpptools process runs for something between 6 and 40 minutes every time I open the project and also from time to time after editing or git operations. Over the day this accumulates to many hours and estimated about 25% of the time my laptop fan runs cause of cpptools (it hardly every runs cause of compiles). Also the memory consumption of cpptools is fairly high - around 7GB is common. I guess this has to do with scanning system headers, but is still seems wasteful to spend 30 minutes on symbol analysis for a project where a release build takes 5 seconds. This is not really a bug, but to me it looks like unnecessary waste of resources.

One more observation: when I delete the folder in which VSCode keeps the database (on Mac ~/Library/Application Support/VSCodium/User/workspaceStorage/) it runs the first time "only" for 6 minutes - Intellisense is fully operational. The second time it decides to run for the same projects - about 1 hour later - it runs for 29 minutes.

Steps to reproduce

  • Open any project with C files
  • Look at the CPU time of the cpptools process

Expected behavior

  • cpptools needs less than 10x the compile time

In case you cannot reproduce this (for me this happens for every C++ project every time since at least 6 months) I can of cause provide logs, a sample project and gprof statistics.

MSoegtropIMC avatar May 27 '20 09:05 MSoegtropIMC

@sean-mcmanus : since cpp-tools is closed source, would it help to setup this up as a CI test on an Azure OSX? I see that you have OSX CI active for this project, so I guess if I would create a pull request which modifies the OSX CI test so that it runs for a long time, this would help you to get this fixed?

Do you have XCode installed on the OSX azure machines (for the system headers)? I guess a cpptools binary should be available in CI, so I just need to create some sort of fake project and call cpptools with proper command line.

The TypeScript CI test just tests some of our TypeScript functionality (we have other internal tests that test cpptools). If you can modify our TypeScript tests to get a repro that sounds like it may help. I'm not sure if XCode is installed (we don't use those machines to build stuff). cpptools doesn't use the command line to communicate -- it uses JSON messages, so you should probably send messages via TypeScript.

sean-mcmanus avatar Jun 25 '20 23:06 sean-mcmanus

Yeah, log level 9 is too noisy even for us. I don't recommend going above 7. For internal development, we can promote some of the level 8 and 9 messages we care about to a lower number temporarily, but we don't release the extension that way.

I mentioned 9 because that is the level at which we log directory iteration and exclusion info (7 is insufficient).

sean-mcmanus avatar Jun 25 '20 23:06 sean-mcmanus

Ok. I figured 7 would be enough since it logs the decision around whether a file is parsed or not (e.g. the timestamp check). If a file doesn't show up there, then it was excluded.

bobbrow avatar Jun 25 '20 23:06 bobbrow

@MSoegtropIMC The issue you are describing appears to be https://github.com/microsoft/vscode-cpptools/issues/5156 , not sure if there are additional issues as well. We would have to add code to avoid unnecessary duplicate directory iteration of the system paths -- the reason for processing the system headers per workspace is that certain folders may have settings that cause different results, e.g. files.exclude, limitSymbolsToIncludedHeaders, etc.

sean-mcmanus avatar Jun 25 '20 23:06 sean-mcmanus

@nsoblath The log isn't filled with cpptools/didChangeCppProperties and "Attempting to get defaults from compiler in "compilerPath" property:", right? You mean it's filled with path/directory_ut8 logging, right?

Yeah, my guess would be your system headers paths are triggering https://github.com/microsoft/vscode-cpptools/issues/3123 -- you should be able to confirm that via setting your compilerPath to "" and see if the problem is fixed.

sean-mcmanus avatar Jun 25 '20 23:06 sean-mcmanus

@MSoegtropIMC You should be able to avoid processing in the non-C/C++ folders via setting C_Cpp.default.browse.path to [] and C_Cpp.default.compilerPath to "" in your workspace settings, and then just overriding those in the folders have have C/C++ files.

sean-mcmanus avatar Jun 26 '20 02:06 sean-mcmanus

@sean-mcmanus : I put these lines into my workspace file and didn't override this in any of the subfolders:

		"C_Cpp.loggingLevel": "7",
		"C_Cpp.default.browse.path": [],
		"C_Cpp.default.compilerPath": ""

The effect is that it doesn't scan my user folders but it still scans the system headers 16 times. I guess I need to set the Mac framework path as well to empty? Setting the compiler path to empty produces these warnings:

cpptools/didChangeCppProperties
Compiler in "compilerPath" property not found: 
Compiler in "compilerPath" property not found: 
  Folder: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/MetricKit.framework/Versions/A/Headers/ will be indexed
:

but it still happily indexed the files.

MSoegtropIMC avatar Jun 26 '20 07:06 MSoegtropIMC

@sean-mcmanus : Setting in addition

"C_Cpp.default.macFrameworkPath": []

in the workspace file doesn't help either. It still scans the system headers once for each workspace folder.

MSoegtropIMC avatar Jun 26 '20 07:06 MSoegtropIMC

One more experiment: If I set:

"C_Cpp.default.browse.path": ["/Users/<me>/Empty"],
"C_Cpp.default.compilerPath": "",
"C_Cpp.default.macFrameworkPath": ["/Users/<me>/Empty"]

where /Users/<me>/Empty is an existing but empty folder, it still scans the system folders, but as it looks only once. The runtime doesn't seem to be reduced by this, though (I am at 12 minutes now). Do I need to clear the cache and restart from scratch for each such experiment?

MSoegtropIMC avatar Jun 26 '20 07:06 MSoegtropIMC

I did one more experiment and this is simply creating a separate workspace which contains only my C files (two workspace folders). For this workspace cpptools takes a perfectly reasonable 11 seconds, including parsing all system headers two times. My other project, which contains the same number of C files but many additional files in other languages did run 27 minutes today.

So in summary I would say:

  • there should be a reliable and obvious way to tell the C++ plugin that it should ignore certain workspace folders.
  • by default the C++ plugin should only parse files which have C typical extensions. As far as I can tell it spends a lot of time looking into non C files - as it looks the 27 minutes minus 11/2*16 seconds it would take to parse the system files 16 times, so about 25 minutes. Maybe this takes even longer per file than looking into C files because it is likely harder to make sense out of such files looking at them with a C parser - possibly trying to find reparse points many times until it gives up after 1000 errors. It seems to look into all files in my workspace folders regardless of extension, because the Level 7 log ends with a few 100.000 files removed from the database lines.

MSoegtropIMC avatar Jun 26 '20 08:06 MSoegtropIMC

@MSoegtropIMC That is odd. All I had to do on Mac was to set C_Cpp.default.compilerPath to "" (in the workspace file) and it caused all the system/framework paths to not be iterated over, although I did hit an issue on Windows where I had to reload window before the setting would take effect on the non-active workspace folders. Also, the processing of the system folders was very fast, so even if it was re-done per-workspace folder it only took seconds -- is your limitSymbolsToIncludedHeaders set to the default of true? So I don't know what is taking the large CPU time for you. Also, my comment to use loggingLevel "9" or "7" was for the other user, so if you're concerned with performance, setting it back to "Debug" is recommended.

@Colengms @michelleangela Are one of you able to repro this performance issue with multiroot?

sean-mcmanus avatar Jun 26 '20 23:06 sean-mcmanus

@sean-mcmanus:

So I don't know what is taking the large CPU time for you.

as I said: I think the most time cpptool spends trying to digest nearly 500.000 files in my workspace folders it wasn't made for (mostly HTML, CSS, PNG, various ML style languages). How can I tell cpptool to ignore files in specific workspace folders? And how can I tell it to ignore files with extensions not typically used for C/C++? As far as I can tell cpptool reads all files regardless of extension - including html, pdf, png, binaries, .... Maybe this is because C++ STL headers tend to have no extension at all (e.g. /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream), but maybe one could restrict cpptool to C++ typical extensions or files with no extension (dot in the name) at all.

As I mentioned, the debug log ends with:

  466966 file(s) removed from database

More than 99% of these removed files are non C files in my workspace folders.

Or alternatively have a way to exclude certain extensions and maybe issue a warning when cpptool reads and later removes more than 100 files with a certain extension. Something like:

Parsed and removed 12,345 files with extension .py - set option XYZ to ignore files with this extension in future runs of cpptool.

MSoegtropIMC avatar Jun 27 '20 07:06 MSoegtropIMC

You can add folders to files.exclude -- that should prevent the files from being added to the database. You can exclude file types by adding something like "**/*.HTML" and changing C_Cpp.exclusionPolicy to "checkFilesAndFolders" (but it might reduce performance). We don't read or parse the files, just add the names to a database (in case the files are #included)...we have plans to change that eventually. If the files.exclude doesn't solve the performance issue, it could be caused by our path iteration code adding all files to a vector before it checks files.exclude, which we're tracking with https://github.com/microsoft/vscode-cpptools/issues/3123 . It's not normal for 500k files to be removed from the database...that sounds like it could be a bug (not sure what the repro is).

sean-mcmanus avatar Jun 29 '20 17:06 sean-mcmanus

@nsoblath The log isn't filled with cpptools/didChangeCppProperties and "Attempting to get defaults from compiler in "compilerPath" property:", right? You mean it's filled with path/directory_ut8 logging, right?

Yeah, my guess would be your system headers paths are triggering #3123 -- you should be able to confirm that via setting your compilerPath to "" and see if the problem is fixed.

@sean-mcmanus That's correct, the log is filled with a whole ton of path/directory_ut8 logging. There are exactly six instances of "Attempting to get defaults . . ." in the entire log that look like this:

Attempting to get defaults from compiler in "compilerPath" property: '/usr/bin/clang'
fork complete (parent process, child pid = 78159)
terminating child process: 78159
Attempting to get defaults from compiler in "compilerPath" property: '/usr/bin/clang'
fork complete (parent process, child pid = 78170)
terminating child process: 78170

I took your suggestion and set the compilerPath to "". I started with the logging level at "9" and the application quickly grew in memory like it did before. Switching the logging back to "Debug," I got the attached log file after a few minutes. At the moment cpptools appears to be behaving reasonably, as far as its CPU usage goes. I'll mention here if anything changes. Thank you!

cpptools output 4.txt

nsoblath avatar Jun 29 '20 21:06 nsoblath

This appears to have been a problem for years. If you need some more info I recommend you open a fresh MacBook, install VSCode and C/C++ tools and set it loose on Zephyr RTOS, and watch it melt your new MacBook. In 5 min I found the following, they may or may not be helpful. But this tool is useless on MacOS because it forces it to overheat in minutes, and runs ALL the time.

2020: MacOS:6149

2019: Linux:3507 Linux:3213 Linux:4418 Linux:2991

2018: Linux:1846 MacOS:2742

MarcianoPreciado avatar Dec 15 '20 23:12 MarcianoPreciado

What I don't understand is why this has a "need repro" tag. For me this happens always on any OS (I am using Mac, Windows, and several Linux distros) with any real world project. is it really so that none of the maintainers can reproduce this?

MSoegtropIMC avatar Dec 16 '20 08:12 MSoegtropIMC

@MSoegtropIMC Yeah, I don't believe I've seen a repro yet, other than https://github.com/microsoft/vscode-cpptools/issues/3123 . We've tested on the Linux kernel, LLVM, Chromium, etc.

This issue is also tagged with "investigate" so there's probably stuff we could do to look into a repro.

sean-mcmanus avatar Dec 16 '20 09:12 sean-mcmanus

@sean-mcmanus : can you tell me on what OS you are working? Then I can test it there with the latest released VSCode and prepare a small script which downloads some open source software and send a zip with matching vscode project file(s).

MSoegtropIMC avatar Dec 16 '20 10:12 MSoegtropIMC

@MSoegtropIMC Windows 10 x64 and ARM64, Mac Big Sur x64, Ubuntu 20 x64, and Raspbian/Debian 10 armhf and aarch64.

sean-mcmanus avatar Dec 17 '20 16:12 sean-mcmanus

@sean-mcmanus : perfect, thanks! I am sure we can find an overlap between your and my systems, where this can be reproduced.

MSoegtropIMC avatar Dec 17 '20 16:12 MSoegtropIMC