Chromium Code Reviews| Index: chrome/browser/chromeos/status/memory_menu_button.cc |
| diff --git a/chrome/browser/chromeos/status/memory_menu_button.cc b/chrome/browser/chromeos/status/memory_menu_button.cc |
| index 606ebc16ebc5732c9b2efafd743668ac88466510..b0b0a4a62c81ddf19e2ad8f595b5fcf61c399a97 100644 |
| --- a/chrome/browser/chromeos/status/memory_menu_button.cc |
| +++ b/chrome/browser/chromeos/status/memory_menu_button.cc |
| @@ -8,6 +8,8 @@ |
| #include "base/stringprintf.h" |
| #include "chrome/browser/chromeos/status/status_area_host.h" |
| #include "chrome/browser/memory_purger.h" |
| +#include "content/browser/renderer_host/render_process_host.h" |
| +#include "content/common/notification_service.h" |
| #include "grit/generated_resources.h" |
| #include "ui/base/l10n/l10n_util.h" |
| #include "views/widget/widget.h" |
| @@ -33,15 +35,17 @@ const int kUpdateIntervalSeconds = 5; |
| MemoryMenuButton::MemoryMenuButton(StatusAreaHost* host) |
| : StatusAreaButton(host, this), |
| - mem_total_(0), |
| - shmem_(0), |
| - mem_free_(0), |
| - mem_buffers_(0), |
| - mem_cache_(0) { |
| + meminfo_(new base::SystemMemoryInfoKB()), |
| + renderer_kills_(0) { |
| + // Track renderer kills, as the kernel OOM killer will start to kill our |
| + // renderers as we run out of memory. |
| + registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| + NotificationService::AllSources()); |
| UpdateTextAndSetNextTimer(); |
| } |
| MemoryMenuButton::~MemoryMenuButton() { |
| + delete meminfo_; |
| } |
| void MemoryMenuButton::UpdateTextAndSetNextTimer() { |
| @@ -52,17 +56,20 @@ void MemoryMenuButton::UpdateTextAndSetNextTimer() { |
| } |
| void MemoryMenuButton::UpdateText() { |
| - base::GetSystemMemoryInfo(&mem_total_, &mem_free_, &mem_buffers_, &mem_cache_, |
| - &shmem_); |
| - std::wstring label = base::StringPrintf(L"%d MB", mem_free_ / 1024); |
| + base::GetSystemMemoryInfo(meminfo_); |
| + // "Anonymous" memory, meaning not mapped to a file (which has a name), |
| + // represents memory that has been dynamically allocated to a process. |
| + // It thus approximates heap memory usage across all processes. |
| + int anon_kb = meminfo_->active_anon + meminfo_->inactive_anon; |
| + std::wstring label = base::StringPrintf(L"%d MB / %d", |
|
stevenjb
2011/08/10 19:45:51
nit: maybe (%d) instead? Do we want to only show n
James Cook
2011/08/10 22:07:52
Done.
|
| + anon_kb / 1024, |
| + renderer_kills_); |
| SetText(label); |
| std::wstring tooltip = base::StringPrintf( |
| - L"%d MB total\n%d MB free\n%d MB buffers\n%d MB cache\n%d MB shmem", |
| - mem_total_ / 1024, |
| - mem_free_ / 1024, |
| - mem_buffers_ / 1024, |
| - mem_cache_ / 1024, |
| - shmem_ / 1024); |
| + L"%d MB allocated (anonymous)\n" |
| + L"%d renderer kill(s)", |
| + anon_kb / 1024, |
| + renderer_kills_); |
| SetTooltipText(tooltip); |
| SchedulePaint(); |
| } |
| @@ -71,15 +78,15 @@ void MemoryMenuButton::UpdateText() { |
| std::wstring MemoryMenuButton::GetLabel(int id) const { |
| switch (id) { |
| case MEM_TOTAL_ITEM: |
| - return StringPrintf(L"%d MB total", mem_total_ / 1024); |
| + return StringPrintf(L"%d MB total", meminfo_->total / 1024); |
| case MEM_FREE_ITEM: |
| - return StringPrintf(L"%d MB free", mem_free_ / 1024); |
| + return StringPrintf(L"%d MB free", meminfo_->free / 1024); |
| case MEM_BUFFERS_ITEM: |
| - return StringPrintf(L"%d MB buffers", mem_buffers_ / 1024); |
| + return StringPrintf(L"%d MB buffers", meminfo_->buffers / 1024); |
| case MEM_CACHE_ITEM: |
| - return StringPrintf(L"%d MB cache", mem_cache_ / 1024); |
| + return StringPrintf(L"%d MB cache", meminfo_->cached / 1024); |
| case SHMEM_ITEM: |
| - return StringPrintf(L"%d MB shmem", shmem_ / 1024); |
| + return StringPrintf(L"%d MB shmem", meminfo_->shmem / 1024); |
| case PURGE_MEMORY_ITEM: |
| return L"Purge memory"; |
| default: |
| @@ -146,4 +153,28 @@ void MemoryMenuButton::EnsureMenu() { |
| menu_->AppendDelegateMenuItem(PURGE_MEMORY_ITEM); |
| } |
| +///////////////////////////////////////////////////////////////////////////// |
| +// NotificationObserver overrides. |
| + |
| +void MemoryMenuButton::Observe(int type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + switch (type) { |
| + case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| + RenderProcessHost::RendererClosedDetails* process_details = |
| + Details<RenderProcessHost::RendererClosedDetails>(details).ptr(); |
| + if (process_details->status == |
| + base::TERMINATION_STATUS_PROCESS_WAS_KILLED) { |
| + renderer_kills_++; |
| + // A kill is a very interesting event, so repaint immediately. |
| + UpdateText(); |
| + } |
| + break; |
| + } |
| + default: |
| + NOTREACHED() << L"Received unexpected notification"; |
| + break; |
| + } |
| +} |
| + |
| } // namespace chromeos |