@@ -171,21 +171,18 @@ static void setupIOBuffer() {
171
171
}
172
172
}
173
173
174
- /* Read profile data in \c ProfileFile and merge with in-memory
175
- profile counters. Returns -1 if there is fatal error, otheriwse
176
- 0 is returned. Returning 0 does not mean merge is actually
177
- performed. If merge is actually done, *MergeDone is set to 1.
178
- */
179
- static int doProfileMerging (FILE * ProfileFile , int * MergeDone ) {
180
- uint64_t ProfileFileSize ;
181
- char * ProfileBuffer ;
182
-
174
+ /* Get the size of the profile file. If there are any errors, print the
175
+ * message under the assumption that the profile is being read for merging
176
+ * purposes, and return -1. Otherwise return the file size in the inout param
177
+ * \p ProfileFileSize. */
178
+ static int getProfileFileSizeForMerging (FILE * ProfileFile ,
179
+ uint64_t * ProfileFileSize ) {
183
180
if (fseek (ProfileFile , 0L , SEEK_END ) == -1 ) {
184
181
PROF_ERR ("Unable to merge profile data, unable to get size: %s\n" ,
185
182
strerror (errno ));
186
183
return -1 ;
187
184
}
188
- ProfileFileSize = ftell (ProfileFile );
185
+ * ProfileFileSize = ftell (ProfileFile );
189
186
190
187
/* Restore file offset. */
191
188
if (fseek (ProfileFile , 0L , SEEK_SET ) == -1 ) {
@@ -194,28 +191,60 @@ static int doProfileMerging(FILE *ProfileFile, int *MergeDone) {
194
191
return -1 ;
195
192
}
196
193
197
- /* Nothing to merge. */
198
- if (ProfileFileSize < sizeof (__llvm_profile_header )) {
199
- if (ProfileFileSize )
200
- PROF_WARN ("Unable to merge profile data: %s\n" ,
201
- "source profile file is too small." );
202
- return 0 ;
194
+ if (* ProfileFileSize > 0 &&
195
+ * ProfileFileSize < sizeof (__llvm_profile_header )) {
196
+ PROF_WARN ("Unable to merge profile data: %s\n" ,
197
+ "source profile file is too small." );
198
+ return -1 ;
203
199
}
200
+ return 0 ;
201
+ }
204
202
205
- ProfileBuffer = mmap (NULL , ProfileFileSize , PROT_READ , MAP_SHARED | MAP_FILE ,
206
- fileno (ProfileFile ), 0 );
207
- if (ProfileBuffer == MAP_FAILED ) {
203
+ /* mmap() \p ProfileFile for profile merging purposes, assuming that an
204
+ * exclusive lock is held on the file and that \p ProfileFileSize is the
205
+ * length of the file. Return the mmap'd buffer in the inout variable
206
+ * \p ProfileBuffer. Returns -1 on failure. On success, the caller is
207
+ * responsible for unmapping the mmap'd buffer in \p ProfileBuffer. */
208
+ static int mmapProfileForMerging (FILE * ProfileFile , uint64_t ProfileFileSize ,
209
+ char * * ProfileBuffer ) {
210
+ * ProfileBuffer = mmap (NULL , ProfileFileSize , PROT_READ , MAP_SHARED | MAP_FILE ,
211
+ fileno (ProfileFile ), 0 );
212
+ if (* ProfileBuffer == MAP_FAILED ) {
208
213
PROF_ERR ("Unable to merge profile data, mmap failed: %s\n" ,
209
214
strerror (errno ));
210
215
return -1 ;
211
216
}
212
217
213
- if (__llvm_profile_check_compatibility (ProfileBuffer , ProfileFileSize )) {
214
- (void )munmap (ProfileBuffer , ProfileFileSize );
218
+ if (__llvm_profile_check_compatibility (* ProfileBuffer , ProfileFileSize )) {
219
+ (void )munmap (* ProfileBuffer , ProfileFileSize );
215
220
PROF_WARN ("Unable to merge profile data: %s\n" ,
216
221
"source profile file is not compatible." );
217
- return 0 ;
222
+ return -1 ;
218
223
}
224
+ return 0 ;
225
+ }
226
+
227
+ /* Read profile data in \c ProfileFile and merge with in-memory
228
+ profile counters. Returns -1 if there is fatal error, otheriwse
229
+ 0 is returned. Returning 0 does not mean merge is actually
230
+ performed. If merge is actually done, *MergeDone is set to 1.
231
+ */
232
+ static int doProfileMerging (FILE * ProfileFile , int * MergeDone ) {
233
+ uint64_t ProfileFileSize ;
234
+ char * ProfileBuffer ;
235
+
236
+ /* Get the size of the profile on disk. */
237
+ if (getProfileFileSizeForMerging (ProfileFile , & ProfileFileSize ) == -1 )
238
+ return -1 ;
239
+
240
+ /* Nothing to merge. */
241
+ if (!ProfileFileSize )
242
+ return 0 ;
243
+
244
+ /* mmap() the profile and check that it is compatible with the data in
245
+ * the current image. */
246
+ if (mmapProfileForMerging (ProfileFile , ProfileFileSize , & ProfileBuffer ) == -1 )
247
+ return -1 ;
219
248
220
249
/* Now start merging */
221
250
__llvm_profile_merge_from_buffer (ProfileBuffer , ProfileFileSize );
0 commit comments