Skip to content

[SourceKit] Report number of instructions executed since SourceKit was started in statistics request #37450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented May 17, 2021

To measure code completion performance, add an option to tell sourcekitd to count the number of instructions it executed for a code completion request.

Since this is counting the number of instructions executed by the entire process, this only works reliably if a single request is executed at a time. It is the client's responsibility to ensure that. For now, this is fine because the intended user of this option is the stress tester, which only issues a single request per SourceKit instance anyway.


Eventually, with swiftlang/swift-stress-tester#150 and swiftlang/swift-source-compat-suite#539 merged, this will allow us to time the duration code completion requests took in the stress tester and generate an analysis like the following.

Output
IMPORTANT:
All measurements printed by this script were done in number of number of
instructions. Measurements in seconds are computed assuming that all machines
execute the same number of instructions per second at all times, which is not
correct. Thus all measurements in seconds should be taken with extreme care.
As they scale linearly with the number of instructions, it is safe to compare
values in seconds output by this script.

---------- All CodeComplete requests ----------

Average: 3e+08 instr (ca. 0.048s)

Histogram:
< 8e+06 instr (ca.  0.001s): ***                                       6.1% (<   6.1%)
< 2e+07 instr (ca.  0.003s): **                                        5.1% (<  11.2%)
< 3e+07 instr (ca.  0.005s):                                           0.8% (<  11.9%)
< 7e+07 instr (ca.  0.010s): *******                                  13.0% (<  24.9%)
< 1e+08 instr (ca.  0.020s):                                           0.0% (<  24.9%)
< 3e+08 instr (ca.  0.040s): **                                        4.7% (<  29.6%)
< 5e+08 instr (ca.  0.080s): **************************************** 68.3% (<  98.0%)
< 1e+09 instr (ca.  0.160s):                                           0.5% (<  98.4%)
< 2e+09 instr (ca.  0.321s):                                           1.5% (<  99.9%)
< 4e+09 instr (ca.  0.642s):                                           0.1% (<  99.9%)
< 9e+09 instr (ca.  1.283s):                                           0.1% (< 100.0%)

---------- 10 slowest files for CodeComplete requests ----------

-- /Users/alex/swift-src/swift-source-compat-suite/project_cache/Dollar/Sources/Dollar.swift --

Average: 4e+08 instr (ca. 0.058s)

Histogram:
< 2e+07 instr (ca.  0.003s): *******                                  11.0% (<  11.0%)
< 3e+07 instr (ca.  0.005s): ****                                      6.2% (<  17.2%)
< 7e+07 instr (ca.  0.010s): ****                                      6.2% (<  23.3%)
< 1e+08 instr (ca.  0.020s):                                           0.0% (<  23.3%)
< 3e+08 instr (ca.  0.040s): *******                                  10.6% (<  33.9%)
< 5e+08 instr (ca.  0.080s): **************************************** 58.6% (<  92.5%)
< 1e+09 instr (ca.  0.160s): **                                        4.0% (<  96.5%)
< 2e+09 instr (ca.  0.321s): *                                         2.6% (<  99.1%)
< 4e+09 instr (ca.  0.642s):                                           0.4% (<  99.6%)
< 9e+09 instr (ca.  1.283s):                                           0.4% (< 100.0%)

-- /Users/alex/swift-src/swift-source-compat-suite/project_cache/Dollar/Sources/AutoBind.swift --

Average: 4e+08 instr (ca. 0.054s)

Histogram:
< 8e+06 instr (ca.  0.001s):                                           1.7% (<   1.7%)
< 2e+07 instr (ca.  0.003s):                                           0.9% (<   2.6%)
< 3e+07 instr (ca.  0.005s):                                           0.0% (<   2.6%)
< 7e+07 instr (ca.  0.010s): ****                                      8.4% (<  11.0%)
< 1e+08 instr (ca.  0.020s):                                           0.0% (<  11.0%)
< 3e+08 instr (ca.  0.040s): ***                                       7.1% (<  18.1%)
< 5e+08 instr (ca.  0.080s): **************************************** 80.4% (<  98.6%)
< 1e+09 instr (ca.  0.160s):                                           0.0% (<  98.6%)
< 2e+09 instr (ca.  0.321s):                                           1.4% (< 100.0%)

-- /Users/alex/swift-src/swift-source-compat-suite/project_cache/Dollar/Sources/AutoCurry.swift --

Average: 3e+08 instr (ca. 0.041s)

Histogram:
< 8e+06 instr (ca.  0.001s): *******                                  11.7% (<  11.7%)
< 2e+07 instr (ca.  0.003s): ****                                      7.2% (<  18.9%)
< 3e+07 instr (ca.  0.005s):                                           0.0% (<  18.9%)
< 7e+07 instr (ca.  0.010s): ************                             18.9% (<  37.8%)
< 1e+08 instr (ca.  0.020s):                                           0.0% (<  37.8%)
< 3e+08 instr (ca.  0.040s):                                           1.1% (<  38.9%)
< 5e+08 instr (ca.  0.080s): **************************************** 60.0% (<  98.8%)
< 1e+09 instr (ca.  0.160s):                                           0.0% (<  98.8%)
< 2e+09 instr (ca.  0.321s):  

@ahoppen
Copy link
Member Author

ahoppen commented May 17, 2021

@ahoppen
Copy link
Member Author

ahoppen commented May 17, 2021

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented May 17, 2021

@swift-ci Please test Windows

@ahoppen ahoppen marked this pull request as ready for review May 17, 2021 21:12
@ahoppen ahoppen requested a review from benlangmuir May 17, 2021 21:16
Copy link
Contributor

@benlangmuir benlangmuir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is global anyway, have you considered adding it to the source.request.statistics request instead of doing it per-request? There are important cases you can't measure per-request like the AST built during an edit request, which happens asynchronously. You could still measure those by calling statistics before the edit and then again after the semantic update notification is received.

Alternatively, if we really need it to be per-request, I think we should wrap it in a dictionary to leave ourselves room for adding more statistics without affecting the top-level response dictionary.

{
  ... response ...
  key.statistics: {
    key.instruction_count: Int
  }
}

and make the key to enable it more generic, like key.enable_request_statistics.

@@ -59,6 +57,16 @@ bool environmentVariableRequestedMaximumDeterminism() {
return false;
}

uint64_t getInstructionsExecuted() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: It's nice to write swift:: explicitly to ensure it matches the header declaration.

@ahoppen
Copy link
Member Author

ahoppen commented May 19, 2021

@benlangmuir I hadn’t thought about using the statistics request like that but that sounds like a good idea. We just need to make sure that the overhead of the statistics request doesn’t skew our measurements. I’ll see how much overhead the statistics request adds tomorrow. If the results are comparable, I’ll change it.

@ahoppen ahoppen force-pushed the pr/count-instructions-for-completion-request branch from 5aeb127 to 848ad2b Compare May 20, 2021 14:19
@ahoppen ahoppen changed the title [SourceKit] Add request option to count the number of instructions executed during the request [SourceKit] Report number of instructions executed since SourceKit was started in statistics request May 20, 2021
@ahoppen
Copy link
Member Author

ahoppen commented May 20, 2021

Using the statistics request was an excellent idea. Greatly simplified this PR and also didn’t make the changes in the stress tester much more complex.

I measured how much overhead the two statistics requests add to the instruction count and it appears to be negligible, as expected (average of the equivalent of 0.4ms, with some extremes going up to 3ms)

…s started in statistics request

This can be used to measure how many instructions a request executes by retrieving the number of instructions executed since the process’s start before and after executing the request.
@ahoppen ahoppen force-pushed the pr/count-instructions-for-completion-request branch from 848ad2b to e8b5bca Compare May 20, 2021 14:30
@ahoppen
Copy link
Member Author

ahoppen commented May 20, 2021

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented May 21, 2021

@swift-ci Please test Windows

2 similar comments
@ahoppen
Copy link
Member Author

ahoppen commented May 22, 2021

@swift-ci Please test Windows

@ahoppen
Copy link
Member Author

ahoppen commented May 24, 2021

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 80d3ded into swiftlang:main May 25, 2021
@ahoppen ahoppen deleted the pr/count-instructions-for-completion-request branch May 25, 2021 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants