summaryrefslogtreecommitdiffstats
path: root/compiler-rt/lib/profile/InstrProfiling.c
blob: e3ad505c434e35d7dc9770b6472b6a8539308ce7 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*===- InstrProfiling.c - Support library for PGO instrumentation ---------===*\
|*
|*                     The LLVM Compiler Infrastructure
|*
|* This file is distributed under the University of Illinois Open Source
|* License. See LICENSE.TXT for details.
|*
\*===----------------------------------------------------------------------===*/

#include "InstrProfiling.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INSTR_PROF_VALUE_PROF_DATA
#define INSTR_PROF_COMMON_API_IMPL
#include "InstrProfData.inc"

#define PROF_OOM(Msg) PROF_ERR(Msg ":%s\n", "Out of memory");
#define PROF_OOM_RETURN(Msg)                                                   \
  {                                                                            \
    PROF_OOM(Msg)                                                              \
    return 0;                                                                  \
  }

#ifdef _MIPS_ARCH
LLVM_LIBRARY_VISIBILITY
uint32_t BoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
  void *R = *Ptr;
  if (R == OldV) {
    *Ptr = NewV;
    return 1;
  }
  return 0;
}
#define BOOL_CMPXCHG(Ptr, OldV, NewV) BoolCmpXchg((void **)Ptr, OldV, NewV)
#else
#define BOOL_CMPXCHG(Ptr, OldV, NewV)                                          \
  __sync_bool_compare_and_swap(Ptr, OldV, NewV)
#endif

LLVM_LIBRARY_VISIBILITY uint64_t __llvm_profile_get_magic(void) {
  return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64)
                                            : (INSTR_PROF_RAW_MAGIC_32);
}

/* Return the number of bytes needed to add to SizeInBytes to make it
 *   the result a multiple of 8.
 */
LLVM_LIBRARY_VISIBILITY uint8_t
__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {
  return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
}

LLVM_LIBRARY_VISIBILITY uint64_t __llvm_profile_get_version(void) {
  return INSTR_PROF_RAW_VERSION;
}

LLVM_LIBRARY_VISIBILITY void __llvm_profile_reset_counters(void) {
  uint64_t *I = __llvm_profile_begin_counters();
  uint64_t *E = __llvm_profile_end_counters();

  memset(I, 0, sizeof(uint64_t) * (E - I));

  const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
  const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
  const __llvm_profile_data *DI;
  for (DI = DataBegin; DI != DataEnd; ++DI) {
    uint64_t CurrentVSiteCount = 0;
    uint32_t VKI, i;
    if (!DI->Values)
      continue;

    ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values;

    for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
      CurrentVSiteCount += DI->NumValueSites[VKI];

    for (i = 0; i < CurrentVSiteCount; ++i) {
      ValueProfNode *CurrentVNode = ValueCounters[i];

      while (CurrentVNode) {
        CurrentVNode->VData.Count = 0;
        CurrentVNode = CurrentVNode->Next;
      }
    }
  }
}

/* This method is only used in value profiler mock testing.  */
LLVM_LIBRARY_VISIBILITY void
__llvm_profile_set_num_value_sites(__llvm_profile_data *Data,
                                   uint32_t ValueKind, uint16_t NumValueSites) {
  *((uint16_t *)&Data->NumValueSites[ValueKind]) = NumValueSites;
}

/* This method is only used in value profiler mock testing.  */
LLVM_LIBRARY_VISIBILITY const __llvm_profile_data *
__llvm_profile_iterate_data(const __llvm_profile_data *Data) {
  return Data + 1;
}

/* This method is only used in value profiler mock testing.  */
LLVM_LIBRARY_VISIBILITY void *
__llvm_get_function_addr(const __llvm_profile_data *Data) {
  return Data->FunctionPointer;
}

/* Allocate an array that holds the pointers to the linked lists of
 * value profile counter nodes. The number of element of the array
 * is the total number of value profile sites instrumented. Returns
 * 0 if allocation fails.
 */

static int allocateValueProfileCounters(__llvm_profile_data *Data) {
  uint64_t NumVSites = 0;
  uint32_t VKI;
  for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
    NumVSites += Data->NumValueSites[VKI];

  ValueProfNode **Mem =
      (ValueProfNode **)calloc(NumVSites, sizeof(ValueProfNode *));
  if (!Mem)
    return 0;
  if (!BOOL_CMPXCHG(&Data->Values, 0, Mem)) {
    free(Mem);
    return 0;
  }
  return 1;
}

static void deallocateValueProfileCounters(__llvm_profile_data *Data) {
  uint64_t NumVSites = 0, I;
  uint32_t VKI;
  if (!Data->Values)
    return;
  for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
    NumVSites += Data->NumValueSites[VKI];
  for (I = 0; I < NumVSites; I++) {
    ValueProfNode *Node = ((ValueProfNode **)Data->Values)[I];
    while (Node) {
      ValueProfNode *Next = Node->Next;
      free(Node);
      Node = Next;
    }
  }
  free(Data->Values);
}

LLVM_LIBRARY_VISIBILITY void
__llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
                                 uint32_t CounterIndex) {

  __llvm_profile_data *PData = (__llvm_profile_data *)Data;
  if (!PData)
    return;

  if (!PData->Values) {
    if (!allocateValueProfileCounters(PData))
      return;
  }

  ValueProfNode **ValueCounters = (ValueProfNode **)PData->Values;
  ValueProfNode *PrevVNode = NULL;
  ValueProfNode *CurrentVNode = ValueCounters[CounterIndex];

  uint8_t VDataCount = 0;
  while (CurrentVNode) {
    if (TargetValue == CurrentVNode->VData.Value) {
      CurrentVNode->VData.Count++;
      return;
    }
    PrevVNode = CurrentVNode;
    CurrentVNode = CurrentVNode->Next;
    ++VDataCount;
  }

  if (VDataCount >= UCHAR_MAX)
    return;

  CurrentVNode = (ValueProfNode *)calloc(1, sizeof(ValueProfNode));
  if (!CurrentVNode)
    return;

  CurrentVNode->VData.Value = TargetValue;
  CurrentVNode->VData.Count++;

  uint32_t Success = 0;
  if (!ValueCounters[CounterIndex])
    Success = BOOL_CMPXCHG(&ValueCounters[CounterIndex], 0, CurrentVNode);
  else if (PrevVNode && !PrevVNode->Next)
    Success = BOOL_CMPXCHG(&(PrevVNode->Next), 0, CurrentVNode);

  if (!Success) {
    free(CurrentVNode);
    return;
  }
}

/* For multi-threaded programs, while the profile is being dumped, other
   threads may still be updating the value profile data and creating new
   value entries. To accommadate this, we need to add extra bytes to the
   data buffer. The size of the extra space is controlled by an environment
   varaible. */
static unsigned getVprofExtraBytes() {
  const char *ExtraStr = getenv("LLVM_VALUE_PROF_BUFFER_EXTRA");
  if (!ExtraStr || !ExtraStr[0])
    return 1024;
  return (unsigned)atoi(ExtraStr);
}

LLVM_LIBRARY_VISIBILITY uint64_t
__llvm_profile_gather_value_data(uint8_t **VDataArray) {
  size_t S = 0, RealSize = 0, BufferCapacity = 0, Extra = 0;
  __llvm_profile_data *I;
  if (!VDataArray)
    PROF_OOM_RETURN("Failed to write value profile data ");

  const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
  const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();

  /*
   * Compute the total Size of the buffer to hold ValueProfData
   * structures for functions with value profile data.
   */
  for (I = (__llvm_profile_data *)DataBegin; I != DataEnd; ++I) {
    ValueProfRuntimeRecord R;
    /* Extract the value profile data info from the runtime. */
    if (initializeValueProfRuntimeRecord(&R, I->NumValueSites, I->Values))
      PROF_OOM_RETURN("Failed to write value profile data ");
    /* Compute the size of ValueProfData from this runtime record.  */
    if (getNumValueKindsRT(&R) != 0)
      S += getValueProfDataSizeRT(&R);
    finalizeValueProfRuntimeRecord(&R);
  }
  /* No value sites or no value profile data is collected. */
  if (!S)
    return 0;

  Extra = getVprofExtraBytes();
  BufferCapacity = S + Extra;
  *VDataArray = calloc(BufferCapacity, sizeof(uint8_t));
  if (!*VDataArray)
    PROF_OOM_RETURN("Failed to write value profile data ");

  ValueProfData *VD = (ValueProfData *)(*VDataArray);
  /*
   * Extract value profile data and write into ValueProfData structure
   * one by one. Note that new value profile data added to any value
   * site (from another thread) after the ValueProfRuntimeRecord is
   * initialized (when the profile data snapshot is taken) won't be
   * collected. This is not a problem as those dropped value will have
   * very low taken count.
   */
  for (I = (__llvm_profile_data *)DataBegin; I != DataEnd; ++I) {
    ValueProfRuntimeRecord R;
    if (initializeValueProfRuntimeRecord(&R, I->NumValueSites, I->Values))
      PROF_OOM_RETURN("Failed to write value profile data ");
    if (getNumValueKindsRT(&R) == 0)
      continue;

    /* Record R has taken a snapshot of the VP data at this point. Newly
       added VP data for this function will be dropped.  */
    /* Check if there is enough space.  */
    if (BufferCapacity - RealSize < getValueProfDataSizeRT(&R)) {
      PROF_ERR("Value profile data is dropped :%s \n",
               "Out of buffer space. Use environment "
               " LLVM_VALUE_PROF_BUFFER_EXTRA to allocate more");
      I->Values = 0;
    }

    serializeValueProfDataFromRT(&R, VD);
    deallocateValueProfileCounters(I);
    I->Values = VD;
    finalizeValueProfRuntimeRecord(&R);
    RealSize += VD->TotalSize;
    VD = (ValueProfData *)((char *)VD + VD->TotalSize);
  }

  return RealSize;
}
OpenPOWER on IntegriCloud