#include "common.h"
#ifdef ENABLE_DEBUG
#define DEBUG(X) \
{ X; }
#else
#define DEBUG(X) \
{}
#endif
#pragma GCC visibility push(hidden)
extern "C" {
#if defined(__APPLE__)
extern uint64_t* _bolt_instr_locations_getter();
extern uint32_t _bolt_num_counters_getter();
extern uint8_t* _bolt_instr_tables_getter();
extern uint32_t _bolt_instr_num_funcs_getter();
#else
extern uint64_t __bolt_instr_locations[];
extern uint32_t __bolt_num_counters;
extern uint32_t __bolt_instr_num_ind_calls;
extern uint32_t __bolt_instr_num_ind_targets;
extern uint32_t __bolt_instr_num_funcs;
extern uint32_t __bolt_instr_sleep_time;
extern bool __bolt_instr_no_counters_clear;
extern bool __bolt_instr_wait_forks;
extern char __bolt_instr_filename[];
extern char __bolt_instr_binpath[];
extern bool __bolt_instr_use_pid;
extern void (*__bolt_ind_call_counter_func_pointer)();
extern void (*__bolt_ind_tailcall_counter_func_pointer)();
extern void __bolt_start_trampoline();
extern void __bolt_fini_trampoline();
#endif
}
namespace {
class BumpPtrAllocator {
struct EntryMetadata {
uint64_t Magic;
uint64_t AllocSize;
};
public:
void *allocate(size_t Size) {
Lock L(M);
if (StackBase == nullptr) {
StackBase = reinterpret_cast<uint8_t *>(
__mmap(0, MaxSize, PROT_READ | PROT_WRITE,
(Shared ? MAP_SHARED : MAP_PRIVATE) | MAP_ANONYMOUS, -1, 0));
assert(StackBase != MAP_FAILED,
"BumpPtrAllocator: failed to mmap stack!");
StackSize = 0;
}
Size = alignTo(Size + sizeof(EntryMetadata), 16);
uint8_t *AllocAddress = StackBase + StackSize + sizeof(EntryMetadata);
auto *M = reinterpret_cast<EntryMetadata *>(StackBase + StackSize);
M->Magic = Magic;
M->AllocSize = Size;
StackSize += Size;
assert(StackSize < MaxSize, "allocator ran out of memory");
return AllocAddress;
}
#ifdef DEBUG
void deallocate(void *Ptr) {
Lock L(M);
uint8_t MetadataOffset = sizeof(EntryMetadata);
auto *M = reinterpret_cast<EntryMetadata *>(
reinterpret_cast<uint8_t *>(Ptr) - MetadataOffset);
const uint8_t *StackTop = StackBase + StackSize + MetadataOffset;
if (Ptr != StackTop - M->AllocSize) {
MetadataOffset +=
sizeof(uint64_t);
M = reinterpret_cast<EntryMetadata *>(reinterpret_cast<uint8_t *>(Ptr) -
MetadataOffset);
assert(Ptr == StackTop - M->AllocSize,
"must deallocate the last element alloc'ed");
}
assert(M->Magic == Magic, "allocator magic is corrupt");
StackSize -= M->AllocSize;
}
#else
void deallocate(void *) {}
#endif
void clear() {
Lock L(M);
StackSize = 0;
}
void setMaxSize(uint64_t Size) { MaxSize = Size; }
void setShared(bool S) { Shared = S; }
void destroy() {
if (StackBase == nullptr)
return;
__munmap(StackBase, MaxSize);
}
static void *operator new(size_t, void *Ptr) { return Ptr; };
private:
static constexpr uint64_t Magic = 0x1122334455667788ull;
uint64_t MaxSize = 0xa00000;
uint8_t *StackBase{nullptr};
uint64_t StackSize{0};
bool Shared{false};
Mutex M;
};
BumpPtrAllocator *GlobalAlloc;
uint64_t TextBaseAddress = 0;
void *GlobalMetadataStorage;
}
void *operator new(size_t Sz, BumpPtrAllocator &A) { return A.allocate(Sz); }
void *operator new(size_t Sz, BumpPtrAllocator &A, char C) {
auto *Ptr = reinterpret_cast<char *>(A.allocate(Sz));
memset(Ptr, C, Sz);
return Ptr;
}
void *operator new[](size_t Sz, BumpPtrAllocator &A) {
return A.allocate(Sz);
}
void *operator new[](size_t Sz, BumpPtrAllocator &A, char C) {
auto *Ptr = reinterpret_cast<char *>(A.allocate(Sz));
memset(Ptr, C, Sz);
return Ptr;
}
void operator delete(void *Ptr, BumpPtrAllocator &A) { A.deallocate(Ptr); }
namespace {
extern "C" bool __bolt_instr_conservative;
struct SimpleHashTableEntryBase {
uint64_t Key;
uint64_t Val;
void dump(const char *Msg = nullptr) {
char Buf[BufSize];
char *Ptr = Buf;
Ptr = intToStr(Ptr, __getpid(), 10);
*Ptr++ = ':';
*Ptr++ = ' ';
if (Msg)
Ptr = strCopy(Ptr, Msg, strLen(Msg));
*Ptr++ = '0';
*Ptr++ = 'x';
Ptr = intToStr(Ptr, (uint64_t)this, 16);
*Ptr++ = ':';
*Ptr++ = ' ';
Ptr = strCopy(Ptr, "MapEntry(0x", sizeof("MapEntry(0x") - 1);
Ptr = intToStr(Ptr, Key, 16);
*Ptr++ = ',';
*Ptr++ = ' ';
*Ptr++ = '0';
*Ptr++ = 'x';
Ptr = intToStr(Ptr, Val, 16);
*Ptr++ = ')';
*Ptr++ = '\n';
assert(Ptr - Buf < BufSize, "Buffer overflow!");
__write(2, Buf, Ptr - Buf);
}
};
template <typename T = SimpleHashTableEntryBase, uint32_t InitialSize = 7,
uint32_t IncSize = 7>
class SimpleHashTable {
public:
using MapEntry = T;
void incrementVal(uint64_t Key, BumpPtrAllocator &Alloc) {
if (!__bolt_instr_conservative) {
TryLock L(M);
if (!L.isLocked())
return;
auto &E = getOrAllocEntry(Key, Alloc);
++E.Val;
return;
}
Lock L(M);
auto &E = getOrAllocEntry(Key, Alloc);
++E.Val;
}
MapEntry &get(uint64_t Key, BumpPtrAllocator &Alloc) {
if (!__bolt_instr_conservative) {
TryLock L(M);
if (!L.isLocked())
return NoEntry;
return getOrAllocEntry(Key, Alloc);
}
Lock L(M);
return getOrAllocEntry(Key, Alloc);
}
template <typename... Args>
void forEachElement(void (*Callback)(MapEntry &, Args...), Args... args) {
Lock L(M);
if (!TableRoot)
return;
return forEachElement(Callback, InitialSize, TableRoot, args...);
}
void resetCounters();
private:
constexpr static uint64_t VacantMarker = 0;
constexpr static uint64_t FollowUpTableMarker = 0x8000000000000000ull;
MapEntry *TableRoot{nullptr};
MapEntry NoEntry;
Mutex M;
template <typename... Args>
void forEachElement(void (*Callback)(MapEntry &, Args...),
uint32_t NumEntries, MapEntry *Entries, Args... args) {
for (uint32_t I = 0; I < NumEntries; ++I) {
MapEntry &Entry = Entries[I];
if (Entry.Key == VacantMarker)
continue;
if (Entry.Key & FollowUpTableMarker) {
MapEntry *Next =
reinterpret_cast<MapEntry *>(Entry.Key & ~FollowUpTableMarker);
assert(Next != Entries, "Circular reference!");
forEachElement(Callback, IncSize, Next, args...);
continue;
}
Callback(Entry, args...);
}
}
MapEntry &firstAllocation(uint64_t Key, BumpPtrAllocator &Alloc) {
TableRoot = new (Alloc, 0) MapEntry[InitialSize];
MapEntry &Entry = TableRoot[Key % InitialSize];
Entry.Key = Key;
return Entry;
}
MapEntry &getEntry(MapEntry *Entries, uint64_t Key, uint64_t Selector,
BumpPtrAllocator &Alloc, int CurLevel) {
const uint32_t NumEntries = CurLevel == 0 ? InitialSize : IncSize;
uint64_t Remainder = Selector / NumEntries;
Selector = Selector % NumEntries;
MapEntry &Entry = Entries[Selector];
if (Entry.Key == Key) {
return Entry;
}
if (Entry.Key == VacantMarker) {
Entry.Key = Key;
return Entry;
}
if (Entry.Key & FollowUpTableMarker) {
return getEntry(
reinterpret_cast<MapEntry *>(Entry.Key & ~FollowUpTableMarker),
Key, Remainder, Alloc, CurLevel + 1);
}
MapEntry *NextLevelTbl = new (Alloc, 0) MapEntry[IncSize];
uint64_t CurEntrySelector = Entry.Key / InitialSize;
for (int I = 0; I < CurLevel; ++I)
CurEntrySelector /= IncSize;
CurEntrySelector = CurEntrySelector % IncSize;
NextLevelTbl[CurEntrySelector] = Entry;
Entry.Key = reinterpret_cast<uint64_t>(NextLevelTbl) | FollowUpTableMarker;
assert((NextLevelTbl[CurEntrySelector].Key & ~FollowUpTableMarker) !=
uint64_t(Entries),
"circular reference created!\n");
return getEntry(NextLevelTbl, Key, Remainder, Alloc, CurLevel + 1);
}
MapEntry &getOrAllocEntry(uint64_t Key, BumpPtrAllocator &Alloc) {
if (TableRoot) {
MapEntry &E = getEntry(TableRoot, Key, Key, Alloc, 0);
assert(!(E.Key & FollowUpTableMarker), "Invalid entry!");
return E;
}
return firstAllocation(Key, Alloc);
}
};
template <typename T> void resetIndCallCounter(T &Entry) {
Entry.Val = 0;
}
template <typename T, uint32_t X, uint32_t Y>
void SimpleHashTable<T, X, Y>::resetCounters() {
forEachElement(resetIndCallCounter);
}
using IndirectCallHashTable = SimpleHashTable<>;
IndirectCallHashTable *GlobalIndCallCounters{
reinterpret_cast<IndirectCallHashTable *>(1)};
Mutex *GlobalWriteProfileMutex{reinterpret_cast<Mutex *>(1)};
struct CallFlowEntryBase : public SimpleHashTableEntryBase {
uint64_t Calls;
};
using CallFlowHashTableBase = SimpleHashTable<CallFlowEntryBase, 11939, 233>;
class CallFlowHashTable : public CallFlowHashTableBase {
public:
CallFlowHashTable(BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
MapEntry &get(uint64_t Key) { return CallFlowHashTableBase::get(Key, Alloc); }
private:
BumpPtrAllocator &Alloc;
};
struct Location {
uint32_t FunctionName;
uint32_t Offset;
};
struct CallDescription {
Location From;
uint32_t FromNode;
Location To;
uint32_t Counter;
uint64_t TargetAddress;
};
using IndCallDescription = Location;
struct IndCallTargetDescription {
Location Loc;
uint64_t Address;
};
struct EdgeDescription {
Location From;
uint32_t FromNode;
Location To;
uint32_t ToNode;
uint32_t Counter;
};
struct InstrumentedNode {
uint32_t Node;
uint32_t Counter;
};
struct EntryNode {
uint64_t Node;
uint64_t Address;
};
struct FunctionDescription {
uint32_t NumLeafNodes;
const InstrumentedNode *LeafNodes;
uint32_t NumEdges;
const EdgeDescription *Edges;
uint32_t NumCalls;
const CallDescription *Calls;
uint32_t NumEntryNodes;
const EntryNode *EntryNodes;
FunctionDescription(const uint8_t *FuncDesc);
uint64_t getSize() const {
return 16 + NumLeafNodes * sizeof(InstrumentedNode) +
NumEdges * sizeof(EdgeDescription) +
NumCalls * sizeof(CallDescription) +
NumEntryNodes * sizeof(EntryNode);
}
};
struct ProfileWriterContext {
IndCallDescription *IndCallDescriptions;
IndCallTargetDescription *IndCallTargets;
uint8_t *FuncDescriptions;
char *Strings;
int FileDesc;
void *MMapPtr;
int MMapSize;
CallFlowHashTable *CallFlowTable;
const IndCallTargetDescription *lookupIndCallTarget(uint64_t Target) const;
};
int compareStr(const char *Str1, const char *Str2, int Size) {
while (*Str1 == *Str2) {
if (*Str1 == '\0' || --Size == 0)
return 0;
++Str1;
++Str2;
}
return 1;
}
char *serializeLoc(const ProfileWriterContext &Ctx, char *OutBuf,
const Location Loc, uint32_t BufSize) {
OutBuf = strCopy(OutBuf, "1 ");
const char *Str = Ctx.Strings + Loc.FunctionName;
uint32_t Size = 25;
while (*Str) {
*OutBuf++ = *Str++;
if (++Size >= BufSize)
break;
}
assert(!*Str, "buffer overflow, function name too large");
*OutBuf++ = ' ';
OutBuf = intToStr(OutBuf, Loc.Offset, 16);
*OutBuf++ = ' ';
return OutBuf;
}
FunctionDescription::FunctionDescription(const uint8_t *FuncDesc) {
NumLeafNodes = *reinterpret_cast<const uint32_t *>(FuncDesc);
DEBUG(reportNumber("NumLeafNodes = ", NumLeafNodes, 10));
LeafNodes = reinterpret_cast<const InstrumentedNode *>(FuncDesc + 4);
NumEdges = *reinterpret_cast<const uint32_t *>(
FuncDesc + 4 + NumLeafNodes * sizeof(InstrumentedNode));
DEBUG(reportNumber("NumEdges = ", NumEdges, 10));
Edges = reinterpret_cast<const EdgeDescription *>(
FuncDesc + 8 + NumLeafNodes * sizeof(InstrumentedNode));
NumCalls = *reinterpret_cast<const uint32_t *>(
FuncDesc + 8 + NumLeafNodes * sizeof(InstrumentedNode) +
NumEdges * sizeof(EdgeDescription));
DEBUG(reportNumber("NumCalls = ", NumCalls, 10));
Calls = reinterpret_cast<const CallDescription *>(
FuncDesc + 12 + NumLeafNodes * sizeof(InstrumentedNode) +
NumEdges * sizeof(EdgeDescription));
NumEntryNodes = *reinterpret_cast<const uint32_t *>(
FuncDesc + 12 + NumLeafNodes * sizeof(InstrumentedNode) +
NumEdges * sizeof(EdgeDescription) + NumCalls * sizeof(CallDescription));
DEBUG(reportNumber("NumEntryNodes = ", NumEntryNodes, 10));
EntryNodes = reinterpret_cast<const EntryNode *>(
FuncDesc + 16 + NumLeafNodes * sizeof(InstrumentedNode) +
NumEdges * sizeof(EdgeDescription) + NumCalls * sizeof(CallDescription));
}
#if defined(HAVE_ELF_H) and !defined(__APPLE__)
void *__attribute__((noinline)) __get_pc() {
return __builtin_extract_return_addr(__builtin_return_address(0));
}
bool parseAddressRange(const char *Str, uint64_t &StartAddress,
uint64_t &EndAddress) {
if (!Str)
return false;
StartAddress = hexToLong(Str, '-');
while (*Str && *Str != '-')
++Str;
if (!*Str)
return false;
++Str;
EndAddress = hexToLong(Str);
return true;
}
static char *getBinaryPath() {
const uint32_t BufSize = 1024;
const uint32_t NameMax = 4096;
const char DirPath[] = "/proc/self/map_files/";
static char TargetPath[NameMax] = {};
char Buf[BufSize];
if (__bolt_instr_binpath[0] != '\0')
return __bolt_instr_binpath;
if (TargetPath[0] != '\0')
return TargetPath;
unsigned long CurAddr = (unsigned long)__get_pc();
uint64_t FDdir = __open(DirPath, O_RDONLY,
0666);
assert(static_cast<int64_t>(FDdir) >= 0,
"failed to open /proc/self/map_files");
while (long Nread = __getdents64(FDdir, (struct dirent64 *)Buf, BufSize)) {
assert(static_cast<int64_t>(Nread) != -1, "failed to get folder entries");
struct dirent64 *d;
for (long Bpos = 0; Bpos < Nread; Bpos += d->d_reclen) {
d = (struct dirent64 *)(Buf + Bpos);
uint64_t StartAddress, EndAddress;
if (!parseAddressRange(d->d_name, StartAddress, EndAddress))
continue;
if (CurAddr < StartAddress || CurAddr > EndAddress)
continue;
char FindBuf[NameMax];
char *C = strCopy(FindBuf, DirPath, NameMax);
C = strCopy(C, d->d_name, NameMax - (C - FindBuf));
*C = '\0';
uint32_t Ret = __readlink(FindBuf, TargetPath, sizeof(TargetPath));
assert(Ret != -1 && Ret != BufSize, "readlink error");
TargetPath[Ret] = '\0';
return TargetPath;
}
}
return nullptr;
}
ProfileWriterContext readDescriptions() {
ProfileWriterContext Result;
char *BinPath = getBinaryPath();
assert(BinPath && BinPath[0] != '\0', "failed to find binary path");
uint64_t FD = __open(BinPath, O_RDONLY,
0666);
assert(static_cast<int64_t>(FD) >= 0, "failed to open binary path");
Result.FileDesc = FD;
uint64_t Size = __lseek(FD, 0, SEEK_END);
uint8_t *BinContents = reinterpret_cast<uint8_t *>(
__mmap(0, Size, PROT_READ, MAP_PRIVATE, FD, 0));
assert(BinContents != MAP_FAILED, "readDescriptions: Failed to mmap self!");
Result.MMapPtr = BinContents;
Result.MMapSize = Size;
Elf64_Ehdr *Hdr = reinterpret_cast<Elf64_Ehdr *>(BinContents);
Elf64_Shdr *Shdr = reinterpret_cast<Elf64_Shdr *>(BinContents + Hdr->e_shoff);
Elf64_Shdr *StringTblHeader = reinterpret_cast<Elf64_Shdr *>(
BinContents + Hdr->e_shoff + Hdr->e_shstrndx * Hdr->e_shentsize);
for (int I = 0; I < Hdr->e_shnum; ++I) {
char *SecName = reinterpret_cast<char *>(
BinContents + StringTblHeader->sh_offset + Shdr->sh_name);
if (compareStr(SecName, ".bolt.instr.tables", 64) != 0) {
Shdr = reinterpret_cast<Elf64_Shdr *>(BinContents + Hdr->e_shoff +
(I + 1) * Hdr->e_shentsize);
continue;
}
uint32_t IndCallDescSize =
*reinterpret_cast<uint32_t *>(BinContents + Shdr->sh_offset + 20);
uint32_t IndCallTargetDescSize = *reinterpret_cast<uint32_t *>(
BinContents + Shdr->sh_offset + 24 + IndCallDescSize);
uint32_t FuncDescSize =
*reinterpret_cast<uint32_t *>(BinContents + Shdr->sh_offset + 28 +
IndCallDescSize + IndCallTargetDescSize);
Result.IndCallDescriptions = reinterpret_cast<IndCallDescription *>(
BinContents + Shdr->sh_offset + 24);
Result.IndCallTargets = reinterpret_cast<IndCallTargetDescription *>(
BinContents + Shdr->sh_offset + 28 + IndCallDescSize);
Result.FuncDescriptions = BinContents + Shdr->sh_offset + 32 +
IndCallDescSize + IndCallTargetDescSize;
Result.Strings = reinterpret_cast<char *>(
BinContents + Shdr->sh_offset + 32 + IndCallDescSize +
IndCallTargetDescSize + FuncDescSize);
return Result;
}
const char ErrMsg[] =
"BOLT instrumentation runtime error: could not find section "
".bolt.instr.tables\n";
reportError(ErrMsg, sizeof(ErrMsg));
return Result;
}
#else
ProfileWriterContext readDescriptions() {
ProfileWriterContext Result;
uint8_t *Tables = _bolt_instr_tables_getter();
uint32_t IndCallDescSize = *reinterpret_cast<uint32_t *>(Tables);
uint32_t IndCallTargetDescSize =
*reinterpret_cast<uint32_t *>(Tables + 4 + IndCallDescSize);
uint32_t FuncDescSize = *reinterpret_cast<uint32_t *>(
Tables + 8 + IndCallDescSize + IndCallTargetDescSize);
Result.IndCallDescriptions =
reinterpret_cast<IndCallDescription *>(Tables + 4);
Result.IndCallTargets = reinterpret_cast<IndCallTargetDescription *>(
Tables + 8 + IndCallDescSize);
Result.FuncDescriptions =
Tables + 12 + IndCallDescSize + IndCallTargetDescSize;
Result.Strings = reinterpret_cast<char *>(
Tables + 12 + IndCallDescSize + IndCallTargetDescSize + FuncDescSize);
return Result;
}
#endif
#if !defined(__APPLE__)
void printStats(const ProfileWriterContext &Ctx) {
char StatMsg[BufSize];
char *StatPtr = StatMsg;
StatPtr =
strCopy(StatPtr,
"\nBOLT INSTRUMENTATION RUNTIME STATISTICS\n\nIndCallDescSize: ");
StatPtr = intToStr(StatPtr,
Ctx.FuncDescriptions -
reinterpret_cast<uint8_t *>(Ctx.IndCallDescriptions),
10);
StatPtr = strCopy(StatPtr, "\nFuncDescSize: ");
StatPtr = intToStr(
StatPtr,
reinterpret_cast<uint8_t *>(Ctx.Strings) - Ctx.FuncDescriptions, 10);
StatPtr = strCopy(StatPtr, "\n__bolt_instr_num_ind_calls: ");
StatPtr = intToStr(StatPtr, __bolt_instr_num_ind_calls, 10);
StatPtr = strCopy(StatPtr, "\n__bolt_instr_num_funcs: ");
StatPtr = intToStr(StatPtr, __bolt_instr_num_funcs, 10);
StatPtr = strCopy(StatPtr, "\n");
__write(2, StatMsg, StatPtr - StatMsg);
}
#endif
struct Edge {
uint32_t Node;
uint32_t ID;
};
struct Node {
uint32_t NumInEdges{0};
uint32_t NumOutEdges{0};
Edge *InEdges{nullptr};
Edge *OutEdges{nullptr};
};
struct Graph {
uint32_t NumNodes;
Node *CFGNodes;
Node *SpanningTreeNodes;
uint64_t *EdgeFreqs;
uint64_t *CallFreqs;
BumpPtrAllocator &Alloc;
const FunctionDescription &D;
Graph(BumpPtrAllocator &Alloc, const FunctionDescription &D,
const uint64_t *Counters, ProfileWriterContext &Ctx);
~Graph();
void dump() const;
private:
void computeEdgeFrequencies(const uint64_t *Counters,
ProfileWriterContext &Ctx);
void dumpEdgeFreqs() const;
};
Graph::Graph(BumpPtrAllocator &Alloc, const FunctionDescription &D,
const uint64_t *Counters, ProfileWriterContext &Ctx)
: Alloc(Alloc), D(D) {
DEBUG(reportNumber("G = 0x", (uint64_t)this, 16));
int32_t MaxNodes = -1;
CallFreqs = nullptr;
EdgeFreqs = nullptr;
for (int I = 0; I < D.NumEdges; ++I) {
if (static_cast<int32_t>(D.Edges[I].FromNode) > MaxNodes)
MaxNodes = D.Edges[I].FromNode;
if (static_cast<int32_t>(D.Edges[I].ToNode) > MaxNodes)
MaxNodes = D.Edges[I].ToNode;
}
for (int I = 0; I < D.NumLeafNodes; ++I)
if (static_cast<int32_t>(D.LeafNodes[I].Node) > MaxNodes)
MaxNodes = D.LeafNodes[I].Node;
for (int I = 0; I < D.NumCalls; ++I)
if (static_cast<int32_t>(D.Calls[I].FromNode) > MaxNodes)
MaxNodes = D.Calls[I].FromNode;
if (MaxNodes < 0) {
DEBUG(report("No nodes!\n"));
CFGNodes = nullptr;
SpanningTreeNodes = nullptr;
NumNodes = 0;
return;
}
++MaxNodes;
DEBUG(reportNumber("NumNodes = ", MaxNodes, 10));
NumNodes = static_cast<uint32_t>(MaxNodes);
CFGNodes = new (Alloc) Node[MaxNodes];
DEBUG(reportNumber("G->CFGNodes = 0x", (uint64_t)CFGNodes, 16));
SpanningTreeNodes = new (Alloc) Node[MaxNodes];
DEBUG(reportNumber("G->SpanningTreeNodes = 0x",
(uint64_t)SpanningTreeNodes, 16));
for (int I = 0; I < D.NumEdges; ++I) {
CFGNodes[D.Edges[I].FromNode].NumOutEdges++;
CFGNodes[D.Edges[I].ToNode].NumInEdges++;
if (D.Edges[I].Counter != 0xffffffff)
continue;
SpanningTreeNodes[D.Edges[I].FromNode].NumOutEdges++;
SpanningTreeNodes[D.Edges[I].ToNode].NumInEdges++;
}
for (int I = 0; I < MaxNodes; ++I) {
if (CFGNodes[I].NumInEdges > 0)
CFGNodes[I].InEdges = new (Alloc) Edge[CFGNodes[I].NumInEdges];
if (CFGNodes[I].NumOutEdges > 0)
CFGNodes[I].OutEdges = new (Alloc) Edge[CFGNodes[I].NumOutEdges];
if (SpanningTreeNodes[I].NumInEdges > 0)
SpanningTreeNodes[I].InEdges =
new (Alloc) Edge[SpanningTreeNodes[I].NumInEdges];
if (SpanningTreeNodes[I].NumOutEdges > 0)
SpanningTreeNodes[I].OutEdges =
new (Alloc) Edge[SpanningTreeNodes[I].NumOutEdges];
CFGNodes[I].NumInEdges = 0;
CFGNodes[I].NumOutEdges = 0;
SpanningTreeNodes[I].NumInEdges = 0;
SpanningTreeNodes[I].NumOutEdges = 0;
}
for (int I = 0; I < D.NumEdges; ++I) {
const uint32_t Src = D.Edges[I].FromNode;
const uint32_t Dst = D.Edges[I].ToNode;
Edge *E = &CFGNodes[Src].OutEdges[CFGNodes[Src].NumOutEdges++];
E->Node = Dst;
E->ID = I;
E = &CFGNodes[Dst].InEdges[CFGNodes[Dst].NumInEdges++];
E->Node = Src;
E->ID = I;
if (D.Edges[I].Counter != 0xffffffff)
continue;
E = &SpanningTreeNodes[Src]
.OutEdges[SpanningTreeNodes[Src].NumOutEdges++];
E->Node = Dst;
E->ID = I;
E = &SpanningTreeNodes[Dst]
.InEdges[SpanningTreeNodes[Dst].NumInEdges++];
E->Node = Src;
E->ID = I;
}
computeEdgeFrequencies(Counters, Ctx);
}
Graph::~Graph() {
if (CallFreqs)
Alloc.deallocate(CallFreqs);
if (EdgeFreqs)
Alloc.deallocate(EdgeFreqs);
for (int I = NumNodes - 1; I >= 0; --I) {
if (SpanningTreeNodes[I].OutEdges)
Alloc.deallocate(SpanningTreeNodes[I].OutEdges);
if (SpanningTreeNodes[I].InEdges)
Alloc.deallocate(SpanningTreeNodes[I].InEdges);
if (CFGNodes[I].OutEdges)
Alloc.deallocate(CFGNodes[I].OutEdges);
if (CFGNodes[I].InEdges)
Alloc.deallocate(CFGNodes[I].InEdges);
}
if (SpanningTreeNodes)
Alloc.deallocate(SpanningTreeNodes);
if (CFGNodes)
Alloc.deallocate(CFGNodes);
}
void Graph::dump() const {
reportNumber("Dumping graph with number of nodes: ", NumNodes, 10);
report(" Full graph:\n");
for (int I = 0; I < NumNodes; ++I) {
const Node *N = &CFGNodes[I];
reportNumber(" Node #", I, 10);
reportNumber(" InEdges total ", N->NumInEdges, 10);
for (int J = 0; J < N->NumInEdges; ++J)
reportNumber(" ", N->InEdges[J].Node, 10);
reportNumber(" OutEdges total ", N->NumOutEdges, 10);
for (int J = 0; J < N->NumOutEdges; ++J)
reportNumber(" ", N->OutEdges[J].Node, 10);
report("\n");
}
report(" Spanning tree:\n");
for (int I = 0; I < NumNodes; ++I) {
const Node *N = &SpanningTreeNodes[I];
reportNumber(" Node #", I, 10);
reportNumber(" InEdges total ", N->NumInEdges, 10);
for (int J = 0; J < N->NumInEdges; ++J)
reportNumber(" ", N->InEdges[J].Node, 10);
reportNumber(" OutEdges total ", N->NumOutEdges, 10);
for (int J = 0; J < N->NumOutEdges; ++J)
reportNumber(" ", N->OutEdges[J].Node, 10);
report("\n");
}
}
void Graph::dumpEdgeFreqs() const {
reportNumber(
"Dumping edge frequencies for graph with num edges: ", D.NumEdges, 10);
for (int I = 0; I < D.NumEdges; ++I) {
reportNumber("* Src: ", D.Edges[I].FromNode, 10);
reportNumber(" Dst: ", D.Edges[I].ToNode, 10);
reportNumber(" Cnt: ", EdgeFreqs[I], 10);
}
}
struct NodeToCallsMap {
struct MapEntry {
uint32_t NumCalls;
uint32_t *Calls;
};
MapEntry *Entries;
BumpPtrAllocator &Alloc;
const uint32_t NumNodes;
NodeToCallsMap(BumpPtrAllocator &Alloc, const FunctionDescription &D,
uint32_t NumNodes)
: Alloc(Alloc), NumNodes(NumNodes) {
Entries = new (Alloc, 0) MapEntry[NumNodes];
for (int I = 0; I < D.NumCalls; ++I) {
DEBUG(reportNumber("Registering call in node ", D.Calls[I].FromNode, 10));
++Entries[D.Calls[I].FromNode].NumCalls;
}
for (int I = 0; I < NumNodes; ++I) {
Entries[I].Calls = Entries[I].NumCalls ? new (Alloc)
uint32_t[Entries[I].NumCalls]
: nullptr;
Entries[I].NumCalls = 0;
}
for (int I = 0; I < D.NumCalls; ++I) {
MapEntry &Entry = Entries[D.Calls[I].FromNode];
Entry.Calls[Entry.NumCalls++] = I;
}
}
uint64_t visitAllCallsIn(uint32_t NodeID, uint64_t Freq, uint64_t *CallFreqs,
const FunctionDescription &D,
const uint64_t *Counters,
ProfileWriterContext &Ctx) const {
const MapEntry &Entry = Entries[NodeID];
uint64_t MaxValue = 0ull;
for (int I = 0, E = Entry.NumCalls; I != E; ++I) {
const uint32_t CallID = Entry.Calls[I];
DEBUG(reportNumber(" Setting freq for call ID: ", CallID, 10));
const CallDescription &CallDesc = D.Calls[CallID];
if (CallDesc.Counter == 0xffffffff) {
CallFreqs[CallID] = Freq;
DEBUG(reportNumber(" with : ", Freq, 10));
} else {
const uint64_t CounterVal = Counters[CallDesc.Counter];
CallFreqs[CallID] = CounterVal;
MaxValue = CounterVal > MaxValue ? CounterVal : MaxValue;
DEBUG(reportNumber(" with (private counter) : ", CounterVal, 10));
}
DEBUG(reportNumber(" Address: 0x", CallDesc.TargetAddress, 16));
if (CallFreqs[CallID] > 0)
Ctx.CallFlowTable->get(CallDesc.TargetAddress).Calls +=
CallFreqs[CallID];
}
return MaxValue;
}
~NodeToCallsMap() {
for (int I = NumNodes - 1; I >= 0; --I)
if (Entries[I].Calls)
Alloc.deallocate(Entries[I].Calls);
Alloc.deallocate(Entries);
}
};
void Graph::computeEdgeFrequencies(const uint64_t *Counters,
ProfileWriterContext &Ctx) {
if (NumNodes == 0)
return;
EdgeFreqs = D.NumEdges ? new (Alloc, 0) uint64_t [D.NumEdges] : nullptr;
CallFreqs = D.NumCalls ? new (Alloc, 0) uint64_t [D.NumCalls] : nullptr;
NodeToCallsMap *CallMap = new (Alloc) NodeToCallsMap(Alloc, D, NumNodes);
uint32_t *Stack = new (Alloc) uint32_t [NumNodes];
uint32_t StackTop = 0;
enum Status : uint8_t { S_NEW = 0, S_VISITING, S_VISITED };
Status *Visited = new (Alloc, 0) Status[NumNodes];
uint64_t *LeafFrequency = new (Alloc, 0) uint64_t[NumNodes];
uint64_t *EntryAddress = new (Alloc, 0) uint64_t[NumNodes];
for (int I = 0; I < D.NumLeafNodes; ++I) {
LeafFrequency[D.LeafNodes[I].Node] = Counters[D.LeafNodes[I].Counter];
DEBUG({
if (Counters[D.LeafNodes[I].Counter] > 0) {
reportNumber("Leaf Node# ", D.LeafNodes[I].Node, 10);
reportNumber(" Counter: ", Counters[D.LeafNodes[I].Counter], 10);
}
});
}
for (int I = 0; I < D.NumEntryNodes; ++I) {
EntryAddress[D.EntryNodes[I].Node] = D.EntryNodes[I].Address;
DEBUG({
reportNumber("Entry Node# ", D.EntryNodes[I].Node, 10);
reportNumber(" Address: ", D.EntryNodes[I].Address, 16);
});
}
for (int I = 0; I < NumNodes; ++I)
if (SpanningTreeNodes[I].NumInEdges == 0)
Stack[StackTop++] = I;
if (StackTop == 0) {
DEBUG(report("Empty stack!\n"));
Alloc.deallocate(EntryAddress);
Alloc.deallocate(LeafFrequency);
Alloc.deallocate(Visited);
Alloc.deallocate(Stack);
CallMap->~NodeToCallsMap();
Alloc.deallocate(CallMap);
if (CallFreqs)
Alloc.deallocate(CallFreqs);
if (EdgeFreqs)
Alloc.deallocate(EdgeFreqs);
EdgeFreqs = nullptr;
CallFreqs = nullptr;
return;
}
for (int I = 0; I < D.NumEdges; ++I) {
const uint32_t C = D.Edges[I].Counter;
if (C == 0xffffffff)
continue;
EdgeFreqs[I] = Counters[C];
}
while (StackTop > 0) {
const uint32_t Cur = Stack[--StackTop];
DEBUG({
if (Visited[Cur] == S_VISITING)
report("(visiting) ");
else
report("(new) ");
reportNumber("Cur: ", Cur, 10);
});
assert(Visited[Cur] != S_VISITED, "should not have visited nodes in stack");
if (Visited[Cur] == S_NEW) {
Visited[Cur] = S_VISITING;
Stack[StackTop++] = Cur;
assert(StackTop <= NumNodes, "stack grew too large");
for (int I = 0, E = SpanningTreeNodes[Cur].NumOutEdges; I < E; ++I) {
const uint32_t Succ = SpanningTreeNodes[Cur].OutEdges[I].Node;
Stack[StackTop++] = Succ;
assert(StackTop <= NumNodes, "stack grew too large");
}
continue;
}
Visited[Cur] = S_VISITED;
int64_t CurNodeFreq = LeafFrequency[Cur];
if (!CurNodeFreq) {
for (int I = 0, E = CFGNodes[Cur].NumOutEdges; I != E; ++I) {
const uint32_t SuccEdge = CFGNodes[Cur].OutEdges[I].ID;
CurNodeFreq += EdgeFreqs[SuccEdge];
}
}
if (CurNodeFreq < 0)
CurNodeFreq = 0;
const uint64_t CallFreq = CallMap->visitAllCallsIn(
Cur, CurNodeFreq > 0 ? CurNodeFreq : 0, CallFreqs, D, Counters, Ctx);
DEBUG({
if (CallFreq > CurNodeFreq)
report("Bumping node frequency with call info\n");
});
CurNodeFreq = CallFreq > CurNodeFreq ? CallFreq : CurNodeFreq;
if (CurNodeFreq > 0) {
if (uint64_t Addr = EntryAddress[Cur]) {
DEBUG(
reportNumber(" Setting flow at entry point address 0x", Addr, 16));
DEBUG(reportNumber(" with: ", CurNodeFreq, 10));
Ctx.CallFlowTable->get(Addr).Val = CurNodeFreq;
}
}
if (SpanningTreeNodes[Cur].NumInEdges == 0)
continue;
assert(SpanningTreeNodes[Cur].NumInEdges == 1, "must have 1 parent");
const uint32_t ParentEdge = SpanningTreeNodes[Cur].InEdges[0].ID;
int64_t ParentEdgeFreq = CurNodeFreq;
for (int I = 0, E = CFGNodes[Cur].NumInEdges; I != E; ++I) {
const uint32_t PredEdge = CFGNodes[Cur].InEdges[I].ID;
ParentEdgeFreq -= EdgeFreqs[PredEdge];
}
if (ParentEdgeFreq < 0) {
DEBUG(dumpEdgeFreqs());
DEBUG(report("WARNING: incorrect flow"));
ParentEdgeFreq = 0;
}
DEBUG(reportNumber(" Setting freq for ParentEdge: ", ParentEdge, 10));
DEBUG(reportNumber(" with ParentEdgeFreq: ", ParentEdgeFreq, 10));
EdgeFreqs[ParentEdge] = ParentEdgeFreq;
}
Alloc.deallocate(EntryAddress);
Alloc.deallocate(LeafFrequency);
Alloc.deallocate(Visited);
Alloc.deallocate(Stack);
CallMap->~NodeToCallsMap();
Alloc.deallocate(CallMap);
DEBUG(dumpEdgeFreqs());
}
const uint8_t *writeFunctionProfile(int FD, ProfileWriterContext &Ctx,
const uint8_t *FuncDesc,
BumpPtrAllocator &Alloc) {
const FunctionDescription F(FuncDesc);
const uint8_t *next = FuncDesc + F.getSize();
#if !defined(__APPLE__)
uint64_t *bolt_instr_locations = __bolt_instr_locations;
#else
uint64_t *bolt_instr_locations = _bolt_instr_locations_getter();
#endif
#ifndef ENABLE_DEBUG
uint64_t CountersFreq = 0;
for (int I = 0; I < F.NumLeafNodes; ++I)
CountersFreq += bolt_instr_locations[F.LeafNodes[I].Counter];
if (CountersFreq == 0) {
for (int I = 0; I < F.NumEdges; ++I) {
const uint32_t C = F.Edges[I].Counter;
if (C == 0xffffffff)
continue;
CountersFreq += bolt_instr_locations[C];
}
if (CountersFreq == 0) {
for (int I = 0; I < F.NumCalls; ++I) {
const uint32_t C = F.Calls[I].Counter;
if (C == 0xffffffff)
continue;
CountersFreq += bolt_instr_locations[C];
}
if (CountersFreq == 0)
return next;
}
}
#endif
Graph *G = new (Alloc) Graph(Alloc, F, bolt_instr_locations, Ctx);
DEBUG(G->dump());
if (!G->EdgeFreqs && !G->CallFreqs) {
G->~Graph();
Alloc.deallocate(G);
return next;
}
for (int I = 0; I < F.NumEdges; ++I) {
const uint64_t Freq = G->EdgeFreqs[I];
if (Freq == 0)
continue;
const EdgeDescription *Desc = &F.Edges[I];
char LineBuf[BufSize];
char *Ptr = LineBuf;
Ptr = serializeLoc(Ctx, Ptr, Desc->From, BufSize);
Ptr = serializeLoc(Ctx, Ptr, Desc->To, BufSize - (Ptr - LineBuf));
Ptr = strCopy(Ptr, "0 ", BufSize - (Ptr - LineBuf) - 22);
Ptr = intToStr(Ptr, Freq, 10);
*Ptr++ = '\n';
__write(FD, LineBuf, Ptr - LineBuf);
}
for (int I = 0; I < F.NumCalls; ++I) {
const uint64_t Freq = G->CallFreqs[I];
if (Freq == 0)
continue;
char LineBuf[BufSize];
char *Ptr = LineBuf;
const CallDescription *Desc = &F.Calls[I];
Ptr = serializeLoc(Ctx, Ptr, Desc->From, BufSize);
Ptr = serializeLoc(Ctx, Ptr, Desc->To, BufSize - (Ptr - LineBuf));
Ptr = strCopy(Ptr, "0 ", BufSize - (Ptr - LineBuf) - 25);
Ptr = intToStr(Ptr, Freq, 10);
*Ptr++ = '\n';
__write(FD, LineBuf, Ptr - LineBuf);
}
G->~Graph();
Alloc.deallocate(G);
return next;
}
#if !defined(__APPLE__)
const IndCallTargetDescription *
ProfileWriterContext::lookupIndCallTarget(uint64_t Target) const {
uint32_t B = 0;
uint32_t E = __bolt_instr_num_ind_targets;
if (E == 0)
return nullptr;
do {
uint32_t I = (E - B) / 2 + B;
if (IndCallTargets[I].Address == Target)
return &IndCallTargets[I];
if (IndCallTargets[I].Address < Target)
B = I + 1;
else
E = I;
} while (B < E);
return nullptr;
}
void visitIndCallCounter(IndirectCallHashTable::MapEntry &Entry,
int FD, int CallsiteID,
ProfileWriterContext *Ctx) {
if (Entry.Val == 0)
return;
DEBUG(reportNumber("Target func 0x", Entry.Key, 16));
DEBUG(reportNumber("Target freq: ", Entry.Val, 10));
const IndCallDescription *CallsiteDesc =
&Ctx->IndCallDescriptions[CallsiteID];
const IndCallTargetDescription *TargetDesc =
Ctx->lookupIndCallTarget(Entry.Key - TextBaseAddress);
if (!TargetDesc) {
DEBUG(report("Failed to lookup indirect call target\n"));
char LineBuf[BufSize];
char *Ptr = LineBuf;
Ptr = serializeLoc(*Ctx, Ptr, *CallsiteDesc, BufSize);
Ptr = strCopy(Ptr, "0 [unknown] 0 0 ", BufSize - (Ptr - LineBuf) - 40);
Ptr = intToStr(Ptr, Entry.Val, 10);
*Ptr++ = '\n';
__write(FD, LineBuf, Ptr - LineBuf);
return;
}
Ctx->CallFlowTable->get(TargetDesc->Address).Calls += Entry.Val;
char LineBuf[BufSize];
char *Ptr = LineBuf;
Ptr = serializeLoc(*Ctx, Ptr, *CallsiteDesc, BufSize);
Ptr = serializeLoc(*Ctx, Ptr, TargetDesc->Loc, BufSize - (Ptr - LineBuf));
Ptr = strCopy(Ptr, "0 ", BufSize - (Ptr - LineBuf) - 25);
Ptr = intToStr(Ptr, Entry.Val, 10);
*Ptr++ = '\n';
__write(FD, LineBuf, Ptr - LineBuf);
}
void writeIndirectCallProfile(int FD, ProfileWriterContext &Ctx) {
for (int I = 0; I < __bolt_instr_num_ind_calls; ++I) {
DEBUG(reportNumber("IndCallsite #", I, 10));
GlobalIndCallCounters[I].forEachElement(visitIndCallCounter, FD, I, &Ctx);
}
}
void visitCallFlowEntry(CallFlowHashTable::MapEntry &Entry, int FD,
ProfileWriterContext *Ctx) {
DEBUG(reportNumber("Call flow entry address: 0x", Entry.Key, 16));
DEBUG(reportNumber("Calls: ", Entry.Calls, 10));
DEBUG(reportNumber("Reported entry frequency: ", Entry.Val, 10));
DEBUG({
if (Entry.Calls > Entry.Val)
report(" More calls than expected!\n");
});
if (Entry.Val <= Entry.Calls)
return;
DEBUG(reportNumber(
" Balancing calls with traffic: ", Entry.Val - Entry.Calls, 10));
const IndCallTargetDescription *TargetDesc =
Ctx->lookupIndCallTarget(Entry.Key);
if (!TargetDesc) {
DEBUG(report("WARNING: failed to look up call target!\n"));
return;
}
char LineBuf[BufSize];
char *Ptr = LineBuf;
Ptr = strCopy(Ptr, "0 [unknown] 0 ", BufSize);
Ptr = serializeLoc(*Ctx, Ptr, TargetDesc->Loc, BufSize - (Ptr - LineBuf));
Ptr = strCopy(Ptr, "0 ", BufSize - (Ptr - LineBuf) - 25);
Ptr = intToStr(Ptr, Entry.Val - Entry.Calls, 10);
*Ptr++ = '\n';
__write(FD, LineBuf, Ptr - LineBuf);
}
int openProfile() {
char Buf[BufSize];
uint64_t PID = __getpid();
char *Ptr = strCopy(Buf, __bolt_instr_filename, BufSize);
if (__bolt_instr_use_pid) {
Ptr = strCopy(Ptr, ".", BufSize - (Ptr - Buf + 1));
Ptr = intToStr(Ptr, PID, 10);
Ptr = strCopy(Ptr, ".fdata", BufSize - (Ptr - Buf + 1));
}
*Ptr++ = '\0';
uint64_t FD = __open(Buf, O_WRONLY | O_TRUNC | O_CREAT,
0666);
if (static_cast<int64_t>(FD) < 0) {
report("Error while trying to open profile file for writing: ");
report(Buf);
reportNumber("\nFailed with error number: 0x",
0 - static_cast<int64_t>(FD), 16);
__exit(1);
}
return FD;
}
#endif
}
#if !defined(__APPLE__)
/// -ex 'set confirm off' -ex quit
extern "C" void __bolt_instr_clear_counters() {
memset(reinterpret_cast<char *>(__bolt_instr_locations), 0,
__bolt_num_counters * 8);
for (int I = 0; I < __bolt_instr_num_ind_calls; ++I)
GlobalIndCallCounters[I].resetCounters();
}
extern "C" void __attribute((force_align_arg_pointer))
__bolt_instr_data_dump(int FD) {
if (!GlobalWriteProfileMutex->acquire())
return;
int ret = __lseek(FD, 0, SEEK_SET);
assert(ret == 0, "Failed to lseek!");
ret = __ftruncate(FD, 0);
assert(ret == 0, "Failed to ftruncate!");
BumpPtrAllocator HashAlloc;
HashAlloc.setMaxSize(0x6400000);
ProfileWriterContext Ctx = readDescriptions();
Ctx.CallFlowTable = new (HashAlloc, 0) CallFlowHashTable(HashAlloc);
DEBUG(printStats(Ctx));
BumpPtrAllocator Alloc;
Alloc.setMaxSize(0x6400000);
const uint8_t *FuncDesc = Ctx.FuncDescriptions;
for (int I = 0, E = __bolt_instr_num_funcs; I < E; ++I) {
FuncDesc = writeFunctionProfile(FD, Ctx, FuncDesc, Alloc);
Alloc.clear();
DEBUG(reportNumber("FuncDesc now: ", (uint64_t)FuncDesc, 16));
}
assert(FuncDesc == (void *)Ctx.Strings,
"FuncDesc ptr must be equal to stringtable");
writeIndirectCallProfile(FD, Ctx);
Ctx.CallFlowTable->forEachElement(visitCallFlowEntry, FD, &Ctx);
__fsync(FD);
__munmap(Ctx.MMapPtr, Ctx.MMapSize);
__close(Ctx.FileDesc);
HashAlloc.destroy();
GlobalWriteProfileMutex->release();
DEBUG(report("Finished writing profile.\n"));
}
void watchProcess() {
timespec ts, rem;
uint64_t Ellapsed = 0ull;
int FD = openProfile();
uint64_t ppid;
if (__bolt_instr_wait_forks) {
ppid = -__getpgid(0);
__setpgid(0, 0);
} else {
ppid = __getppid();
if (ppid == 1) {
__bolt_instr_data_dump(FD);
goto out;
}
}
ts.tv_sec = 1;
ts.tv_nsec = 0;
while (1) {
__nanosleep(&ts, &rem);
if (__kill(ppid, 0) < 0) {
if (__bolt_instr_no_counters_clear)
__bolt_instr_data_dump(FD);
break;
}
if (++Ellapsed < __bolt_instr_sleep_time)
continue;
Ellapsed = 0;
__bolt_instr_data_dump(FD);
if (__bolt_instr_no_counters_clear == false)
__bolt_instr_clear_counters();
}
out:;
DEBUG(report("My parent process is dead, bye!\n"));
__close(FD);
__exit(0);
}
extern "C" void __bolt_instr_indirect_call();
extern "C" void __bolt_instr_indirect_tailcall();
extern "C" void __attribute((force_align_arg_pointer)) __bolt_instr_setup() {
__bolt_ind_call_counter_func_pointer = __bolt_instr_indirect_call;
__bolt_ind_tailcall_counter_func_pointer = __bolt_instr_indirect_tailcall;
TextBaseAddress = getTextBaseAddress();
const uint64_t CountersStart =
reinterpret_cast<uint64_t>(&__bolt_instr_locations[0]);
const uint64_t CountersEnd = alignTo(
reinterpret_cast<uint64_t>(&__bolt_instr_locations[__bolt_num_counters]),
0x1000);
DEBUG(reportNumber("replace mmap start: ", CountersStart, 16));
DEBUG(reportNumber("replace mmap stop: ", CountersEnd, 16));
assert(CountersEnd > CountersStart, "no counters");
const bool Shared = !__bolt_instr_use_pid;
const uint64_t MapPrivateOrShared = Shared ? MAP_SHARED : MAP_PRIVATE;
void *Ret =
__mmap(CountersStart, CountersEnd - CountersStart, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MapPrivateOrShared | MAP_FIXED, -1, 0);
assert(Ret != MAP_FAILED, "__bolt_instr_setup: Failed to mmap counters!");
GlobalMetadataStorage = __mmap(0, 4096, PROT_READ | PROT_WRITE,
MapPrivateOrShared | MAP_ANONYMOUS, -1, 0);
assert(GlobalMetadataStorage != MAP_FAILED,
"__bolt_instr_setup: failed to mmap page for metadata!");
GlobalAlloc = new (GlobalMetadataStorage) BumpPtrAllocator;
GlobalAlloc->setMaxSize(0x6400000);
GlobalAlloc->setShared(Shared);
GlobalWriteProfileMutex = new (*GlobalAlloc, 0) Mutex();
if (__bolt_instr_num_ind_calls > 0)
GlobalIndCallCounters =
new (*GlobalAlloc, 0) IndirectCallHashTable[__bolt_instr_num_ind_calls];
if (__bolt_instr_sleep_time != 0) {
if (__bolt_instr_wait_forks)
__setpgid(0, 0);
if (long PID = __fork())
return;
watchProcess();
}
}
extern "C" __attribute((force_align_arg_pointer)) void
instrumentIndirectCall(uint64_t Target, uint64_t IndCallID) {
GlobalIndCallCounters[IndCallID].incrementVal(Target, *GlobalAlloc);
}
extern "C" __attribute((naked)) void __bolt_instr_indirect_call()
{
#if defined(__aarch64__)
__asm__ __volatile__(SAVE_ALL
"ldp x0, x1, [sp, #288]\n"
"bl instrumentIndirectCall\n"
RESTORE_ALL
"ret\n"
:::);
#else
__asm__ __volatile__(SAVE_ALL
"mov 0xa0(%%rsp), %%rdi\n"
"mov 0x98(%%rsp), %%rsi\n"
"call instrumentIndirectCall\n"
RESTORE_ALL
"ret\n"
:::);
#endif
}
extern "C" __attribute((naked)) void __bolt_instr_indirect_tailcall()
{
#if defined(__aarch64__)
__asm__ __volatile__(SAVE_ALL
"ldp x0, x1, [sp, #288]\n"
"bl instrumentIndirectCall\n"
RESTORE_ALL
"ret\n"
:::);
#else
__asm__ __volatile__(SAVE_ALL
"mov 0x98(%%rsp), %%rdi\n"
"mov 0x90(%%rsp), %%rsi\n"
"call instrumentIndirectCall\n"
RESTORE_ALL
"ret\n"
:::);
#endif
}
extern "C" __attribute((naked)) void __bolt_instr_start()
{
#if defined(__aarch64__)
__asm__ __volatile__(SAVE_ALL
"bl __bolt_instr_setup\n"
RESTORE_ALL
"adrp x16, __bolt_start_trampoline\n"
"add x16, x16, #:lo12:__bolt_start_trampoline\n"
"br x16\n"
:::);
#else
__asm__ __volatile__(SAVE_ALL
"call __bolt_instr_setup\n"
RESTORE_ALL
"jmp __bolt_start_trampoline\n"
:::);
#endif
}
extern "C" void __bolt_instr_fini() {
#if defined(__aarch64__)
__asm__ __volatile__(SAVE_ALL
"adrp x16, __bolt_fini_trampoline\n"
"add x16, x16, #:lo12:__bolt_fini_trampoline\n"
"blr x16\n"
RESTORE_ALL
:::);
#else
__asm__ __volatile__("call __bolt_fini_trampoline\n" :::);
#endif
if (__bolt_instr_sleep_time == 0) {
int FD = openProfile();
__bolt_instr_data_dump(FD);
__close(FD);
}
DEBUG(report("Finished.\n"));
}
#endif
#if defined(__APPLE__)
extern "C" void __bolt_instr_data_dump() {
ProfileWriterContext Ctx = readDescriptions();
int FD = 2;
BumpPtrAllocator Alloc;
const uint8_t *FuncDesc = Ctx.FuncDescriptions;
uint32_t bolt_instr_num_funcs = _bolt_instr_num_funcs_getter();
for (int I = 0, E = bolt_instr_num_funcs; I < E; ++I) {
FuncDesc = writeFunctionProfile(FD, Ctx, FuncDesc, Alloc);
Alloc.clear();
DEBUG(reportNumber("FuncDesc now: ", (uint64_t)FuncDesc, 16));
}
assert(FuncDesc == (void *)Ctx.Strings,
"FuncDesc ptr must be equal to stringtable");
}
extern "C"
__attribute__((section("__TEXT,__setup")))
__attribute__((force_align_arg_pointer))
void _bolt_instr_setup() {
__asm__ __volatile__(SAVE_ALL :::);
report("Hello!\n");
__asm__ __volatile__(RESTORE_ALL :::);
}
extern "C"
__attribute__((section("__TEXT,__fini")))
__attribute__((force_align_arg_pointer))
void _bolt_instr_fini() {
report("Bye!\n");
__bolt_instr_data_dump();
}
#endif