2021-12-22 02:21:41 +08:00
|
|
|
//===- bolt/runtime/instr.cpp ---------------------------------------------===//
|
2019-07-25 05:03:43 +08:00
|
|
|
//
|
2021-03-18 06:04:19 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-07-25 05:03:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2019-12-14 09:27:03 +08:00
|
|
|
// BOLT runtime instrumentation library for x86 Linux. Currently, BOLT does
|
|
|
|
// not support linking modules with dependencies on one another into the final
|
|
|
|
// binary (TODO?), which means this library has to be self-contained in a single
|
|
|
|
// module.
|
|
|
|
//
|
|
|
|
// All extern declarations here need to be defined by BOLT itself. Those will be
|
|
|
|
// undefined symbols that BOLT needs to resolve by emitting these symbols with
|
|
|
|
// MCStreamer. Currently, Passes/Instrumentation.cpp is the pass responsible
|
|
|
|
// for defining the symbols here and these two files have a tight coupling: one
|
|
|
|
// working statically when you run BOLT and another during program runtime when
|
|
|
|
// you run an instrumented binary. The main goal here is to output an fdata file
|
|
|
|
// (BOLT profile) with the instrumentation counters inserted by the static pass.
|
|
|
|
// Counters for indirect calls are an exception, as we can't know them
|
|
|
|
// statically. These counters are created and managed here. To allow this, we
|
|
|
|
// need a minimal framework for allocating memory dynamically. We provide this
|
|
|
|
// with the BumpPtrAllocator class (not LLVM's, but our own version of it).
|
|
|
|
//
|
|
|
|
// Since this code is intended to be inserted into any executable, we decided to
|
|
|
|
// make it standalone and do not depend on any external libraries (i.e. language
|
|
|
|
// support libraries, such as glibc or stdc++). To allow this, we provide a few
|
|
|
|
// light implementations of common OS interacting functionalities using direct
|
|
|
|
// syscall wrappers. Our simple allocator doesn't manage deallocations that
|
|
|
|
// fragment the memory space, so it's stack based. This is the minimal framework
|
|
|
|
// provided here to allow processing instrumented counters and writing fdata.
|
|
|
|
//
|
|
|
|
// In the C++ idiom used here, we never use or rely on constructors or
|
|
|
|
// destructors for global objects. That's because those need support from the
|
|
|
|
// linker in initialization/finalization code, and we want to keep our linker
|
|
|
|
// very simple. Similarly, we don't create any global objects that are zero
|
|
|
|
// initialized, since those would need to go .bss, which our simple linker also
|
|
|
|
// don't support (TODO?).
|
2019-07-25 05:03:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-10-16 22:35:29 +08:00
|
|
|
#if defined (__x86_64__)
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-03 02:14:38 +08:00
|
|
|
#include "common.h"
|
2019-07-25 05:03:43 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
// Enables a very verbose logging to stderr useful when debugging
|
2019-08-08 07:09:50 +08:00
|
|
|
//#define ENABLE_DEBUG
|
|
|
|
|
|
|
|
#ifdef ENABLE_DEBUG
|
|
|
|
#define DEBUG(X) \
|
|
|
|
{ X; }
|
|
|
|
#else
|
|
|
|
#define DEBUG(X) \
|
|
|
|
{}
|
|
|
|
#endif
|
|
|
|
|
2021-07-22 05:04:28 +08:00
|
|
|
#pragma GCC visibility push(hidden)
|
2021-01-29 04:32:03 +08:00
|
|
|
|
|
|
|
extern "C" {
|
2021-07-31 05:29:23 +08:00
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
2021-01-29 04:32:03 +08:00
|
|
|
extern uint64_t* _bolt_instr_locations_getter();
|
|
|
|
extern uint32_t _bolt_num_counters_getter();
|
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
extern uint8_t* _bolt_instr_tables_getter();
|
|
|
|
extern uint32_t _bolt_instr_num_funcs_getter();
|
2021-01-29 04:32:03 +08:00
|
|
|
|
|
|
|
#else
|
2020-10-15 18:51:56 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
// Main counters inserted by instrumentation, incremented during runtime when
|
|
|
|
// points of interest (locations) in the program are reached. Those are direct
|
|
|
|
// calls and direct and indirect branches (local ones). There are also counters
|
|
|
|
// for basic block execution if they are a spanning tree leaf and need to be
|
|
|
|
// counted in order to infer the execution count of other edges of the CFG.
|
2019-07-25 05:03:43 +08:00
|
|
|
extern uint64_t __bolt_instr_locations[];
|
2019-12-14 09:27:03 +08:00
|
|
|
extern uint32_t __bolt_num_counters;
|
|
|
|
// Descriptions are serialized metadata about binary functions written by BOLT,
|
|
|
|
// so we have a minimal understanding about the program structure. For a
|
|
|
|
// reference on the exact format of this metadata, see *Description structs,
|
|
|
|
// Location, IntrumentedNode and EntryNode.
|
|
|
|
// Number of indirect call site descriptions
|
|
|
|
extern uint32_t __bolt_instr_num_ind_calls;
|
|
|
|
// Number of indirect call target descriptions
|
|
|
|
extern uint32_t __bolt_instr_num_ind_targets;
|
2019-08-08 07:09:50 +08:00
|
|
|
// Number of function descriptions
|
|
|
|
extern uint32_t __bolt_instr_num_funcs;
|
2019-12-14 09:27:03 +08:00
|
|
|
// Time to sleep across dumps (when we write the fdata profile to disk)
|
|
|
|
extern uint32_t __bolt_instr_sleep_time;
|
2021-03-10 08:18:11 +08:00
|
|
|
// Do not clear counters across dumps, rewrite file with the updated values
|
|
|
|
extern bool __bolt_instr_no_counters_clear;
|
|
|
|
// Wait until all forks of instrumented process will finish
|
|
|
|
extern bool __bolt_instr_wait_forks;
|
2019-08-08 07:09:50 +08:00
|
|
|
// Filename to dump data to
|
2019-07-25 05:03:43 +08:00
|
|
|
extern char __bolt_instr_filename[];
|
2021-07-30 23:07:53 +08:00
|
|
|
// Instumented binary file path
|
|
|
|
extern char __bolt_instr_binpath[];
|
2019-12-14 09:27:03 +08:00
|
|
|
// If true, append current PID to the fdata filename when creating it so
|
|
|
|
// different invocations of the same program can be differentiated.
|
|
|
|
extern bool __bolt_instr_use_pid;
|
|
|
|
// Functions that will be used to instrument indirect calls. BOLT static pass
|
|
|
|
// will identify indirect calls and modify them to load the address in these
|
|
|
|
// trampolines and call this address instead. BOLT can't use direct calls to
|
|
|
|
// our handlers because our addresses here are not known at analysis time. We
|
|
|
|
// only support resolving dependencies from this file to the output of BOLT,
|
|
|
|
// *not* the other way around.
|
|
|
|
// TODO: We need better linking support to make that happen.
|
2021-06-24 02:24:09 +08:00
|
|
|
extern void (*__bolt_ind_call_counter_func_pointer)();
|
|
|
|
extern void (*__bolt_ind_tailcall_counter_func_pointer)();
|
2021-06-19 04:08:35 +08:00
|
|
|
// Function pointers to init/fini trampoline routines in the binary, so we can
|
|
|
|
// resume regular execution of these functions that we hooked
|
2021-07-31 05:29:23 +08:00
|
|
|
extern void __bolt_start_trampoline();
|
|
|
|
extern void __bolt_fini_trampoline();
|
2019-07-25 05:03:43 +08:00
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
#endif
|
2021-07-31 05:29:23 +08:00
|
|
|
}
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// A simple allocator that mmaps a fixed size region and manages this space
|
|
|
|
/// in a stack fashion, meaning you always deallocate the last element that
|
2019-12-14 09:27:03 +08:00
|
|
|
/// was allocated. In practice, we don't need to deallocate individual elements.
|
|
|
|
/// We monotonically increase our usage and then deallocate everything once we
|
|
|
|
/// are done processing something.
|
2019-08-08 07:09:50 +08:00
|
|
|
class BumpPtrAllocator {
|
2019-12-14 09:27:03 +08:00
|
|
|
/// This is written before each allocation and act as a canary to detect when
|
|
|
|
/// a bug caused our program to cross allocation boundaries.
|
2019-08-08 07:09:50 +08:00
|
|
|
struct EntryMetadata {
|
|
|
|
uint64_t Magic;
|
|
|
|
uint64_t AllocSize;
|
|
|
|
};
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-03 02:14:38 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
public:
|
2021-01-21 04:56:41 +08:00
|
|
|
void *allocate(size_t Size) {
|
2019-12-14 09:27:03 +08:00
|
|
|
Lock L(M);
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
if (StackBase == nullptr) {
|
2021-01-29 04:44:14 +08:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
int MAP_PRIVATE_MAP_ANONYMOUS = 0x1002;
|
|
|
|
#else
|
|
|
|
int MAP_PRIVATE_MAP_ANONYMOUS = 0x22;
|
|
|
|
#endif
|
2019-12-14 09:27:03 +08:00
|
|
|
StackBase = reinterpret_cast<uint8_t *>(
|
|
|
|
__mmap(0, MaxSize, 0x3 /* PROT_READ | PROT_WRITE*/,
|
|
|
|
Shared ? 0x21 /*MAP_SHARED | MAP_ANONYMOUS*/
|
2021-01-29 04:44:14 +08:00
|
|
|
: MAP_PRIVATE_MAP_ANONYMOUS /* MAP_PRIVATE | MAP_ANONYMOUS*/,
|
2019-12-14 09:27:03 +08:00
|
|
|
-1, 0));
|
2019-08-08 07:09:50 +08:00
|
|
|
StackSize = 0;
|
|
|
|
}
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
Size = alignTo(Size + sizeof(EntryMetadata), 16);
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-03 02:14:38 +08:00
|
|
|
uint8_t *AllocAddress = StackBase + StackSize + sizeof(EntryMetadata);
|
2019-08-08 07:09:50 +08:00
|
|
|
auto *M = reinterpret_cast<EntryMetadata *>(StackBase + StackSize);
|
2019-12-14 09:27:03 +08:00
|
|
|
M->Magic = Magic;
|
2019-08-08 07:09:50 +08:00
|
|
|
M->AllocSize = Size;
|
|
|
|
StackSize += Size;
|
2019-12-14 09:27:03 +08:00
|
|
|
assert(StackSize < MaxSize, "allocator ran out of memory");
|
2019-08-08 07:09:50 +08:00
|
|
|
return AllocAddress;
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
/// Element-wise deallocation is only used for debugging to catch memory
|
|
|
|
/// bugs by checking magic bytes. Ordinarily, we reset the allocator once
|
|
|
|
/// we are done with it. Reset is done with clear(). There's no need
|
|
|
|
/// to deallocate each element individually.
|
2019-08-08 07:09:50 +08:00
|
|
|
void deallocate(void *Ptr) {
|
2019-12-14 09:27:03 +08:00
|
|
|
Lock L(M);
|
2019-08-08 07:09:50 +08:00
|
|
|
uint8_t MetadataOffset = sizeof(EntryMetadata);
|
|
|
|
auto *M = reinterpret_cast<EntryMetadata *>(
|
|
|
|
reinterpret_cast<uint8_t *>(Ptr) - MetadataOffset);
|
|
|
|
const uint8_t *StackTop = StackBase + StackSize + MetadataOffset;
|
|
|
|
// Validate size
|
|
|
|
if (Ptr != StackTop - M->AllocSize) {
|
2019-12-14 09:27:03 +08:00
|
|
|
// Failed validation, check if it is a pointer returned by operator new []
|
2019-08-08 07:09:50 +08:00
|
|
|
MetadataOffset +=
|
|
|
|
sizeof(uint64_t); // Space for number of elements alloc'ed
|
|
|
|
M = reinterpret_cast<EntryMetadata *>(reinterpret_cast<uint8_t *>(Ptr) -
|
|
|
|
MetadataOffset);
|
2019-12-14 09:27:03 +08:00
|
|
|
// Ok, it failed both checks if this assertion fails. Stop the program, we
|
|
|
|
// have a memory bug.
|
2019-08-08 07:09:50 +08:00
|
|
|
assert(Ptr == StackTop - M->AllocSize,
|
|
|
|
"must deallocate the last element alloc'ed");
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
assert(M->Magic == Magic, "allocator magic is corrupt");
|
2019-08-08 07:09:50 +08:00
|
|
|
StackSize -= M->AllocSize;
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
#else
|
|
|
|
void deallocate(void *) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
Lock L(M);
|
|
|
|
StackSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set mmap reservation size (only relevant before first allocation)
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-03 02:14:38 +08:00
|
|
|
void setMaxSize(uint64_t Size) { MaxSize = Size; }
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
/// Set mmap reservation privacy (only relevant before first allocation)
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-03 02:14:38 +08:00
|
|
|
void setShared(bool S) { Shared = S; }
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
void destroy() {
|
|
|
|
if (StackBase == nullptr)
|
|
|
|
return;
|
|
|
|
__munmap(StackBase, MaxSize);
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
private:
|
2019-12-14 09:27:03 +08:00
|
|
|
static constexpr uint64_t Magic = 0x1122334455667788ull;
|
|
|
|
uint64_t MaxSize = 0xa00000;
|
2019-08-08 07:09:50 +08:00
|
|
|
uint8_t *StackBase{nullptr};
|
|
|
|
uint64_t StackSize{0};
|
2019-12-14 09:27:03 +08:00
|
|
|
bool Shared{false};
|
|
|
|
Mutex M;
|
2019-08-08 07:09:50 +08:00
|
|
|
};
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Used for allocating indirect call instrumentation counters. Initialized by
|
|
|
|
/// __bolt_instr_setup, our initialization routine.
|
|
|
|
BumpPtrAllocator GlobalAlloc;
|
2019-08-08 07:09:50 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// User-defined placement new operators. We only use those (as opposed to
|
|
|
|
// overriding the regular operator new) so we can keep our allocator in the
|
|
|
|
// stack instead of in a data section (global).
|
2021-01-21 04:56:41 +08:00
|
|
|
void *operator new(size_t Sz, BumpPtrAllocator &A) { return A.allocate(Sz); }
|
|
|
|
void *operator new(size_t Sz, BumpPtrAllocator &A, char C) {
|
2019-08-08 07:09:50 +08:00
|
|
|
auto *Ptr = reinterpret_cast<char *>(A.allocate(Sz));
|
|
|
|
memSet(Ptr, C, Sz);
|
|
|
|
return Ptr;
|
|
|
|
}
|
2021-01-21 04:56:41 +08:00
|
|
|
void *operator new[](size_t Sz, BumpPtrAllocator &A) {
|
2019-08-08 07:09:50 +08:00
|
|
|
return A.allocate(Sz);
|
|
|
|
}
|
2021-01-21 04:56:41 +08:00
|
|
|
void *operator new[](size_t Sz, BumpPtrAllocator &A, char C) {
|
2019-08-08 07:09:50 +08:00
|
|
|
auto *Ptr = reinterpret_cast<char *>(A.allocate(Sz));
|
|
|
|
memSet(Ptr, C, Sz);
|
|
|
|
return Ptr;
|
|
|
|
}
|
|
|
|
// Only called during exception unwinding (useless). We must manually dealloc.
|
|
|
|
// C++ language weirdness
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-03 02:14:38 +08:00
|
|
|
void operator delete(void *Ptr, BumpPtrAllocator &A) { A.deallocate(Ptr); }
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-08-08 04:50:06 +08:00
|
|
|
// Disable instrumentation optimizations that sacrifice profile accuracy
|
|
|
|
extern "C" bool __bolt_instr_conservative;
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Basic key-val atom stored in our hash
|
|
|
|
struct SimpleHashTableEntryBase {
|
|
|
|
uint64_t Key;
|
|
|
|
uint64_t Val;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// This hash table implementation starts by allocating a table of size
|
|
|
|
/// InitialSize. When conflicts happen in this main table, it resolves
|
|
|
|
/// them by chaining a new table of size IncSize. It never reallocs as our
|
|
|
|
/// allocator doesn't support it. The key is intended to be function pointers.
|
|
|
|
/// There's no clever hash function (it's just x mod size, size being prime).
|
|
|
|
/// I never tuned the coefficientes in the modular equation (TODO)
|
|
|
|
/// This is used for indirect calls (each call site has one of this, so it
|
|
|
|
/// should have a small footprint) and for tallying call counts globally for
|
|
|
|
/// each target to check if we missed the origin of some calls (this one is a
|
|
|
|
/// large instantiation of this template, since it is global for all call sites)
|
|
|
|
template <typename T = SimpleHashTableEntryBase, uint32_t InitialSize = 7,
|
|
|
|
uint32_t IncSize = 7>
|
|
|
|
class SimpleHashTable {
|
|
|
|
public:
|
|
|
|
using MapEntry = T;
|
|
|
|
|
|
|
|
/// Increment by 1 the value of \p Key. If it is not in this table, it will be
|
|
|
|
/// added to the table and its value set to 1.
|
|
|
|
void incrementVal(uint64_t Key, BumpPtrAllocator &Alloc) {
|
|
|
|
++get(Key, Alloc).Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Basic member accessing interface. Here we pass the allocator explicitly to
|
|
|
|
/// avoid storing a pointer to it as part of this table (remember there is one
|
|
|
|
/// hash for each indirect call site, so we wan't to minimize our footprint).
|
|
|
|
MapEntry &get(uint64_t Key, BumpPtrAllocator &Alloc) {
|
2021-08-08 04:50:06 +08:00
|
|
|
if (!__bolt_instr_conservative) {
|
|
|
|
TryLock L(M);
|
|
|
|
if (!L.isLocked())
|
|
|
|
return NoEntry;
|
|
|
|
return getOrAllocEntry(Key, Alloc);
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
Lock L(M);
|
2021-08-08 04:50:06 +08:00
|
|
|
return getOrAllocEntry(Key, Alloc);
|
2019-12-14 09:27:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Traverses all elements in the table
|
|
|
|
template <typename... Args>
|
|
|
|
void forEachElement(void (*Callback)(MapEntry &, Args...), Args... args) {
|
|
|
|
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};
|
2021-08-08 04:50:06 +08:00
|
|
|
MapEntry NoEntry;
|
2019-12-14 09:27:03 +08:00
|
|
|
Mutex M;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
void forEachElement(void (*Callback)(MapEntry &, Args...),
|
|
|
|
uint32_t NumEntries, MapEntry *Entries, Args... args) {
|
2021-04-08 15:19:26 +08:00
|
|
|
for (uint32_t I = 0; I < NumEntries; ++I) {
|
|
|
|
MapEntry &Entry = Entries[I];
|
2019-12-14 09:27:03 +08:00
|
|
|
if (Entry.Key == VacantMarker)
|
|
|
|
continue;
|
|
|
|
if (Entry.Key & FollowUpTableMarker) {
|
|
|
|
forEachElement(Callback, IncSize,
|
|
|
|
reinterpret_cast<MapEntry *>(Entry.Key &
|
|
|
|
~FollowUpTableMarker),
|
|
|
|
args...);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Callback(Entry, args...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MapEntry &firstAllocation(uint64_t Key, BumpPtrAllocator &Alloc) {
|
|
|
|
TableRoot = new (Alloc, 0) MapEntry[InitialSize];
|
2021-04-08 15:19:26 +08:00
|
|
|
MapEntry &Entry = TableRoot[Key % InitialSize];
|
2019-12-14 09:27:03 +08:00
|
|
|
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;
|
2021-04-08 15:19:26 +08:00
|
|
|
MapEntry &Entry = Entries[Selector];
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
// A hit
|
|
|
|
if (Entry.Key == Key) {
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vacant - add new entry
|
|
|
|
if (Entry.Key == VacantMarker) {
|
|
|
|
Entry.Key = Key;
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defer to the next level
|
|
|
|
if (Entry.Key & FollowUpTableMarker) {
|
|
|
|
return getEntry(
|
|
|
|
reinterpret_cast<MapEntry *>(Entry.Key & ~FollowUpTableMarker),
|
|
|
|
Key, Remainder, Alloc, CurLevel + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conflict - create the next level
|
|
|
|
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;
|
|
|
|
return getEntry(NextLevelTbl, Key, Remainder, Alloc, CurLevel + 1);
|
|
|
|
}
|
2021-08-08 04:50:06 +08:00
|
|
|
|
|
|
|
MapEntry &getOrAllocEntry(uint64_t Key, BumpPtrAllocator &Alloc) {
|
|
|
|
if (TableRoot)
|
|
|
|
return getEntry(TableRoot, Key, Key, Alloc, 0);
|
|
|
|
return firstAllocation(Key, Alloc);
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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() {
|
|
|
|
Lock L(M);
|
|
|
|
forEachElement(resetIndCallCounter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a hash table mapping a function target address to its counter.
|
|
|
|
using IndirectCallHashTable = SimpleHashTable<>;
|
|
|
|
|
|
|
|
/// Initialize with number 1 instead of 0 so we don't go into .bss. This is the
|
|
|
|
/// global array of all hash tables storing indirect call destinations happening
|
|
|
|
/// during runtime, one table per call site.
|
|
|
|
IndirectCallHashTable *GlobalIndCallCounters{
|
|
|
|
reinterpret_cast<IndirectCallHashTable *>(1)};
|
|
|
|
|
|
|
|
/// Don't allow reentrancy in the fdata writing phase - only one thread writes
|
|
|
|
/// it
|
|
|
|
Mutex *GlobalWriteProfileMutex{reinterpret_cast<Mutex *>(1)};
|
|
|
|
|
|
|
|
/// Store number of calls in additional to target address (Key) and frequency
|
|
|
|
/// as perceived by the basic block counter (Val).
|
|
|
|
struct CallFlowEntryBase : public SimpleHashTableEntryBase {
|
|
|
|
uint64_t Calls;
|
|
|
|
};
|
|
|
|
|
|
|
|
using CallFlowHashTableBase = SimpleHashTable<CallFlowEntryBase, 11939, 233>;
|
|
|
|
|
|
|
|
/// This is a large table indexing all possible call targets (indirect and
|
|
|
|
/// direct ones). The goal is to find mismatches between number of calls (for
|
|
|
|
/// those calls we were able to track) and the entry basic block counter of the
|
|
|
|
/// callee. In most cases, these two should be equal. If not, there are two
|
|
|
|
/// possible scenarios here:
|
|
|
|
///
|
|
|
|
/// * Entry BB has higher frequency than all known calls to this function.
|
|
|
|
/// In this case, we have dynamic library code or any uninstrumented code
|
|
|
|
/// calling this function. We will write the profile for these untracked
|
|
|
|
/// calls as having source "0 [unknown] 0" in the fdata file.
|
|
|
|
///
|
|
|
|
/// * Number of known calls is higher than the frequency of entry BB
|
|
|
|
/// This only happens when there is no counter for the entry BB / callee
|
|
|
|
/// function is not simple (in BOLT terms). We don't do anything special
|
|
|
|
/// here and just ignore those (we still report all calls to the non-simple
|
|
|
|
/// function, though).
|
|
|
|
///
|
|
|
|
class CallFlowHashTable : public CallFlowHashTableBase {
|
|
|
|
public:
|
|
|
|
CallFlowHashTable(BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
|
|
|
|
|
|
|
|
MapEntry &get(uint64_t Key) { return CallFlowHashTableBase::get(Key, Alloc); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Different than the hash table for indirect call targets, we do store the
|
|
|
|
// allocator here since there is only one call flow hash and space overhead
|
|
|
|
// is negligible.
|
|
|
|
BumpPtrAllocator &Alloc;
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Description metadata emitted by BOLT to describe the program - refer to
|
|
|
|
/// Passes/Instrumentation.cpp - Instrumentation::emitTablesAsELFNote()
|
|
|
|
///
|
|
|
|
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;
|
|
|
|
|
|
|
|
/// Constructor will parse the serialized function metadata written by BOLT
|
|
|
|
FunctionDescription(const uint8_t *FuncDesc);
|
|
|
|
|
|
|
|
uint64_t getSize() const {
|
|
|
|
return 16 + NumLeafNodes * sizeof(InstrumentedNode) +
|
|
|
|
NumEdges * sizeof(EdgeDescription) +
|
|
|
|
NumCalls * sizeof(CallDescription) +
|
|
|
|
NumEntryNodes * sizeof(EntryNode);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The context is created when the fdata profile needs to be written to disk
|
|
|
|
/// and we need to interpret our runtime counters. It contains pointers to the
|
|
|
|
/// mmaped binary (only the BOLT written metadata section). Deserialization
|
|
|
|
/// should be straightforward as most data is POD or an array of POD elements.
|
|
|
|
/// This metadata is used to reconstruct function CFGs.
|
|
|
|
struct ProfileWriterContext {
|
|
|
|
IndCallDescription *IndCallDescriptions;
|
|
|
|
IndCallTargetDescription *IndCallTargets;
|
|
|
|
uint8_t *FuncDescriptions;
|
|
|
|
char *Strings; // String table with function names used in this binary
|
|
|
|
int FileDesc; // File descriptor for the file on disk backing this
|
|
|
|
// information in memory via mmap
|
|
|
|
void *MMapPtr; // The mmap ptr
|
|
|
|
int MMapSize; // The mmap size
|
|
|
|
|
|
|
|
/// Hash table storing all possible call destinations to detect untracked
|
|
|
|
/// calls and correctly report them as [unknown] in output fdata.
|
|
|
|
CallFlowHashTable *CallFlowTable;
|
|
|
|
|
|
|
|
/// Lookup the sorted indirect call target vector to fetch function name and
|
|
|
|
/// offset for an arbitrary function pointer.
|
|
|
|
const IndCallTargetDescription *lookupIndCallTarget(uint64_t Target) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Perform a string comparison and returns zero if Str1 matches Str2. Compares
|
|
|
|
/// at most Size characters.
|
2019-08-08 07:09:50 +08:00
|
|
|
int compareStr(const char *Str1, const char *Str2, int Size) {
|
2019-08-03 02:20:13 +08:00
|
|
|
while (*Str1 == *Str2) {
|
|
|
|
if (*Str1 == '\0' || --Size == 0)
|
|
|
|
return 0;
|
|
|
|
++Str1;
|
|
|
|
++Str2;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Output Location to the fdata file
|
|
|
|
char *serializeLoc(const ProfileWriterContext &Ctx, char *OutBuf,
|
2019-08-08 07:09:50 +08:00
|
|
|
const Location Loc, uint32_t BufSize) {
|
2019-08-03 02:20:13 +08:00
|
|
|
// fdata location format: Type Name Offset
|
|
|
|
// Type 1 - regular symbol
|
|
|
|
OutBuf = strCopy(OutBuf, "1 ");
|
2019-12-14 09:27:03 +08:00
|
|
|
const char *Str = Ctx.Strings + Loc.FunctionName;
|
2019-08-08 07:09:50 +08:00
|
|
|
uint32_t Size = 25;
|
2019-07-25 05:03:43 +08:00
|
|
|
while (*Str) {
|
|
|
|
*OutBuf++ = *Str++;
|
2019-08-08 07:09:50 +08:00
|
|
|
if (++Size >= BufSize)
|
|
|
|
break;
|
2019-07-25 05:03:43 +08:00
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
assert(!*Str, "buffer overflow, function name too large");
|
2019-07-25 05:03:43 +08:00
|
|
|
*OutBuf++ = ' ';
|
2019-08-03 02:20:13 +08:00
|
|
|
OutBuf = intToStr(OutBuf, Loc.Offset, 16);
|
2019-07-25 05:03:43 +08:00
|
|
|
*OutBuf++ = ' ';
|
|
|
|
return OutBuf;
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Read and deserialize a function description written by BOLT. \p FuncDesc
|
|
|
|
/// points at the beginning of the function metadata structure in the file.
|
|
|
|
/// See Instrumentation::emitTablesAsELFNote()
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read and mmap descriptions written by BOLT from the executable's notes
|
|
|
|
/// section
|
2021-01-29 04:44:14 +08:00
|
|
|
#if defined(HAVE_ELF_H) and !defined(__APPLE__)
|
2021-01-19 02:08:55 +08:00
|
|
|
|
|
|
|
void *__attribute__((noinline)) __get_pc() {
|
|
|
|
return __builtin_extract_return_addr(__builtin_return_address(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get string with address and parse it to hex pair <StartAddress, EndAddress>
|
|
|
|
bool parseAddressRange(const char *Str, uint64_t &StartAddress,
|
|
|
|
uint64_t &EndAddress) {
|
|
|
|
if (!Str)
|
|
|
|
return false;
|
|
|
|
// Parsed string format: <hex1>-<hex2>
|
|
|
|
StartAddress = hexToLong(Str, '-');
|
|
|
|
while (*Str && *Str != '-')
|
|
|
|
++Str;
|
|
|
|
if (!*Str)
|
|
|
|
return false;
|
|
|
|
++Str; // swallow '-'
|
|
|
|
EndAddress = hexToLong(Str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get full path to the real binary by getting current virtual address
|
|
|
|
/// and searching for the appropriate link in address range in
|
|
|
|
/// /proc/self/map_files
|
|
|
|
static char *getBinaryPath() {
|
|
|
|
const uint32_t BufSize = 1024;
|
2021-10-26 02:42:29 +08:00
|
|
|
const uint32_t NameMax = 4096;
|
2021-01-19 02:08:55 +08:00
|
|
|
const char DirPath[] = "/proc/self/map_files/";
|
|
|
|
static char TargetPath[NameMax] = {};
|
|
|
|
char Buf[BufSize];
|
|
|
|
|
2021-07-30 23:07:53 +08:00
|
|
|
if (__bolt_instr_binpath[0] != '\0')
|
|
|
|
return __bolt_instr_binpath;
|
|
|
|
|
2021-01-19 02:08:55 +08:00
|
|
|
if (TargetPath[0] != '\0')
|
|
|
|
return TargetPath;
|
|
|
|
|
|
|
|
unsigned long CurAddr = (unsigned long)__get_pc();
|
|
|
|
uint64_t FDdir = __open(DirPath,
|
|
|
|
/*flags=*/0 /*O_RDONLY*/,
|
|
|
|
/*mode=*/0666);
|
2021-11-05 23:47:30 +08:00
|
|
|
assert(static_cast<int64_t>(FDdir) >= 0,
|
2021-01-19 02:08:55 +08:00
|
|
|
"failed to open /proc/self/map_files");
|
|
|
|
|
|
|
|
while (long Nread = __getdents(FDdir, (struct dirent *)Buf, BufSize)) {
|
|
|
|
assert(static_cast<int64_t>(Nread) != -1, "failed to get folder entries");
|
|
|
|
|
|
|
|
struct dirent *d;
|
|
|
|
for (long Bpos = 0; Bpos < Nread; Bpos += d->d_reclen) {
|
|
|
|
d = (struct dirent *)(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;
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
ProfileWriterContext readDescriptions() {
|
|
|
|
ProfileWriterContext Result;
|
2021-01-19 02:08:55 +08:00
|
|
|
char *BinPath = getBinaryPath();
|
|
|
|
assert(BinPath && BinPath[0] != '\0', "failed to find binary path");
|
|
|
|
|
|
|
|
uint64_t FD = __open(BinPath,
|
2019-08-03 02:20:13 +08:00
|
|
|
/*flags=*/0 /*O_RDONLY*/,
|
|
|
|
/*mode=*/0666);
|
2021-11-05 23:47:30 +08:00
|
|
|
assert(static_cast<int64_t>(FD) >= 0, "failed to open binary path");
|
2021-01-19 02:08:55 +08:00
|
|
|
|
2019-08-03 02:20:13 +08:00
|
|
|
Result.FileDesc = FD;
|
|
|
|
|
|
|
|
// mmap our binary to memory
|
|
|
|
uint64_t Size = __lseek(FD, 0, 2 /*SEEK_END*/);
|
|
|
|
uint8_t *BinContents = reinterpret_cast<uint8_t *>(
|
|
|
|
__mmap(0, Size, 0x1 /* PROT_READ*/, 0x2 /* MAP_PRIVATE*/, FD, 0));
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Find .bolt.instr.tables with the data we need and set pointers to it
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// Actual contents of the ELF note start after offset 20 decimal:
|
2019-08-08 07:09:50 +08:00
|
|
|
// Offset 0: Producer name size (4 bytes)
|
|
|
|
// Offset 4: Contents size (4 bytes)
|
|
|
|
// Offset 8: Note type (4 bytes)
|
|
|
|
// Offset 12: Producer name (BOLT\0) (5 bytes + align to 4-byte boundary)
|
|
|
|
// Offset 20: Contents
|
2019-12-14 09:27:03 +08:00
|
|
|
uint32_t IndCallDescSize =
|
2019-08-08 07:09:50 +08:00
|
|
|
*reinterpret_cast<uint32_t *>(BinContents + Shdr->sh_offset + 20);
|
2019-12-14 09:27:03 +08:00
|
|
|
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);
|
2019-08-03 02:20:13 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
const char ErrMsg[] =
|
|
|
|
"BOLT instrumentation runtime error: could not find section "
|
|
|
|
".bolt.instr.tables\n";
|
|
|
|
reportError(ErrMsg, sizeof(ErrMsg));
|
|
|
|
return Result;
|
|
|
|
}
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2019-09-21 02:29:35 +08:00
|
|
|
#else
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
ProfileWriterContext readDescriptions() {
|
|
|
|
ProfileWriterContext Result;
|
2021-01-29 04:44:14 +08:00
|
|
|
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);
|
2019-09-21 02:29:35 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2019-09-21 02:29:35 +08:00
|
|
|
#endif
|
2019-08-03 02:20:13 +08:00
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
#if !defined(__APPLE__)
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Debug by printing overall metadata global numbers to check it is sane
|
|
|
|
void printStats(const ProfileWriterContext &Ctx) {
|
2019-08-08 07:09:50 +08:00
|
|
|
char StatMsg[BufSize];
|
|
|
|
char *StatPtr = StatMsg;
|
2019-12-14 09:27:03 +08:00
|
|
|
StatPtr =
|
|
|
|
strCopy(StatPtr,
|
|
|
|
"\nBOLT INSTRUMENTATION RUNTIME STATISTICS\n\nIndCallDescSize: ");
|
2019-08-08 07:09:50 +08:00
|
|
|
StatPtr = intToStr(StatPtr,
|
2019-12-14 09:27:03 +08:00
|
|
|
Ctx.FuncDescriptions -
|
|
|
|
reinterpret_cast<uint8_t *>(Ctx.IndCallDescriptions),
|
2019-08-08 07:09:50 +08:00
|
|
|
10);
|
|
|
|
StatPtr = strCopy(StatPtr, "\nFuncDescSize: ");
|
|
|
|
StatPtr = intToStr(
|
|
|
|
StatPtr,
|
2019-12-14 09:27:03 +08:00
|
|
|
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);
|
2019-08-08 07:09:50 +08:00
|
|
|
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);
|
|
|
|
}
|
2021-01-29 04:44:14 +08:00
|
|
|
#endif
|
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
/// This is part of a simple CFG representation in memory, where we store
|
|
|
|
/// a dynamically sized array of input and output edges per node, and store
|
|
|
|
/// a dynamically sized array of nodes per graph. We also store the spanning
|
|
|
|
/// tree edges for that CFG in a separate array of nodes in
|
|
|
|
/// \p SpanningTreeNodes, while the regular nodes live in \p CFGNodes.
|
|
|
|
struct Edge {
|
|
|
|
uint32_t Node; // Index in nodes array regarding the destination of this edge
|
|
|
|
uint32_t ID; // Edge index in an array comprising all edges of the graph
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A regular graph node or a spanning tree node
|
|
|
|
struct Node {
|
|
|
|
uint32_t NumInEdges{0}; // Input edge count used to size InEdge
|
|
|
|
uint32_t NumOutEdges{0}; // Output edge count used to size OutEdges
|
|
|
|
Edge *InEdges{nullptr}; // Created and managed by \p Graph
|
|
|
|
Edge *OutEdges{nullptr}; // ditto
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Main class for CFG representation in memory. Manages object creation and
|
|
|
|
/// destruction, populates an array of CFG nodes as well as corresponding
|
|
|
|
/// spanning tree nodes.
|
|
|
|
struct Graph {
|
|
|
|
uint32_t NumNodes;
|
|
|
|
Node *CFGNodes;
|
|
|
|
Node *SpanningTreeNodes;
|
2019-12-14 09:27:03 +08:00
|
|
|
uint64_t *EdgeFreqs;
|
|
|
|
uint64_t *CallFreqs;
|
2019-08-08 07:09:50 +08:00
|
|
|
BumpPtrAllocator &Alloc;
|
2019-12-14 09:27:03 +08:00
|
|
|
const FunctionDescription &D;
|
2019-08-08 07:09:50 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Reads a list of edges from function description \p D and builds
|
2019-08-08 07:09:50 +08:00
|
|
|
/// the graph from it. Allocates several internal dynamic structures that are
|
2019-12-14 09:27:03 +08:00
|
|
|
/// later destroyed by ~Graph() and uses \p Alloc. D.LeafNodes contain all
|
2019-08-08 07:09:50 +08:00
|
|
|
/// spanning tree leaf nodes descriptions (their counters). They are the seed
|
|
|
|
/// used to compute the rest of the missing edge counts in a bottom-up
|
|
|
|
/// traversal of the spanning tree.
|
2019-12-14 09:27:03 +08:00
|
|
|
Graph(BumpPtrAllocator &Alloc, const FunctionDescription &D,
|
|
|
|
const uint64_t *Counters, ProfileWriterContext &Ctx);
|
2019-08-08 07:09:50 +08:00
|
|
|
~Graph();
|
|
|
|
void dump() const;
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void computeEdgeFrequencies(const uint64_t *Counters,
|
|
|
|
ProfileWriterContext &Ctx);
|
|
|
|
void dumpEdgeFreqs() const;
|
2019-08-08 07:09:50 +08:00
|
|
|
};
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
Graph::Graph(BumpPtrAllocator &Alloc, const FunctionDescription &D,
|
|
|
|
const uint64_t *Counters, ProfileWriterContext &Ctx)
|
|
|
|
: Alloc(Alloc), D(D) {
|
2019-08-08 07:09:50 +08:00
|
|
|
DEBUG(reportNumber("G = 0x", (uint64_t)this, 16));
|
|
|
|
// First pass to determine number of nodes
|
2019-12-14 09:27:03 +08:00
|
|
|
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;
|
|
|
|
}
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2021-12-29 10:43:53 +08:00
|
|
|
for (int I = 0; I < D.NumLeafNodes; ++I)
|
2019-12-14 09:27:03 +08:00
|
|
|
if (static_cast<int32_t>(D.LeafNodes[I].Node) > MaxNodes)
|
|
|
|
MaxNodes = D.LeafNodes[I].Node;
|
2021-12-29 10:43:53 +08:00
|
|
|
|
|
|
|
for (int I = 0; I < D.NumCalls; ++I)
|
2019-12-14 09:27:03 +08:00
|
|
|
if (static_cast<int32_t>(D.Calls[I].FromNode) > MaxNodes)
|
|
|
|
MaxNodes = D.Calls[I].FromNode;
|
2021-12-29 10:43:53 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
// No nodes? Nothing to do
|
|
|
|
if (MaxNodes < 0) {
|
|
|
|
DEBUG(report("No nodes!\n"));
|
2019-08-08 07:09:50 +08:00
|
|
|
CFGNodes = nullptr;
|
|
|
|
SpanningTreeNodes = nullptr;
|
|
|
|
NumNodes = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++MaxNodes;
|
|
|
|
DEBUG(reportNumber("NumNodes = ", MaxNodes, 10));
|
2019-12-14 09:27:03 +08:00
|
|
|
NumNodes = static_cast<uint32_t>(MaxNodes);
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
// Initial allocations
|
|
|
|
CFGNodes = new (Alloc) Node[MaxNodes];
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
DEBUG(reportNumber("G->CFGNodes = 0x", (uint64_t)CFGNodes, 16));
|
|
|
|
SpanningTreeNodes = new (Alloc) Node[MaxNodes];
|
|
|
|
DEBUG(reportNumber("G->SpanningTreeNodes = 0x",
|
|
|
|
(uint64_t)SpanningTreeNodes, 16));
|
|
|
|
|
|
|
|
// Figure out how much to allocate to each vector (in/out edge sets)
|
2019-12-14 09:27:03 +08:00
|
|
|
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)
|
2019-08-08 07:09:50 +08:00
|
|
|
continue;
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
SpanningTreeNodes[D.Edges[I].FromNode].NumOutEdges++;
|
|
|
|
SpanningTreeNodes[D.Edges[I].ToNode].NumInEdges++;
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate in/out edge sets
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in/out edge sets
|
2019-12-14 09:27:03 +08:00
|
|
|
for (int I = 0; I < D.NumEdges; ++I) {
|
|
|
|
const uint32_t Src = D.Edges[I].FromNode;
|
|
|
|
const uint32_t Dst = D.Edges[I].ToNode;
|
2019-08-08 07:09:50 +08:00
|
|
|
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;
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
if (D.Edges[I].Counter != 0xffffffff)
|
2019-08-08 07:09:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
|
|
|
|
computeEdgeFrequencies(Counters, Ctx);
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Graph::~Graph() {
|
2019-12-14 09:27:03 +08:00
|
|
|
if (CallFreqs)
|
|
|
|
Alloc.deallocate(CallFreqs);
|
|
|
|
if (EdgeFreqs)
|
|
|
|
Alloc.deallocate(EdgeFreqs);
|
2019-08-08 07:09:50 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
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);
|
2019-08-08 07:09:50 +08:00
|
|
|
reportNumber(" Cnt: ", EdgeFreqs[I], 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Auxiliary map structure for fast lookups of which calls map to each node of
|
|
|
|
/// the function CFG
|
|
|
|
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) {
|
2021-04-08 15:19:26 +08:00
|
|
|
MapEntry &Entry = Entries[D.Calls[I].FromNode];
|
2019-12-14 09:27:03 +08:00
|
|
|
Entry.Calls[Entry.NumCalls++] = I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the frequency of all calls in node \p NodeID to Freq. However, if
|
|
|
|
/// the calls have their own counters and do not depend on the basic block
|
|
|
|
/// counter, this means they have landing pads and throw exceptions. In this
|
|
|
|
/// case, set their frequency with their counters and return the maximum
|
|
|
|
/// value observed in such counters. This will be used as the new frequency
|
|
|
|
/// at basic block entry. This is used to fix the CFG edge frequencies in the
|
|
|
|
/// presence of exceptions.
|
|
|
|
uint64_t visitAllCallsIn(uint32_t NodeID, uint64_t Freq, uint64_t *CallFreqs,
|
|
|
|
const FunctionDescription &D,
|
|
|
|
const uint64_t *Counters,
|
|
|
|
ProfileWriterContext &Ctx) const {
|
2021-04-08 15:19:26 +08:00
|
|
|
const MapEntry &Entry = Entries[NodeID];
|
2019-12-14 09:27:03 +08:00
|
|
|
uint64_t MaxValue = 0ull;
|
|
|
|
for (int I = 0, E = Entry.NumCalls; I != E; ++I) {
|
2021-04-08 15:19:26 +08:00
|
|
|
const uint32_t CallID = Entry.Calls[I];
|
2019-12-14 09:27:03 +08:00
|
|
|
DEBUG(reportNumber(" Setting freq for call ID: ", CallID, 10));
|
2021-04-08 15:19:26 +08:00
|
|
|
const CallDescription &CallDesc = D.Calls[CallID];
|
2019-12-14 09:27:03 +08:00
|
|
|
if (CallDesc.Counter == 0xffffffff) {
|
|
|
|
CallFreqs[CallID] = Freq;
|
|
|
|
DEBUG(reportNumber(" with : ", Freq, 10));
|
|
|
|
} else {
|
2021-04-08 15:19:26 +08:00
|
|
|
const uint64_t CounterVal = Counters[CallDesc.Counter];
|
2019-12-14 09:27:03 +08:00
|
|
|
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() {
|
2021-12-29 10:43:53 +08:00
|
|
|
for (int I = NumNodes - 1; I >= 0; --I)
|
2019-12-14 09:27:03 +08:00
|
|
|
if (Entries[I].Calls)
|
|
|
|
Alloc.deallocate(Entries[I].Calls);
|
|
|
|
Alloc.deallocate(Entries);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Fill an array with the frequency of each edge in the function represented
|
|
|
|
/// by G, as well as another array for each call.
|
|
|
|
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;
|
2019-08-08 07:09:50 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
// Setup a lookup for calls present in each node (BB)
|
|
|
|
NodeToCallsMap *CallMap = new (Alloc) NodeToCallsMap(Alloc, D, NumNodes);
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
// Perform a bottom-up, BFS traversal of the spanning tree in G. Edges in the
|
|
|
|
// spanning tree don't have explicit counters. We must infer their value using
|
|
|
|
// a linear combination of other counters (sum of counters of the outgoing
|
|
|
|
// edges minus sum of counters of the incoming edges).
|
2019-12-14 09:27:03 +08:00
|
|
|
uint32_t *Stack = new (Alloc) uint32_t [NumNodes];
|
2019-08-08 07:09:50 +08:00
|
|
|
uint32_t StackTop = 0;
|
|
|
|
enum Status : uint8_t { S_NEW = 0, S_VISITING, S_VISITED };
|
2019-12-14 09:27:03 +08:00
|
|
|
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];
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
// Setup a fast lookup for frequency of leaf nodes, which have special
|
|
|
|
// basic block frequency instrumentation (they are not edge profiled).
|
2019-12-14 09:27:03 +08:00
|
|
|
for (int I = 0; I < D.NumLeafNodes; ++I) {
|
|
|
|
LeafFrequency[D.LeafNodes[I].Node] = Counters[D.LeafNodes[I].Counter];
|
2019-08-08 07:09:50 +08:00
|
|
|
DEBUG({
|
2019-12-14 09:27:03 +08:00
|
|
|
if (Counters[D.LeafNodes[I].Counter] > 0) {
|
|
|
|
reportNumber("Leaf Node# ", D.LeafNodes[I].Node, 10);
|
|
|
|
reportNumber(" Counter: ", Counters[D.LeafNodes[I].Counter], 10);
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
});
|
2019-12-14 09:27:03 +08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
});
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
// Add all root nodes to the stack
|
2021-12-29 10:43:53 +08:00
|
|
|
for (int I = 0; I < NumNodes; ++I)
|
2019-12-14 09:27:03 +08:00
|
|
|
if (SpanningTreeNodes[I].NumInEdges == 0)
|
2019-08-08 07:09:50 +08:00
|
|
|
Stack[StackTop++] = I;
|
2021-12-29 10:43:53 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
// Empty stack?
|
|
|
|
if (StackTop == 0) {
|
2019-12-14 09:27:03 +08:00
|
|
|
DEBUG(report("Empty stack!\n"));
|
|
|
|
Alloc.deallocate(EntryAddress);
|
2019-08-08 07:09:50 +08:00
|
|
|
Alloc.deallocate(LeafFrequency);
|
|
|
|
Alloc.deallocate(Visited);
|
|
|
|
Alloc.deallocate(Stack);
|
2019-12-14 09:27:03 +08:00
|
|
|
CallMap->~NodeToCallsMap();
|
|
|
|
Alloc.deallocate(CallMap);
|
|
|
|
if (CallFreqs)
|
|
|
|
Alloc.deallocate(CallFreqs);
|
|
|
|
if (EdgeFreqs)
|
|
|
|
Alloc.deallocate(EdgeFreqs);
|
|
|
|
EdgeFreqs = nullptr;
|
|
|
|
CallFreqs = nullptr;
|
|
|
|
return;
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
// Add all known edge counts, will infer the rest
|
2019-12-14 09:27:03 +08:00
|
|
|
for (int I = 0; I < D.NumEdges; ++I) {
|
|
|
|
const uint32_t C = D.Edges[I].Counter;
|
2019-08-08 07:09:50 +08:00
|
|
|
if (C == 0xffffffff) // inferred counter - we will compute its value
|
|
|
|
continue;
|
2019-12-14 09:27:03 +08:00
|
|
|
EdgeFreqs[I] = Counters[C];
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (StackTop > 0) {
|
|
|
|
const uint32_t Cur = Stack[--StackTop];
|
|
|
|
DEBUG({
|
|
|
|
if (Visited[Cur] == S_VISITING)
|
|
|
|
report("(visiting) ");
|
|
|
|
else
|
|
|
|
report("(new) ");
|
|
|
|
reportNumber("Cur: ", Cur, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
// This shouldn't happen in a tree
|
|
|
|
assert(Visited[Cur] != S_VISITED, "should not have visited nodes in stack");
|
|
|
|
if (Visited[Cur] == S_NEW) {
|
|
|
|
Visited[Cur] = S_VISITING;
|
|
|
|
Stack[StackTop++] = Cur;
|
2019-12-14 09:27:03 +08:00
|
|
|
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;
|
2019-08-08 07:09:50 +08:00
|
|
|
Stack[StackTop++] = Succ;
|
2019-12-14 09:27:03 +08:00
|
|
|
assert(StackTop <= NumNodes, "stack grew too large");
|
2021-12-29 10:43:53 +08:00
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Visited[Cur] = S_VISITED;
|
|
|
|
|
|
|
|
// Establish our node frequency based on outgoing edges, which should all be
|
|
|
|
// resolved by now.
|
|
|
|
int64_t CurNodeFreq = LeafFrequency[Cur];
|
|
|
|
// Not a leaf?
|
|
|
|
if (!CurNodeFreq) {
|
2019-12-14 09:27:03 +08:00
|
|
|
for (int I = 0, E = CFGNodes[Cur].NumOutEdges; I != E; ++I) {
|
|
|
|
const uint32_t SuccEdge = CFGNodes[Cur].OutEdges[I].ID;
|
|
|
|
CurNodeFreq += EdgeFreqs[SuccEdge];
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
if (CurNodeFreq < 0)
|
|
|
|
CurNodeFreq = 0;
|
|
|
|
|
|
|
|
const uint64_t CallFreq = CallMap->visitAllCallsIn(
|
|
|
|
Cur, CurNodeFreq > 0 ? CurNodeFreq : 0, CallFreqs, D, Counters, Ctx);
|
|
|
|
|
|
|
|
// Exception handling affected our output flow? Fix with calls info
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No parent? Reached a tree root, limit to call frequency updating.
|
2021-12-29 10:43:53 +08:00
|
|
|
if (SpanningTreeNodes[Cur].NumInEdges == 0)
|
2019-12-14 09:27:03 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
assert(SpanningTreeNodes[Cur].NumInEdges == 1, "must have 1 parent");
|
|
|
|
const uint32_t Parent = SpanningTreeNodes[Cur].InEdges[0].Node;
|
|
|
|
const uint32_t ParentEdge = SpanningTreeNodes[Cur].InEdges[0].ID;
|
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
// Calculate parent edge freq.
|
2019-12-14 09:27:03 +08:00
|
|
|
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];
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
// Sometimes the conservative CFG that BOLT builds will lead to incorrect
|
|
|
|
// flow computation. For example, in a BB that transitively calls the exit
|
|
|
|
// syscall, BOLT will add a fall-through successor even though it should not
|
|
|
|
// have any successors. So this block execution will likely be wrong. We
|
|
|
|
// tolerate this imperfection since this case should be quite infrequent.
|
|
|
|
if (ParentEdgeFreq < 0) {
|
2019-12-14 09:27:03 +08:00
|
|
|
DEBUG(dumpEdgeFreqs());
|
2019-08-08 07:09:50 +08:00
|
|
|
DEBUG(report("WARNING: incorrect flow"));
|
|
|
|
ParentEdgeFreq = 0;
|
|
|
|
}
|
|
|
|
DEBUG(reportNumber(" Setting freq for ParentEdge: ", ParentEdge, 10));
|
|
|
|
DEBUG(reportNumber(" with ParentEdgeFreq: ", ParentEdgeFreq, 10));
|
2019-12-14 09:27:03 +08:00
|
|
|
EdgeFreqs[ParentEdge] = ParentEdgeFreq;
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
Alloc.deallocate(EntryAddress);
|
2019-08-08 07:09:50 +08:00
|
|
|
Alloc.deallocate(LeafFrequency);
|
|
|
|
Alloc.deallocate(Visited);
|
|
|
|
Alloc.deallocate(Stack);
|
2019-12-14 09:27:03 +08:00
|
|
|
CallMap->~NodeToCallsMap();
|
|
|
|
Alloc.deallocate(CallMap);
|
|
|
|
DEBUG(dumpEdgeFreqs());
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Write to \p FD all of the edge profiles for function \p FuncDesc. Uses
|
|
|
|
/// \p Alloc to allocate helper dynamic structures used to compute profile for
|
|
|
|
/// edges that we do not explictly instrument.
|
|
|
|
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();
|
2019-08-08 07:09:50 +08:00
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
#if !defined(__APPLE__)
|
|
|
|
uint64_t *bolt_instr_locations = __bolt_instr_locations;
|
|
|
|
#else
|
|
|
|
uint64_t *bolt_instr_locations = _bolt_instr_locations_getter();
|
|
|
|
#endif
|
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
// Skip funcs we know are cold
|
|
|
|
#ifndef ENABLE_DEBUG
|
2019-12-14 09:27:03 +08:00
|
|
|
uint64_t CountersFreq = 0;
|
2021-12-29 10:43:53 +08:00
|
|
|
for (int I = 0; I < F.NumLeafNodes; ++I)
|
2021-01-29 04:44:14 +08:00
|
|
|
CountersFreq += bolt_instr_locations[F.LeafNodes[I].Counter];
|
2021-12-29 10:43:53 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
if (CountersFreq == 0) {
|
|
|
|
for (int I = 0; I < F.NumEdges; ++I) {
|
|
|
|
const uint32_t C = F.Edges[I].Counter;
|
|
|
|
if (C == 0xffffffff)
|
|
|
|
continue;
|
2021-01-29 04:44:14 +08:00
|
|
|
CountersFreq += bolt_instr_locations[C];
|
2019-12-14 09:27:03 +08:00
|
|
|
}
|
|
|
|
if (CountersFreq == 0) {
|
|
|
|
for (int I = 0; I < F.NumCalls; ++I) {
|
|
|
|
const uint32_t C = F.Calls[I].Counter;
|
|
|
|
if (C == 0xffffffff)
|
|
|
|
continue;
|
2021-01-29 04:44:14 +08:00
|
|
|
CountersFreq += bolt_instr_locations[C];
|
2019-12-14 09:27:03 +08:00
|
|
|
}
|
|
|
|
if (CountersFreq == 0)
|
|
|
|
return next;
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
Graph *G = new (Alloc) Graph(Alloc, F, bolt_instr_locations, Ctx);
|
2019-08-08 07:09:50 +08:00
|
|
|
DEBUG(G->dump());
|
2021-01-29 04:44:14 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
if (!G->EdgeFreqs && !G->CallFreqs) {
|
2019-08-08 07:09:50 +08:00
|
|
|
G->~Graph();
|
|
|
|
Alloc.deallocate(G);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
for (int I = 0; I < F.NumEdges; ++I) {
|
|
|
|
const uint64_t Freq = G->EdgeFreqs[I];
|
2019-08-08 07:09:50 +08:00
|
|
|
if (Freq == 0)
|
|
|
|
continue;
|
2019-12-14 09:27:03 +08:00
|
|
|
const EdgeDescription *Desc = &F.Edges[I];
|
2019-08-08 07:09:50 +08:00
|
|
|
char LineBuf[BufSize];
|
|
|
|
char *Ptr = LineBuf;
|
2019-12-14 09:27:03 +08:00
|
|
|
Ptr = serializeLoc(Ctx, Ptr, Desc->From, BufSize);
|
|
|
|
Ptr = serializeLoc(Ctx, Ptr, Desc->To, BufSize - (Ptr - LineBuf));
|
2019-08-08 07:09:50 +08:00
|
|
|
Ptr = strCopy(Ptr, "0 ", BufSize - (Ptr - LineBuf) - 22);
|
|
|
|
Ptr = intToStr(Ptr, Freq, 10);
|
|
|
|
*Ptr++ = '\n';
|
|
|
|
__write(FD, LineBuf, Ptr - LineBuf);
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-08 07:09:50 +08:00
|
|
|
G->~Graph();
|
|
|
|
Alloc.deallocate(G);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
#if !defined(__APPLE__)
|
2019-12-14 09:27:03 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Write a single indirect call <src, target> pair to the fdata file
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|
2019-08-03 02:20:13 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Write to \p FD all of the indirect call profiles.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check a single call flow for a callee versus all known callers. If there are
|
|
|
|
/// less callers than what the callee expects, write the difference with source
|
|
|
|
/// [unknown] in the profile.
|
|
|
|
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) {
|
|
|
|
// There is probably something wrong with this callee and this should be
|
|
|
|
// investigated, but I don't want to assert and lose all data collected.
|
|
|
|
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);
|
|
|
|
}
|
2019-08-08 07:09:50 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Open fdata file for writing and return a valid file descriptor, aborting
|
|
|
|
/// program upon failure.
|
|
|
|
int openProfile() {
|
|
|
|
// Build the profile name string by appending our PID
|
|
|
|
char Buf[BufSize];
|
|
|
|
char *Ptr = Buf;
|
|
|
|
uint64_t PID = __getpid();
|
|
|
|
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,
|
2019-07-25 05:03:43 +08:00
|
|
|
/*flags=*/0x241 /*O_WRONLY|O_TRUNC|O_CREAT*/,
|
|
|
|
/*mode=*/0666);
|
2019-08-08 07:09:50 +08:00
|
|
|
if (static_cast<int64_t>(FD) < 0) {
|
2019-12-14 09:27:03 +08:00
|
|
|
report("Error while trying to open profile file for writing: ");
|
|
|
|
report(Buf);
|
|
|
|
reportNumber("\nFailed with error number: 0x",
|
2019-08-08 07:09:50 +08:00
|
|
|
0 - static_cast<int64_t>(FD), 16);
|
|
|
|
__exit(1);
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
return FD;
|
|
|
|
}
|
2021-01-29 04:44:14 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
} // anonymous namespace
|
2019-08-08 07:09:50 +08:00
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
#if !defined(__APPLE__)
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
/// Reset all counters in case you want to start profiling a new phase of your
|
|
|
|
/// program independently of prior phases.
|
|
|
|
/// The address of this function is printed by BOLT and this can be called by
|
|
|
|
/// any attached debugger during runtime. There is a useful oneliner for gdb:
|
|
|
|
///
|
|
|
|
/// gdb -p $(pgrep -xo PROCESSNAME) -ex 'p ((void(*)())0xdeadbeef)()' \
|
|
|
|
/// -ex 'set confirm off' -ex quit
|
|
|
|
///
|
|
|
|
/// Where 0xdeadbeef is this function address and PROCESSNAME your binary file
|
|
|
|
/// name.
|
|
|
|
extern "C" void __bolt_instr_clear_counters() {
|
|
|
|
memSet(reinterpret_cast<char *>(__bolt_instr_locations), 0,
|
|
|
|
__bolt_num_counters * 8);
|
2021-12-29 10:43:53 +08:00
|
|
|
for (int I = 0; I < __bolt_instr_num_ind_calls; ++I)
|
2019-12-14 09:27:03 +08:00
|
|
|
GlobalIndCallCounters[I].resetCounters();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is the entry point for profile writing.
|
|
|
|
/// There are three ways of getting here:
|
|
|
|
///
|
|
|
|
/// * Program execution ended, finalization methods are running and BOLT
|
|
|
|
/// hooked into FINI from your binary dynamic section;
|
|
|
|
/// * You used the sleep timer option and during initialization we forked
|
|
|
|
/// a separete process that will call this function periodically;
|
|
|
|
/// * BOLT prints this function address so you can attach a debugger and
|
|
|
|
/// call this function directly to get your profile written to disk
|
|
|
|
/// on demand.
|
|
|
|
///
|
2021-06-19 04:08:35 +08:00
|
|
|
extern "C" void __attribute((force_align_arg_pointer))
|
|
|
|
__bolt_instr_data_dump() {
|
2019-12-14 09:27:03 +08:00
|
|
|
// Already dumping
|
|
|
|
if (!GlobalWriteProfileMutex->acquire())
|
|
|
|
return;
|
|
|
|
|
|
|
|
BumpPtrAllocator HashAlloc;
|
|
|
|
HashAlloc.setMaxSize(0x6400000);
|
|
|
|
ProfileWriterContext Ctx = readDescriptions();
|
|
|
|
Ctx.CallFlowTable = new (HashAlloc, 0) CallFlowHashTable(HashAlloc);
|
|
|
|
|
|
|
|
DEBUG(printStats(Ctx));
|
|
|
|
|
|
|
|
int FD = openProfile();
|
2019-08-08 07:09:50 +08:00
|
|
|
|
|
|
|
BumpPtrAllocator Alloc;
|
2019-12-14 09:27:03 +08:00
|
|
|
const uint8_t *FuncDesc = Ctx.FuncDescriptions;
|
2019-08-08 07:09:50 +08:00
|
|
|
for (int I = 0, E = __bolt_instr_num_funcs; I < E; ++I) {
|
2019-12-14 09:27:03 +08:00
|
|
|
FuncDesc = writeFunctionProfile(FD, Ctx, FuncDesc, Alloc);
|
|
|
|
Alloc.clear();
|
2019-08-08 07:09:50 +08:00
|
|
|
DEBUG(reportNumber("FuncDesc now: ", (uint64_t)FuncDesc, 16));
|
|
|
|
}
|
2019-12-14 09:27:03 +08:00
|
|
|
assert(FuncDesc == (void *)Ctx.Strings,
|
2019-08-08 07:09:50 +08:00
|
|
|
"FuncDesc ptr must be equal to stringtable");
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
writeIndirectCallProfile(FD, Ctx);
|
|
|
|
Ctx.CallFlowTable->forEachElement(visitCallFlowEntry, FD, &Ctx);
|
|
|
|
|
2021-10-16 01:46:09 +08:00
|
|
|
__fsync(FD);
|
2019-08-03 02:20:13 +08:00
|
|
|
__close(FD);
|
2019-12-14 09:27:03 +08:00
|
|
|
__munmap(Ctx.MMapPtr, Ctx.MMapSize);
|
|
|
|
__close(Ctx.FileDesc);
|
|
|
|
HashAlloc.destroy();
|
|
|
|
GlobalWriteProfileMutex->release();
|
|
|
|
DEBUG(report("Finished writing profile.\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Event loop for our child process spawned during setup to dump profile data
|
|
|
|
/// at user-specified intervals
|
|
|
|
void watchProcess() {
|
|
|
|
timespec ts, rem;
|
|
|
|
uint64_t Ellapsed = 0ull;
|
2021-03-10 08:18:11 +08:00
|
|
|
uint64_t ppid;
|
|
|
|
if (__bolt_instr_wait_forks) {
|
|
|
|
// Store parent pgid
|
|
|
|
ppid = -__getpgid(0);
|
|
|
|
// And leave parent process group
|
|
|
|
__setpgid(0, 0);
|
|
|
|
} else {
|
|
|
|
// Store parent pid
|
|
|
|
ppid = __getppid();
|
|
|
|
if (ppid == 1) {
|
|
|
|
// Parent already dead
|
2021-10-16 01:46:09 +08:00
|
|
|
__bolt_instr_data_dump();
|
2021-03-10 08:18:11 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
ts.tv_sec = 1;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
while (1) {
|
|
|
|
__nanosleep(&ts, &rem);
|
2021-03-10 08:18:11 +08:00
|
|
|
// This means our parent process or all its forks are dead,
|
|
|
|
// so no need for us to keep dumping.
|
|
|
|
if (__kill(ppid, 0) < 0) {
|
|
|
|
if (__bolt_instr_no_counters_clear)
|
|
|
|
__bolt_instr_data_dump();
|
2019-12-14 09:27:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-03-10 08:18:11 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
if (++Ellapsed < __bolt_instr_sleep_time)
|
|
|
|
continue;
|
2021-03-10 08:18:11 +08:00
|
|
|
|
2019-12-14 09:27:03 +08:00
|
|
|
Ellapsed = 0;
|
|
|
|
__bolt_instr_data_dump();
|
2021-03-10 08:18:11 +08:00
|
|
|
if (__bolt_instr_no_counters_clear == false)
|
|
|
|
__bolt_instr_clear_counters();
|
2019-12-14 09:27:03 +08:00
|
|
|
}
|
2021-03-10 08:18:11 +08:00
|
|
|
|
|
|
|
out:;
|
2019-12-14 09:27:03 +08:00
|
|
|
DEBUG(report("My parent process is dead, bye!\n"));
|
|
|
|
__exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void __bolt_instr_indirect_call();
|
|
|
|
extern "C" void __bolt_instr_indirect_tailcall();
|
|
|
|
|
|
|
|
/// Initialization code
|
2021-06-19 04:08:35 +08:00
|
|
|
extern "C" void __attribute((force_align_arg_pointer)) __bolt_instr_setup() {
|
2019-12-14 09:27:03 +08:00
|
|
|
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");
|
|
|
|
// Maps our counters to be shared instead of private, so we keep counting for
|
|
|
|
// forked processes
|
|
|
|
__mmap(CountersStart, CountersEnd - CountersStart,
|
|
|
|
0x3 /*PROT_READ|PROT_WRITE*/,
|
|
|
|
0x31 /*MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED*/, -1, 0);
|
|
|
|
|
2021-06-24 02:24:09 +08:00
|
|
|
__bolt_ind_call_counter_func_pointer = __bolt_instr_indirect_call;
|
|
|
|
__bolt_ind_tailcall_counter_func_pointer = __bolt_instr_indirect_tailcall;
|
2019-12-14 09:27:03 +08:00
|
|
|
// Conservatively reserve 100MiB shared pages
|
|
|
|
GlobalAlloc.setMaxSize(0x6400000);
|
|
|
|
GlobalAlloc.setShared(true);
|
|
|
|
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) {
|
2021-03-10 08:18:11 +08:00
|
|
|
// Separate instrumented process to the own process group
|
|
|
|
if (__bolt_instr_wait_forks)
|
|
|
|
__setpgid(0, 0);
|
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
if (long PID = __fork())
|
2019-12-14 09:27:03 +08:00
|
|
|
return;
|
|
|
|
watchProcess();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 02:24:09 +08:00
|
|
|
extern "C" __attribute((force_align_arg_pointer)) void
|
|
|
|
instrumentIndirectCall(uint64_t Target, uint64_t IndCallID) {
|
2019-12-14 09:27:03 +08:00
|
|
|
GlobalIndCallCounters[IndCallID].incrementVal(Target, GlobalAlloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We receive as in-stack arguments the identifier of the indirect call site
|
|
|
|
/// as well as the target address for the call
|
|
|
|
extern "C" __attribute((naked)) void __bolt_instr_indirect_call()
|
|
|
|
{
|
|
|
|
__asm__ __volatile__(SAVE_ALL
|
2021-06-24 02:24:09 +08:00
|
|
|
"mov 0xa0(%%rsp), %%rdi\n"
|
|
|
|
"mov 0x98(%%rsp), %%rsi\n"
|
2019-12-14 09:27:03 +08:00
|
|
|
"call instrumentIndirectCall\n"
|
|
|
|
RESTORE_ALL
|
2021-06-24 02:24:09 +08:00
|
|
|
"ret\n"
|
2019-12-14 09:27:03 +08:00
|
|
|
:::);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __attribute((naked)) void __bolt_instr_indirect_tailcall()
|
|
|
|
{
|
|
|
|
__asm__ __volatile__(SAVE_ALL
|
2021-06-24 02:24:09 +08:00
|
|
|
"mov 0x98(%%rsp), %%rdi\n"
|
|
|
|
"mov 0x90(%%rsp), %%rsi\n"
|
2019-12-14 09:27:03 +08:00
|
|
|
"call instrumentIndirectCall\n"
|
|
|
|
RESTORE_ALL
|
2021-06-24 02:24:09 +08:00
|
|
|
"ret\n"
|
2019-12-14 09:27:03 +08:00
|
|
|
:::);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is hooking ELF's entry, it needs to save all machine state.
|
|
|
|
extern "C" __attribute((naked)) void __bolt_instr_start()
|
|
|
|
{
|
|
|
|
__asm__ __volatile__(SAVE_ALL
|
|
|
|
"call __bolt_instr_setup\n"
|
|
|
|
RESTORE_ALL
|
2021-06-19 04:08:35 +08:00
|
|
|
"jmp __bolt_start_trampoline\n"
|
2019-12-14 09:27:03 +08:00
|
|
|
:::);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is hooking into ELF's DT_FINI
|
|
|
|
extern "C" void __bolt_instr_fini() {
|
2021-07-31 05:29:23 +08:00
|
|
|
__bolt_fini_trampoline();
|
2019-12-14 09:27:03 +08:00
|
|
|
if (__bolt_instr_sleep_time == 0)
|
|
|
|
__bolt_instr_data_dump();
|
|
|
|
DEBUG(report("Finished.\n"));
|
2019-07-25 05:03:43 +08:00
|
|
|
}
|
2020-10-15 18:51:56 +08:00
|
|
|
|
2021-01-29 04:32:03 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
2020-10-15 18:51:56 +08:00
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:51:56 +08:00
|
|
|
// On OSX/iOS the final symbol name of an extern "C" function/variable contains
|
|
|
|
// one extra leading underscore: _bolt_instr_setup -> __bolt_instr_setup.
|
2021-01-29 04:32:03 +08:00
|
|
|
extern "C"
|
|
|
|
__attribute__((section("__TEXT,__setup")))
|
|
|
|
__attribute__((force_align_arg_pointer))
|
|
|
|
void _bolt_instr_setup() {
|
2021-01-29 04:44:14 +08:00
|
|
|
__asm__ __volatile__(SAVE_ALL :::);
|
2021-01-29 04:32:03 +08:00
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
report("Hello!\n");
|
2021-01-29 04:32:03 +08:00
|
|
|
|
2021-01-29 04:44:14 +08:00
|
|
|
__asm__ __volatile__(RESTORE_ALL :::);
|
2020-11-18 05:57:29 +08:00
|
|
|
}
|
2020-10-15 18:51:56 +08:00
|
|
|
|
2021-01-29 04:32:03 +08:00
|
|
|
extern "C"
|
|
|
|
__attribute__((section("__TEXT,__fini")))
|
|
|
|
__attribute__((force_align_arg_pointer))
|
|
|
|
void _bolt_instr_fini() {
|
2021-01-29 04:44:14 +08:00
|
|
|
report("Bye!\n");
|
|
|
|
__bolt_instr_data_dump();
|
2020-11-20 10:18:28 +08:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:51:56 +08:00
|
|
|
#endif
|
2021-10-16 22:35:29 +08:00
|
|
|
#endif
|