-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add basic output of where memory is stored after a compile. #4136
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54e9149
Add basic output of where memory is stored after a compile.
jonmeow c824fea
Apply suggestions from code review
jonmeow 5c5b3c4
Work on comments
jonmeow 4ff0712
A couple more comments
jonmeow b4e6463
Update toolchain/base/mem_usage.h
jonmeow 152ee9e
Update test
jonmeow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,145 @@ | ||
// 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_MEM_USAGE_H_ | ||
#define CARBON_TOOLCHAIN_BASE_MEM_USAGE_H_ | ||
|
||
#include <cstdint> | ||
|
||
#include "common/map.h" | ||
#include "common/set.h" | ||
#include "llvm/ADT/STLFunctionalExtras.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/FormatVariadic.h" | ||
#include "toolchain/base/yaml.h" | ||
|
||
namespace Carbon { | ||
|
||
// Helps track memory usage for a compile. | ||
// | ||
// Users will mix `Add` and `Collect` calls, using `ConcatLabel` to label | ||
// allocation sources. Typically we'll collect stats for growable, potentially | ||
// large data types (such as `SmallVector`), ignoring small fixed-size members | ||
// (such as pointers or `int32_t`). | ||
// | ||
// For example: | ||
// | ||
// auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const | ||
// -> void { | ||
// // Explicit tracking. | ||
// mem_usage.Add(MemUsage::ConcatLabel(label, "data_"), data_.used_bytes(), | ||
// data_.reserved_bytes()); | ||
// // Common library types like `Map` and `llvm::SmallVector` have | ||
// // type-specific support. | ||
// mem_usage.Add(MemUsage::Concat(label, "array_"), array_); | ||
// // Implementing `CollectMemUsage` allows use with the same interface. | ||
// mem_usage.Collect(MemUsage::Concat(label, "obj_"), obj_); | ||
// } | ||
class MemUsage { | ||
public: | ||
// Adds tracking for used and reserved bytes, paired with the given label. | ||
auto Add(std::string label, int64_t used_bytes, int64_t reserved_bytes) | ||
-> void { | ||
mem_usage_.push_back({.label = std::move(label), | ||
.used_bytes = used_bytes, | ||
.reserved_bytes = reserved_bytes}); | ||
} | ||
|
||
// Adds usage tracking for an allocator. | ||
auto Add(std::string label, const llvm::BumpPtrAllocator& allocator) -> void { | ||
Add(std::move(label), allocator.getBytesAllocated(), | ||
allocator.getTotalMemory()); | ||
} | ||
|
||
// Adds usage tracking for a map. | ||
template <typename KeyT, typename ValueT, ssize_t SmallSize, | ||
typename KeyContextT> | ||
auto Add(std::string label, Map<KeyT, ValueT, SmallSize, KeyContextT> map, | ||
KeyContextT key_context = KeyContextT()) -> void { | ||
// These don't track used bytes, so we set the same value for used and | ||
// reserved bytes. | ||
auto bytes = map.ComputeMetrics(key_context).storage_bytes; | ||
Add(std::move(label), bytes, bytes); | ||
} | ||
|
||
// Adds usage tracking for a set. | ||
template <typename KeyT, ssize_t SmallSize, typename KeyContextT> | ||
auto Add(std::string label, Set<KeyT, SmallSize, KeyContextT> set, | ||
KeyContextT key_context = KeyContextT()) -> void { | ||
// These don't track used bytes, so we set the same value for used and | ||
// reserved bytes. | ||
auto bytes = set.ComputeMetrics(key_context).storage_bytes; | ||
Add(std::move(label), bytes, bytes); | ||
} | ||
|
||
// Adds memory usage of an array's data. This ignores the possible overhead of | ||
// a SmallVector's in-place storage; if it's used, it's going to be tiny | ||
// relative to scaling memory costs. | ||
// | ||
// This uses SmallVector in order to get proper inference for T, which | ||
// ArrayRef misses. | ||
template <typename T, unsigned N> | ||
auto Add(std::string label, const llvm::SmallVector<T, N>& array) -> void { | ||
Add(std::move(label), array.size_in_bytes(), array.capacity_in_bytes()); | ||
} | ||
|
||
// Adds memory usage for an object that provides `CollectMemUsage`. | ||
// | ||
// The expected signature of `CollectMemUsage` is above, in MemUsage class | ||
// comments. | ||
template <typename T> | ||
auto Collect(llvm::StringRef label, const T& arg) -> void { | ||
arg.CollectMemUsage(*this, label); | ||
} | ||
|
||
// Constructs a label for memory usage, handling the `.` concatenation. | ||
// We don't expect much depth in labels per-call. | ||
static auto ConcatLabel(llvm::StringRef label, llvm::StringRef child_label) | ||
-> std::string { | ||
return llvm::formatv("{0}.{1}", label, child_label); | ||
} | ||
static auto ConcatLabel(llvm::StringRef label, llvm::StringRef child_label1, | ||
llvm::StringRef child_label2) -> std::string { | ||
return llvm::formatv("{0}.{1}.{2}", label, child_label1, child_label2); | ||
} | ||
|
||
auto OutputYaml(llvm::StringRef filename) const -> Yaml::OutputMapping { | ||
// Explicitly copy the filename. | ||
return Yaml::OutputMapping([&, filename](Yaml::OutputMapping::Map map) { | ||
map.Add("filename", filename); | ||
int64_t total_used = 0; | ||
int64_t total_reserved = 0; | ||
for (const auto& entry : mem_usage_) { | ||
total_used += entry.used_bytes; | ||
total_reserved += entry.reserved_bytes; | ||
map.Add(entry.label, | ||
Yaml::OutputMapping([&](Yaml::OutputMapping::Map byte_map) { | ||
byte_map.Add("used_bytes", entry.used_bytes); | ||
byte_map.Add("reserved_bytes", entry.reserved_bytes); | ||
})); | ||
} | ||
map.Add("Total", | ||
Yaml::OutputMapping([&](Yaml::OutputMapping::Map byte_map) { | ||
byte_map.Add("used_bytes", total_used); | ||
byte_map.Add("reserved_bytes", total_reserved); | ||
})); | ||
}); | ||
} | ||
|
||
private: | ||
// Memory usage for a specific label. | ||
struct Entry { | ||
std::string label; | ||
int64_t used_bytes; | ||
int64_t reserved_bytes; | ||
}; | ||
|
||
// The accumulated data on memory usage. | ||
llvm::SmallVector<Entry> mem_usage_; | ||
}; | ||
|
||
} // namespace Carbon | ||
|
||
#endif // CARBON_TOOLCHAIN_BASE_MEM_USAGE_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 --phase=check --dump-mem-usage %s | ||
// | ||
// NOAUTOUPDATE | ||
// | ||
// Avoid testing specific values: | ||
// SET-CHECK-SUBSET | ||
|
||
var x: i32 = 1; | ||
|
||
// CHECK:STDOUT: --- | ||
// CHECK:STDOUT: filename: dump_mem_usage.carbon | ||
// CHECK:STDOUT: source_: | ||
// CHECK:STDOUT: used_bytes: 0 | ||
// CHECK:STDOUT: reserved_bytes: 0 | ||
// CHECK:STDOUT: ... |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the advantage of using this function instead of calling
CollectMemUsage
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to enforce a standard API, keeping arguments with similar structure.
I tried making this another
Add
overload, but I thinkBumpPtrAllocator
gets a little hostile to overload resolution in a way I'm not able to solve.i.e., as-is, parameters are in similar positions, with the tracked data last:
Otherwise, these are fundamentally different: