llvm.org GIT mirror llvm / e26e940
Change sample profile writer to make it deterministic. Summary: This patch changes the function profile output order to be deterministic. In order to make it easier to understand, hottest functions (with most total samples) is ordered first. Reviewers: dnovillo, davidxl Reviewed By: dnovillo Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D33111 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302851 91177308-0d34-0410-b5e6-96231b3b80d8 Dehao Chen 3 years ago
3 changed file(s) with 43 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
4242 /// Write all the sample profiles in the given map of samples.
4343 ///
4444 /// \returns status code of the file update operation.
45 std::error_code write(const StringMap &ProfileMap) {
46 if (std::error_code EC = writeHeader(ProfileMap))
47 return EC;
48 for (const auto &I : ProfileMap) {
49 const FunctionSamples &Profile = I.second;
50 if (std::error_code EC = write(Profile))
51 return EC;
52 }
53 return sampleprof_error::success;
54 }
45 std::error_code write(const StringMap &ProfileMap);
5546
5647 raw_ostream &getOutputStream() { return *OutputStream; }
5748
2828 #include
2929 #include
3030 #include
31 #include
3132 #include
3233 #include
3334 #include
3435
3536 using namespace llvm;
3637 using namespace sampleprof;
38
39 std::error_code
40 SampleProfileWriter::write(const StringMap &ProfileMap) {
41 if (std::error_code EC = writeHeader(ProfileMap))
42 return EC;
43
44 // Sort the ProfileMap by total samples.
45 typedef std::pair NameFunctionSamples;
46 std::vector V;
47 for (const auto &I : ProfileMap)
48 V.push_back(std::make_pair(I.getKey(), &I.second));
49
50 std::stable_sort(
51 V.begin(), V.end(),
52 [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
53 if (A.second->getTotalSamples() == B.second->getTotalSamples())
54 return A.first > B.first;
55 return A.second->getTotalSamples() > B.second->getTotalSamples();
56 });
57
58 for (const auto &I : V) {
59 if (std::error_code EC = write(*I.second))
60 return EC;
61 }
62 return sampleprof_error::success;
63 }
3764
3865 /// \brief Write samples to a text file.
3966 ///
96123 }
97124
98125 void SampleProfileWriterBinary::addName(StringRef FName) {
99 auto NextIdx = NameTable.size();
100 NameTable.insert(std::make_pair(FName, NextIdx));
126 NameTable.insert(std::make_pair(FName, 0));
101127 }
102128
103129 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
135161 addNames(I.second);
136162 }
137163
164 // Sort the names to make NameTable is deterministic.
165 std::set V;
166 for (const auto &I : NameTable)
167 V.insert(I.first);
168 int i = 0;
169 for (const StringRef &N : V)
170 NameTable[N] = i++;
171
138172 // Write out the name table.
139173 encodeULEB128(NameTable.size(), OS);
140 for (auto N : NameTable) {
141 OS << N.first;
174 for (auto N : V) {
175 OS << N;
142176 encodeULEB128(0, OS);
143177 }
144178 return sampleprof_error::success;
2424 counters have doubled.
2525 RUN: llvm-profdata merge --sample %p/Inputs/sample-profile.proftext -o %t-binprof
2626 RUN: llvm-profdata merge --sample --text %p/Inputs/sample-profile.proftext %t-binprof -o - | FileCheck %s --check-prefix=MERGE1
27 MERGE1-DAG: main:368038:0
28 MERGE1-DAG: 9: 4128 _Z3fooi:1262 _Z3bari:2942
29 MERGE1-DAG: _Z3fooi:15422:1220
27 MERGE1: main:368038:0
28 MERGE1: 9: 4128 _Z3fooi:1262 _Z3bari:2942
29 MERGE1: _Z3bari:40602:2874
30 MERGE1: _Z3fooi:15422:1220
3031
3132 5- Detect invalid text encoding (e.g. instrumentation profile text format).
3233 RUN: not llvm-profdata show --sample %p/Inputs/foo3bar3-1.proftext 2>&1 | FileCheck %s --check-prefix=BADTEXT