Skip to content

perf(compiler-cli): refactor the performance tracing infrastructure - #41125

Closed
alxhub wants to merge 2 commits into
angular:masterfrom
alxhub:ngtsc/perf/tracing
Closed

alxhub wants to merge 2 commits into
angular:masterfrom
alxhub:ngtsc/perf/tracing

Conversation

@alxhub

@alxhub alxhub commented Mar 9, 2021

Copy link
Copy Markdown
Member

No description provided.

@google-cla google-cla Bot added the cla: yes label Mar 9, 2021
@alxhub
alxhub force-pushed the ngtsc/perf/tracing branch 2 times, most recently from a7252ab to 2670b32 Compare March 10, 2021 22:58
@jessicajaniuk jessicajaniuk added the area: compiler Issues related to `ngc`, Angular's template compiler label Mar 11, 2021
@ngbot ngbot Bot added this to the Backlog milestone Mar 11, 2021
@alxhub
alxhub force-pushed the ngtsc/perf/tracing branch 3 times, most recently from d2e4f89 to 398852d Compare March 16, 2021 00:00
@alxhub
alxhub marked this pull request as ready for review March 16, 2021 00:05
@alxhub
alxhub requested a review from zarend March 16, 2021 00:05
@pullapprove
pullapprove Bot requested review from IgorMinar and atscott March 16, 2021 00:05
@alxhub
alxhub requested review from IgorMinar and dgp1130 and removed request for IgorMinar and atscott March 16, 2021 00:05
@pullapprove
pullapprove Bot requested a review from atscott March 16, 2021 00:06
@alxhub alxhub added the target: minor This PR is targeted for the next minor release label Mar 16, 2021
@alxhub
alxhub force-pushed the ngtsc/perf/tracing branch from 398852d to 8dcc6d2 Compare March 16, 2021 18:36

@zarend zarend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few questions, but overall this looks good, and will be very helpful for debugging perf issues.

Comment thread packages/bazel/src/ng_module.bzl Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@alxhub do you know what directory these json files will go in? I'm wondering if they with be "git ignored" or not for apps generated with the CLI.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the Bazel ng_module rule definition, so it only applies to builds done using Bazel (so not the CLI).

They're emitted into blaze-bin along with all other build artifacts, so they're under the .gitignore rules for all build outputs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 this is a conventional way to do analytics in general. Many many analytics apps or APIs model analytics as events which are string-number pairs (or maybe enums instead of strings).

If we want to really make this interface future-proof, we can add an option to make events enum-string pairs because sometimes it's helpful to record additional metadata when an event happens. I do not think we have a use case for this right now, which is why this is not a blocker for me.

This is something to consider so we can make this perf engine something that we can grow into. If we have both eventCount, and eventLabel, then we can do pretty much anything with this perf system engine.

 /**
   * Record that a specific event has occurred, possibly more than once.
   */
  eventCount(event: PerfNumericalEvent, incrementBy?: number): void;

  /**
   * Record that a specific event has occurred, and assign a string label to that event. 
   */
  eventLabel(event: PerfCategoricalEvent, label?: string): void;
  
// events that we take the total count of over time
enum  PerfIncrementalEvent {
 // ...
}

// events that that map to the string labels that we record at each occurrence of the event
enum  PerfCategoricalEvent {
 // ...
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The general concept here is that these are counters, which track the occurrences of an event (e.g. "processed a .d.ts input"). We don't currently track when that thing happens, or differentiate between different occurrences in any way.

This is done so that the output is fixed size (a predefined number of counter buckets) and is efficient (no allocation required to track an event which may happen hundreds or thousands of times).

We might decide to have a special logging operation to track certain infrequent events along with the actual timestamp at which they've occurred, plus additional metadata potentially, but right now there isn't a use case for this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 this is an efficient way to do this.

@dgp1130 dgp1130 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Already discussed most of this offline.

For future reference, I did suggest using an output group, but this was fiddly to implement and the Bazel flag is protection enough to avoid external users depending on the perf files and seeing non-deterministic outputs.

This does mean that the compiler action will output a non-deterministic file as a side effect, but this is impractical to depend upon and we don't have any more actions to run, so this isn't an issue for now. If this becomes more complex in the future, we might want to consider changing this to an output group.

Comment thread packages/bazel/src/ng_module.bzl Resolved
Comment thread packages/bazel/src/ng_module.bzl Resolved
Comment thread packages/bazel/src/ng_module.bzl Resolved
Comment thread BUILD.bazel Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Skylib does provide some useful primitives for flags: https://github.com/bazelbuild/bazel-skylib/blob/2b38b2f8bd4b8603d610cfc651fcbb299498147f/rules/common_settings.bzl.

We could use a bool_flag() here and drop the provider, but it seems like we don't have Skylib directly included in the workspace right now (I'm sure we depend on it transitively).

I'm fine doing this with a direct provider, but just wanted to mention the Skylib option to simplify this if we're willing to depend on that directly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I saw some references to Skylib, but it didn't seem to be something we imported from, and setting something like that up is beyond my knowledge :)

Comment thread BUILD.bazel Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe this flag should be defined in packages/bazel/BUILD.bazel? No need for it to be at the root package.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I moved it to //packages/compiler-cli:ng_perf since it's conceptually a configuration option for the compiler.

Comment thread packages/bazel/src/ng_module.bzl Resolved

@dgp1130 dgp1130 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couple more things that just randomly came to mind.

Comment thread packages/bazel/src/ng_module.bzl Resolved
Comment thread packages/bazel/src/ng_module.bzl Resolved
Comment thread packages/bazel/src/ng_module.bzl Resolved
@alxhub
alxhub force-pushed the ngtsc/perf/tracing branch from 8dcc6d2 to 7cd0ea9 Compare March 16, 2021 22:20

@dgp1130 dgp1130 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The Starlark bits LGTM.

@IgorMinar IgorMinar left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

overall I like this! It's going to be a great feature for diagnosing issues. Can you please consider making the phase tracking more robust (see my comment below).

Comment thread packages/bazel/src/ng_module.bzl Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please note that all of these changes will need to be reimplemented in g3 because it uses forked version of this code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep - we should be able to reuse the same mechanism in g3.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

interesting, I was going to suggest that you use this try/finally pattern throughout, I wonder why you decided to use it here and not elsewhere.

I suggest that you consider using this pattern everywhere, or even better - use it in combination with a wrapper function:

this.compiler.perfRecorder.phase(PerfPhase.LsReferencesAndRenames, () => {
  const templateInfo = getTemplateInfoAtPosition(filePath, position, this.compiler);
  if (templateInfo === undefined) {
    return this.getReferencesAtTypescriptPosition(filePath, position);
  }
  return this.getReferencesAtTemplatePosition(templateInfo, position);
});

and internally within phase use try/finally. The overhead of this should be negligible with today's V8, and in return you can automatically restore previous phase and prevent issues with accidentally missing phase end calls.

@alxhub alxhub Mar 17, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I considered that kind of an API, but I originally decided against it for a couple reasons:

  • It adds overhead, although as you mention in v8 it's really not that much.
  • It complicates debugging by introducing an additional callback to step through. This is a large part of why we prefer for..of loops instead of .forEach in the compiler.
  • It increases the indentation of functions which use it, and may obscure the actual code being run.
  • It can interfere with features like type narrowing, since type assertions that happen within the callback don't apply after it returns. There is no way to tell TypeScript "this function will synchronously execute its callback, so any narrowing that happens within the callback should apply after the call as well".

That said, I modified the API to add a .inPhase() operation which works the way you suggest, and it turned out to be fairly clean and readable in most of the situations that tracked a phase. I also like the explicitness of "this chunk of code is considered to be in the given phase" - it makes it easier to reason about where time is going. I think for that reason, we'll go with that API despite the debugging/nesting complications.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sounds good. I do agree that the code is much more readable with inPhase. Did you intentionally keep some of the phase callsites in the PR because those parts of the code base are more sensitive to the downsides you listed above? Or is that an oversight?

Could also expand the docs for phase and inPhase and document when to use which API in the future?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, there are a couple places where the inPhase pattern doesn't apply so cleanly, such as in ngc-wrapped where the compiler may not support performance tracing at all, or in program.ts where wrapping the operations in a function would cause problems with the flow of types through that particular section.

@alxhub
alxhub force-pushed the ngtsc/perf/tracing branch 4 times, most recently from a3cb9b4 to f862827 Compare March 19, 2021 20:14

@IgorMinar IgorMinar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

in general LGTM. Please consider/address the feedback posted my previous comment.

@IgorMinar IgorMinar added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Mar 22, 2021
@zarend zarend mentioned this pull request Mar 22, 2021
@alxhub
alxhub force-pushed the ngtsc/perf/tracing branch from f862827 to be71f52 Compare March 24, 2021 18:16
alxhub added 2 commits March 24, 2021 11:16
ngtsc has an internal performance tracing package, which previously has not
really seen much use. It used to track performance statistics on a very
granular basis (microseconds per actual class analysis, for example). This
had two problems:

* it produced voluminous amounts of data, complicating the analysis of such
  results and providing dubious value.
* it added nontrivial overhead to compilation when used (which also affected
  the very performance of the operations being measured).

This commit replaces the old system with a streamlined performance tracing
setup which is lightweight and designed to be always-on. The new system
tracks 3 metrics:

* time taken by various phases and operations within the compiler
* events (counters) which measure the shape and size of the compilation
* memory usage measured at various points of the compilation process

If the compiler option `tracePerformance` is set, the compiler will
serialize these metrics to a JSON file at that location after compilation is
complete.
A previous commit implemented a streamlined performance metric reporting
system for the compiler-cli, controlled via the compiler option
`tracePerformance`.

This commit adds a custom Bazel flag rule //packages/compiler-cli:ng_perf
to the repository, and wires it through to the `ng_module` implementation
such that if the flag is set, `ng_module` will produce perf results as part
of the build. The underlying mechanism of `//:ng_perf` is not exported from
`@angular/bazel` as a public rule that consumers can use, so there is little
risk of accidental dependency on the contents of these perf traces.

An alias is added so that `--ng_perf` is a Bazel flag which works in our
repository.
@alxhub
alxhub force-pushed the ngtsc/perf/tracing branch from be71f52 to 314f36a Compare March 24, 2021 18:17
@alxhub alxhub added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Mar 24, 2021
@ngbot

ngbot Bot commented Mar 24, 2021

Copy link
Copy Markdown

I see that you just added the action: merge label, but the following checks are still failing:
    failure status "cla/google" is failing
    pending status "ci/circleci: test_win" is pending
    pending status "google3" is pending
    pending status "pullapprove" is pending
    pending 1 pending code review

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

@alxhub alxhub added target: patch This PR is targeted for the next patch release cla: yes and removed target: minor This PR is targeted for the next minor release cla: yes labels Mar 24, 2021
@alxhub
alxhub removed the request for review from atscott March 24, 2021 20:32
@pullapprove
pullapprove Bot requested a review from atscott March 24, 2021 20:33
alxhub added a commit that referenced this pull request Mar 24, 2021
…41125)

ngtsc has an internal performance tracing package, which previously has not
really seen much use. It used to track performance statistics on a very
granular basis (microseconds per actual class analysis, for example). This
had two problems:

* it produced voluminous amounts of data, complicating the analysis of such
  results and providing dubious value.
* it added nontrivial overhead to compilation when used (which also affected
  the very performance of the operations being measured).

This commit replaces the old system with a streamlined performance tracing
setup which is lightweight and designed to be always-on. The new system
tracks 3 metrics:

* time taken by various phases and operations within the compiler
* events (counters) which measure the shape and size of the compilation
* memory usage measured at various points of the compilation process

If the compiler option `tracePerformance` is set, the compiler will
serialize these metrics to a JSON file at that location after compilation is
complete.

PR Close #41125
alxhub added a commit that referenced this pull request Mar 24, 2021
A previous commit implemented a streamlined performance metric reporting
system for the compiler-cli, controlled via the compiler option
`tracePerformance`.

This commit adds a custom Bazel flag rule //packages/compiler-cli:ng_perf
to the repository, and wires it through to the `ng_module` implementation
such that if the flag is set, `ng_module` will produce perf results as part
of the build. The underlying mechanism of `//:ng_perf` is not exported from
`@angular/bazel` as a public rule that consumers can use, so there is little
risk of accidental dependency on the contents of these perf traces.

An alias is added so that `--ng_perf` is a Bazel flag which works in our
repository.

PR Close #41125
@alxhub alxhub closed this in 48fec08 Mar 24, 2021
alxhub added a commit that referenced this pull request Mar 24, 2021
A previous commit implemented a streamlined performance metric reporting
system for the compiler-cli, controlled via the compiler option
`tracePerformance`.

This commit adds a custom Bazel flag rule //packages/compiler-cli:ng_perf
to the repository, and wires it through to the `ng_module` implementation
such that if the flag is set, `ng_module` will produce perf results as part
of the build. The underlying mechanism of `//:ng_perf` is not exported from
`@angular/bazel` as a public rule that consumers can use, so there is little
risk of accidental dependency on the contents of these perf traces.

An alias is added so that `--ng_perf` is a Bazel flag which works in our
repository.

PR Close #41125
TeriGlover pushed a commit to TeriGlover/angular that referenced this pull request Apr 5, 2021
…ngular#41125)

ngtsc has an internal performance tracing package, which previously has not
really seen much use. It used to track performance statistics on a very
granular basis (microseconds per actual class analysis, for example). This
had two problems:

* it produced voluminous amounts of data, complicating the analysis of such
  results and providing dubious value.
* it added nontrivial overhead to compilation when used (which also affected
  the very performance of the operations being measured).

This commit replaces the old system with a streamlined performance tracing
setup which is lightweight and designed to be always-on. The new system
tracks 3 metrics:

* time taken by various phases and operations within the compiler
* events (counters) which measure the shape and size of the compilation
* memory usage measured at various points of the compilation process

If the compiler option `tracePerformance` is set, the compiler will
serialize these metrics to a JSON file at that location after compilation is
complete.

PR Close angular#41125
TeriGlover pushed a commit to TeriGlover/angular that referenced this pull request Apr 5, 2021
A previous commit implemented a streamlined performance metric reporting
system for the compiler-cli, controlled via the compiler option
`tracePerformance`.

This commit adds a custom Bazel flag rule //packages/compiler-cli:ng_perf
to the repository, and wires it through to the `ng_module` implementation
such that if the flag is set, `ng_module` will produce perf results as part
of the build. The underlying mechanism of `//:ng_perf` is not exported from
`@angular/bazel` as a public rule that consumers can use, so there is little
risk of accidental dependency on the contents of these perf traces.

An alias is added so that `--ng_perf` is a Bazel flag which works in our
repository.

PR Close angular#41125
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Apr 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler cla: yes target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants