1*7c3d14c8STreehugger Robot /*===- InstrProfilingMerge.c - Profile in-process Merging ---------------===*\
2*7c3d14c8STreehugger Robot |*
3*7c3d14c8STreehugger Robot |* The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot |*
5*7c3d14c8STreehugger Robot |* This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot |* License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot |*
8*7c3d14c8STreehugger Robot |*===----------------------------------------------------------------------===*
9*7c3d14c8STreehugger Robot |* This file defines the API needed for in-process merging of profile data
10*7c3d14c8STreehugger Robot |* stored in memory buffer.
11*7c3d14c8STreehugger Robot \*===---------------------------------------------------------------------===*/
12*7c3d14c8STreehugger Robot
13*7c3d14c8STreehugger Robot #include "InstrProfiling.h"
14*7c3d14c8STreehugger Robot #include "InstrProfilingInternal.h"
15*7c3d14c8STreehugger Robot #include "InstrProfilingUtil.h"
16*7c3d14c8STreehugger Robot
17*7c3d14c8STreehugger Robot #define INSTR_PROF_VALUE_PROF_DATA
18*7c3d14c8STreehugger Robot #include "InstrProfData.inc"
19*7c3d14c8STreehugger Robot
20*7c3d14c8STreehugger Robot COMPILER_RT_WEAK void (*VPMergeHook)(ValueProfData *,
21*7c3d14c8STreehugger Robot __llvm_profile_data *) = NULL;
22*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY
lprofGetLoadModuleSignature()23*7c3d14c8STreehugger Robot uint64_t lprofGetLoadModuleSignature() {
24*7c3d14c8STreehugger Robot /* A very fast way to compute a module signature. */
25*7c3d14c8STreehugger Robot uint64_t CounterSize = (uint64_t)(__llvm_profile_end_counters() -
26*7c3d14c8STreehugger Robot __llvm_profile_begin_counters());
27*7c3d14c8STreehugger Robot uint64_t DataSize = __llvm_profile_get_data_size(__llvm_profile_begin_data(),
28*7c3d14c8STreehugger Robot __llvm_profile_end_data());
29*7c3d14c8STreehugger Robot uint64_t NamesSize =
30*7c3d14c8STreehugger Robot (uint64_t)(__llvm_profile_end_names() - __llvm_profile_begin_names());
31*7c3d14c8STreehugger Robot uint64_t NumVnodes =
32*7c3d14c8STreehugger Robot (uint64_t)(__llvm_profile_end_vnodes() - __llvm_profile_begin_vnodes());
33*7c3d14c8STreehugger Robot const __llvm_profile_data *FirstD = __llvm_profile_begin_data();
34*7c3d14c8STreehugger Robot
35*7c3d14c8STreehugger Robot return (NamesSize << 40) + (CounterSize << 30) + (DataSize << 20) +
36*7c3d14c8STreehugger Robot (NumVnodes << 10) + (DataSize > 0 ? FirstD->NameRef : 0);
37*7c3d14c8STreehugger Robot }
38*7c3d14c8STreehugger Robot
39*7c3d14c8STreehugger Robot /* Returns 1 if profile is not structurally compatible. */
40*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY
__llvm_profile_check_compatibility(const char * ProfileData,uint64_t ProfileSize)41*7c3d14c8STreehugger Robot int __llvm_profile_check_compatibility(const char *ProfileData,
42*7c3d14c8STreehugger Robot uint64_t ProfileSize) {
43*7c3d14c8STreehugger Robot /* Check profile header only for now */
44*7c3d14c8STreehugger Robot __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
45*7c3d14c8STreehugger Robot __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
46*7c3d14c8STreehugger Robot SrcDataStart =
47*7c3d14c8STreehugger Robot (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header));
48*7c3d14c8STreehugger Robot SrcDataEnd = SrcDataStart + Header->DataSize;
49*7c3d14c8STreehugger Robot
50*7c3d14c8STreehugger Robot if (ProfileSize < sizeof(__llvm_profile_header))
51*7c3d14c8STreehugger Robot return 1;
52*7c3d14c8STreehugger Robot
53*7c3d14c8STreehugger Robot /* Check the header first. */
54*7c3d14c8STreehugger Robot if (Header->Magic != __llvm_profile_get_magic() ||
55*7c3d14c8STreehugger Robot Header->Version != __llvm_profile_get_version() ||
56*7c3d14c8STreehugger Robot Header->DataSize !=
57*7c3d14c8STreehugger Robot __llvm_profile_get_data_size(__llvm_profile_begin_data(),
58*7c3d14c8STreehugger Robot __llvm_profile_end_data()) ||
59*7c3d14c8STreehugger Robot Header->CountersSize != (uint64_t)(__llvm_profile_end_counters() -
60*7c3d14c8STreehugger Robot __llvm_profile_begin_counters()) ||
61*7c3d14c8STreehugger Robot Header->NamesSize != (uint64_t)(__llvm_profile_end_names() -
62*7c3d14c8STreehugger Robot __llvm_profile_begin_names()) ||
63*7c3d14c8STreehugger Robot Header->ValueKindLast != IPVK_Last)
64*7c3d14c8STreehugger Robot return 1;
65*7c3d14c8STreehugger Robot
66*7c3d14c8STreehugger Robot if (ProfileSize < sizeof(__llvm_profile_header) +
67*7c3d14c8STreehugger Robot Header->DataSize * sizeof(__llvm_profile_data) +
68*7c3d14c8STreehugger Robot Header->NamesSize + Header->CountersSize)
69*7c3d14c8STreehugger Robot return 1;
70*7c3d14c8STreehugger Robot
71*7c3d14c8STreehugger Robot for (SrcData = SrcDataStart,
72*7c3d14c8STreehugger Robot DstData = (__llvm_profile_data *)__llvm_profile_begin_data();
73*7c3d14c8STreehugger Robot SrcData < SrcDataEnd; ++SrcData, ++DstData) {
74*7c3d14c8STreehugger Robot if (SrcData->NameRef != DstData->NameRef ||
75*7c3d14c8STreehugger Robot SrcData->FuncHash != DstData->FuncHash ||
76*7c3d14c8STreehugger Robot SrcData->NumCounters != DstData->NumCounters)
77*7c3d14c8STreehugger Robot return 1;
78*7c3d14c8STreehugger Robot }
79*7c3d14c8STreehugger Robot
80*7c3d14c8STreehugger Robot /* Matched! */
81*7c3d14c8STreehugger Robot return 0;
82*7c3d14c8STreehugger Robot }
83*7c3d14c8STreehugger Robot
84*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY
__llvm_profile_merge_from_buffer(const char * ProfileData,uint64_t ProfileSize)85*7c3d14c8STreehugger Robot void __llvm_profile_merge_from_buffer(const char *ProfileData,
86*7c3d14c8STreehugger Robot uint64_t ProfileSize) {
87*7c3d14c8STreehugger Robot __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
88*7c3d14c8STreehugger Robot __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
89*7c3d14c8STreehugger Robot uint64_t *SrcCountersStart;
90*7c3d14c8STreehugger Robot const char *SrcNameStart;
91*7c3d14c8STreehugger Robot ValueProfData *SrcValueProfDataStart, *SrcValueProfData;
92*7c3d14c8STreehugger Robot
93*7c3d14c8STreehugger Robot SrcDataStart =
94*7c3d14c8STreehugger Robot (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header));
95*7c3d14c8STreehugger Robot SrcDataEnd = SrcDataStart + Header->DataSize;
96*7c3d14c8STreehugger Robot SrcCountersStart = (uint64_t *)SrcDataEnd;
97*7c3d14c8STreehugger Robot SrcNameStart = (const char *)(SrcCountersStart + Header->CountersSize);
98*7c3d14c8STreehugger Robot SrcValueProfDataStart =
99*7c3d14c8STreehugger Robot (ValueProfData *)(SrcNameStart + Header->NamesSize +
100*7c3d14c8STreehugger Robot __llvm_profile_get_num_padding_bytes(
101*7c3d14c8STreehugger Robot Header->NamesSize));
102*7c3d14c8STreehugger Robot
103*7c3d14c8STreehugger Robot for (SrcData = SrcDataStart,
104*7c3d14c8STreehugger Robot DstData = (__llvm_profile_data *)__llvm_profile_begin_data(),
105*7c3d14c8STreehugger Robot SrcValueProfData = SrcValueProfDataStart;
106*7c3d14c8STreehugger Robot SrcData < SrcDataEnd; ++SrcData, ++DstData) {
107*7c3d14c8STreehugger Robot uint64_t *SrcCounters;
108*7c3d14c8STreehugger Robot uint64_t *DstCounters = (uint64_t *)DstData->CounterPtr;
109*7c3d14c8STreehugger Robot unsigned I, NC, NVK = 0;
110*7c3d14c8STreehugger Robot
111*7c3d14c8STreehugger Robot NC = SrcData->NumCounters;
112*7c3d14c8STreehugger Robot SrcCounters = SrcCountersStart +
113*7c3d14c8STreehugger Robot ((size_t)SrcData->CounterPtr - Header->CountersDelta) /
114*7c3d14c8STreehugger Robot sizeof(uint64_t);
115*7c3d14c8STreehugger Robot for (I = 0; I < NC; I++)
116*7c3d14c8STreehugger Robot DstCounters[I] += SrcCounters[I];
117*7c3d14c8STreehugger Robot
118*7c3d14c8STreehugger Robot /* Now merge value profile data. */
119*7c3d14c8STreehugger Robot if (!VPMergeHook)
120*7c3d14c8STreehugger Robot continue;
121*7c3d14c8STreehugger Robot
122*7c3d14c8STreehugger Robot for (I = 0; I <= IPVK_Last; I++)
123*7c3d14c8STreehugger Robot NVK += (SrcData->NumValueSites[I] != 0);
124*7c3d14c8STreehugger Robot
125*7c3d14c8STreehugger Robot if (!NVK)
126*7c3d14c8STreehugger Robot continue;
127*7c3d14c8STreehugger Robot
128*7c3d14c8STreehugger Robot VPMergeHook(SrcValueProfData, DstData);
129*7c3d14c8STreehugger Robot SrcValueProfData = (ValueProfData *)((char *)SrcValueProfData +
130*7c3d14c8STreehugger Robot SrcValueProfData->TotalSize);
131*7c3d14c8STreehugger Robot }
132*7c3d14c8STreehugger Robot }
133