1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/android/reached_code_profiler.h"
#include <signal.h>
#include <sys/time.h>
#include <ucontext.h>
#include <unistd.h>
#include <atomic>
#include "base/android/library_loader/anchor_functions.h"
#include "base/android/orderfile/orderfile_buildflags.h"
#include "base/android/reached_addresses_bitset.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/functional/bind.h"
#include "base/linux_util.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/scoped_generic.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#if !BUILDFLAG(SUPPORTS_CODE_ORDERING)
#error Code ordering support is required for the reached code profiler.
#endif
namespace base {
namespace android {
namespace {
#if !defined(NDEBUG) || defined(COMPONENT_BUILD) || defined(OFFICIAL_BUILD)
// Always disabled for debug builds to avoid hitting a limit of signal
// interrupts that can get delivered into a single HANDLE_EINTR. Also
// debugging experience would be bad if there are a lot of signals flying
// around.
// Always disabled for component builds because in this case the code is not
// organized in one contiguous region which is required for the reached code
// profiler.
// Disabled for official builds because `g_text_bitfield` isn't included in
// official builds.
constexpr const bool kConfigurationSupported = false;
#else
constexpr const bool kConfigurationSupported = true;
#endif
constexpr const char kDumpToFileFlag[] = "reached-code-profiler-dump-to-file";
constexpr uint64_t kIterationsBeforeSkipping = 50;
constexpr uint64_t kIterationsBetweenUpdates = 100;
constexpr int kProfilerSignal = SIGWINCH;
constexpr base::TimeDelta kSamplingInterval = base::Milliseconds(10);
constexpr base::TimeDelta kDumpInterval = base::Seconds(30);
void HandleSignal(int signal, siginfo_t* info, void* context) {
if (signal != kProfilerSignal)
return;
ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
#if defined(ARCH_CPU_ARM64)
uintptr_t address = ucontext->uc_mcontext.pc;
#else
uintptr_t address = ucontext->uc_mcontext.arm_pc;
#endif
ReachedAddressesBitset::GetTextBitset()->RecordAddress(address);
}
struct ScopedTimerCloseTraits {
static absl::optional<timer_t> InvalidValue() { return absl::nullopt; }
static void Free(absl::optional<timer_t> x) { timer_delete(*x); }
};
// RAII object holding an interval timer.
using ScopedTimer =
base::ScopedGeneric<absl::optional<timer_t>, ScopedTimerCloseTraits>;
void DumpToFile(const base::FilePath& path,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
DCHECK(task_runner->BelongsToCurrentThread());
auto dir_path = path.DirName();
if (!base::DirectoryExists(dir_path) && !base::CreateDirectory(dir_path)) {
PLOG(ERROR) << "Could not create " << dir_path;
return;
}
std::vector<uint32_t> reached_offsets =
ReachedAddressesBitset::GetTextBitset()->GetReachedOffsets();
base::StringPiece contents(
reinterpret_cast<const char*>(reached_offsets.data()),
reached_offsets.size());
if (!base::ImportantFileWriter::WriteFileAtomically(path, contents,
"ReachedDump")) {
LOG(ERROR) << "Could not write reached dump into " << path;
}
task_runner->PostDelayedTask(
FROM_HERE, base::BindOnce(&DumpToFile, path, task_runner), kDumpInterval);
}
class ReachedCodeProfiler {
public:
static ReachedCodeProfiler* GetInstance() {
static base::NoDestructor<ReachedCodeProfiler> instance;
return instance.get();
}
ReachedCodeProfiler(const ReachedCodeProfiler&) = delete;
ReachedCodeProfiler& operator=(const ReachedCodeProfiler&) = delete;
// Starts to periodically send |kProfilerSignal| to all threads.
void Start(LibraryProcessType library_process_type,
base::TimeDelta sampling_interval) {
if (is_enabled_)
return;
// Set |kProfilerSignal| signal handler.
// TODO(crbug.com/916263): consider restoring |old_handler| after the
// profiler gets stopped.
struct sigaction old_handler;
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = &HandleSignal;
sa.sa_flags = SA_RESTART | SA_SIGINFO;
int ret = sigaction(kProfilerSignal, &sa, &old_handler);
if (ret) {
PLOG(ERROR) << "Error setting signal handler. The reached code profiler "
"is disabled";
return;
}
// Create a new interval timer.
struct sigevent sevp;
memset(&sevp, 0, sizeof(sevp));
sevp.sigev_notify = SIGEV_THREAD;
sevp.sigev_notify_function = &OnTimerNotify;
timer_t timerid;
ret = timer_create(CLOCK_PROCESS_CPUTIME_ID, &sevp, &timerid);
if (ret) {
PLOG(ERROR)
<< "timer_create() failed. The reached code profiler is disabled";
return;
}
timer_.reset(timerid);
// Start the interval timer.
struct itimerspec its;
memset(&its, 0, sizeof(its));
its.it_interval.tv_nsec =
checked_cast<long>(sampling_interval.InNanoseconds());
its.it_value = its.it_interval;
ret = timer_settime(timerid, 0, &its, nullptr);
if (ret) {
PLOG(ERROR)
<< "timer_settime() failed. The reached code profiler is disabled";
return;
}
if (library_process_type == PROCESS_BROWSER)
StartDumpingReachedCode();
is_enabled_ = true;
}
// Stops profiling.
void Stop() {
timer_.reset();
dumping_thread_.reset();
is_enabled_ = false;
}
// Returns whether the profiler is currently enabled.
bool IsEnabled() { return is_enabled_; }
private:
ReachedCodeProfiler()
: current_pid_(getpid()), iteration_number_(0), is_enabled_(false) {}
static void OnTimerNotify(sigval_t ignored) {
ReachedCodeProfiler::GetInstance()->SendSignalToAllThreads();
}
void SendSignalToAllThreads() {
// This code should be thread-safe.
base::AutoLock scoped_lock(lock_);
++iteration_number_;
if (iteration_number_ <= kIterationsBeforeSkipping ||
iteration_number_ % kIterationsBetweenUpdates == 0) {
tids_.clear();
if (!base::GetThreadsForProcess(current_pid_, &tids_)) {
LOG(WARNING) << "Failed to get a list of threads for process "
<< current_pid_;
return;
}
}
pid_t current_tid = gettid();
for (pid_t tid : tids_) {
if (tid != current_tid)
tgkill(current_pid_, tid, kProfilerSignal);
}
}
void StartDumpingReachedCode() {
const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
if (!cmdline->HasSwitch(kDumpToFileFlag))
return;
base::FilePath dir_path(cmdline->GetSwitchValueASCII(kDumpToFileFlag));
if (dir_path.empty()) {
if (!base::PathService::Get(base::DIR_CACHE, &dir_path)) {
LOG(WARNING) << "Failed to get cache dir path.";
return;
}
}
auto file_path =
dir_path.Append(base::StringPrintf("reached-code-%d.txt", getpid()));
dumping_thread_ =
std::make_unique<base::Thread>("ReachedCodeProfilerDumpingThread");
dumping_thread_->StartWithOptions(
base::Thread::Options(base::ThreadType::kBackground));
dumping_thread_->task_runner()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&DumpToFile, file_path, dumping_thread_->task_runner()),
kDumpInterval);
}
base::Lock lock_;
std::vector<pid_t> tids_;
const pid_t current_pid_;
uint64_t iteration_number_;
ScopedTimer timer_;
std::unique_ptr<base::Thread> dumping_thread_;
bool is_enabled_;
friend class NoDestructor<ReachedCodeProfiler>;
};
bool ShouldEnableReachedCodeProfiler() {
if (!kConfigurationSupported)
return false;
const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
return cmdline->HasSwitch(switches::kEnableReachedCodeProfiler);
}
} // namespace
void InitReachedCodeProfilerAtStartup(LibraryProcessType library_process_type) {
// The profiler shouldn't be run as part of webview.
CHECK(library_process_type == PROCESS_BROWSER ||
library_process_type == PROCESS_CHILD);
if (!ShouldEnableReachedCodeProfiler())
return;
int interval_us = 0;
base::TimeDelta sampling_interval = kSamplingInterval;
if (base::StringToInt(
base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
switches::kReachedCodeSamplingIntervalUs),
&interval_us) &&
interval_us > 0) {
sampling_interval = base::Microseconds(interval_us);
}
ReachedCodeProfiler::GetInstance()->Start(library_process_type,
sampling_interval);
}
bool IsReachedCodeProfilerEnabled() {
return ReachedCodeProfiler::GetInstance()->IsEnabled();
}
bool IsReachedCodeProfilerSupported() {
return kConfigurationSupported;
}
} // namespace android
} // namespace base
|