summaryrefslogtreecommitdiff
path: root/lib/esan/cache_frag.cpp
blob: af36d90bb50c08518b092dce0a3efdb21d617017 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//===-- 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"
#include "sanitizer_common/sanitizer_addrhashmap.h"
#include "sanitizer_common/sanitizer_placement_new.h"

namespace __esan {

//===-- Struct field access counter runtime -------------------------------===//

// This should be kept consistent with LLVM's EfficiencySanitizer StructInfo.
struct StructInfo {
  const char *StructName;
  u32 NumFields;
  u64 *FieldCounters;
  const char **FieldTypeNames;
};

// This should be kept consistent with LLVM's EfficiencySanitizer CacheFragInfo.
// The tool-specific information per compilation unit (module).
struct CacheFragInfo {
  const char *UnitName;
  u32 NumStructs;
  StructInfo *Structs;
};

struct StructCounter {
  StructInfo *Struct;
  u64 Count;    // The total access count of the struct.
  u32 Variance; // Variance score for the struct layout access.
};

// 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;

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()) {
      VPrintf(2, " Register %s: %u fields\n",
              Struct->StructName, Struct->NumFields);
      H->Struct = Struct;
      ++Ctx->NumStructs;
    } else {
      VPrintf(2, " Duplicated %s: %u fields\n",
              Struct->StructName, Struct->NumFields);
    }
  }
}

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()) {
      VPrintf(2, " Unregister %s: %u fields\n",
              Struct->StructName, Struct->NumFields);
      --Ctx->NumStructs;
    } else {
      VPrintf(2, " Duplicated %s: %u fields\n",
              Struct->StructName, Struct->NumFields);
    }
  }
}

static void reportStructSummary() {
  // FIXME: iterate StructHashMap and generate the final report.
  Report("%s is not finished: nothing yet to report\n", SanitizerToolName);
}

//===-- Init/exit functions -----------------------------------------------===//

void processCacheFragCompilationUnitInit(void *Ptr) {
  CacheFragInfo *CacheFrag = (CacheFragInfo *)Ptr;
  VPrintf(2, "in esan::%s: %s with %u class(es)/struct(s)\n",
          __FUNCTION__, CacheFrag->UnitName, CacheFrag->NumStructs);
  registerStructInfo(CacheFrag);
}

void processCacheFragCompilationUnitExit(void *Ptr) {
  CacheFragInfo *CacheFrag = (CacheFragInfo *)Ptr;
  VPrintf(2, "in esan::%s: %s with %u class(es)/struct(s)\n",
          __FUNCTION__, CacheFrag->UnitName, CacheFrag->NumStructs);
  unregisterStructInfo(CacheFrag);
}

void initializeCacheFrag() {
  VPrintf(2, "in esan::%s\n", __FUNCTION__);
  // 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];
  Ctx = new(CtxMem) Context();
  Ctx->NumStructs = 0;
}

int finalizeCacheFrag() {
  VPrintf(2, "in esan::%s\n", __FUNCTION__);
  reportStructSummary();
  return 0;
}

} // namespace __esan