xref: /aosp_15_r20/external/cronet/base/test/clang_profiling.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/test/clang_profiling.h"
6 
7 #include "base/no_destructor.h"
8 #include "base/synchronization/lock.h"
9 #include "build/build_config.h"
10 
11 extern "C" int __llvm_profile_dump(void);
12 
13 namespace base {
14 
WriteClangProfilingProfile()15 void WriteClangProfilingProfile() {
16   // __llvm_profile_dump() guarantees that it will not dump profiling
17   // information if it is being called twice or more. However, it is not thread
18   // safe, as it is supposed to be called from atexit() handler rather than
19   // being called directly from random places. Since we have to call it
20   // ourselves, we must ensure thread safety in order to prevent duplication of
21   // profiling counters.
22   static base::NoDestructor<base::Lock> lock;
23   base::AutoLock auto_lock(*lock);
24 
25 // Fuchsia's profile runtime does not handle profile dumping.
26 // Coverage builds are built with runtime counter relocation and are expected to
27 // be run under continuous coverage mode (enabled by adding %c to the
28 // LLVM_PROFILE_FILE environment variable), which updates counters in real time,
29 // so __llvm_profile_dump() is not needed.
30 #if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(USE_CLANG_COVERAGE)
31   __llvm_profile_dump();
32 #endif  // !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(USE_CLANG_COVERAGE)
33 }
34 
35 }  // namespace base
36