1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 03:02:36 +01:00
llvm-mirror/include/llvm/XRay/Profile.h
Dean Michael Berris a9b91bad26 [XRay][llvm] Load XRay Profiles
Summary:
This change implements the profile loading functionality in LLVM to
support XRay's profiling mode in compiler-rt.

We introduce a type named `llvm::xray::Profile` which allows building a
profile representation. We can load an XRay profile from a file to build
Profile instances, or do it manually through the Profile type's API.

The intent is to get the `llvm-xray` tool to generate `Profile`
instances and use that as the common abstraction through which all
conversion and analysis can be done. In the future we can generate
`Profile` instances from `Trace` instances as well, through conversion
functions.

Some of the key operations supported by the `Profile` API are:

- Path interning (`Profile::internPath(...)`) which returns a unique path
  identifier.

- Block appending (`Profile::addBlock(...)`) to add thread-associated
  profile information.

- Path ID to Path lookup (`Profile::expandPath(...)`) to look up a
  PathID and return the original interned path.

- Block iteration.

A 'Path' in this context represents the function call stack in
leaf-to-root order. This is represented as a path in an internally
managed prefix tree in the `Profile` instance. Having a handle (PathID)
to identify the unique Paths we encounter for a particular Profile
allows us to reduce the amount of memory required to associate profile
data to a particular Path.

This is the first of a series of patches to migrate the `llvm-stacks`
tool towards using a single profile representation.

Depends on D48653.

Reviewers: kpw, eizan

Reviewed By: kpw

Subscribers: kpw, thakis, mgorny, llvm-commits, hiraditya

Differential Revision: https://reviews.llvm.org/D48370

llvm-svn: 341012
2018-08-30 01:43:22 +00:00

151 lines
4.2 KiB
C++

//===- Profile.h - XRay Profile Abstraction -------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Defines the XRay Profile class representing the latency profile generated by
// XRay's profiling mode.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_XRAY_PROFILE_H
#define LLVM_XRAY_PROFILE_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include <list>
#include <utility>
#include <vector>
namespace llvm {
namespace xray {
class Profile;
// We forward declare the Trace type for turning a Trace into a Profile.
class Trace;
/// This function will attempt to load an XRay Profiling Mode profile from the
/// provided |Filename|.
///
/// For any errors encountered in the loading of the profile data from
/// |Filename|, this function will return an Error condition appropriately.
Expected<Profile> loadProfile(StringRef Filename);
/// This algorithm will merge two Profile instances into a single Profile
/// instance, aggregating blocks by Thread ID.
Profile mergeProfilesByThread(const Profile &L, const Profile &R);
/// This algorithm will merge two Profile instances into a single Profile
/// instance, aggregating blocks by function call stack.
Profile mergeProfilesByStack(const Profile &L, const Profile &R);
/// This function takes a Trace and creates a Profile instance from it.
Expected<Profile> profileFromTrace(const Trace &T);
/// Profile instances are thread-compatible.
class Profile {
public:
using ThreadID = uint64_t;
using PathID = unsigned;
using FuncID = int32_t;
struct Data {
uint64_t CallCount;
uint64_t CumulativeLocalTime;
};
struct Block {
ThreadID Thread;
std::vector<std::pair<PathID, Data>> PathData;
};
/// Provides a sequence of function IDs from a previously interned PathID.
///
/// Returns an error if |P| had not been interned before into the Profile.
///
Expected<std::vector<FuncID>> expandPath(PathID P) const;
/// The stack represented in |P| must be in stack order (leaf to root). This
/// will always return the same PathID for |P| that has the same sequence.
PathID internPath(ArrayRef<FuncID> P);
/// Appends a fully-formed Block instance into the Profile.
///
/// Returns an error condition in the following cases:
///
/// - The PathData component of the Block is empty
///
Error addBlock(Block &&B);
Profile() = default;
~Profile() = default;
Profile(Profile &&O) noexcept
: Blocks(std::move(O.Blocks)), NodeStorage(std::move(O.NodeStorage)),
Roots(std::move(O.Roots)), PathIDMap(std::move(O.PathIDMap)),
NextID(O.NextID) {}
Profile &operator=(Profile &&O) noexcept {
Blocks = std::move(O.Blocks);
NodeStorage = std::move(O.NodeStorage);
Roots = std::move(O.Roots);
PathIDMap = std::move(O.PathIDMap);
NextID = O.NextID;
return *this;
}
Profile(const Profile &);
Profile &operator=(const Profile &);
friend void swap(Profile &L, Profile &R) {
using std::swap;
swap(L.Blocks, R.Blocks);
swap(L.NodeStorage, R.NodeStorage);
swap(L.Roots, R.Roots);
swap(L.PathIDMap, R.PathIDMap);
swap(L.NextID, R.NextID);
}
private:
using BlockList = std::list<Block>;
struct TrieNode {
FuncID Func = 0;
std::vector<TrieNode *> Callees{};
TrieNode *Caller = nullptr;
PathID ID = 0;
};
// List of blocks associated with a Profile.
BlockList Blocks;
// List of TrieNode elements we've seen.
std::list<TrieNode> NodeStorage;
// List of call stack roots.
SmallVector<TrieNode *, 4> Roots;
// Reverse mapping between a PathID to a TrieNode*.
DenseMap<PathID, TrieNode *> PathIDMap;
// Used to identify paths.
PathID NextID = 1;
public:
using const_iterator = BlockList::const_iterator;
const_iterator begin() const { return Blocks.begin(); }
const_iterator end() const { return Blocks.end(); }
bool empty() const { return Blocks.empty(); }
};
} // namespace xray
} // namespace llvm
#endif