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