-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[MemProf] Support for random hotness when writing profile #113998
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| #include "llvm/ProfileData/InstrProf.h" | ||
| #include "llvm/ProfileData/MemProf.h" | ||
| #include "llvm/ProfileData/ProfileCommon.h" | ||
| #include "llvm/Support/CommandLine.h" | ||
| #include "llvm/Support/Compression.h" | ||
| #include "llvm/Support/Endian.h" | ||
| #include "llvm/Support/EndianStream.h" | ||
|
|
@@ -36,6 +37,14 @@ | |
|
|
||
| using namespace llvm; | ||
|
|
||
| static cl::opt<bool> | ||
| MemprofGenerateRandomHotness("memprof-random-hotness", cl::init(false), | ||
| cl::Hidden, | ||
| cl::desc("Generate random hotness values")); | ||
| static cl::opt<unsigned> MemprofGenerateRandomHotnessSeed( | ||
| "memprof-random-hotness-seed", cl::init(0), cl::Hidden, | ||
| cl::desc("Random hotness seed to use (0 to generate new seed)")); | ||
|
|
||
| // A struct to define how the data stream should be patched. For Indexed | ||
| // profiling, only uint64_t data type is needed. | ||
| struct PatchItem { | ||
|
|
@@ -190,7 +199,16 @@ InstrProfWriter::InstrProfWriter( | |
| InfoObj(new InstrProfRecordWriterTrait()), | ||
| WritePrevVersion(WritePrevVersion), | ||
| MemProfVersionRequested(MemProfVersionRequested), | ||
| MemProfFullSchema(MemProfFullSchema) {} | ||
| MemProfFullSchema(MemProfFullSchema) { | ||
| // Set up the random number seed if requested. | ||
| if (MemprofGenerateRandomHotness) { | ||
| unsigned seed = MemprofGenerateRandomHotnessSeed | ||
| ? MemprofGenerateRandomHotnessSeed | ||
| : std::time(nullptr); | ||
| errs() << "random hotness seed = " << seed << "\n"; | ||
| std::srand(seed); | ||
| } | ||
| } | ||
|
|
||
| InstrProfWriter::~InstrProfWriter() { delete InfoObj; } | ||
|
|
||
|
|
@@ -273,13 +291,37 @@ void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash, | |
|
|
||
| void InstrProfWriter::addMemProfRecord( | ||
| const Function::GUID Id, const memprof::IndexedMemProfRecord &Record) { | ||
| auto [Iter, Inserted] = MemProfData.Records.insert({Id, Record}); | ||
| auto NewRecord = Record; | ||
| // Provoke random hotness values if requested. We specify the lifetime access | ||
| // density and lifetime length that will result in a cold or not cold hotness. | ||
| // See the logic in getAllocType() in Analysis/MemoryProfileInfo.cpp. | ||
| if (MemprofGenerateRandomHotness) { | ||
| for (auto &Alloc : NewRecord.AllocSites) { | ||
| uint64_t NewTLAD = 0; | ||
|
||
| uint64_t NewTL = 0; | ||
| bool IsCold = std::rand() % 2; | ||
| if (IsCold) { | ||
| // To get a cold context, set the lifetime access density to 0 and the | ||
| // lifetime to the maximum value. | ||
| NewTLAD = 0; | ||
| NewTL = std::numeric_limits<uint64_t>::max(); | ||
| } else { | ||
| // To get a not cold context, set the lifetime access density to the | ||
| // maximum value and the lifetime to 0. | ||
| NewTLAD = std::numeric_limits<uint64_t>::max(); | ||
| NewTL = 0; | ||
| } | ||
| Alloc.Info.setTotalLifetimeAccessDensity(NewTLAD); | ||
| Alloc.Info.setTotalLifetime(NewTL); | ||
| } | ||
| } | ||
| auto [Iter, Inserted] = MemProfData.Records.insert({Id, NewRecord}); | ||
| // If we inserted a new record then we are done. | ||
| if (Inserted) { | ||
| return; | ||
| } | ||
| memprof::IndexedMemProfRecord &Existing = Iter->second; | ||
| Existing.merge(Record); | ||
| Existing.merge(NewRecord); | ||
| } | ||
|
|
||
| bool InstrProfWriter::addMemProfFrame(const memprof::FrameId Id, | ||
|
|
||
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.
For internal tooling it will be cleaner if we plumb it through as a parameter instead of setting the cl::opt.
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.
done