| Qin Zhao | 7e4933f | 2016-05-25 17:49:00 +0000 | [diff] [blame] | 1 | //===-- cache_frag.cpp ----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of EfficiencySanitizer, a family of performance tuners. |
| 11 | // |
| 12 | // This file contains cache fragmentation-specific code. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "esan.h" |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 16 | #include "esan_flags.h" |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_addrhashmap.h" |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_common.h" |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_placement_new.h" |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 20 | #include <string.h> |
| Qin Zhao | 7e4933f | 2016-05-25 17:49:00 +0000 | [diff] [blame] | 21 | |
| 22 | namespace __esan { |
| 23 | |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 24 | //===-- Struct field access counter runtime -------------------------------===// |
| 25 | |
| Qin Zhao | 9e39638 | 2016-05-31 21:27:39 +0000 | [diff] [blame] | 26 | // This should be kept consistent with LLVM's EfficiencySanitizer StructInfo. |
| 27 | struct StructInfo { |
| 28 | const char *StructName; |
| Qin Zhao | a4a7220 | 2016-06-10 02:10:34 +0000 | [diff] [blame] | 29 | u32 Size; |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 30 | u32 NumFields; |
| Qin Zhao | a4a7220 | 2016-06-10 02:10:34 +0000 | [diff] [blame] | 31 | u32 *FieldOffsets; |
| Qin Zhao | e24bc36 | 2016-06-17 04:50:11 +0000 | [diff] [blame^] | 32 | u32 *FieldSize; |
| Qin Zhao | 9e39638 | 2016-05-31 21:27:39 +0000 | [diff] [blame] | 33 | u64 *FieldCounters; |
| 34 | const char **FieldTypeNames; |
| 35 | }; |
| 36 | |
| 37 | // This should be kept consistent with LLVM's EfficiencySanitizer CacheFragInfo. |
| 38 | // The tool-specific information per compilation unit (module). |
| 39 | struct CacheFragInfo { |
| 40 | const char *UnitName; |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 41 | u32 NumStructs; |
| Qin Zhao | 9e39638 | 2016-05-31 21:27:39 +0000 | [diff] [blame] | 42 | StructInfo *Structs; |
| 43 | }; |
| 44 | |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 45 | struct StructCounter { |
| 46 | StructInfo *Struct; |
| Qin Zhao | cd5fe14 | 2016-06-15 17:27:29 +0000 | [diff] [blame] | 47 | u64 Count; // The total access count of the struct. |
| 48 | u64 Ratio; // Difference ratio for the struct layout access. |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // We use StructHashMap to keep track of an unique copy of StructCounter. |
| 52 | typedef AddrHashMap<StructCounter, 31051> StructHashMap; |
| 53 | struct Context { |
| 54 | StructHashMap StructMap; |
| 55 | u32 NumStructs; |
| 56 | u64 TotalCount; // The total access count of all structs. |
| 57 | }; |
| 58 | static Context *Ctx; |
| 59 | |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 60 | static void reportStructSummary() { |
| 61 | // FIXME: provide a better struct field access summary report. |
| Qin Zhao | cd5fe14 | 2016-06-15 17:27:29 +0000 | [diff] [blame] | 62 | Report("%s: total struct field access count = %llu\n", SanitizerToolName, |
| 63 | Ctx->TotalCount); |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // FIXME: we are still exploring proper ways to evaluate the difference between |
| 67 | // struct field counts. Currently, we use a simple formula to calculate the |
| 68 | // difference ratio: V1/V2. |
| 69 | static inline u64 computeDifferenceRatio(u64 Val1, u64 Val2) { |
| Qin Zhao | cd5fe14 | 2016-06-15 17:27:29 +0000 | [diff] [blame] | 70 | if (Val2 > Val1) { |
| 71 | Swap(Val1, Val2); |
| 72 | } |
| 73 | if (Val2 == 0) |
| 74 | Val2 = 1; |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 75 | return (Val1 / Val2); |
| 76 | } |
| 77 | |
| 78 | static void reportStructCounter(StructHashMap::Handle &Handle) { |
| Qin Zhao | cd5fe14 | 2016-06-15 17:27:29 +0000 | [diff] [blame] | 79 | const u32 TypePrintLimit = 512; |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 80 | const char *type, *start, *end; |
| 81 | StructInfo *Struct = Handle->Struct; |
| 82 | // Union field address calculation is done via bitcast instead of GEP, |
| 83 | // so the count for union is always 0. |
| 84 | // We skip the union report to avoid confusion. |
| 85 | if (strncmp(Struct->StructName, "union.", 6) == 0) |
| 86 | return; |
| 87 | // Remove the '.' after class/struct during print. |
| 88 | if (strncmp(Struct->StructName, "class.", 6) == 0) { |
| 89 | type = "class"; |
| 90 | start = &Struct->StructName[6]; |
| 91 | } else { |
| 92 | type = "struct"; |
| 93 | start = &Struct->StructName[7]; |
| 94 | } |
| 95 | // Remove the suffixes with '#' during print. |
| 96 | end = strchr(start, '#'); |
| 97 | CHECK(end != nullptr); |
| 98 | Report(" %s %.*s\n", type, end - start, start); |
| Qin Zhao | cd5fe14 | 2016-06-15 17:27:29 +0000 | [diff] [blame] | 99 | Report(" size = %u, count = %llu, ratio = %llu\n", Struct->Size, |
| 100 | Handle->Count, Handle->Ratio); |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 101 | for (u32 i = 0; i < Struct->NumFields; ++i) { |
| Qin Zhao | e24bc36 | 2016-06-17 04:50:11 +0000 | [diff] [blame^] | 102 | Report(" #%2u: offset = %u,\t size = %u,\t count = %llu,\t type = %.*s\n", |
| 103 | i, Struct->FieldOffsets[i], Struct->FieldSize[i], |
| 104 | Struct->FieldCounters[i], TypePrintLimit, Struct->FieldTypeNames[i]); |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | static void computeStructRatio(StructHashMap::Handle &Handle) { |
| 109 | Handle->Ratio = 0; |
| 110 | Handle->Count = Handle->Struct->FieldCounters[0]; |
| 111 | for (u32 i = 1; i < Handle->Struct->NumFields; ++i) { |
| 112 | Handle->Count += Handle->Struct->FieldCounters[i]; |
| 113 | Handle->Ratio += computeDifferenceRatio( |
| 114 | Handle->Struct->FieldCounters[i - 1], Handle->Struct->FieldCounters[i]); |
| 115 | } |
| 116 | Ctx->TotalCount += Handle->Count; |
| Qin Zhao | cd5fe14 | 2016-06-15 17:27:29 +0000 | [diff] [blame] | 117 | if (Handle->Ratio >= (u64)getFlags()->report_threshold || |
| 118 | (Verbosity() >= 1 && Handle->Count > 0)) |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 119 | reportStructCounter(Handle); |
| 120 | } |
| 121 | |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 122 | static void registerStructInfo(CacheFragInfo *CacheFrag) { |
| 123 | for (u32 i = 0; i < CacheFrag->NumStructs; ++i) { |
| 124 | StructInfo *Struct = &CacheFrag->Structs[i]; |
| 125 | StructHashMap::Handle H(&Ctx->StructMap, (uptr)Struct->FieldCounters); |
| 126 | if (H.created()) { |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 127 | VPrintf(2, " Register %s: %u fields\n", Struct->StructName, |
| 128 | Struct->NumFields); |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 129 | H->Struct = Struct; |
| 130 | ++Ctx->NumStructs; |
| 131 | } else { |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 132 | VPrintf(2, " Duplicated %s: %u fields\n", Struct->StructName, |
| 133 | Struct->NumFields); |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void unregisterStructInfo(CacheFragInfo *CacheFrag) { |
| 139 | // FIXME: if the library is unloaded before finalizeCacheFrag, we should |
| 140 | // collect the result for later report. |
| 141 | for (u32 i = 0; i < CacheFrag->NumStructs; ++i) { |
| 142 | StructInfo *Struct = &CacheFrag->Structs[i]; |
| 143 | StructHashMap::Handle H(&Ctx->StructMap, (uptr)Struct->FieldCounters, true); |
| 144 | if (H.exists()) { |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 145 | VPrintf(2, " Unregister %s: %u fields\n", Struct->StructName, |
| 146 | Struct->NumFields); |
| 147 | // FIXME: we should move this call to finalizeCacheFrag once we can |
| 148 | // iterate over the hash map there. |
| 149 | computeStructRatio(H); |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 150 | --Ctx->NumStructs; |
| 151 | } else { |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 152 | VPrintf(2, " Duplicated %s: %u fields\n", Struct->StructName, |
| 153 | Struct->NumFields); |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 156 | static bool Reported = false; |
| 157 | if (Ctx->NumStructs == 0 && !Reported) { |
| 158 | Reported = true; |
| 159 | reportStructSummary(); |
| 160 | } |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| Qin Zhao | 7e4933f | 2016-05-25 17:49:00 +0000 | [diff] [blame] | 163 | //===-- Init/exit functions -----------------------------------------------===// |
| 164 | |
| 165 | void processCacheFragCompilationUnitInit(void *Ptr) { |
| Qin Zhao | 9e39638 | 2016-05-31 21:27:39 +0000 | [diff] [blame] | 166 | CacheFragInfo *CacheFrag = (CacheFragInfo *)Ptr; |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 167 | VPrintf(2, "in esan::%s: %s with %u class(es)/struct(s)\n", __FUNCTION__, |
| 168 | CacheFrag->UnitName, CacheFrag->NumStructs); |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 169 | registerStructInfo(CacheFrag); |
| Qin Zhao | 7e4933f | 2016-05-25 17:49:00 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void processCacheFragCompilationUnitExit(void *Ptr) { |
| Qin Zhao | 9e39638 | 2016-05-31 21:27:39 +0000 | [diff] [blame] | 173 | CacheFragInfo *CacheFrag = (CacheFragInfo *)Ptr; |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 174 | VPrintf(2, "in esan::%s: %s with %u class(es)/struct(s)\n", __FUNCTION__, |
| 175 | CacheFrag->UnitName, CacheFrag->NumStructs); |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 176 | unregisterStructInfo(CacheFrag); |
| Qin Zhao | 7e4933f | 2016-05-25 17:49:00 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void initializeCacheFrag() { |
| 180 | VPrintf(2, "in esan::%s\n", __FUNCTION__); |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 181 | // We use placement new to initialize Ctx before C++ static initializaion. |
| 182 | // We make CtxMem 8-byte aligned for atomic operations in AddrHashMap. |
| 183 | static u64 CtxMem[sizeof(Context) / sizeof(u64) + 1]; |
| Qin Zhao | bc929e4 | 2016-06-03 20:48:17 +0000 | [diff] [blame] | 184 | Ctx = new (CtxMem) Context(); |
| Qin Zhao | 4175a6d | 2016-06-02 18:45:25 +0000 | [diff] [blame] | 185 | Ctx->NumStructs = 0; |
| Qin Zhao | 7e4933f | 2016-05-25 17:49:00 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | int finalizeCacheFrag() { |
| 189 | VPrintf(2, "in esan::%s\n", __FUNCTION__); |
| Qin Zhao | 7e4933f | 2016-05-25 17:49:00 +0000 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | } // namespace __esan |