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 __llvm_profile_get_magic(); 39 } 40 41 /* Returns 1 if profile is not structurally compatible. */ 42 COMPILER_RT_VISIBILITY 43 int __llvm_profile_check_compatibility(const char *ProfileData, 44 uint64_t ProfileSize) { 45 /* Check profile header only for now */ 46 __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData; 47 __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData; 48 SrcDataStart = 49 (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) + 50 Header->BinaryIdsSize); 51 SrcDataEnd = SrcDataStart + Header->DataSize; 52 53 if (ProfileSize < sizeof(__llvm_profile_header)) 54 return 1; 55 56 /* Check the header first. */ 57 if (Header->Magic != __llvm_profile_get_magic() || 58 Header->Version != __llvm_profile_get_version() || 59 Header->DataSize != 60 __llvm_profile_get_data_size(__llvm_profile_begin_data(), 61 __llvm_profile_end_data()) || 62 Header->CountersSize != (uint64_t)(__llvm_profile_end_counters() - 63 __llvm_profile_begin_counters()) || 64 Header->NamesSize != (uint64_t)(__llvm_profile_end_names() - 65 __llvm_profile_begin_names()) || 66 Header->ValueKindLast != IPVK_Last) 67 return 1; 68 69 if (ProfileSize < sizeof(__llvm_profile_header) + Header->BinaryIdsSize + 70 Header->DataSize * sizeof(__llvm_profile_data) + 71 Header->NamesSize + Header->CountersSize) 72 return 1; 73 74 for (SrcData = SrcDataStart, 75 DstData = (__llvm_profile_data *)__llvm_profile_begin_data(); 76 SrcData < SrcDataEnd; ++SrcData, ++DstData) { 77 if (SrcData->NameRef != DstData->NameRef || 78 SrcData->FuncHash != DstData->FuncHash || 79 SrcData->NumCounters != DstData->NumCounters) 80 return 1; 81 } 82 83 /* Matched! */ 84 return 0; 85 } 86 87 static uintptr_t signextIfWin64(void *V) { 88 #ifdef _WIN64 89 return (uintptr_t)(int32_t)(uintptr_t)V; 90 #else 91 return (uintptr_t)V; 92 #endif 93 } 94 95 COMPILER_RT_VISIBILITY 96 int __llvm_profile_merge_from_buffer(const char *ProfileData, 97 uint64_t ProfileSize) { 98 __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData; 99 __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData; 100 uint64_t *SrcCountersStart; 101 const char *SrcNameStart; 102 const char *SrcValueProfDataStart, *SrcValueProfData; 103 uintptr_t CountersDelta = Header->CountersDelta; 104 105 SrcDataStart = 106 (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) + 107 Header->BinaryIdsSize); 108 SrcDataEnd = SrcDataStart + Header->DataSize; 109 SrcCountersStart = (uint64_t *)SrcDataEnd; 110 SrcNameStart = (const char *)(SrcCountersStart + Header->CountersSize); 111 SrcValueProfDataStart = 112 SrcNameStart + Header->NamesSize + 113 __llvm_profile_get_num_padding_bytes(Header->NamesSize); 114 if (SrcNameStart < (const char *)SrcCountersStart) 115 return 1; 116 117 for (SrcData = SrcDataStart, 118 DstData = (__llvm_profile_data *)__llvm_profile_begin_data(), 119 SrcValueProfData = SrcValueProfDataStart; 120 SrcData < SrcDataEnd; ++SrcData, ++DstData) { 121 // For the in-memory destination, CounterPtr is the distance from the start 122 // address of the data to the start address of the counter. On WIN64, 123 // CounterPtr is a truncated 32-bit value due to COFF limitation. Sign 124 // extend CounterPtr to get the original value. 125 uint64_t *DstCounters = 126 (uint64_t *)((uintptr_t)DstData + signextIfWin64(DstData->CounterPtr)); 127 unsigned NVK = 0; 128 129 // SrcData is a serialized representation of the memory image. We need to 130 // compute the in-buffer counter offset from the in-memory address distance. 131 // The initial CountersDelta is the in-memory address difference 132 // start(__llvm_prf_cnts)-start(__llvm_prf_data), so SrcData->CounterPtr - 133 // CountersDelta computes the offset into the in-buffer counter section. 134 // 135 // On WIN64, CountersDelta is truncated as well, so no need for signext. 136 uint64_t *SrcCounters = 137 SrcCountersStart + 138 ((uintptr_t)SrcData->CounterPtr - CountersDelta) / sizeof(uint64_t); 139 // CountersDelta needs to be decreased as we advance to the next data 140 // record. 141 CountersDelta -= sizeof(*SrcData); 142 unsigned NC = SrcData->NumCounters; 143 if (NC == 0) 144 return 1; 145 if (SrcCounters < SrcCountersStart || 146 (const char *)SrcCounters >= SrcNameStart || 147 (const char *)(SrcCounters + NC) > SrcNameStart) 148 return 1; 149 for (unsigned I = 0; I < NC; I++) 150 DstCounters[I] += SrcCounters[I]; 151 152 /* Now merge value profile data. */ 153 if (!VPMergeHook) 154 continue; 155 156 for (unsigned I = 0; I <= IPVK_Last; I++) 157 NVK += (SrcData->NumValueSites[I] != 0); 158 159 if (!NVK) 160 continue; 161 162 if (SrcValueProfData >= ProfileData + ProfileSize) 163 return 1; 164 VPMergeHook((ValueProfData *)SrcValueProfData, DstData); 165 SrcValueProfData = 166 SrcValueProfData + ((ValueProfData *)SrcValueProfData)->TotalSize; 167 } 168 169 return 0; 170 } 171