1 // Copyright 2019 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.
5 #include "base/android/reached_code_profiler.h"
14 #include "base/android/library_loader/anchor_functions.h"
15 #include "base/android/orderfile/orderfile_buildflags.h"
16 #include "base/android/reached_addresses_bitset.h"
17 #include "base/base_switches.h"
18 #include "base/command_line.h"
19 #include "base/feature_list.h"
20 #include "base/files/file_path.h"
21 #include "base/files/file_util.h"
22 #include "base/files/important_file_writer.h"
23 #include "base/functional/bind.h"
24 #include "base/linux_util.h"
25 #include "base/logging.h"
26 #include "base/no_destructor.h"
27 #include "base/path_service.h"
28 #include "base/scoped_generic.h"
29 #include "base/strings/string_number_conversions.h"
30 #include "base/strings/string_piece.h"
31 #include "base/strings/stringprintf.h"
32 #include "base/synchronization/lock.h"
33 #include "base/task/single_thread_task_runner.h"
34 #include "base/threading/thread.h"
35 #include "base/time/time.h"
36 #include "base/timer/timer.h"
37 #include "build/build_config.h"
38 #include "third_party/abseil-cpp/absl/types/optional.h"
40 #if !BUILDFLAG(SUPPORTS_CODE_ORDERING)
41 #error Code ordering support is required for the reached code profiler.
49 #if !defined(NDEBUG) || defined(COMPONENT_BUILD) || defined(OFFICIAL_BUILD)
50 // Always disabled for debug builds to avoid hitting a limit of signal
51 // interrupts that can get delivered into a single HANDLE_EINTR. Also
52 // debugging experience would be bad if there are a lot of signals flying
54 // Always disabled for component builds because in this case the code is not
55 // organized in one contiguous region which is required for the reached code
57 // Disabled for official builds because `g_text_bitfield` isn't included in
59 constexpr const bool kConfigurationSupported = false;
61 constexpr const bool kConfigurationSupported = true;
64 constexpr const char kDumpToFileFlag[] = "reached-code-profiler-dump-to-file";
66 constexpr uint64_t kIterationsBeforeSkipping = 50;
67 constexpr uint64_t kIterationsBetweenUpdates = 100;
68 constexpr int kProfilerSignal = SIGWINCH;
70 constexpr base::TimeDelta kSamplingInterval = base::Milliseconds(10);
71 constexpr base::TimeDelta kDumpInterval = base::Seconds(30);
73 void HandleSignal(int signal, siginfo_t* info, void* context) {
74 if (signal != kProfilerSignal)
77 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
78 #if defined(ARCH_CPU_ARM64)
79 uintptr_t address = ucontext->uc_mcontext.pc;
81 uintptr_t address = ucontext->uc_mcontext.arm_pc;
83 ReachedAddressesBitset::GetTextBitset()->RecordAddress(address);
86 struct ScopedTimerCloseTraits {
87 static absl::optional<timer_t> InvalidValue() { return absl::nullopt; }
89 static void Free(absl::optional<timer_t> x) { timer_delete(*x); }
92 // RAII object holding an interval timer.
94 base::ScopedGeneric<absl::optional<timer_t>, ScopedTimerCloseTraits>;
96 void DumpToFile(const base::FilePath& path,
97 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
98 DCHECK(task_runner->BelongsToCurrentThread());
100 auto dir_path = path.DirName();
101 if (!base::DirectoryExists(dir_path) && !base::CreateDirectory(dir_path)) {
102 PLOG(ERROR) << "Could not create " << dir_path;
106 std::vector<uint32_t> reached_offsets =
107 ReachedAddressesBitset::GetTextBitset()->GetReachedOffsets();
108 base::StringPiece contents(
109 reinterpret_cast<const char*>(reached_offsets.data()),
110 reached_offsets.size());
111 if (!base::ImportantFileWriter::WriteFileAtomically(path, contents,
113 LOG(ERROR) << "Could not write reached dump into " << path;
116 task_runner->PostDelayedTask(
117 FROM_HERE, base::BindOnce(&DumpToFile, path, task_runner), kDumpInterval);
120 class ReachedCodeProfiler {
122 static ReachedCodeProfiler* GetInstance() {
123 static base::NoDestructor<ReachedCodeProfiler> instance;
124 return instance.get();
127 ReachedCodeProfiler(const ReachedCodeProfiler&) = delete;
128 ReachedCodeProfiler& operator=(const ReachedCodeProfiler&) = delete;
130 // Starts to periodically send |kProfilerSignal| to all threads.
131 void Start(LibraryProcessType library_process_type,
132 base::TimeDelta sampling_interval) {
136 // Set |kProfilerSignal| signal handler.
137 // TODO(crbug.com/916263): consider restoring |old_handler| after the
138 // profiler gets stopped.
139 struct sigaction old_handler;
141 sigemptyset(&sa.sa_mask);
142 sa.sa_sigaction = &HandleSignal;
143 sa.sa_flags = SA_RESTART | SA_SIGINFO;
144 int ret = sigaction(kProfilerSignal, &sa, &old_handler);
146 PLOG(ERROR) << "Error setting signal handler. The reached code profiler "
151 // Create a new interval timer.
152 struct sigevent sevp;
153 memset(&sevp, 0, sizeof(sevp));
154 sevp.sigev_notify = SIGEV_THREAD;
155 sevp.sigev_notify_function = &OnTimerNotify;
157 ret = timer_create(CLOCK_PROCESS_CPUTIME_ID, &sevp, &timerid);
160 << "timer_create() failed. The reached code profiler is disabled";
164 timer_.reset(timerid);
166 // Start the interval timer.
167 struct itimerspec its;
168 memset(&its, 0, sizeof(its));
169 its.it_interval.tv_nsec =
170 checked_cast<long>(sampling_interval.InNanoseconds());
171 its.it_value = its.it_interval;
172 ret = timer_settime(timerid, 0, &its, nullptr);
175 << "timer_settime() failed. The reached code profiler is disabled";
179 if (library_process_type == PROCESS_BROWSER)
180 StartDumpingReachedCode();
188 dumping_thread_.reset();
192 // Returns whether the profiler is currently enabled.
193 bool IsEnabled() { return is_enabled_; }
196 ReachedCodeProfiler()
197 : current_pid_(getpid()), iteration_number_(0), is_enabled_(false) {}
199 static void OnTimerNotify(sigval_t ignored) {
200 ReachedCodeProfiler::GetInstance()->SendSignalToAllThreads();
203 void SendSignalToAllThreads() {
204 // This code should be thread-safe.
205 base::AutoLock scoped_lock(lock_);
208 if (iteration_number_ <= kIterationsBeforeSkipping ||
209 iteration_number_ % kIterationsBetweenUpdates == 0) {
211 if (!base::GetThreadsForProcess(current_pid_, &tids_)) {
212 LOG(WARNING) << "Failed to get a list of threads for process "
218 pid_t current_tid = gettid();
219 for (pid_t tid : tids_) {
220 if (tid != current_tid)
221 tgkill(current_pid_, tid, kProfilerSignal);
225 void StartDumpingReachedCode() {
226 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
227 if (!cmdline->HasSwitch(kDumpToFileFlag))
230 base::FilePath dir_path(cmdline->GetSwitchValueASCII(kDumpToFileFlag));
231 if (dir_path.empty()) {
232 if (!base::PathService::Get(base::DIR_CACHE, &dir_path)) {
233 LOG(WARNING) << "Failed to get cache dir path.";
239 dir_path.Append(base::StringPrintf("reached-code-%d.txt", getpid()));
242 std::make_unique<base::Thread>("ReachedCodeProfilerDumpingThread");
243 dumping_thread_->StartWithOptions(
244 base::Thread::Options(base::ThreadType::kBackground));
245 dumping_thread_->task_runner()->PostDelayedTask(
247 base::BindOnce(&DumpToFile, file_path, dumping_thread_->task_runner()),
252 std::vector<pid_t> tids_;
253 const pid_t current_pid_;
254 uint64_t iteration_number_;
256 std::unique_ptr<base::Thread> dumping_thread_;
260 friend class NoDestructor<ReachedCodeProfiler>;
263 bool ShouldEnableReachedCodeProfiler() {
264 if (!kConfigurationSupported)
267 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
268 return cmdline->HasSwitch(switches::kEnableReachedCodeProfiler);
273 void InitReachedCodeProfilerAtStartup(LibraryProcessType library_process_type) {
274 // The profiler shouldn't be run as part of webview.
275 CHECK(library_process_type == PROCESS_BROWSER ||
276 library_process_type == PROCESS_CHILD);
278 if (!ShouldEnableReachedCodeProfiler())
282 base::TimeDelta sampling_interval = kSamplingInterval;
283 if (base::StringToInt(
284 base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
285 switches::kReachedCodeSamplingIntervalUs),
288 sampling_interval = base::Microseconds(interval_us);
290 ReachedCodeProfiler::GetInstance()->Start(library_process_type,
294 bool IsReachedCodeProfilerEnabled() {
295 return ReachedCodeProfiler::GetInstance()->IsEnabled();
298 bool IsReachedCodeProfilerSupported() {
299 return kConfigurationSupported;
302 } // namespace android