profiling.cpp (1497B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #if defined(MOZ_PROFILE_GENERATE) && defined(XP_LINUX) && !defined(ANDROID) 6 7 # include <pthread.h> 8 # include <stdio.h> 9 # include <stdlib.h> 10 # include <unistd.h> 11 # include <errno.h> 12 13 # include "mozilla/Attributes.h" 14 15 # if defined(__cplusplus) 16 extern "C" { 17 # endif 18 void __llvm_profile_initialize(void); 19 void __llvm_profile_initialize_file(void); 20 void __llvm_profile_set_filename(const char*); 21 22 // Use the API to force a different filename, then set back the original one. 23 // This will make sure the pattern is re-parsed and thus the PID properly 24 // updated within the lprofCurFilename struct. 25 static void updateFilenameAfterFork(void) { 26 __llvm_profile_set_filename("default.profraw"); 27 __llvm_profile_initialize_file(); 28 __llvm_profile_set_filename(getenv("LLVM_PROFILE_FILE")); 29 __llvm_profile_initialize_file(); 30 } 31 32 static int CustomRegisterRuntime(void) { 33 __llvm_profile_initialize(); 34 35 if (pthread_atfork(NULL, NULL, updateFilenameAfterFork) < 0) { 36 fprintf(stderr, "[%d] [%s] pthread_atfork()=%d\n", getpid(), 37 __PRETTY_FUNCTION__, errno); 38 } 39 return 0; 40 } 41 42 MOZ_RUNINIT int __llvm_profile_runtime = CustomRegisterRuntime(); 43 # if defined(__cplusplus) 44 } 45 # endif 46 #endif // defined(MOZ_PROFILE_GENERATE) && defined(XP_LINUX) && 47 // !defined(ANDROID)