| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H |
| 13 |
#define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H |
| 14 |
|
| 15 |
#include "llvm/ADT/MapVector.h" |
| 16 |
#include "llvm/ADT/StringMap.h" |
| 17 |
#include "llvm/ADT/StringRef.h" |
| 18 |
#include "llvm/ADT/StringSet.h" |
| 19 |
#include "llvm/IR/ProfileSummary.h" |
| 20 |
#include "llvm/ProfileData/SampleProf.h" |
| 21 |
#include "llvm/Support/ErrorOr.h" |
| 22 |
#include "llvm/Support/raw_ostream.h" |
| 23 |
#include <algorithm> |
| 24 |
#include <cstdint> |
| 25 |
#include <memory> |
| 26 |
#include <set> |
| 27 |
#include <system_error> |
| 28 |
#include <unordered_set> |
| 29 |
|
| 30 |
namespace llvm { |
| 31 |
namespace sampleprof { |
| 32 |
|
| 33 |
enum SectionLayout { |
| 34 |
DefaultLayout, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
CtxSplitLayout, |
| 39 |
NumOfLayout, |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
class SampleProfileWriter { |
| 44 |
public: |
| 45 |
virtual ~SampleProfileWriter() = default; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
virtual std::error_code writeSample(const FunctionSamples &S) = 0; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
virtual std::error_code write(const SampleProfileMap &ProfileMap); |
| 56 |
|
| 57 |
raw_ostream &getOutputStream() { return *OutputStream; } |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 63 |
create(StringRef Filename, SampleProfileFormat Format); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 68 |
create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format); |
| 69 |
|
| 70 |
virtual void setProfileSymbolList(ProfileSymbolList *PSL) {} |
| 71 |
virtual void setToCompressAllSections() {} |
| 72 |
virtual void setUseMD5() {} |
| 73 |
virtual void setPartialProfile() {} |
| 74 |
virtual void resetSecLayout(SectionLayout SL) {} |
| 75 |
|
| 76 |
protected: |
| 77 |
SampleProfileWriter(std::unique_ptr<raw_ostream> &OS) |
| 78 |
: OutputStream(std::move(OS)) {} |
| 79 |
|
| 80 |
|
| 81 |
virtual std::error_code writeHeader(const SampleProfileMap &ProfileMap) = 0; |
| 82 |
|
| 83 |
|
| 84 |
virtual std::error_code writeFuncProfiles(const SampleProfileMap &ProfileMap); |
| 85 |
|
| 86 |
|
| 87 |
std::unique_ptr<raw_ostream> OutputStream; |
| 88 |
|
| 89 |
|
| 90 |
std::unique_ptr<ProfileSummary> Summary; |
| 91 |
|
| 92 |
|
| 93 |
void computeSummary(const SampleProfileMap &ProfileMap); |
| 94 |
|
| 95 |
|
| 96 |
SampleProfileFormat Format = SPF_None; |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
class SampleProfileWriterText : public SampleProfileWriter { |
| 101 |
public: |
| 102 |
std::error_code writeSample(const FunctionSamples &S) override; |
| 103 |
|
| 104 |
protected: |
| 105 |
SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS) |
| 106 |
: SampleProfileWriter(OS), Indent(0) {} |
| 107 |
|
| 108 |
std::error_code writeHeader(const SampleProfileMap &ProfileMap) override { |
| 109 |
return sampleprof_error::success; |
| 110 |
} |
| 111 |
|
| 112 |
private: |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
unsigned Indent; |
| 117 |
|
| 118 |
friend ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 119 |
SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, |
| 120 |
SampleProfileFormat Format); |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
class SampleProfileWriterBinary : public SampleProfileWriter { |
| 125 |
public: |
| 126 |
SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS) |
| 127 |
: SampleProfileWriter(OS) {} |
| 128 |
|
| 129 |
virtual std::error_code writeSample(const FunctionSamples &S) override; |
| 130 |
|
| 131 |
protected: |
| 132 |
virtual MapVector<StringRef, uint32_t> &getNameTable() { return NameTable; } |
| 133 |
virtual std::error_code writeMagicIdent(SampleProfileFormat Format); |
| 134 |
virtual std::error_code writeNameTable(); |
| 135 |
virtual std::error_code |
| 136 |
writeHeader(const SampleProfileMap &ProfileMap) override; |
| 137 |
std::error_code writeSummary(); |
| 138 |
virtual std::error_code writeContextIdx(const SampleContext &Context); |
| 139 |
std::error_code writeNameIdx(StringRef FName); |
| 140 |
std::error_code writeBody(const FunctionSamples &S); |
| 141 |
inline void stablizeNameTable(MapVector<StringRef, uint32_t> &NameTable, |
| 142 |
std::set<StringRef> &V); |
| 143 |
|
| 144 |
MapVector<StringRef, uint32_t> NameTable; |
| 145 |
|
| 146 |
void addName(StringRef FName); |
| 147 |
virtual void addContext(const SampleContext &Context); |
| 148 |
void addNames(const FunctionSamples &S); |
| 149 |
|
| 150 |
private: |
| 151 |
friend ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 152 |
SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, |
| 153 |
SampleProfileFormat Format); |
| 154 |
}; |
| 155 |
|
| 156 |
class SampleProfileWriterRawBinary : public SampleProfileWriterBinary { |
| 157 |
using SampleProfileWriterBinary::SampleProfileWriterBinary; |
| 158 |
}; |
| 159 |
|
| 160 |
const std::array<SmallVector<SecHdrTableEntry, 8>, NumOfLayout> |
| 161 |
ExtBinaryHdrLayoutTable = { |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
SmallVector<SecHdrTableEntry, 8>({{SecProfSummary, 0, 0, 0, 0}, |
| 170 |
{SecNameTable, 0, 0, 0, 0}, |
| 171 |
{SecCSNameTable, 0, 0, 0, 0}, |
| 172 |
{SecFuncOffsetTable, 0, 0, 0, 0}, |
| 173 |
{SecLBRProfile, 0, 0, 0, 0}, |
| 174 |
{SecProfileSymbolList, 0, 0, 0, 0}, |
| 175 |
{SecFuncMetadata, 0, 0, 0, 0}}), |
| 176 |
|
| 177 |
SmallVector<SecHdrTableEntry, 8>({{SecProfSummary, 0, 0, 0, 0}, |
| 178 |
{SecNameTable, 0, 0, 0, 0}, |
| 179 |
|
| 180 |
|
| 181 |
{SecFuncOffsetTable, 0, 0, 0, 0}, |
| 182 |
{SecLBRProfile, 0, 0, 0, 0}, |
| 183 |
|
| 184 |
|
| 185 |
{SecFuncOffsetTable, 0, 0, 0, 0}, |
| 186 |
{SecLBRProfile, 0, 0, 0, 0}, |
| 187 |
{SecProfileSymbolList, 0, 0, 0, 0}, |
| 188 |
{SecFuncMetadata, 0, 0, 0, 0}}), |
| 189 |
}; |
| 190 |
|
| 191 |
class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary { |
| 192 |
using SampleProfileWriterBinary::SampleProfileWriterBinary; |
| 193 |
public: |
| 194 |
virtual std::error_code write(const SampleProfileMap &ProfileMap) override; |
| 195 |
|
| 196 |
virtual void setToCompressAllSections() override; |
| 197 |
void setToCompressSection(SecType Type); |
| 198 |
virtual std::error_code writeSample(const FunctionSamples &S) override; |
| 199 |
|
| 200 |
|
| 201 |
virtual void setUseMD5() override { |
| 202 |
UseMD5 = true; |
| 203 |
addSectionFlag(SecNameTable, SecNameTableFlags::SecFlagMD5Name); |
| 204 |
|
| 205 |
|
| 206 |
addSectionFlag(SecNameTable, SecNameTableFlags::SecFlagFixedLengthMD5); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
virtual void setPartialProfile() override { |
| 213 |
addSectionFlag(SecProfSummary, SecProfSummaryFlags::SecFlagPartial); |
| 214 |
} |
| 215 |
|
| 216 |
virtual void setProfileSymbolList(ProfileSymbolList *PSL) override { |
| 217 |
ProfSymList = PSL; |
| 218 |
}; |
| 219 |
|
| 220 |
virtual void resetSecLayout(SectionLayout SL) override { |
| 221 |
verifySecLayout(SL); |
| 222 |
#ifndef NDEBUG |
| 223 |
|
| 224 |
for (auto &Entry : SectionHdrLayout) { |
| 225 |
assert(Entry.Flags == 0 && |
| 226 |
"resetSecLayout has to be called before any flag setting"); |
| 227 |
} |
| 228 |
#endif |
| 229 |
SecLayout = SL; |
| 230 |
SectionHdrLayout = ExtBinaryHdrLayoutTable[SL]; |
| 231 |
} |
| 232 |
|
| 233 |
protected: |
| 234 |
uint64_t markSectionStart(SecType Type, uint32_t LayoutIdx); |
| 235 |
std::error_code addNewSection(SecType Sec, uint32_t LayoutIdx, |
| 236 |
uint64_t SectionStart); |
| 237 |
template <class SecFlagType> |
| 238 |
void addSectionFlag(SecType Type, SecFlagType Flag) { |
| 239 |
for (auto &Entry : SectionHdrLayout) { |
| 240 |
if (Entry.Type == Type) |
| 241 |
addSecFlag(Entry, Flag); |
| 242 |
} |
| 243 |
} |
| 244 |
template <class SecFlagType> |
| 245 |
void addSectionFlag(uint32_t SectionIdx, SecFlagType Flag) { |
| 246 |
addSecFlag(SectionHdrLayout[SectionIdx], Flag); |
| 247 |
} |
| 248 |
|
| 249 |
virtual void addContext(const SampleContext &Context) override; |
| 250 |
|
| 251 |
|
| 252 |
virtual std::error_code writeCustomSection(SecType Type) = 0; |
| 253 |
|
| 254 |
virtual void verifySecLayout(SectionLayout SL) = 0; |
| 255 |
|
| 256 |
|
| 257 |
virtual std::error_code writeSections(const SampleProfileMap &ProfileMap) = 0; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
virtual std::error_code writeOneSection(SecType Type, uint32_t LayoutIdx, |
| 262 |
const SampleProfileMap &ProfileMap); |
| 263 |
|
| 264 |
|
| 265 |
virtual std::error_code writeNameTable() override; |
| 266 |
virtual std::error_code |
| 267 |
writeContextIdx(const SampleContext &Context) override; |
| 268 |
std::error_code writeCSNameIdx(const SampleContext &Context); |
| 269 |
std::error_code writeCSNameTableSection(); |
| 270 |
|
| 271 |
std::error_code writeFuncMetadata(const SampleProfileMap &Profiles); |
| 272 |
std::error_code writeFuncMetadata(const FunctionSamples &Profile); |
| 273 |
|
| 274 |
|
| 275 |
std::error_code writeNameTableSection(const SampleProfileMap &ProfileMap); |
| 276 |
std::error_code writeFuncOffsetTable(); |
| 277 |
std::error_code writeProfileSymbolListSection(); |
| 278 |
|
| 279 |
SectionLayout SecLayout = DefaultLayout; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
SmallVector<SecHdrTableEntry, 8> SectionHdrLayout = |
| 285 |
ExtBinaryHdrLayoutTable[DefaultLayout]; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
uint64_t SecLBRProfileStart = 0; |
| 291 |
|
| 292 |
private: |
| 293 |
void allocSecHdrTable(); |
| 294 |
std::error_code writeSecHdrTable(); |
| 295 |
virtual std::error_code |
| 296 |
writeHeader(const SampleProfileMap &ProfileMap) override; |
| 297 |
std::error_code compressAndOutput(); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
std::unique_ptr<raw_ostream> LocalBufStream; |
| 308 |
|
| 309 |
uint64_t FileStart; |
| 310 |
|
| 311 |
|
| 312 |
uint64_t SecHdrTableOffset; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
std::vector<SecHdrTableEntry> SecHdrTable; |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
MapVector<SampleContext, uint64_t> FuncOffsetTable; |
| 322 |
|
| 323 |
bool UseMD5 = false; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
MapVector<SampleContext, uint32_t> CSNameTable; |
| 328 |
|
| 329 |
ProfileSymbolList *ProfSymList = nullptr; |
| 330 |
}; |
| 331 |
|
| 332 |
class SampleProfileWriterExtBinary : public SampleProfileWriterExtBinaryBase { |
| 333 |
public: |
| 334 |
SampleProfileWriterExtBinary(std::unique_ptr<raw_ostream> &OS) |
| 335 |
: SampleProfileWriterExtBinaryBase(OS) {} |
| 336 |
|
| 337 |
private: |
| 338 |
std::error_code writeDefaultLayout(const SampleProfileMap &ProfileMap); |
| 339 |
std::error_code writeCtxSplitLayout(const SampleProfileMap &ProfileMap); |
| 340 |
|
| 341 |
virtual std::error_code |
| 342 |
writeSections(const SampleProfileMap &ProfileMap) override; |
| 343 |
|
| 344 |
virtual std::error_code writeCustomSection(SecType Type) override { |
| 345 |
return sampleprof_error::success; |
| 346 |
}; |
| 347 |
|
| 348 |
virtual void verifySecLayout(SectionLayout SL) override { |
| 349 |
assert((SL == DefaultLayout || SL == CtxSplitLayout) && |
| 350 |
"Unsupported layout"); |
| 351 |
} |
| 352 |
}; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
class SampleProfileWriterCompactBinary : public SampleProfileWriterBinary { |
| 385 |
using SampleProfileWriterBinary::SampleProfileWriterBinary; |
| 386 |
|
| 387 |
public: |
| 388 |
virtual std::error_code writeSample(const FunctionSamples &S) override; |
| 389 |
virtual std::error_code write(const SampleProfileMap &ProfileMap) override; |
| 390 |
|
| 391 |
protected: |
| 392 |
|
| 393 |
|
| 394 |
MapVector<StringRef, uint64_t> FuncOffsetTable; |
| 395 |
|
| 396 |
|
| 397 |
uint64_t TableOffset; |
| 398 |
virtual std::error_code writeNameTable() override; |
| 399 |
virtual std::error_code |
| 400 |
writeHeader(const SampleProfileMap &ProfileMap) override; |
| 401 |
std::error_code writeFuncOffsetTable(); |
| 402 |
}; |
| 403 |
|
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
#endif |