2016-05-26 01:49:00 +08:00
|
|
|
//===-- cache_frag.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of EfficiencySanitizer, a family of performance tuners.
|
|
|
|
//
|
|
|
|
// This file contains cache fragmentation-specific code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "esan.h"
|
2016-06-04 04:48:17 +08:00
|
|
|
#include "esan_flags.h"
|
2016-06-03 02:45:25 +08:00
|
|
|
#include "sanitizer_common/sanitizer_addrhashmap.h"
|
2016-06-04 04:48:17 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2016-06-03 02:45:25 +08:00
|
|
|
#include "sanitizer_common/sanitizer_placement_new.h"
|
2016-06-04 04:48:17 +08:00
|
|
|
#include <string.h>
|
2016-05-26 01:49:00 +08:00
|
|
|
|
|
|
|
namespace __esan {
|
|
|
|
|
2016-06-03 02:45:25 +08:00
|
|
|
//===-- Struct field access counter runtime -------------------------------===//
|
|
|
|
|
2016-06-01 05:27:39 +08:00
|
|
|
// This should be kept consistent with LLVM's EfficiencySanitizer StructInfo.
|
|
|
|
struct StructInfo {
|
|
|
|
const char *StructName;
|
2016-06-10 10:10:34 +08:00
|
|
|
u32 Size;
|
2016-06-03 02:45:25 +08:00
|
|
|
u32 NumFields;
|
2016-07-07 11:20:24 +08:00
|
|
|
u32 *FieldOffset; // auxiliary struct field info.
|
|
|
|
u32 *FieldSize; // auxiliary struct field info.
|
|
|
|
const char **FieldTypeName; // auxiliary struct field info.
|
2016-07-02 11:25:55 +08:00
|
|
|
u64 *FieldCounters;
|
|
|
|
u64 *ArrayCounter;
|
2016-07-07 11:20:24 +08:00
|
|
|
bool hasAuxFieldInfo() { return FieldOffset != nullptr; }
|
2016-06-01 05:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// This should be kept consistent with LLVM's EfficiencySanitizer CacheFragInfo.
|
|
|
|
// The tool-specific information per compilation unit (module).
|
|
|
|
struct CacheFragInfo {
|
|
|
|
const char *UnitName;
|
2016-06-03 02:45:25 +08:00
|
|
|
u32 NumStructs;
|
2016-06-01 05:27:39 +08:00
|
|
|
StructInfo *Structs;
|
|
|
|
};
|
|
|
|
|
2016-06-03 02:45:25 +08:00
|
|
|
struct StructCounter {
|
|
|
|
StructInfo *Struct;
|
2016-06-16 01:27:29 +08:00
|
|
|
u64 Count; // The total access count of the struct.
|
|
|
|
u64 Ratio; // Difference ratio for the struct layout access.
|
2016-06-03 02:45:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// We use StructHashMap to keep track of an unique copy of StructCounter.
|
|
|
|
typedef AddrHashMap<StructCounter, 31051> StructHashMap;
|
|
|
|
struct Context {
|
|
|
|
StructHashMap StructMap;
|
|
|
|
u32 NumStructs;
|
|
|
|
u64 TotalCount; // The total access count of all structs.
|
|
|
|
};
|
|
|
|
static Context *Ctx;
|
|
|
|
|
2016-06-04 04:48:17 +08:00
|
|
|
static void reportStructSummary() {
|
|
|
|
// FIXME: provide a better struct field access summary report.
|
2016-06-16 01:27:29 +08:00
|
|
|
Report("%s: total struct field access count = %llu\n", SanitizerToolName,
|
|
|
|
Ctx->TotalCount);
|
2016-06-04 04:48:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: we are still exploring proper ways to evaluate the difference between
|
|
|
|
// struct field counts. Currently, we use a simple formula to calculate the
|
|
|
|
// difference ratio: V1/V2.
|
|
|
|
static inline u64 computeDifferenceRatio(u64 Val1, u64 Val2) {
|
2016-06-16 01:27:29 +08:00
|
|
|
if (Val2 > Val1) {
|
|
|
|
Swap(Val1, Val2);
|
|
|
|
}
|
|
|
|
if (Val2 == 0)
|
|
|
|
Val2 = 1;
|
2016-06-04 04:48:17 +08:00
|
|
|
return (Val1 / Val2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportStructCounter(StructHashMap::Handle &Handle) {
|
2016-06-16 01:27:29 +08:00
|
|
|
const u32 TypePrintLimit = 512;
|
2016-06-04 04:48:17 +08:00
|
|
|
const char *type, *start, *end;
|
|
|
|
StructInfo *Struct = Handle->Struct;
|
|
|
|
// Union field address calculation is done via bitcast instead of GEP,
|
|
|
|
// so the count for union is always 0.
|
|
|
|
// We skip the union report to avoid confusion.
|
|
|
|
if (strncmp(Struct->StructName, "union.", 6) == 0)
|
|
|
|
return;
|
|
|
|
// Remove the '.' after class/struct during print.
|
|
|
|
if (strncmp(Struct->StructName, "class.", 6) == 0) {
|
|
|
|
type = "class";
|
|
|
|
start = &Struct->StructName[6];
|
|
|
|
} else {
|
|
|
|
type = "struct";
|
|
|
|
start = &Struct->StructName[7];
|
|
|
|
}
|
2016-09-22 16:36:54 +08:00
|
|
|
// Remove the suffixes with '$' during print.
|
|
|
|
end = strchr(start, '$');
|
2016-06-04 04:48:17 +08:00
|
|
|
CHECK(end != nullptr);
|
|
|
|
Report(" %s %.*s\n", type, end - start, start);
|
2016-07-02 11:25:55 +08:00
|
|
|
Report(" size = %u, count = %llu, ratio = %llu, array access = %llu\n",
|
|
|
|
Struct->Size, Handle->Count, Handle->Ratio, *Struct->ArrayCounter);
|
2016-07-07 11:20:24 +08:00
|
|
|
if (Struct->hasAuxFieldInfo()) {
|
|
|
|
for (u32 i = 0; i < Struct->NumFields; ++i) {
|
|
|
|
Report(" #%2u: offset = %u,\t size = %u,"
|
|
|
|
"\t count = %llu,\t type = %.*s\n",
|
|
|
|
i, Struct->FieldOffset[i], Struct->FieldSize[i],
|
|
|
|
Struct->FieldCounters[i], TypePrintLimit, Struct->FieldTypeName[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (u32 i = 0; i < Struct->NumFields; ++i) {
|
|
|
|
Report(" #%2u: count = %llu\n", i, Struct->FieldCounters[i]);
|
|
|
|
}
|
2016-06-04 04:48:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void computeStructRatio(StructHashMap::Handle &Handle) {
|
|
|
|
Handle->Ratio = 0;
|
|
|
|
Handle->Count = Handle->Struct->FieldCounters[0];
|
|
|
|
for (u32 i = 1; i < Handle->Struct->NumFields; ++i) {
|
|
|
|
Handle->Count += Handle->Struct->FieldCounters[i];
|
|
|
|
Handle->Ratio += computeDifferenceRatio(
|
|
|
|
Handle->Struct->FieldCounters[i - 1], Handle->Struct->FieldCounters[i]);
|
|
|
|
}
|
|
|
|
Ctx->TotalCount += Handle->Count;
|
2016-06-16 01:27:29 +08:00
|
|
|
if (Handle->Ratio >= (u64)getFlags()->report_threshold ||
|
|
|
|
(Verbosity() >= 1 && Handle->Count > 0))
|
2016-06-04 04:48:17 +08:00
|
|
|
reportStructCounter(Handle);
|
|
|
|
}
|
|
|
|
|
2016-06-03 02:45:25 +08:00
|
|
|
static void registerStructInfo(CacheFragInfo *CacheFrag) {
|
|
|
|
for (u32 i = 0; i < CacheFrag->NumStructs; ++i) {
|
|
|
|
StructInfo *Struct = &CacheFrag->Structs[i];
|
|
|
|
StructHashMap::Handle H(&Ctx->StructMap, (uptr)Struct->FieldCounters);
|
|
|
|
if (H.created()) {
|
2016-06-04 04:48:17 +08:00
|
|
|
VPrintf(2, " Register %s: %u fields\n", Struct->StructName,
|
|
|
|
Struct->NumFields);
|
2016-06-03 02:45:25 +08:00
|
|
|
H->Struct = Struct;
|
|
|
|
++Ctx->NumStructs;
|
|
|
|
} else {
|
2016-06-04 04:48:17 +08:00
|
|
|
VPrintf(2, " Duplicated %s: %u fields\n", Struct->StructName,
|
|
|
|
Struct->NumFields);
|
2016-06-03 02:45:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unregisterStructInfo(CacheFragInfo *CacheFrag) {
|
|
|
|
// FIXME: if the library is unloaded before finalizeCacheFrag, we should
|
|
|
|
// collect the result for later report.
|
|
|
|
for (u32 i = 0; i < CacheFrag->NumStructs; ++i) {
|
|
|
|
StructInfo *Struct = &CacheFrag->Structs[i];
|
|
|
|
StructHashMap::Handle H(&Ctx->StructMap, (uptr)Struct->FieldCounters, true);
|
|
|
|
if (H.exists()) {
|
2016-06-04 04:48:17 +08:00
|
|
|
VPrintf(2, " Unregister %s: %u fields\n", Struct->StructName,
|
|
|
|
Struct->NumFields);
|
|
|
|
// FIXME: we should move this call to finalizeCacheFrag once we can
|
|
|
|
// iterate over the hash map there.
|
|
|
|
computeStructRatio(H);
|
2016-06-03 02:45:25 +08:00
|
|
|
--Ctx->NumStructs;
|
|
|
|
} else {
|
2016-06-04 04:48:17 +08:00
|
|
|
VPrintf(2, " Duplicated %s: %u fields\n", Struct->StructName,
|
|
|
|
Struct->NumFields);
|
2016-06-03 02:45:25 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-04 04:48:17 +08:00
|
|
|
static bool Reported = false;
|
|
|
|
if (Ctx->NumStructs == 0 && !Reported) {
|
|
|
|
Reported = true;
|
|
|
|
reportStructSummary();
|
|
|
|
}
|
2016-06-03 02:45:25 +08:00
|
|
|
}
|
|
|
|
|
2016-05-26 01:49:00 +08:00
|
|
|
//===-- Init/exit functions -----------------------------------------------===//
|
|
|
|
|
|
|
|
void processCacheFragCompilationUnitInit(void *Ptr) {
|
2016-06-01 05:27:39 +08:00
|
|
|
CacheFragInfo *CacheFrag = (CacheFragInfo *)Ptr;
|
2016-06-04 04:48:17 +08:00
|
|
|
VPrintf(2, "in esan::%s: %s with %u class(es)/struct(s)\n", __FUNCTION__,
|
|
|
|
CacheFrag->UnitName, CacheFrag->NumStructs);
|
2016-06-03 02:45:25 +08:00
|
|
|
registerStructInfo(CacheFrag);
|
2016-05-26 01:49:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void processCacheFragCompilationUnitExit(void *Ptr) {
|
2016-06-01 05:27:39 +08:00
|
|
|
CacheFragInfo *CacheFrag = (CacheFragInfo *)Ptr;
|
2016-06-04 04:48:17 +08:00
|
|
|
VPrintf(2, "in esan::%s: %s with %u class(es)/struct(s)\n", __FUNCTION__,
|
|
|
|
CacheFrag->UnitName, CacheFrag->NumStructs);
|
2016-06-03 02:45:25 +08:00
|
|
|
unregisterStructInfo(CacheFrag);
|
2016-05-26 01:49:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void initializeCacheFrag() {
|
|
|
|
VPrintf(2, "in esan::%s\n", __FUNCTION__);
|
2016-06-03 02:45:25 +08:00
|
|
|
// We use placement new to initialize Ctx before C++ static initializaion.
|
|
|
|
// We make CtxMem 8-byte aligned for atomic operations in AddrHashMap.
|
|
|
|
static u64 CtxMem[sizeof(Context) / sizeof(u64) + 1];
|
2016-06-04 04:48:17 +08:00
|
|
|
Ctx = new (CtxMem) Context();
|
2016-06-03 02:45:25 +08:00
|
|
|
Ctx->NumStructs = 0;
|
2016-05-26 01:49:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int finalizeCacheFrag() {
|
|
|
|
VPrintf(2, "in esan::%s\n", __FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-09 12:13:25 +08:00
|
|
|
void reportCacheFrag() {
|
|
|
|
VPrintf(2, "in esan::%s\n", __FUNCTION__);
|
|
|
|
// FIXME: Not yet implemented. We need to iterate over all of the
|
|
|
|
// compilation unit data.
|
|
|
|
}
|
|
|
|
|
2016-05-26 01:49:00 +08:00
|
|
|
} // namespace __esan
|