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
|
// Copyright 2025 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/threading/platform_thread_metrics.h"
#include <memory>
#include <mutex>
#include <optional>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/sparse_histogram.h"
#include "base/no_destructor.h"
#include "base/process/current_process.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "base/task/thread_pool.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/threading/platform_thread_internal_posix.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace base {
namespace {
#if BUILDFLAG(IS_ANDROID)
std::string GetCurrentProcessName(CurrentProcess::NameKey key) {
std::string process_name = CurrentProcess::GetInstance().GetName(key);
// The process name may have whitespaces (e.g. 'GPU Process') and that won't
// work as a histogram name.
RemoveChars(process_name, " ", &process_name);
return process_name;
}
#endif // BUILDFLAG(IS_ANDROID)
} // namespace
// Apple and Windows have platform-specific create methods.
#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN)
// static
std::unique_ptr<PlatformThreadMetrics>
PlatformThreadMetrics::CreateForCurrentThread() {
return CreateFromHandle(PlatformThread::CurrentHandle());
}
#elif BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_LINUX)
// static
std::unique_ptr<PlatformThreadMetrics>
PlatformThreadMetrics::CreateForCurrentThread() {
return CreateFromId(PlatformThread::CurrentId());
}
// static
std::unique_ptr<PlatformThreadMetrics> PlatformThreadMetrics::CreateFromId(
PlatformThreadId tid) {
if (tid == kInvalidThreadId) {
return nullptr;
}
return WrapUnique(new PlatformThreadMetrics(tid));
}
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) ||
// BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_LINUX)
PlatformThreadMetrics::~PlatformThreadMetrics() = default;
double PlatformThreadMetrics::GetCPUUsageProportion(TimeDelta cumulative_cpu) {
TimeTicks time = TimeTicks::Now();
if (!last_cpu_time_.has_value()) {
// First call, just set the last values.
CHECK(last_cumulative_cpu_.is_zero());
last_cumulative_cpu_ = cumulative_cpu;
last_cpu_time_ = time;
return 0;
}
TimeDelta cpu_time_delta = cumulative_cpu - last_cumulative_cpu_;
TimeDelta time_delta = time - last_cpu_time_.value();
if (time_delta.is_zero()) {
return 0;
}
last_cumulative_cpu_ = cumulative_cpu;
last_cpu_time_ = time;
return 100.0 * cpu_time_delta / time_delta;
}
std::optional<double> PlatformThreadMetrics::GetCPUUsageProportion() {
if (const std::optional<TimeDelta> cpu_usage = GetCumulativeCPUUsage()) {
return GetCPUUsageProportion(cpu_usage.value());
}
return std::nullopt;
}
#if BUILDFLAG(IS_ANDROID)
PlatformThreadPriorityMonitor::PlatformThreadPriorityMonitor()
: process_name_(GetCurrentProcessName({})) {}
PlatformThreadPriorityMonitor::~PlatformThreadPriorityMonitor() = default;
// static
PlatformThreadPriorityMonitor& PlatformThreadPriorityMonitor::Get() {
static base::NoDestructor<PlatformThreadPriorityMonitor> instance;
return *instance;
}
std::string PlatformThreadPriorityMonitor::GetHistogramNameForSuffix(
const std::string_view suffix) {
return StrCat({"Scheduling.ThreadPriority.", process_name_, ".", suffix});
}
void PlatformThreadPriorityMonitor::RegisterCurrentThread(
const std::string_view suffix) {
PlatformThreadId current_thread_id = PlatformThread::CurrentId();
HistogramBase* histogram =
SparseHistogram::FactoryGet(GetHistogramNameForSuffix(suffix),
HistogramBase::kUmaTargetedHistogramFlag);
AutoLock auto_lock(lock_);
thread_id_to_histogram_[current_thread_id] = histogram;
}
void PlatformThreadPriorityMonitor::UnregisterCurrentThread() {
PlatformThreadId current_thread_id = PlatformThread::CurrentId();
AutoLock auto_lock(lock_);
thread_id_to_histogram_.erase(current_thread_id);
}
void PlatformThreadPriorityMonitor::RecordThreadPriorities() {
AutoLock auto_lock(lock_);
for (auto& [thread_id, histogram] : thread_id_to_histogram_) {
histogram->Add(internal::GetThreadNiceValue(thread_id));
}
}
// static
void PlatformThreadPriorityMonitor::ScheduleRecordingTask() {
static NoDestructor<RepeatingClosure> closure(BindRepeating([] {
auto& monitor = PlatformThreadPriorityMonitor::Get();
monitor.RecordThreadPriorities();
monitor.ScheduleRecordingTask();
}));
ThreadPool::PostDelayedTask(
FROM_HERE,
{TaskPriority::BEST_EFFORT, TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
*closure, kMinSamplingInterval);
}
void PlatformThreadPriorityMonitor::Start() {
std::call_once(once_flag_,
&PlatformThreadPriorityMonitor::ScheduleRecordingTask);
}
#endif // BUILDFLAG(IS_ANDROID)
} // namespace base
|