aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-profdata/llvm-profdata.cpp
diff options
context:
space:
mode:
authorWilliam Huang <williamjhuang@google.com>2022-12-29 00:35:06 +0000
committerWilliam Huang <williamjhuang@google.com>2023-01-09 22:01:10 +0000
commit5b72d0e4f5eeb8f90c744cac8e0728cffeca61a9 (patch)
treebfdfc4d7a09b20ed7b1963d9bdcceb0ffd220da5 /llvm/tools/llvm-profdata/llvm-profdata.cpp
parent13b909ef27e7cb7a4c55d1778254be1ac5c0884d (diff)
downloadllvm-5b72d0e4f5eeb8f90c744cac8e0728cffeca61a9.zip
llvm-5b72d0e4f5eeb8f90c744cac8e0728cffeca61a9.tar.gz
llvm-5b72d0e4f5eeb8f90c744cac8e0728cffeca61a9.tar.bz2
[llvm-profdata] Add option to cap profile output size
Allow user to specify `--output-size-limit=n` to cap the size of generated profile to be strictly under n. Functions with the lowest total sample count are dropped first if necessary. Due to using a heuristic, excessive functions may be dropped to satisfy the size requirement Reviewed By: snehasish Differential Revision: https://reviews.llvm.org/D139603
Diffstat (limited to 'llvm/tools/llvm-profdata/llvm-profdata.cpp')
-rw-r--r--llvm/tools/llvm-profdata/llvm-profdata.cpp21
1 files changed, 18 insertions, 3 deletions
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index 90c9e56..0b7d934 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -967,7 +967,7 @@ mergeSampleProfile(const WeightedFileVector &Inputs, SymbolRemapper *Remapper,
bool UseMD5, bool GenPartialProfile, bool GenCSNestedProfile,
bool SampleMergeColdContext, bool SampleTrimColdContext,
bool SampleColdContextFrameDepth, FailureMode FailMode,
- bool DropProfileSymbolList) {
+ bool DropProfileSymbolList, size_t OutputSizeLimit) {
using namespace sampleprof;
SampleProfileMap ProfileMap;
SmallVector<std::unique_ptr<sampleprof::SampleProfileReader>, 5> Readers;
@@ -1049,6 +1049,13 @@ mergeSampleProfile(const WeightedFileVector &Inputs, SymbolRemapper *Remapper,
ProfileIsCS = FunctionSamples::ProfileIsCS = false;
}
+ // If limiting the output size, write to a string buffer first, and drop
+ // functions if the output size exceeds limit. This iterates multiple times
+ // until the limit is satisfied.
+ SmallVector<char> StringBuffer;
+ std::unique_ptr<raw_ostream> BufferStream(
+ new raw_svector_ostream(StringBuffer));
+
auto WriterOrErr =
SampleProfileWriter::create(OutputFilename, FormatMap[OutputFormat]);
if (std::error_code EC = WriterOrErr.getError())
@@ -1060,7 +1067,9 @@ mergeSampleProfile(const WeightedFileVector &Inputs, SymbolRemapper *Remapper,
auto Buffer = getInputFileBuf(ProfileSymbolListFile);
handleExtBinaryWriter(*Writer, OutputFormat, Buffer.get(), WriterList,
CompressAllSections, UseMD5, GenPartialProfile);
- if (std::error_code EC = Writer->write(ProfileMap))
+
+ if (std::error_code EC =
+ Writer->writeWithSizeLimit(ProfileMap, OutputSizeLimit))
exitWithErrorCode(std::move(EC));
}
@@ -1203,6 +1212,11 @@ static int merge_main(int argc, const char *argv[]) {
"sample-frame-depth-for-cold-context", cl::init(1),
cl::desc("Keep the last K frames while merging cold profile. 1 means the "
"context-less base profile"));
+ cl::opt<size_t> OutputSizeLimit(
+ "output-size-limit", cl::init(0), cl::Hidden,
+ cl::desc("Trim cold functions until profile size is below specified "
+ "limit in bytes. This uses a heursitic and functions may be "
+ "excessively trimmed"));
cl::opt<bool> GenPartialProfile(
"gen-partial-profile", cl::init(false), cl::Hidden,
cl::desc("Generate a partial profile (only meaningful for -extbinary)"));
@@ -1289,7 +1303,8 @@ static int merge_main(int argc, const char *argv[]) {
WeightedInputs, Remapper.get(), OutputFilename, OutputFormat,
ProfileSymbolListFile, CompressAllSections, UseMD5, GenPartialProfile,
GenCSNestedProfile, SampleMergeColdContext, SampleTrimColdContext,
- SampleColdContextFrameDepth, FailureMode, DropProfileSymbolList);
+ SampleColdContextFrameDepth, FailureMode, DropProfileSymbolList,
+ OutputSizeLimit);
return 0;
}