Skip to content

Truly independent web browser

LibWebView+UI/Qt: Show shared process usage in the performance monitor

Andreas Klingd8bd5f146334
diff --git a/Documentation/TabPerformanceMonitor.md b/Documentation/TabPerformanceMonitor.md
index 77872392de2..19d706f5e9a 100644
--- a/Documentation/TabPerformanceMonitor.md
+++ b/Documentation/TabPerformanceMonitor.md
@@ -17,11 +17,13 @@ counter differencing and windowing independently of the UI and OS APIs.
* **CPU:** CPU-time deltas divided by actual monotonic elapsed time. 100% is
one logical CPU; values above 100% are expected. All live and terminated
- threads in exclusively owned WebContent processes contribute, so a worker
+ threads in the tab's WebContent processes contribute, so a worker
counts under the WebContent process that hosts it. Registered page and
- embedded-frame ownership identify WebContent owners. Processes shared across
- tabs remain unattributed, and so does a process that runs on without pages
- for its shared workers.
+ embedded-frame ownership identify WebContent owners. A process shared across
+ tabs counts in full toward each of them, so their values overlap. The toolbar
+ then marks CPU, MEM and PEAK with `*`, and its tooltip gives the number of
+ other tabs that share the processes. A process that runs on without pages for
+ its shared workers remains unattributed.
The first sample establishes a baseline. The sparkline retains ten seconds.
* **MEM:** An estimate of owned process memory, counted once per process.
macOS uses the kernel's physical-footprint ledger, including compressed
@@ -31,6 +33,7 @@ counter differencing and windowing independently of the UI and OS APIs.
are not directly comparable. Other platforms report unavailable until a
collector is implemented. Shared helper/cache allocations and resources
without a process-level ownership charge are excluded.
+ Memory of a process shared across tabs counts in full toward each of them.
* **Download/upload:** HTTP response/request body bytes transferred by curl,
before response decompression. Disk-cache hits contribute no traffic.
Headers, TLS/TCP overhead, WebSockets, and helper-initiated background
diff --git a/Libraries/LibWebView/TabPerformanceMonitor.cpp b/Libraries/LibWebView/TabPerformanceMonitor.cpp
index 6857658a33d..a8685d3ffd9 100644
--- a/Libraries/LibWebView/TabPerformanceMonitor.cpp
+++ b/Libraries/LibWebView/TabPerformanceMonitor.cpp
@@ -99,16 +99,28 @@ void TabPerformanceMonitor::did_present(u64 view_id)
void TabPerformanceMonitor::sample()
{
HashMap<u64, HashMap<pid_t, Core::Platform::ProcessResourceUsage>> processes;
+ HashMap<u64, HashTable<u64>> tabs_sharing_processes;
Application::process_manager().for_each_process([&](Process& process) {
- Optional<u64> owner;
- if (process.type() == ProcessType::WebContent) {
- if (auto client = process.client<WebContentClient>())
- owner = client->exclusive_performance_owner();
- }
- if (!owner.has_value())
+ if (process.type() != ProcessType::WebContent)
+ return;
+ auto client = process.client<WebContentClient>();
+ if (!client)
+ return;
+ auto owners = client->performance_owners();
+ if (owners.is_empty())
return;
- if (auto usage = Application::process_manager().resource_usage(process.pid()); usage.has_value())
- processes.ensure(*owner).set(process.pid(), *usage);
+ auto usage = Application::process_manager().resource_usage(process.pid());
+ if (!usage.has_value())
+ return;
+ // NB: A process that several tabs share counts in full toward each of them. Each tab then reports how
+ // many other tabs share its processes, so the UI can show that the numbers overlap.
+ for (auto owner : owners) {
+ processes.ensure(owner).set(process.pid(), *usage);
+ for (auto other_owner : owners) {
+ if (other_owner != owner)
+ tabs_sharing_processes.ensure(owner).set(other_owner);
+ }
+ }
});
auto now = MonotonicTime::now();
ViewImplementation::for_each_view([&](ViewImplementation& view) {
@@ -118,6 +130,8 @@ void TabPerformanceMonitor::sample()
if (auto page = view.traversable().display_page())
top_level_process = page->client().pid();
auto stats = m_tabs.ensure(view.view_id()).sample(now, processes.ensure(view.view_id()), top_level_process);
+ if (auto sharing = tabs_sharing_processes.get(view.view_id()); sharing.has_value())
+ stats.other_tabs_sharing_processes = sharing->size();
if (view.on_performance_stats)
view.on_performance_stats(stats);
return IterationDecision::Continue;
diff --git a/Libraries/LibWebView/TabPerformanceStats.h b/Libraries/LibWebView/TabPerformanceStats.h
index 452b4bc43fb..4e5c795fc8a 100644
--- a/Libraries/LibWebView/TabPerformanceStats.h
+++ b/Libraries/LibWebView/TabPerformanceStats.h
@@ -26,6 +26,8 @@ struct TabPerformanceStats {
u64 upload_bytes_per_second { 0 };
Optional<double> frames_per_second;
Vector<double> cpu_history;
+ // CPU and memory include whole processes that this many other tabs also use.
+ u32 other_tabs_sharing_processes { 0 };
};
// Pure accounting state: callers supply monotonic timestamps and cumulative process counters.
diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cpp
index 9b4b1d83aca..3bbd808cfbd 100644
--- a/Libraries/LibWebView/WebContentClient.cpp
+++ b/Libraries/LibWebView/WebContentClient.cpp
@@ -1327,24 +1327,27 @@ void WebContentClient::did_release_image_decoder()
ImageDecoderManager::the().process_did_release_decoder(child_id(), *generation);
}
-Optional<u64> WebContentClient::exclusive_performance_owner() const
+Vector<u64> WebContentClient::performance_owners() const
{
- Optional<u64> owner;
- auto add_owner = [&](u64 id) {
- if (owner.has_value() && *owner != id)
- return false;
- owner = id;
- return true;
- };
+ Vector<u64> owners;
for (auto const& it : m_pages) {
auto const& page = it.value;
if (!page->is_open())
continue;
auto const& view = page->view();
- if (!add_owner(view.owner_view_id().value_or(view.view_id())))
- return {};
+ auto owner = view.owner_view_id().value_or(view.view_id());
+ if (!owners.contains_slow(owner))
+ owners.append(owner);
}
- return owner;
+ return owners;
+}
+
+Optional<u64> WebContentClient::exclusive_performance_owner() const
+{
+ auto owners = performance_owners();
+ if (owners.size() != 1)
+ return {};
+ return owners.first();
}
}
diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.h
index 0ef2f0a83dd..318f12a49f6 100644
--- a/Libraries/LibWebView/WebContentClient.h
+++ b/Libraries/LibWebView/WebContentClient.h
@@ -166,6 +166,8 @@ public:
// the same reason.
virtual bool may_act_for_storage_area(Web::StorageAPI::StorageAreaId) const override;
+ // The tabs with an open page in this process, each listed once.
+ Vector<u64> performance_owners() const;
Optional<u64> exclusive_performance_owner() const;
void did_lose_process();
diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipc
index a7e748e9905..bbb2edda955 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -65,6 +65,7 @@ struct WebView::TabPerformanceStats {
u64 upload_bytes_per_second;
Optional<double> frames_per_second;
Vector<double> cpu_history;
+ u32 other_tabs_sharing_processes;
}
[ExistingCpp]
diff --git a/UI/Qt/PerformanceMonitorWidget.cpp b/UI/Qt/PerformanceMonitorWidget.cpp
index f00e589286d..874a2442d03 100644
--- a/UI/Qt/PerformanceMonitorWidget.cpp
+++ b/UI/Qt/PerformanceMonitorWidget.cpp
@@ -16,6 +16,7 @@
namespace Ladybird {
static constexpr Array<char const*, 6> s_labels { "CPU ", "MEM ", "PEAK ", "↓ ", "↑ ", "FPS " };
+static constexpr Array<char const*, 6> s_shared_labels { "CPU* ", "MEM* ", "PEAK* ", "↓ ", "↑ ", "FPS " };
static constexpr Array<char const*, 6> s_value_templates { "9999%", "999.9 MB", "999.9 MB", "999.9 MB/s", "999.9 MB/s", "999" };
static constexpr int metric_spacing = 12;
static constexpr int text_start = 48;
@@ -31,17 +32,29 @@ PerformanceMonitorWidget::PerformanceMonitorWidget(QWidget* parent)
setFixedSize(sizeHint());
}
+// NB: A label column fits its widest form, so marking shared values does not move anything.
+int PerformanceMonitorWidget::label_width(size_t index) const
+{
+ return max(fontMetrics().horizontalAdvance(s_labels[index]), fontMetrics().horizontalAdvance(s_shared_labels[index]));
+}
+
QSize PerformanceMonitorWidget::sizeHint() const
{
auto width = text_start + trailing_padding + metric_spacing * (s_labels.size() - 1);
for (size_t i = 0; i < s_labels.size(); ++i)
- width += fontMetrics().horizontalAdvance(s_labels[i]) + fontMetrics().horizontalAdvance(s_value_templates[i]);
+ width += label_width(i) + fontMetrics().horizontalAdvance(s_value_templates[i]);
return { static_cast<int>(width), 24 };
}
void PerformanceMonitorWidget::set_stats(WebView::TabPerformanceStats const& stats)
{
m_stats = stats;
+ if (m_stats.other_tabs_sharing_processes == 0)
+ setToolTip({});
+ else if (m_stats.other_tabs_sharing_processes == 1)
+ setToolTip("* CPU and memory are for processes this tab shares with 1 other tab.");
+ else
+ setToolTip(QString("* CPU and memory are for processes this tab shares with %1 other tabs.").arg(m_stats.other_tabs_sharing_processes));
update();
}
@@ -71,8 +84,8 @@ void PerformanceMonitorWidget::paintEvent(QPaintEvent*)
auto dark = ChromeStyle::is_dark(palette());
Array<QColor, 6> colors { neutral, neutral, neutral, QColor(dark ? BrandColors::SlateBlue100 : BrandColors::SlateBlue900), QColor(dark ? BrandColors::Violet50 : BrandColors::Violet900), neutral };
for (size_t i = 0; i < s_labels.size(); ++i) {
- QString label = s_labels[i];
- auto label_width = fontMetrics().horizontalAdvance(label);
+ QString label = m_stats.other_tabs_sharing_processes > 0 ? s_shared_labels[i] : s_labels[i];
+ auto label_width = this->label_width(i);
auto value_width = fontMetrics().horizontalAdvance(s_value_templates[i]);
painter.setPen(i == 3 || i == 4 ? colors[i] : label_color);
painter.drawText(QRect(x, 0, label_width, height()), Qt::AlignVCenter | Qt::AlignLeft, label);
@@ -92,6 +105,8 @@ void PerformanceMonitorWidget::paintEvent(QPaintEvent*)
x += label_width + value_width + metric_spacing;
accessible_text += label + segments[i] + ' ';
}
+ if (!toolTip().isEmpty())
+ accessible_text += toolTip();
setAccessibleName(accessible_text);
if (m_stats.cpu_history.size() < 2)
return;
diff --git a/UI/Qt/PerformanceMonitorWidget.h b/UI/Qt/PerformanceMonitorWidget.h
index 186f31b20c1..6cde74d1e8c 100644
--- a/UI/Qt/PerformanceMonitorWidget.h
+++ b/UI/Qt/PerformanceMonitorWidget.h
@@ -24,6 +24,7 @@ public:
private:
virtual void paintEvent(QPaintEvent*) override;
QStringList text_segments() const;
+ int label_width(size_t index) const;
WebView::TabPerformanceStats m_stats;
};