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() { |
48 delete meminfo_; | |
45 } | 49 } |
46 | 50 |
47 void MemoryMenuButton::UpdateTextAndSetNextTimer() { | 51 void MemoryMenuButton::UpdateTextAndSetNextTimer() { |
48 UpdateText(); | 52 UpdateText(); |
49 | 53 |
50 timer_.Start(base::TimeDelta::FromSeconds(kUpdateIntervalSeconds), this, | 54 timer_.Start(base::TimeDelta::FromSeconds(kUpdateIntervalSeconds), this, |
51 &MemoryMenuButton::UpdateTextAndSetNextTimer); | 55 &MemoryMenuButton::UpdateTextAndSetNextTimer); |
52 } | 56 } |
53 | 57 |
54 void MemoryMenuButton::UpdateText() { | 58 void MemoryMenuButton::UpdateText() { |
55 base::GetSystemMemoryInfo(&mem_total_, &mem_free_, &mem_buffers_, &mem_cache_, | 59 base::GetSystemMemoryInfo(meminfo_); |
56 &shmem_); | 60 // "Anonymous" memory, meaning not mapped to a file (which has a name), |
57 std::wstring label = base::StringPrintf(L"%d MB", mem_free_ / 1024); | 61 // represents memory that has been dynamically allocated to a process. |
62 // It thus approximates heap memory usage across all processes. | |
63 int anon_kb = meminfo_->active_anon + meminfo_->inactive_anon; | |
64 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.
| |
65 anon_kb / 1024, | |
66 renderer_kills_); | |
58 SetText(label); | 67 SetText(label); |
59 std::wstring tooltip = base::StringPrintf( | 68 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", | 69 L"%d MB allocated (anonymous)\n" |
61 mem_total_ / 1024, | 70 L"%d renderer kill(s)", |
62 mem_free_ / 1024, | 71 anon_kb / 1024, |
63 mem_buffers_ / 1024, | 72 renderer_kills_); |
64 mem_cache_ / 1024, | |
65 shmem_ / 1024); | |
66 SetTooltipText(tooltip); | 73 SetTooltipText(tooltip); |
67 SchedulePaint(); | 74 SchedulePaint(); |
68 } | 75 } |
69 | 76 |
70 // MemoryMenuButton, views::MenuDelegate implementation: | 77 // MemoryMenuButton, views::MenuDelegate implementation: |
71 std::wstring MemoryMenuButton::GetLabel(int id) const { | 78 std::wstring MemoryMenuButton::GetLabel(int id) const { |
72 switch (id) { | 79 switch (id) { |
73 case MEM_TOTAL_ITEM: | 80 case MEM_TOTAL_ITEM: |
74 return StringPrintf(L"%d MB total", mem_total_ / 1024); | 81 return StringPrintf(L"%d MB total", meminfo_->total / 1024); |
75 case MEM_FREE_ITEM: | 82 case MEM_FREE_ITEM: |
76 return StringPrintf(L"%d MB free", mem_free_ / 1024); | 83 return StringPrintf(L"%d MB free", meminfo_->free / 1024); |
77 case MEM_BUFFERS_ITEM: | 84 case MEM_BUFFERS_ITEM: |
78 return StringPrintf(L"%d MB buffers", mem_buffers_ / 1024); | 85 return StringPrintf(L"%d MB buffers", meminfo_->buffers / 1024); |
79 case MEM_CACHE_ITEM: | 86 case MEM_CACHE_ITEM: |
80 return StringPrintf(L"%d MB cache", mem_cache_ / 1024); | 87 return StringPrintf(L"%d MB cache", meminfo_->cached / 1024); |
81 case SHMEM_ITEM: | 88 case SHMEM_ITEM: |
82 return StringPrintf(L"%d MB shmem", shmem_ / 1024); | 89 return StringPrintf(L"%d MB shmem", meminfo_->shmem / 1024); |
83 case PURGE_MEMORY_ITEM: | 90 case PURGE_MEMORY_ITEM: |
84 return L"Purge memory"; | 91 return L"Purge memory"; |
85 default: | 92 default: |
86 return std::wstring(); | 93 return std::wstring(); |
87 } | 94 } |
88 } | 95 } |
89 | 96 |
90 bool MemoryMenuButton::IsCommandEnabled(int id) const { | 97 bool MemoryMenuButton::IsCommandEnabled(int id) const { |
91 switch (id) { | 98 switch (id) { |
92 case PURGE_MEMORY_ITEM: | 99 case PURGE_MEMORY_ITEM: |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 menu_->AppendDelegateMenuItem(MEM_TOTAL_ITEM); | 146 menu_->AppendDelegateMenuItem(MEM_TOTAL_ITEM); |
140 menu_->AppendDelegateMenuItem(MEM_FREE_ITEM); | 147 menu_->AppendDelegateMenuItem(MEM_FREE_ITEM); |
141 menu_->AppendDelegateMenuItem(MEM_BUFFERS_ITEM); | 148 menu_->AppendDelegateMenuItem(MEM_BUFFERS_ITEM); |
142 menu_->AppendDelegateMenuItem(MEM_CACHE_ITEM); | 149 menu_->AppendDelegateMenuItem(MEM_CACHE_ITEM); |
143 menu_->AppendDelegateMenuItem(SHMEM_ITEM); | 150 menu_->AppendDelegateMenuItem(SHMEM_ITEM); |
144 // TODO(jamescook): Dump heap profiles? | 151 // TODO(jamescook): Dump heap profiles? |
145 menu_->AppendSeparator(); | 152 menu_->AppendSeparator(); |
146 menu_->AppendDelegateMenuItem(PURGE_MEMORY_ITEM); | 153 menu_->AppendDelegateMenuItem(PURGE_MEMORY_ITEM); |
147 } | 154 } |
148 | 155 |
156 ///////////////////////////////////////////////////////////////////////////// | |
157 // NotificationObserver overrides. | |
158 | |
159 void MemoryMenuButton::Observe(int type, | |
160 const NotificationSource& source, | |
161 const NotificationDetails& details) { | |
162 switch (type) { | |
163 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
164 RenderProcessHost::RendererClosedDetails* process_details = | |
165 Details<RenderProcessHost::RendererClosedDetails>(details).ptr(); | |
166 if (process_details->status == | |
167 base::TERMINATION_STATUS_PROCESS_WAS_KILLED) { | |
168 renderer_kills_++; | |
169 // A kill is a very interesting event, so repaint immediately. | |
170 UpdateText(); | |
171 } | |
172 break; | |
173 } | |
174 default: | |
175 NOTREACHED() << L"Received unexpected notification"; | |
176 break; | |
177 } | |
178 } | |
179 | |
149 } // namespace chromeos | 180 } // namespace chromeos |
OLD | NEW |