2014-10-30 18:00:06 +00:00
|
|
|
//===- SampleProfWriter.h - Write LLVM sample profile data ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains definitions needed for writing sample profiles.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
|
|
|
|
#define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
|
|
|
|
|
2015-10-13 22:48:46 +00:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2014-10-30 18:00:06 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-02-19 03:15:33 +00:00
|
|
|
#include "llvm/ProfileData/ProfileCommon.h"
|
2014-10-30 18:00:06 +00:00
|
|
|
#include "llvm/ProfileData/SampleProf.h"
|
2014-11-03 00:51:45 +00:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2014-10-30 18:00:06 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
namespace sampleprof {
|
|
|
|
|
2014-11-01 00:56:55 +00:00
|
|
|
enum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC };
|
|
|
|
|
2014-10-30 18:00:06 +00:00
|
|
|
/// \brief Sample-based profile writer. Base class.
|
|
|
|
class SampleProfileWriter {
|
|
|
|
public:
|
|
|
|
virtual ~SampleProfileWriter() {}
|
|
|
|
|
2016-03-03 18:09:32 +00:00
|
|
|
/// Write sample profiles in \p S.
|
2014-10-30 18:00:06 +00:00
|
|
|
///
|
2015-10-13 22:48:46 +00:00
|
|
|
/// \returns status code of the file update operation.
|
2016-03-03 18:09:32 +00:00
|
|
|
virtual std::error_code write(const FunctionSamples &S) = 0;
|
2014-11-01 00:56:55 +00:00
|
|
|
|
2015-10-13 22:48:46 +00:00
|
|
|
/// Write all the sample profiles in the given map of samples.
|
2014-11-01 00:56:55 +00:00
|
|
|
///
|
2015-10-13 22:48:46 +00:00
|
|
|
/// \returns status code of the file update operation.
|
|
|
|
std::error_code write(const StringMap<FunctionSamples> &ProfileMap) {
|
|
|
|
if (std::error_code EC = writeHeader(ProfileMap))
|
|
|
|
return EC;
|
|
|
|
for (const auto &I : ProfileMap) {
|
|
|
|
const FunctionSamples &Profile = I.second;
|
2016-03-03 18:09:32 +00:00
|
|
|
if (std::error_code EC = write(Profile))
|
2015-10-13 22:48:46 +00:00
|
|
|
return EC;
|
2014-11-01 00:56:55 +00:00
|
|
|
}
|
2015-10-13 22:48:46 +00:00
|
|
|
return sampleprof_error::success;
|
2014-10-30 18:00:06 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 17:21:42 +00:00
|
|
|
raw_ostream &getOutputStream() { return *OutputStream; }
|
|
|
|
|
2015-10-13 22:48:46 +00:00
|
|
|
/// Profile writer factory.
|
|
|
|
///
|
2015-12-10 17:21:42 +00:00
|
|
|
/// Create a new file writer based on the value of \p Format.
|
2014-11-03 00:51:45 +00:00
|
|
|
static ErrorOr<std::unique_ptr<SampleProfileWriter>>
|
|
|
|
create(StringRef Filename, SampleProfileFormat Format);
|
2014-11-01 00:56:55 +00:00
|
|
|
|
2015-12-10 17:21:42 +00:00
|
|
|
/// Create a new stream writer based on the value of \p Format.
|
|
|
|
/// For testing.
|
|
|
|
static ErrorOr<std::unique_ptr<SampleProfileWriter>>
|
|
|
|
create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
|
|
|
|
|
2014-10-30 18:00:06 +00:00
|
|
|
protected:
|
2015-12-10 17:21:42 +00:00
|
|
|
SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
|
|
|
|
: OutputStream(std::move(OS)) {}
|
|
|
|
|
2015-10-13 22:48:46 +00:00
|
|
|
/// \brief Write a file header for the profile file.
|
|
|
|
virtual std::error_code
|
|
|
|
writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
|
|
|
|
|
2014-10-30 18:00:06 +00:00
|
|
|
/// \brief Output stream where to emit the profile to.
|
2015-12-10 17:21:42 +00:00
|
|
|
std::unique_ptr<raw_ostream> OutputStream;
|
2016-02-19 03:15:33 +00:00
|
|
|
|
|
|
|
/// \brief Profile summary.
|
2016-05-19 21:53:28 +00:00
|
|
|
std::unique_ptr<ProfileSummary> Summary;
|
2016-02-19 03:15:33 +00:00
|
|
|
|
|
|
|
/// \brief Compute summary for this profile.
|
|
|
|
void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
|
2014-10-30 18:00:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Sample-based profile writer (text format).
|
|
|
|
class SampleProfileWriterText : public SampleProfileWriter {
|
|
|
|
public:
|
2016-03-03 18:09:32 +00:00
|
|
|
std::error_code write(const FunctionSamples &S) override;
|
2015-10-13 22:48:46 +00:00
|
|
|
|
|
|
|
protected:
|
2015-12-10 17:21:42 +00:00
|
|
|
SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
|
|
|
|
: SampleProfileWriter(OS), Indent(0) {}
|
|
|
|
|
2015-10-13 22:48:46 +00:00
|
|
|
std::error_code
|
|
|
|
writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
|
|
|
|
return sampleprof_error::success;
|
|
|
|
}
|
2015-10-08 19:40:37 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Indent level to use when writing.
|
|
|
|
///
|
|
|
|
/// This is used when printing inlined callees.
|
|
|
|
unsigned Indent;
|
2015-12-10 17:21:42 +00:00
|
|
|
|
|
|
|
friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
|
|
|
|
SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
|
|
|
|
SampleProfileFormat Format);
|
2014-10-30 18:00:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Sample-based profile writer (binary format).
|
|
|
|
class SampleProfileWriterBinary : public SampleProfileWriter {
|
|
|
|
public:
|
2016-03-03 18:09:32 +00:00
|
|
|
std::error_code write(const FunctionSamples &S) override;
|
2015-10-13 22:48:46 +00:00
|
|
|
|
|
|
|
protected:
|
2015-12-10 17:21:42 +00:00
|
|
|
SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
|
|
|
|
: SampleProfileWriter(OS), NameTable() {}
|
|
|
|
|
2015-10-13 22:48:46 +00:00
|
|
|
std::error_code
|
|
|
|
writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
|
2016-02-19 03:15:33 +00:00
|
|
|
std::error_code writeSummary();
|
2015-10-13 22:48:46 +00:00
|
|
|
std::error_code writeNameIdx(StringRef FName);
|
2016-03-03 18:09:32 +00:00
|
|
|
std::error_code writeBody(const FunctionSamples &S);
|
2015-10-13 22:48:46 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void addName(StringRef FName);
|
|
|
|
void addNames(const FunctionSamples &S);
|
2014-10-30 18:00:06 +00:00
|
|
|
|
2015-10-15 16:36:21 +00:00
|
|
|
MapVector<StringRef, uint32_t> NameTable;
|
2015-12-10 17:21:42 +00:00
|
|
|
|
|
|
|
friend ErrorOr<std::unique_ptr<SampleProfileWriter>>
|
|
|
|
SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
|
|
|
|
SampleProfileFormat Format);
|
2014-10-30 18:00:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End namespace sampleprof
|
|
|
|
|
2014-10-30 20:19:19 +00:00
|
|
|
} // End namespace llvm
|
2014-10-30 18:00:06 +00:00
|
|
|
|
|
|
|
#endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
|