OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/status/memory_menu_button.h" | 5 #include "chrome/browser/chromeos/status/memory_menu_button.h" |
6 | 6 |
7 #include "base/process_util.h" // GetSystemMemoryInfo | 7 #include "base/process_util.h" // GetSystemMemoryInfo |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "chrome/browser/chromeos/status/status_area_host.h" | 9 #include "chrome/browser/chromeos/status/status_area_host.h" |
10 #include "chrome/browser/memory_purger.h" | 10 #include "chrome/browser/memory_purger.h" |
| 11 #include "content/browser/renderer_host/render_process_host.h" |
| 12 #include "content/common/notification_service.h" |
11 #include "grit/generated_resources.h" | 13 #include "grit/generated_resources.h" |
12 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
13 #include "views/widget/widget.h" | 15 #include "views/widget/widget.h" |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 // views::MenuItemView item ids | 19 // views::MenuItemView item ids |
18 enum { | 20 enum { |
19 MEM_TOTAL_ITEM, | 21 MEM_TOTAL_ITEM, |
20 MEM_FREE_ITEM, | 22 MEM_FREE_ITEM, |
21 MEM_BUFFERS_ITEM, | 23 MEM_BUFFERS_ITEM, |
22 MEM_CACHE_ITEM, | 24 MEM_CACHE_ITEM, |
23 SHMEM_ITEM, | 25 SHMEM_ITEM, |
24 PURGE_MEMORY_ITEM, | 26 PURGE_MEMORY_ITEM, |
25 }; | 27 }; |
26 | 28 |
27 } // namespace | 29 } // namespace |
28 | 30 |
29 namespace chromeos { | 31 namespace chromeos { |
30 | 32 |
31 // Delay between updates, in seconds. | 33 // Delay between updates, in seconds. |
32 const int kUpdateIntervalSeconds = 5; | 34 const int kUpdateIntervalSeconds = 5; |
33 | 35 |
34 MemoryMenuButton::MemoryMenuButton(StatusAreaHost* host) | 36 MemoryMenuButton::MemoryMenuButton(StatusAreaHost* host) |
35 : StatusAreaButton(host, this), | 37 : StatusAreaButton(host, this), |
36 mem_total_(0), | 38 meminfo_(new base::SystemMemoryInfoKB()), |
37 shmem_(0), | 39 renderer_kills_(0) { |
38 mem_free_(0), | 40 // Track renderer kills, as the kernel OOM killer will start to kill our |
39 mem_buffers_(0), | 41 // renderers as we run out of memory. |
40 mem_cache_(0) { | 42 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 43 NotificationService::AllSources()); |
41 UpdateTextAndSetNextTimer(); | 44 UpdateTextAndSetNextTimer(); |
42 } | 45 } |
43 | 46 |
44 MemoryMenuButton::~MemoryMenuButton() { | 47 MemoryMenuButton::~MemoryMenuButton() { |
45 } | 48 } |
46 | 49 |
47 void MemoryMenuButton::UpdateTextAndSetNextTimer() { | 50 void MemoryMenuButton::UpdateTextAndSetNextTimer() { |
48 UpdateText(); | 51 UpdateText(); |
49 | 52 |
50 timer_.Start(base::TimeDelta::FromSeconds(kUpdateIntervalSeconds), this, | 53 timer_.Start(base::TimeDelta::FromSeconds(kUpdateIntervalSeconds), this, |
51 &MemoryMenuButton::UpdateTextAndSetNextTimer); | 54 &MemoryMenuButton::UpdateTextAndSetNextTimer); |
52 } | 55 } |
53 | 56 |
54 void MemoryMenuButton::UpdateText() { | 57 void MemoryMenuButton::UpdateText() { |
55 base::GetSystemMemoryInfo(&mem_total_, &mem_free_, &mem_buffers_, &mem_cache_, | 58 base::GetSystemMemoryInfo(meminfo_.get()); |
56 &shmem_); | 59 // "Anonymous" memory, meaning not mapped to a file (which has a name), |
57 std::wstring label = base::StringPrintf(L"%d MB", mem_free_ / 1024); | 60 // represents memory that has been dynamically allocated to a process. |
| 61 // It thus approximates heap memory usage across all processes. |
| 62 int anon_kb = meminfo_->active_anon + meminfo_->inactive_anon; |
| 63 std::wstring label = base::StringPrintf(L"%d MB (%d)", |
| 64 anon_kb / 1024, |
| 65 renderer_kills_); |
58 SetText(label); | 66 SetText(label); |
59 std::wstring tooltip = base::StringPrintf( | 67 std::wstring tooltip = base::StringPrintf( |
60 L"%d MB total\n%d MB free\n%d MB buffers\n%d MB cache\n%d MB shmem", | 68 L"%d MB allocated (anonymous)\n" |
61 mem_total_ / 1024, | 69 L"%d renderer kill(s)", |
62 mem_free_ / 1024, | 70 anon_kb / 1024, |
63 mem_buffers_ / 1024, | 71 renderer_kills_); |
64 mem_cache_ / 1024, | |
65 shmem_ / 1024); | |
66 SetTooltipText(tooltip); | 72 SetTooltipText(tooltip); |
67 SchedulePaint(); | 73 SchedulePaint(); |
68 } | 74 } |
69 | 75 |
70 // MemoryMenuButton, views::MenuDelegate implementation: | 76 // MemoryMenuButton, views::MenuDelegate implementation: |
71 std::wstring MemoryMenuButton::GetLabel(int id) const { | 77 std::wstring MemoryMenuButton::GetLabel(int id) const { |
72 switch (id) { | 78 switch (id) { |
73 case MEM_TOTAL_ITEM: | 79 case MEM_TOTAL_ITEM: |
74 return StringPrintf(L"%d MB total", mem_total_ / 1024); | 80 return StringPrintf(L"%d MB total", meminfo_->total / 1024); |
75 case MEM_FREE_ITEM: | 81 case MEM_FREE_ITEM: |
76 return StringPrintf(L"%d MB free", mem_free_ / 1024); | 82 return StringPrintf(L"%d MB free", meminfo_->free / 1024); |
77 case MEM_BUFFERS_ITEM: | 83 case MEM_BUFFERS_ITEM: |
78 return StringPrintf(L"%d MB buffers", mem_buffers_ / 1024); | 84 return StringPrintf(L"%d MB buffers", meminfo_->buffers / 1024); |
79 case MEM_CACHE_ITEM: | 85 case MEM_CACHE_ITEM: |
80 return StringPrintf(L"%d MB cache", mem_cache_ / 1024); | 86 return StringPrintf(L"%d MB cache", meminfo_->cached / 1024); |
81 case SHMEM_ITEM: | 87 case SHMEM_ITEM: |
82 return StringPrintf(L"%d MB shmem", shmem_ / 1024); | 88 return StringPrintf(L"%d MB shmem", meminfo_->shmem / 1024); |
83 case PURGE_MEMORY_ITEM: | 89 case PURGE_MEMORY_ITEM: |
84 return L"Purge memory"; | 90 return L"Purge memory"; |
85 default: | 91 default: |
86 return std::wstring(); | 92 return std::wstring(); |
87 } | 93 } |
88 } | 94 } |
89 | 95 |
90 bool MemoryMenuButton::IsCommandEnabled(int id) const { | 96 bool MemoryMenuButton::IsCommandEnabled(int id) const { |
91 switch (id) { | 97 switch (id) { |
92 case PURGE_MEMORY_ITEM: | 98 case PURGE_MEMORY_ITEM: |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 menu_->AppendDelegateMenuItem(MEM_TOTAL_ITEM); | 145 menu_->AppendDelegateMenuItem(MEM_TOTAL_ITEM); |
140 menu_->AppendDelegateMenuItem(MEM_FREE_ITEM); | 146 menu_->AppendDelegateMenuItem(MEM_FREE_ITEM); |
141 menu_->AppendDelegateMenuItem(MEM_BUFFERS_ITEM); | 147 menu_->AppendDelegateMenuItem(MEM_BUFFERS_ITEM); |
142 menu_->AppendDelegateMenuItem(MEM_CACHE_ITEM); | 148 menu_->AppendDelegateMenuItem(MEM_CACHE_ITEM); |
143 menu_->AppendDelegateMenuItem(SHMEM_ITEM); | 149 menu_->AppendDelegateMenuItem(SHMEM_ITEM); |
144 // TODO(jamescook): Dump heap profiles? | 150 // TODO(jamescook): Dump heap profiles? |
145 menu_->AppendSeparator(); | 151 menu_->AppendSeparator(); |
146 menu_->AppendDelegateMenuItem(PURGE_MEMORY_ITEM); | 152 menu_->AppendDelegateMenuItem(PURGE_MEMORY_ITEM); |
147 } | 153 } |
148 | 154 |
| 155 ///////////////////////////////////////////////////////////////////////////// |
| 156 // NotificationObserver overrides. |
| 157 |
| 158 void MemoryMenuButton::Observe(int type, |
| 159 const NotificationSource& source, |
| 160 const NotificationDetails& details) { |
| 161 switch (type) { |
| 162 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 163 RenderProcessHost::RendererClosedDetails* process_details = |
| 164 Details<RenderProcessHost::RendererClosedDetails>(details).ptr(); |
| 165 if (process_details->status == |
| 166 base::TERMINATION_STATUS_PROCESS_WAS_KILLED) { |
| 167 renderer_kills_++; |
| 168 // A kill is a very interesting event, so repaint immediately. |
| 169 UpdateText(); |
| 170 } |
| 171 break; |
| 172 } |
| 173 default: |
| 174 NOTREACHED() << L"Received unexpected notification"; |
| 175 break; |
| 176 } |
| 177 } |
| 178 |
149 } // namespace chromeos | 179 } // namespace chromeos |
OLD | NEW |