-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect timing data per unit for each phase (#4512)
This PR adds a `--dump-timings` flag to the `compile` subcommand (similar to the existing `--dump-mem-usage` flag), which collects timing data per compilation unit for each compilation phase. For example, on my 2020 M1 MacBook: ``` $ bazel build -c opt //toolchain $ bazel-bin/toolchain/install/run_carbon compile --phase=lower --dump-timings examples/sieve.carbon | tail ... --- filename: 'examples/sieve.carbon' nanoseconds: lex: 30792 parse: 25458 check: 226625 lower: 1136958 Total: 1419833 ... ``` Most of the changes are pretty straightforward. There were a couple I wasn't sure about though; let me know if I should change: - new `Timings` class in its own file, pretty similar to the existing `MemUsage` class - added a `timings_` field to the `CompilationUnit` class - added a `timings` field to the `Check::Unit` struct - renamed `CheckParseTree` function to `CheckParseTreeInner` for ease of timing with early `return` --------- Co-authored-by: Jon Ross-Perkins <[email protected]>
- Loading branch information
Showing
9 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See /LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#ifndef CARBON_TOOLCHAIN_BASE_TIMINGS_H_ | ||
#define CARBON_TOOLCHAIN_BASE_TIMINGS_H_ | ||
|
||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "toolchain/base/yaml.h" | ||
|
||
namespace Carbon { | ||
|
||
// Helps track timings for a compile. | ||
class Timings { | ||
public: | ||
// Adds tracking for nanoseconds, paired with the given label. | ||
auto Add(std::string label, std::chrono::nanoseconds nanoseconds) -> void { | ||
timings_.push_back({.label = std::move(label), .nanoseconds = nanoseconds}); | ||
} | ||
|
||
auto OutputYaml(llvm::StringRef filename) const -> Yaml::OutputMapping { | ||
// Explicitly copy the filename. | ||
return Yaml::OutputMapping([&, filename](Yaml::OutputMapping::Map map) { | ||
map.Add("filename", filename); | ||
map.Add("nanoseconds", | ||
Yaml::OutputMapping([&](Yaml::OutputMapping::Map label_map) { | ||
std::chrono::nanoseconds total_nanoseconds(0); | ||
for (const auto& entry : timings_) { | ||
total_nanoseconds += entry.nanoseconds; | ||
label_map.Add(entry.label, static_cast<int64_t>( | ||
entry.nanoseconds.count())); | ||
} | ||
label_map.Add("Total", | ||
static_cast<int64_t>(total_nanoseconds.count())); | ||
})); | ||
}); | ||
} | ||
|
||
private: | ||
// Timing for a specific label. | ||
struct Entry { | ||
std::string label; | ||
std::chrono::nanoseconds nanoseconds; | ||
}; | ||
|
||
// The accumulated data on timings. | ||
llvm::SmallVector<Entry> timings_; | ||
}; | ||
|
||
} // namespace Carbon | ||
|
||
#endif // CARBON_TOOLCHAIN_BASE_TIMINGS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM | ||
// Exceptions. See /LICENSE for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
// ARGS: compile --dump-timings --phase=check %s | ||
// | ||
// SET-CHECK-SUBSET | ||
// | ||
// NOAUTOUPDATE | ||
// TIP: To test this file alone, run: | ||
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/driver/testdata/dump_timings.carbon | ||
// TIP: To dump output, run: | ||
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/driver/testdata/dump_timings.carbon | ||
|
||
// CHECK:STDOUT: filename: dump_timings.carbon | ||
// CHECK:STDOUT: nanoseconds: | ||
// CHECK:STDOUT: lex: {{\d+}} | ||
// CHECK:STDOUT: parse: {{\d+}} | ||
// CHECK:STDOUT: check: {{\d+}} | ||
// CHECK:STDOUT: Total: {{\d+}} | ||
// CHECK:STDOUT: ... |